Skip to content

Commit 4aeed3d

Browse files
author
Marian Ganisin
authored
Merge pull request 3scale-qe#117 from mkudlej/crd_client_changes
Crd client changes
2 parents e76cd55 + 210f57e commit 4aeed3d

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.vscode/
22
.idea/
3+
tags
4+
*.swp
5+
*.swo
36

47
# Byte-compiled / optimized / DLL files
58
__pycache__/

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ requests = "*"
1515
backoff = "*"
1616

1717
[requires]
18-
python_version = "3.7"
18+
python_version = "3"

threescale_api/auth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class BaseClientAuth(requests.auth.AuthBase):
1111
def __init__(self, app, location=None):
1212
self.app = app
1313
self.location = location
14+
self.credentials = {}
1415
if location is None:
1516
self.location = app.service.proxy.list().entity["credentials_location"]
1617

threescale_api/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ def request(self, method='GET', url=None, path='', params: dict = None,
284284
Returns:
285285
286286
"""
287-
287+
if 'resource' in kwargs:
288+
del(kwargs['resource'])
288289
full_url = url if url else urljoin(self.url, path)
289290
full_url = full_url + ".json"
290291
headers = headers or {}

threescale_api/defaults.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, parent=None, instance_klass=None,
2323
entity_name(str): Entity name - required for extraction
2424
entity_collection(str): Collection name - required for extraction
2525
"""
26-
self._parent = parent
26+
self.parent = parent
2727
self._instance_klass = instance_klass
2828
self._entity_name = entity_name
2929
if entity_collection is None and entity_name is not None:
@@ -45,14 +45,6 @@ def threescale_client(self) -> 'ThreeScaleClient':
4545
"""
4646
return self.parent.threescale_client
4747

48-
@property
49-
def parent(self) -> 'DefaultResource':
50-
""" Instance of the parent resource
51-
Returns(DefaultResource): Parent of the client is an subclass of the default resource
52-
53-
"""
54-
return self._parent
55-
5648
@property
5749
def rest(self) -> 'RestApiClient':
5850
"""Rest API client for the 3scale instance
@@ -154,7 +146,7 @@ def __getitem__(self, selector: Union[int, 'str']) -> 'DefaultResource':
154146
def __len__(self) -> int:
155147
return len(self._list())
156148

157-
def __iter__(self) -> Iterator['CRUDResource']:
149+
def __iter__(self) -> Iterator['DefaultResource']:
158150
return next(iter(self._list()))
159151

160152
def read(self, entity_id: int = None) -> 'DefaultResource':
@@ -190,6 +182,7 @@ def select(self, predicate, **kwargs) -> List['DefaultResource']:
190182

191183
def select_by(self, **params) -> List['DefaultResource']:
192184
"""Select by params - logical and
185+
Usage example: select_by(role='admin')
193186
Args:
194187
**params: params used for selection
195188
Returns: List of resources
@@ -289,6 +282,10 @@ def threescale_client(self) -> 'ThreeScaleClient':
289282
def parent(self) -> 'DefaultResource':
290283
return self.client.parent
291284

285+
@parent.setter
286+
def parent(self, parent):
287+
self.client.parent = parent
288+
292289
@property
293290
def entity_name(self) -> Optional[str]:
294291
return self[self._entity_name]
@@ -310,6 +307,10 @@ def client(self) -> DefaultClient:
310307
def entity_id(self) -> int:
311308
return self._entity_id or self._entity.get('id')
312309

310+
@entity_id.setter
311+
def entity_id(self, value):
312+
self._entity_id = value
313+
313314
def __getitem__(self, item: str):
314315
return self.entity.get(item)
315316

@@ -345,9 +346,13 @@ def set(self, item: str, value: Any):
345346
self.entity[item] = value
346347

347348
def _lazy_load(self, **kwargs) -> 'DefaultResource':
348-
if not self._entity:
349+
if self._entity is None:
349350
# Lazy load the entity
350-
self._entity = self.fetch(**kwargs)
351+
fetched = self.fetch(**kwargs)
352+
if isinstance(fetched, dict):
353+
self._entity = fetched
354+
else:
355+
self._entity = fetched._entity
351356
return self
352357

353358
def read(self, **kwargs) -> 'DefaultResource':
@@ -362,13 +367,16 @@ def exists(self, **kwargs) -> bool:
362367
return self.client.exists(entity_id=self.entity_id, **kwargs)
363368

364369
def delete(self, **kwargs):
365-
self.client.delete(entity_id=self.entity_id, **kwargs)
370+
self.client.delete(entity_id=self.entity_id, resource=self, **kwargs)
366371

367372
def update(self, params: dict = None, **kwargs) -> 'DefaultResource':
368373
new_params = {**self.entity}
369374
if params:
370375
new_params.update(params)
371-
new_entity = self.client.update(entity_id=self.entity_id, params=new_params, **kwargs)
376+
new_entity = self.client.update(entity_id=self.entity_id,
377+
params=new_params,
378+
resource=self,
379+
**kwargs)
372380
self._entity = new_entity.entity
373381
return self
374382

threescale_api/resources.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def url(self) -> str:
4444

4545

4646
class Limits(DefaultClient):
47-
def __init__(self, *args, entity_name='limit', entity_collection='limits', metric=None,
47+
def __init__(self, *args, entity_name='limit', entity_collection='limits', metric,
4848
**kwargs):
4949
super().__init__(*args, entity_name=entity_name,
5050
entity_collection=entity_collection, **kwargs)
@@ -914,11 +914,9 @@ def plans_url(self) -> str:
914914
def service(self) -> 'Service':
915915
return self.parent
916916

917-
@property
918917
def limits(self, metric: 'Metric' = None) -> 'Limits':
919918
return Limits(self, metric=metric, instance_klass=Limit)
920919

921-
@property
922920
def pricing_rules(self, metric: 'Metric' = None) -> 'PricingRules':
923921
return PricingRules(self, metric=metric, instance_klass=PricingRule)
924922

@@ -1021,10 +1019,6 @@ def configs(self) -> 'ProxyConfigs':
10211019
def policies(self) -> 'Policies':
10221020
return Policies(parent=self, instance_klass=Policy)
10231021

1024-
@property
1025-
def entity_id(self):
1026-
return None
1027-
10281022
def promote(self, **kwargs) -> 'Proxy':
10291023
return self.configs.promote(**kwargs)
10301024

@@ -1337,7 +1331,7 @@ def _extract_entity_id(entity: Union['DefaultResource', int]):
13371331

13381332

13391333
class PolicyRegistry(DefaultResource):
1340-
def __init__(self, entity_name='system_name', **kwargs):
1334+
def __init__(self, entity_name='name', **kwargs):
13411335
super().__init__(entity_name=entity_name, **kwargs)
13421336

13431337
@property

threescale_api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import requests
77
from requests.adapters import HTTPAdapter
8-
from requests.packages.urllib3.util.retry import Retry
8+
from urllib3.util import Retry
99

1010
logger = logging.getLogger(__name__)
1111

0 commit comments

Comments
 (0)