Browse Source

WebAPI: fix list serialization

pull/41/head
Rossen Georgiev 9 years ago
parent
commit
aef88e4547
  1. 24
      steam/webapi.py

24
steam/webapi.py

@ -305,25 +305,16 @@ class WebAPIMethod(object):
islist = param['_array'] islist = param['_array']
optional = param['optional'] optional = param['optional']
# raise if we are missing a required parameter
if not optional and name not in kwargs and name != 'key': if not optional and name not in kwargs and name != 'key':
raise ValueError("Method requires %s to be set" % repr(name)) raise ValueError("Method requires %s to be set" % repr(name))
# populate params that will be passed to _api_request
if name in kwargs: if name in kwargs:
# some parameters can be an array, they need to be send as seperate field if islist and not isinstance(kwargs[name], list):
# the array index is append to the name (e.g. name[0], name[1] etc)
if islist:
if not isinstance(kwargs[name], list):
raise ValueError("Expected %s to be a list, got %s" % ( raise ValueError("Expected %s to be a list, got %s" % (
repr(name), repr(name),
repr(type(kwargs[name]))) repr(type(kwargs[name])))
) )
params[name] = kwargs[name]
for idx, value in enumerate(kwargs[name]):
params['%s[%d]' % (name, idx)] = value
else:
params[name] = kwargs[name]
url = "%s://%s/%s/%s/v%s/" % ( url = "%s://%s/%s/%s/v%s/" % (
'https' if self._parent.https else 'http', 'https' if self._parent.https else 'http',
@ -413,14 +404,19 @@ def webapi_request(url, method='GET', caller=None, session=None, params=None):
onetime = {} onetime = {}
for param in DEFAULT_PARAMS: for param in DEFAULT_PARAMS:
params[param] = onetime[param] = params.get(param, DEFAULT_PARAMS[param]) params[param] = onetime[param] = params.get(param, DEFAULT_PARAMS[param])
map(params.pop, ('raw', 'apihost', 'https', 'http_timeout')) for param in ('raw', 'apihost', 'https', 'http_timeout'):
del params[param]
if onetime['format'] not in ('json', 'vdf', 'xml'): if onetime['format'] not in ('json', 'vdf', 'xml'):
raise ValueError("Expected format to be json,vdf or xml; got %s" % onetime['format']) raise ValueError("Expected format to be json,vdf or xml; got %s" % onetime['format'])
for k, v in params.items(): # serialize some types for k, v in list(params.items()): # serialize some types
if isinstance(v, bool): params[k] = 1 if v else 0 if isinstance(v, bool): params[k] = 1 if v else 0
elif isinstance(v, (list, dict)): params[k] = _json.dumps(v) elif isinstance(v, dict): params[k] = _json.dumps(v)
elif isinstance(v, list):
del params[k]
for i, lvalue in enumerate(v):
params["%s[%d]" % (k, i)] = lvalue
kwargs = {'params': params} if method == "GET" else {'data': params} # params to data for POST kwargs = {'params': params} if method == "GET" else {'data': params} # params to data for POST

Loading…
Cancel
Save