diff --git a/.gitignore b/.gitignore index 15596f1..ffcda3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ .vscode/ .idea/ +tags +*.swp +*.swo # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/Pipfile b/Pipfile index bb5358b..1fc5ba7 100644 --- a/Pipfile +++ b/Pipfile @@ -15,4 +15,4 @@ requests = "*" backoff = "*" [requires] -python_version = "3.7" +python_version = "3" diff --git a/threescale_api/auth.py b/threescale_api/auth.py index 87a4af9..b337cb3 100644 --- a/threescale_api/auth.py +++ b/threescale_api/auth.py @@ -11,6 +11,7 @@ class BaseClientAuth(requests.auth.AuthBase): def __init__(self, app, location=None): self.app = app self.location = location + self.credentials = {} if location is None: self.location = app.service.proxy.list().entity["credentials_location"] diff --git a/threescale_api/client.py b/threescale_api/client.py index f6c44f8..8e5f243 100644 --- a/threescale_api/client.py +++ b/threescale_api/client.py @@ -284,7 +284,8 @@ def request(self, method='GET', url=None, path='', params: dict = None, Returns: """ - + if 'resource' in kwargs: + del(kwargs['resource']) full_url = url if url else urljoin(self.url, path) full_url = full_url + ".json" headers = headers or {} diff --git a/threescale_api/defaults.py b/threescale_api/defaults.py index 8108baa..8213c04 100644 --- a/threescale_api/defaults.py +++ b/threescale_api/defaults.py @@ -23,7 +23,7 @@ def __init__(self, parent=None, instance_klass=None, entity_name(str): Entity name - required for extraction entity_collection(str): Collection name - required for extraction """ - self._parent = parent + self.parent = parent self._instance_klass = instance_klass self._entity_name = entity_name if entity_collection is None and entity_name is not None: @@ -45,14 +45,6 @@ def threescale_client(self) -> 'ThreeScaleClient': """ return self.parent.threescale_client - @property - def parent(self) -> 'DefaultResource': - """ Instance of the parent resource - Returns(DefaultResource): Parent of the client is an subclass of the default resource - - """ - return self._parent - @property def rest(self) -> 'RestApiClient': """Rest API client for the 3scale instance @@ -154,7 +146,7 @@ def __getitem__(self, selector: Union[int, 'str']) -> 'DefaultResource': def __len__(self) -> int: return len(self._list()) - def __iter__(self) -> Iterator['CRUDResource']: + def __iter__(self) -> Iterator['DefaultResource']: return next(iter(self._list())) def read(self, entity_id: int = None) -> 'DefaultResource': @@ -190,6 +182,7 @@ def select(self, predicate, **kwargs) -> List['DefaultResource']: def select_by(self, **params) -> List['DefaultResource']: """Select by params - logical and + Usage example: select_by(role='admin') Args: **params: params used for selection Returns: List of resources @@ -289,6 +282,10 @@ def threescale_client(self) -> 'ThreeScaleClient': def parent(self) -> 'DefaultResource': return self.client.parent + @parent.setter + def parent(self, parent): + self.client.parent = parent + @property def entity_name(self) -> Optional[str]: return self[self._entity_name] @@ -310,6 +307,10 @@ def client(self) -> DefaultClient: def entity_id(self) -> int: return self._entity_id or self._entity.get('id') + @entity_id.setter + def entity_id(self, value): + self._entity_id = value + def __getitem__(self, item: str): return self.entity.get(item) @@ -345,9 +346,13 @@ def set(self, item: str, value: Any): self.entity[item] = value def _lazy_load(self, **kwargs) -> 'DefaultResource': - if not self._entity: + if self._entity is None: # Lazy load the entity - self._entity = self.fetch(**kwargs) + fetched = self.fetch(**kwargs) + if isinstance(fetched, dict): + self._entity = fetched + else: + self._entity = fetched._entity return self def read(self, **kwargs) -> 'DefaultResource': @@ -362,13 +367,16 @@ def exists(self, **kwargs) -> bool: return self.client.exists(entity_id=self.entity_id, **kwargs) def delete(self, **kwargs): - self.client.delete(entity_id=self.entity_id, **kwargs) + self.client.delete(entity_id=self.entity_id, resource=self, **kwargs) def update(self, params: dict = None, **kwargs) -> 'DefaultResource': new_params = {**self.entity} if params: new_params.update(params) - new_entity = self.client.update(entity_id=self.entity_id, params=new_params, **kwargs) + new_entity = self.client.update(entity_id=self.entity_id, + params=new_params, + resource=self, + **kwargs) self._entity = new_entity.entity return self diff --git a/threescale_api/resources.py b/threescale_api/resources.py index fbf816d..d0d46c2 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -44,7 +44,7 @@ def url(self) -> str: class Limits(DefaultClient): - def __init__(self, *args, entity_name='limit', entity_collection='limits', metric=None, + def __init__(self, *args, entity_name='limit', entity_collection='limits', metric, **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -914,11 +914,9 @@ def plans_url(self) -> str: def service(self) -> 'Service': return self.parent - @property def limits(self, metric: 'Metric' = None) -> 'Limits': return Limits(self, metric=metric, instance_klass=Limit) - @property def pricing_rules(self, metric: 'Metric' = None) -> 'PricingRules': return PricingRules(self, metric=metric, instance_klass=PricingRule) @@ -1021,10 +1019,6 @@ def configs(self) -> 'ProxyConfigs': def policies(self) -> 'Policies': return Policies(parent=self, instance_klass=Policy) - @property - def entity_id(self): - return None - def promote(self, **kwargs) -> 'Proxy': return self.configs.promote(**kwargs) @@ -1337,7 +1331,7 @@ def _extract_entity_id(entity: Union['DefaultResource', int]): class PolicyRegistry(DefaultResource): - def __init__(self, entity_name='system_name', **kwargs): + def __init__(self, entity_name='name', **kwargs): super().__init__(entity_name=entity_name, **kwargs) @property diff --git a/threescale_api/utils.py b/threescale_api/utils.py index 45ca7d3..0213fa9 100644 --- a/threescale_api/utils.py +++ b/threescale_api/utils.py @@ -5,7 +5,7 @@ import requests from requests.adapters import HTTPAdapter -from requests.packages.urllib3.util.retry import Retry +from urllib3.util import Retry logger = logging.getLogger(__name__)