all: fix all the arguments

This commit is contained in:
2018-12-12 15:15:26 +01:00
parent cf16476a0f
commit 23f054d3b8
2 changed files with 49 additions and 9 deletions

View File

@@ -32,13 +32,46 @@ def mkname(method, name, params):
return name return name
def mkparam(param): def mkparam(param, data):
param = inflection.underscore(param) if '$ref' in param:
if param in reserved: keys = param['$ref'][2:].split("/")
return "{}_".format(param) param = data
for key in keys:
param = param[key]
name = param["name"]
name = inflection.underscore(name)
if name in reserved:
name = "{}_".format(name)
param["name"] = name
return param return param
def mkkwargs(params):
for param in params:
name = param['name']
if param['required']:
yield name
for param in params:
name = param['name']
if not param['required']:
yield "{}=None".format(name)
def getparams(params, where):
ps = []
for param in params:
if param['in'] == where:
ps.append(param['name'])
return ps
def mkparams(params, where):
ps = getparams(params, where)
if not ps:
return None
return '{{\n{}\n }}'.format(',\n'.join(' "{}": {}'.format(p, p) for p in ps))
def pretty_print(name, data): def pretty_print(name, data):
info = data['info'] info = data['info']
r = re.compile('\\/\\{([^\\}]*)\\}') r = re.compile('\\/\\{([^\\}]*)\\}')
@@ -50,13 +83,19 @@ def pretty_print(name, data):
for method, body in fns.items(): for method, body in fns.items():
method = method.lower() method = method.lower()
fmt = r2.sub('{}', path) fmt = r2.sub('{}', path)
params = [mkparam(param) for param in r.findall(path)] paramdef = [mkparam(param, data) for param in body.get('parameters', [])]
name = r.sub('', path).replace("/", "_").replace('-', '_')[1:] name = r.sub('', path).replace("/", "_").replace('-', '_')[1:]
params = [p["name"] for p in paramdef]
name = mkname(method, name, params) name = mkname(method, name, params)
params = ", {}".format(", ".join(params)) if params else '' params = ", {}".format(", ".join(mkkwargs(paramdef))) if params else ''
print(' def {}(self{}):'.format(name, params)) print(' def {}(self{}):'.format(name, params))
print(' """{}"""'.format(', '.join(body['tags']))) print(' """{}"""'.format(body.get("summary", "")))
print(' return self.{}("{}"{})\n'.format(method, fmt, params)) print(' data = {}'.format(mkparams(paramdef, 'body')))
print(' params = {}'.format(mkparams(paramdef, 'query')))
tpl = ' return self.{}("{}"{}, data=data, params=params)\n'
args = getparams(paramdef, 'path')
args = '.format({})'.format(', '.join(args)) if args else ''
print(tpl.format(method, fmt, args))
def generate(name, url): def generate(name, url):

View File

@@ -30,7 +30,8 @@ class Client():
# we pass the scheme as string, but have it as attributes in requests # we pass the scheme as string, but have it as attributes in requests
fn = requests.__getattribute__(scheme) fn = requests.__getattribute__(scheme)
res = fn(url, headers=headers, json=data, params=params, auth=self.auth) res = fn(url, headers=headers, json=data, params=params,
verify=self.verify, auth=self.auth)
res.raise_for_status() res.raise_for_status()