Skip to content

Commit

Permalink
add setter for parent property
Browse files Browse the repository at this point in the history
return directly object from fetch instead of dict because crd objects have many more attributes than just dict with data
change import of `Retry` to `from urllib3.util import Retry` because of
newer verion of requests and its dependecies
  • Loading branch information
mkudlej committed Feb 11, 2022
1 parent e76cd55 commit 210f57e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.vscode/
.idea/
tags
*.swp
*.swo

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requests = "*"
backoff = "*"

[requires]
python_version = "3.7"
python_version = "3"
1 change: 1 addition & 0 deletions threescale_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
3 changes: 2 additions & 1 deletion threescale_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
36 changes: 22 additions & 14 deletions threescale_api/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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)

Expand Down Expand Up @@ -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':
Expand All @@ -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

Expand Down
10 changes: 2 additions & 8 deletions threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion threescale_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down

0 comments on commit 210f57e

Please sign in to comment.