summaryrefslogtreecommitdiffstats
path: root/fatcat_scholar/api_entities.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2020-05-14 23:10:41 -0700
committerBryan Newbold <bnewbold@archive.org>2020-05-14 23:10:41 -0700
commitc4852afdec87712e09b9cdba5b5db7e1ad1a0701 (patch)
tree3d50a3641245d0d30a0f7f4e0704ffa9d19f03a1 /fatcat_scholar/api_entities.py
parent4df616706146fce16dbc1fdc3b5502abd13144df (diff)
downloadfatcat-scholar-c4852afdec87712e09b9cdba5b5db7e1ad1a0701.tar.gz
fatcat-scholar-c4852afdec87712e09b9cdba5b5db7e1ad1a0701.zip
start implementing ES transform helpers
Diffstat (limited to 'fatcat_scholar/api_entities.py')
-rw-r--r--fatcat_scholar/api_entities.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/fatcat_scholar/api_entities.py b/fatcat_scholar/api_entities.py
new file mode 100644
index 0000000..53455e8
--- /dev/null
+++ b/fatcat_scholar/api_entities.py
@@ -0,0 +1,34 @@
+
+import json
+import collections
+from fatcat_openapi_client import ApiClient
+
+def entity_to_dict(entity, api_client=None):
+ """
+ Hack to take advantage of the code-generated serialization code.
+
+ Initializing/destroying ApiClient objects is surprisingly expensive
+ (because it involves a threadpool), so we allow passing an existing
+ instance. If you already have a full-on API connection `api`, you can
+ access the ApiClient object as `api.api_client`. This is such a speed-up
+ that this argument may become mandatory.
+ """
+ if not api_client:
+ api_client = ApiClient()
+ return api_client.sanitize_for_serialization(entity)
+
+def entity_from_json(json_str, entity_type, api_client=None):
+ """
+ Hack to take advantage of the code-generated deserialization code
+
+ See note on `entity_to_dict()` about api_client argument.
+ """
+ if not api_client:
+ api_client = ApiClient()
+ thing = collections.namedtuple('Thing', ['data'])
+ thing.data = json_str
+ return api_client.deserialize(thing, entity_type)
+
+def entity_from_dict(obj, entity_type, api_client=None):
+ json_str = json.dumps(obj)
+ return entity_from_json(json_str, entity_type, api_client=api_client)