Created
December 12, 2019 19:59
-
-
Save loganlinn/4357ddfbe536c4b62aef243a153c6d7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/results_notebooks/optimizely/api.py b/src/results_notebooks/optimizely/api.py | |
index 2eb2fa2..6132c4c 100644 | |
--- a/src/results_notebooks/optimizely/api.py | |
+++ b/src/results_notebooks/optimizely/api.py | |
@@ -13,7 +13,7 @@ See Also | |
import datetime | |
import os | |
import re | |
-from typing import ClassVar, Generic, List, Optional, Type, TypeVar | |
+from typing import ClassVar, Dict, Generic, List, Type, TypeVar | |
import requests | |
import six | |
@@ -100,14 +100,15 @@ class ApiClient: | |
if not 200 <= resp.status_code <= 299: | |
raise ApiException(http_resp=resp) | |
- if response_type: | |
- try: | |
- data = resp.json() | |
- except ValueError: | |
- data = resp.content | |
- return self.deserialize(data, response_type) | |
+ if response_type is None: | |
+ return resp | |
- return resp | |
+ try: | |
+ data = resp.json() | |
+ except ValueError: | |
+ data = resp.content | |
+ | |
+ return self.deserialize(data, response_type) | |
def sanitize_for_serialization(self, obj): | |
"""Builds a JSON POST object. | |
@@ -186,6 +187,17 @@ class ApiClient: | |
if data is None: | |
return None | |
+ # List[Item] | |
+ if type(klass) == type(List) and klass.__origin__ == list: | |
+ item_kls, = klass.__args__ | |
+ return [cls.deserialize(item_data, item_kls) for item_data in data] | |
+ | |
+ # Dict[Key, Val] | |
+ if type(klass) == type(Dict) and klass.__origin__ == dict: | |
+ key_kls, val_kls = klass.__args__ | |
+ return {key_kls(k): cls.deserialize(v, val_kls) for k, v in data.items()} | |
+ | |
+ # "list[Item]" or "dict[str, Item]" | |
if type(klass) == str: | |
if klass.startswith("list["): | |
sub_kls = re.match(r"list\[(.*)\]", klass).group(1) | |
@@ -310,58 +322,41 @@ class RestApi(Generic[T]): | |
entity_cls: ClassVar[Type[T]] | |
entity_url: ClassVar[str] | |
- def __init__(self, client: ApiClient, cache=True): | |
+ def __init__(self, client: ApiClient): | |
self._client = client | |
- self._entity_cache = dict() if cache else None | |
- | |
- def entity_id(self, entity: T) -> int: | |
- return getattr(entity, "id") | |
- def list(self, **kwargs) -> List[T]: | |
- entities = self._client.request( | |
+ def list(self, query_params=None, headers=None) -> List[T]: | |
+ return self._client.request( | |
"GET", | |
self.entity_url, | |
path_params=dict(), | |
- response_type=self.entity_cls, | |
- **kwargs, | |
+ query_params=self._query_params_list(query_params), | |
+ header_params=self._headers_list(headers), | |
+ response_type=List[self.entity_cls], | |
) | |
- for entity in entities: | |
- entity_id = self.entity_id(entity) | |
- if entity_id: | |
- self.cache_set(entity_id, entity) | |
- return entities | |
- | |
- def get(self, entity_id) -> T: | |
- entity = self.cache_get(entity_id) | |
- if entity is not None: | |
- return entity | |
- entity = self._client.request( | |
+ | |
+ def get(self, entity_id, query_params=None, headers=None) -> T: | |
+ return self._client.request( | |
"GET", | |
self.entity_url + "/{entity_id}", | |
path_params=dict(entity_id=entity_id), | |
+ query_params=self._query_params_get(query_params), | |
+ header_params=self._headers_get(headers), | |
response_type=self.entity_cls, | |
) | |
- self.cache_set(entity_id, entity) | |
- return entity | |
- | |
- def cache_get(self, entity_id) -> Optional[T]: | |
- return ( | |
- self._entity_cache.get(entity_id) | |
- if self._entity_cache is not None | |
- else None | |
- ) | |
- def cache_set(self, entity_id, entity: T): | |
- if self._entity_cache is not None: | |
- self._entity_cache[entity_id] = entity | |
+ def _query_params_list(self, kwargs): | |
+ return kwargs or None | |
+ | |
+ def _headers_list(self, kwargs): | |
+ return kwargs or None | |
+ | |
+ def _query_params_get(self, kwargs): | |
+ return kwargs or None | |
+ | |
+ def _headers_get(self, kwargs): | |
+ return kwargs or None | |
- def cache_clear(self, entity_id=None): | |
- if self._entity_cache is None: | |
- return | |
- if entity_id is not None: | |
- del self._entity_cache[entity_id] | |
- else: | |
- self._entity_cache = {} | |
###################################################################################### | |
diff --git a/src/results_notebooks/optimizely/exceptions.py b/src/results_notebooks/optimizely/exceptions.py | |
index 8777082..04081ef 100644 | |
--- a/src/results_notebooks/optimizely/exceptions.py | |
+++ b/src/results_notebooks/optimizely/exceptions.py | |
@@ -7,11 +7,13 @@ class ApiException(Exception): | |
def __init__(self, status=None, reason=None, http_resp: requests.Response = None): | |
if http_resp is not None: | |
+ self.url = http_resp.url | |
self.status = http_resp.status_code | |
self.reason = http_resp.reason | |
self.body = http_resp.content | |
self.headers = http_resp.headers | |
else: | |
+ self.url = None | |
self.status = status | |
self.reason = reason | |
self.body = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment