lsrestclient 1.3.0__tar.gz → 1.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lsrestclient
3
- Version: 1.3.0
3
+ Version: 1.3.2
4
4
  Summary: REST Api Client
5
5
  Author: mba
6
6
  Author-email: bartel@electronic-shop.lu
@@ -1,10 +1,14 @@
1
1
  from lsrestclient import LsRestClient, DownStreamError
2
2
 
3
3
 
4
- def auth_am_login_apikey(org_id: str, api_key: str) -> str:
4
+ def auth_am_login(**kwargs) -> str:
5
5
  am = LsRestClient.from_env("AM_BASE_URL", "am", True)
6
- r = am.post("/auth/login", body=dict(ORG_ID=org_id, API_KEY=api_key))
6
+ r = am.post("/auth/login", body=kwargs)
7
7
  if r.status_code == 200:
8
8
  return r.json().get("access_token")
9
9
  else:
10
10
  raise DownStreamError("/auth/login", r.status_code, r.content)
11
+
12
+
13
+ def auth_am_login_apikey(org_id: str, api_key: str) -> str:
14
+ return auth_am_login(ORG_ID=org_id, API_KEY=api_key)
@@ -13,13 +13,13 @@ class ConnectionError(Exception):
13
13
  """Exception class for connection errors.
14
14
 
15
15
  Args:
16
- url (Optional[str]): The URL that the connection could not be established to.
16
+ url (Optional[str]): The URL that the connection could not be established to.
17
17
 
18
18
  Attributes:
19
- url (Optional[str]): The URL that the connection could not be established to.
19
+ url (Optional[str]): The URL that the connection could not be established to.
20
20
 
21
21
  Raises:
22
- ConnectionError: If a connection could not be established to the given URL.
22
+ ConnectionError: If a connection could not be established to the given URL.
23
23
 
24
24
  """
25
25
 
@@ -51,8 +51,12 @@ def raise_errors(r, exceptions: List[Type[Exception]]):
51
51
  try:
52
52
  json = r.json()
53
53
  detail = pydash.get(json, "detail", json)
54
- error_class = pydash.get(detail, "error_class")
55
- payload = pydash.get(detail, "error_payload", {})
54
+ error_class = pydash.get(detail, "error_class", None)
55
+ if error_class is not None:
56
+ payload = pydash.get(detail, "error_payload", {})
57
+ else:
58
+ error_class = pydash.get(detail, "ERROR_CLASS", None)
59
+ payload = {}
56
60
 
57
61
  if error_class in exceptions_by_class:
58
62
  e = exceptions_by_class[error_class](**payload)
@@ -0,0 +1,46 @@
1
+ import dataclasses
2
+ from contextlib import contextmanager
3
+ from unittest.mock import MagicMock
4
+
5
+ from lsrestclient import LsRestClient
6
+
7
+
8
+ @dataclasses.dataclass
9
+ class LsRestClientMockModel:
10
+ mock_name: str
11
+ client: LsRestClient
12
+ mock: MagicMock
13
+
14
+
15
+ class LsRestClientMocker(object):
16
+ def __init__(self):
17
+ super().__init__()
18
+ self.mocks = {}
19
+
20
+ def mock(self, client: LsRestClient | str, method: str, url: str, *args, **kwargs):
21
+ if isinstance(client, str):
22
+ client = LsRestClient.client(client)
23
+
24
+ mock_name = LsRestClient.mock_name(client.name, method, url)
25
+ mock = MagicMock(*args, **kwargs)
26
+ client.mock(mock_name, mock)
27
+ self.mocks[mock_name] = LsRestClientMockModel(client=client, mock_name=mock_name, mock=mock)
28
+ return mock
29
+
30
+ def unmock_all(self):
31
+ for mock_name, mock_model in self.mocks.items():
32
+ mock_model.client.unmock(mock_name)
33
+ self.mocks = {}
34
+
35
+ def unmock(self, client: LsRestClient, method: str, url: str):
36
+ mock_name = LsRestClient.mock_name(client.name, method, url)
37
+ mock_model = self.mocks[mock_name]
38
+ mock_model.client.unmock(mock_name)
39
+ del self.mocks[mock_name]
40
+
41
+
42
+ @contextmanager
43
+ def lsrestclient_mock_context():
44
+ mocker = LsRestClientMocker()
45
+ yield mocker
46
+ mocker.unmock_all()
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "lsrestclient"
3
- version = "1.3.0"
3
+ version = "1.3.2"
4
4
  description = "REST Api Client"
5
5
  authors = ["mba <bartel@electronic-shop.lu>"]
6
6
  readme = "README.md"
@@ -1,43 +0,0 @@
1
- import dataclasses
2
- from contextlib import contextmanager
3
- from unittest.mock import MagicMock
4
-
5
- from lsrestclient import LsRestClient
6
-
7
-
8
- @dataclasses.dataclass
9
- class LsRestClientMockModel:
10
- mock_name: str
11
- client: LsRestClient
12
- mock: MagicMock
13
-
14
-
15
- class LsRestClientMocker(object):
16
- def __init__(self):
17
- super().__init__()
18
- self.mocks = {}
19
-
20
- def mock(self, client: LsRestClient, method: str, url: str, *args, **kwargs):
21
- mock_name = LsRestClient.mock_name(client.name, method, url)
22
- mock = MagicMock(*args, **kwargs)
23
- client.mock(mock_name, mock)
24
- self.mocks[mock_name] = LsRestClientMockModel(client=client, mock_name=mock_name, mock=mock)
25
- return mock
26
-
27
- def unmock_all(self):
28
- for mock_name, mock_model in self.mocks.items():
29
- mock_model.client.unmock(mock_name)
30
- self.mocks = {}
31
-
32
- def unmock(self, client: LsRestClient, method: str, url: str):
33
- mock_name = LsRestClient.mock_name(client.name, method, url)
34
- mock_model = self.mocks[mock_name]
35
- mock_model.client.unmock(mock_name)
36
- del self.mocks[mock_name]
37
-
38
-
39
- @contextmanager
40
- def lsrestclient_mock_context():
41
- mocker = LsRestClientMocker()
42
- yield mocker
43
- mocker.unmock_all()
File without changes