lsrestclient 1.5.1__tar.gz → 1.7.0__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/PKG-INFO +7 -5
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/client.py +2 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/exceptions.py +7 -3
- lsrestclient-1.7.0/lsrestclient/mock.py +47 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/response.py +11 -2
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/pyproject.toml +5 -5
- lsrestclient-1.5.1/lsrestclient/mock.py +0 -46
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/README.md +0 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/__init__.py +0 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/auth.py +0 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/contexts/__init__.py +0 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/contexts/bearer_token.py +0 -0
- {lsrestclient-1.5.1 → lsrestclient-1.7.0}/lsrestclient/fixtures.py +0 -0
@@ -1,16 +1,18 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: lsrestclient
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.7.0
|
4
4
|
Summary: REST Api Client
|
5
5
|
Author: mba
|
6
6
|
Author-email: bartel@electronic-shop.lu
|
7
|
-
Requires-Python: >=3.
|
7
|
+
Requires-Python: >=3.9,<4.0
|
8
8
|
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
9
11
|
Classifier: Programming Language :: Python :: 3.11
|
10
|
-
Requires-Dist: lsjsonclasses (>=2.0.
|
11
|
-
Requires-Dist: pydash (
|
12
|
+
Requires-Dist: lsjsonclasses (>=2.0.1,<3.0.0)
|
13
|
+
Requires-Dist: pydash (>4.0.0)
|
12
14
|
Requires-Dist: requests (>=2.31.0,<3.0.0)
|
13
|
-
Requires-Dist: webexception (>=1.0.
|
15
|
+
Requires-Dist: webexception (>=1.0.5,<2.0.0)
|
14
16
|
Description-Content-Type: text/markdown
|
15
17
|
|
16
18
|
# lsrestclient
|
@@ -66,6 +66,7 @@ class LsRestClient(Session):
|
|
66
66
|
self._mocks = {}
|
67
67
|
self.base_url = base_url
|
68
68
|
self.base_headers = {"content-type": "application/json"}
|
69
|
+
self.base_kwargs = {}
|
69
70
|
self.name = name
|
70
71
|
super().__init__()
|
71
72
|
self._clients[name] = self
|
@@ -164,6 +165,7 @@ class LsRestClient(Session):
|
|
164
165
|
|
165
166
|
headers = self.base_headers | bearer_headers | kwargs.get("headers", {})
|
166
167
|
|
168
|
+
kwargs |= self.base_kwargs
|
167
169
|
kwargs |= dict(headers=headers)
|
168
170
|
|
169
171
|
# params
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import contextlib
|
2
|
+
import logging
|
2
3
|
from typing import Optional, List, Type
|
3
4
|
|
4
5
|
import pydash
|
5
6
|
from webexception.webexception import WebException
|
6
|
-
import logging
|
7
7
|
|
8
8
|
log = logging.getLogger(__name__)
|
9
9
|
|
@@ -43,8 +43,12 @@ class DownStreamError(Exception):
|
|
43
43
|
|
44
44
|
|
45
45
|
@contextlib.contextmanager
|
46
|
-
def raise_errors(r, exceptions: List[Type[Exception]]):
|
47
|
-
|
46
|
+
def raise_errors(r, exceptions: Optional[List[Type[Exception]]] = None):
|
47
|
+
if exceptions is None:
|
48
|
+
exceptions_by_class = {}
|
49
|
+
else:
|
50
|
+
exceptions_by_class = {e.__name__: e for e in exceptions}
|
51
|
+
|
48
52
|
if r.status_code < 399:
|
49
53
|
yield r
|
50
54
|
else:
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import dataclasses
|
2
|
+
from contextlib import contextmanager
|
3
|
+
from typing import Union
|
4
|
+
from unittest.mock import MagicMock
|
5
|
+
|
6
|
+
from lsrestclient import LsRestClient
|
7
|
+
|
8
|
+
|
9
|
+
@dataclasses.dataclass
|
10
|
+
class LsRestClientMockModel:
|
11
|
+
mock_name: str
|
12
|
+
client: LsRestClient
|
13
|
+
mock: MagicMock
|
14
|
+
|
15
|
+
|
16
|
+
class LsRestClientMocker(object):
|
17
|
+
def __init__(self):
|
18
|
+
super().__init__()
|
19
|
+
self.mocks = {}
|
20
|
+
|
21
|
+
def mock(self, client: Union[LsRestClient, str], method: str, url: str, *args, **kwargs):
|
22
|
+
if isinstance(client, str):
|
23
|
+
client = LsRestClient.client(client)
|
24
|
+
|
25
|
+
mock_name = LsRestClient.mock_name(client.name, method, url)
|
26
|
+
mock = MagicMock(*args, **kwargs)
|
27
|
+
client.mock(mock_name, mock)
|
28
|
+
self.mocks[mock_name] = LsRestClientMockModel(client=client, mock_name=mock_name, mock=mock)
|
29
|
+
return mock
|
30
|
+
|
31
|
+
def unmock_all(self):
|
32
|
+
for mock_name, mock_model in self.mocks.items():
|
33
|
+
mock_model.client.unmock(mock_name)
|
34
|
+
self.mocks = {}
|
35
|
+
|
36
|
+
def unmock(self, client: LsRestClient, method: str, url: str):
|
37
|
+
mock_name = LsRestClient.mock_name(client.name, method, url)
|
38
|
+
mock_model = self.mocks[mock_name]
|
39
|
+
mock_model.client.unmock(mock_name)
|
40
|
+
del self.mocks[mock_name]
|
41
|
+
|
42
|
+
|
43
|
+
@contextmanager
|
44
|
+
def lsrestclient_mock_context():
|
45
|
+
mocker = LsRestClientMocker()
|
46
|
+
yield mocker
|
47
|
+
mocker.unmock_all()
|
@@ -2,10 +2,10 @@ from dataclasses import dataclass
|
|
2
2
|
from typing import Optional
|
3
3
|
|
4
4
|
import lsjsonclasses
|
5
|
+
import pydash
|
5
6
|
from requests import Response
|
6
7
|
from requests.structures import CaseInsensitiveDict
|
7
8
|
|
8
|
-
|
9
9
|
@dataclass
|
10
10
|
class LsRestClientResponse:
|
11
11
|
"""
|
@@ -33,9 +33,18 @@ class LsRestClientResponse:
|
|
33
33
|
:return: An instance of LsRestClientResponse representing the response.
|
34
34
|
:rtype: LsRestClientResponse
|
35
35
|
"""
|
36
|
+
|
37
|
+
encoding = pydash.get(response, "encoding", None)
|
38
|
+
headers = pydash.get(response, "headers", None)
|
39
|
+
content_type = headers.get("Content-Type", None)
|
40
|
+
if content_type == 'application/pdf':
|
41
|
+
content = response.content
|
42
|
+
else:
|
43
|
+
content = response.content.decode("utf8" if encoding is None else encoding)
|
44
|
+
|
36
45
|
return cls(
|
37
46
|
status_code=response.status_code,
|
38
|
-
content=
|
47
|
+
content=content,
|
39
48
|
headers=response.headers,
|
40
49
|
)
|
41
50
|
|
@@ -1,16 +1,16 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "lsrestclient"
|
3
|
-
version = "1.
|
3
|
+
version = "1.7.0"
|
4
4
|
description = "REST Api Client"
|
5
5
|
authors = ["mba <bartel@electronic-shop.lu>"]
|
6
6
|
readme = "README.md"
|
7
7
|
|
8
8
|
[tool.poetry.dependencies]
|
9
|
-
python = "^3.
|
9
|
+
python = "^3.9"
|
10
10
|
requests = "^2.31.0"
|
11
|
-
lsjsonclasses = "^2.0.
|
12
|
-
pydash = "
|
13
|
-
webexception = "^1.0.
|
11
|
+
lsjsonclasses = "^2.0.1"
|
12
|
+
pydash = ">4.0.0"
|
13
|
+
webexception = "^1.0.5"
|
14
14
|
|
15
15
|
[tool.poetry.group.dev.dependencies]
|
16
16
|
pytest = "^7.4.0"
|
@@ -1,46 +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 | 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()
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|