Mercurial > lbo > hg > async-google-apis
changeset 88:cc6bd5604788
Cache API descriptions
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 24 Oct 2020 19:32:49 +0200 |
parents | f1ab59530f8f |
children | c260d92db27f |
files | generate/generate.py |
diffstat | 1 files changed, 26 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/generate/generate.py Sat Oct 24 18:14:28 2020 +0200 +++ b/generate/generate.py Sat Oct 24 19:32:49 2020 +0200 @@ -10,6 +10,7 @@ import re import requests +import os from os import path from templates import * @@ -393,6 +394,7 @@ http_method, } method_fragments.append(chevron.render(UploadMethodTmpl, data_upload)) + method_fragments.append(chevron.render(ResumableUploadMethodTmpl, data_upload)) return chevron.render( ServiceImplementationTmpl, { @@ -472,6 +474,24 @@ for s in services: f.write(s) +def from_cache(apiId): + try: + with open(path.join('cache', apiId+'.json'), 'r') as f: + print('Found API description in cache!') + return json.load(f) + except Exception as e: + print('Fetching description from cache failed:', e) + return None + +def to_cache(apiId, doc): + try: + os.makedirs('cache', exist_ok=True) + with open(path.join('cache', apiId+'.json'), 'w') as f: + json.dump(doc, f) + except Exception as e: + print(e) + return None + return None def fetch_discovery_base(url, apis): """Fetch the discovery base document from `url`. Return api documents for APIs with IDs in `apis`. @@ -486,8 +506,13 @@ def fetch_discovery_doc(api_doc=None, url=None): """Fetch discovery document for a given (short) API doc from the overall discovery document.""" if api_doc: + cached = from_cache(api_doc['id']) + if cached: + return cached url = api_doc['discoveryRestUrl'] - return json.loads(requests.get(url).text) + js = json.loads(requests.get(url).text) + to_cache(api_doc['id'], js) + return js def main():