lsrestclient 1.3.0__tar.gz → 1.3.2__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/PKG-INFO +1 -1
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/auth.py +6 -2
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/exceptions.py +9 -5
- lsrestclient-1.3.2/lsrestclient/mock.py +46 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/pyproject.toml +1 -1
- lsrestclient-1.3.0/lsrestclient/mock.py +0 -43
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/README.md +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/__init__.py +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/client.py +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/contexts/__init__.py +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/contexts/bearer_token.py +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/fixtures.py +0 -0
- {lsrestclient-1.3.0 → lsrestclient-1.3.2}/lsrestclient/response.py +0 -0
@@ -1,10 +1,14 @@
|
|
1
1
|
from lsrestclient import LsRestClient, DownStreamError
|
2
2
|
|
3
3
|
|
4
|
-
def
|
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=
|
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
|
-
|
16
|
+
url (Optional[str]): The URL that the connection could not be established to.
|
17
17
|
|
18
18
|
Attributes:
|
19
|
-
|
19
|
+
url (Optional[str]): The URL that the connection could not be established to.
|
20
20
|
|
21
21
|
Raises:
|
22
|
-
|
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
|
-
|
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,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
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|