Mercurial > lbo > hg > async-google-apis
changeset 3:09ee80214be1
yapf python & remove old client secret
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 17 Oct 2020 10:39:19 +0200 |
parents | c7e2e5de7401 |
children | 95fca1b07ef5 |
files | generate/generate.py manual_demo/client_secret.json |
diffstat | 2 files changed, 33 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/generate/generate.py Sat Oct 17 10:36:44 2020 +0200 +++ b/generate/generate.py Sat Oct 17 10:39:19 2020 +0200 @@ -20,16 +20,20 @@ } ''' + def capitalize_first(name): return name[0].upper() + name[1:] + def snake_case(name): def r(c): if c.islower(): return c - return '_'+c.lower() + return '_' + c.lower() + return ''.join([r(c) for c in name]) + def type_of_property(name, prop): typ = '' comment = '' @@ -42,18 +46,28 @@ typ = name struct = {'name': name, 'fields': []} for pn, pp in prop['properties'].items(): - subtyp, substructs = type_of_property(name+capitalize_first(pn), pp) + subtyp, substructs = type_of_property( + name + capitalize_first(pn), pp) if type(subtyp) is tuple: subtyp, comment = subtyp else: comment = None - struct['fields'].append({'name': snake_case(pn), 'attr': '#[serde(rename = "{}")]'.format(pn), - 'typ': subtyp, 'comment': comment}) + struct['fields'].append({ + 'name': + snake_case(pn), + 'attr': + '#[serde(rename = "{}")]'.format(pn), + 'typ': + subtyp, + 'comment': + comment + }) structs.extend(substructs) structs.append(struct) return typ, structs if 'additionalProperties' in prop: - field, substructs = type_of_property(name, prop['additionalProperties']) + field, substructs = type_of_property( + name, prop['additionalProperties']) structs.extend(substructs) if type(field) is tuple: typ = field[0] @@ -65,7 +79,7 @@ typ, substructs = type_of_property(name, prop['items']) if type(typ) is tuple: typ = typ[0] - return 'Vec<' + typ + '>', structs+substructs + return 'Vec<' + typ + '>', structs + substructs if prop['type'] == 'string': if 'format' in prop: if prop['format'] == 'int64': @@ -96,6 +110,7 @@ print(e) raise e + def generate_structs(discdoc): schemas = discdoc['schemas'] structs = [] @@ -103,8 +118,8 @@ typ, substructs = type_of_property(name, desc) structs.extend(substructs) - modname = (discdoc['id']+'_types').replace(':', '_') - with open(path.join('gen', modname+'.rs'), 'w') as f: + modname = (discdoc['id'] + '_types').replace(':', '_') + with open(path.join('gen', modname + '.rs'), 'w') as f: f.write('use serde::{Deserialize, Serialize};') for s in structs: f.write(chevron.render(ResourceStructTmpl, s)) @@ -119,14 +134,21 @@ doc = json.loads(requests.get(url).text) return [it for it in doc['items'] if it['id'] in apis] + def fetch_discovery_doc(api_doc): url = api_doc['discoveryRestUrl'] return json.loads(requests.get(url).text) + def main(): - p = argparse.ArgumentParser(description='Generate Rust code for asynchronous REST Google APIs.') - p.add_argument('--discovery_base', default='https://www.googleapis.com/discovery/v1/apis', help='Base Discovery document.') - p.add_argument('--only_apis', default='drive:v3', help='Only process APIs with these IDs (comma-separated)') + p = argparse.ArgumentParser( + description='Generate Rust code for asynchronous REST Google APIs.') + p.add_argument('--discovery_base', + default='https://www.googleapis.com/discovery/v1/apis', + help='Base Discovery document.') + p.add_argument('--only_apis', + default='drive:v3', + help='Only process APIs with these IDs (comma-separated)') args = p.parse_args() print(args.only_apis) docs = fetch_discovery_base(args.discovery_base, args.only_apis)
--- a/manual_demo/client_secret.json Sat Oct 17 10:36:44 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -{"installed":{"client_id":"902216714886-kmg05j6t6bkbs4ocaab7usqf8s4oelv1.apps.googleusercontent.com","project_id":"yup-test-243420","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"wljFYQSsYqu0Sq_vQU_UXiqB","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}