lsrestclient 1.5.2__tar.gz → 1.7.1__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,18 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lsrestclient
3
- Version: 1.5.2
3
+ Version: 1.7.1
4
4
  Summary: REST Api Client
5
5
  Author: mba
6
6
  Author-email: bartel@electronic-shop.lu
7
- Requires-Python: >=3.11,<4.0
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.0,<3.0.0)
11
- Requires-Dist: pydash (>=7.0.6,<8.0.0)
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.4,<2.0.0)
15
+ Requires-Dist: webexception (>=1.0.5,<2.0.0)
14
16
  Description-Content-Type: text/markdown
15
17
 
16
18
  # lsrestclient
@@ -56,6 +56,9 @@ class LsRestClient(Session):
56
56
  except KeyError:
57
57
  raise Exception(f"LsRestClient with name '{name}' not initialized.")
58
58
 
59
+ def __repr__(self):
60
+ return f"<LsRestClient name:'{self.name}' base_url:'{self.base_url}'>"
61
+
59
62
  def __init__(
60
63
  self,
61
64
  base_url: str = None,
@@ -66,6 +69,7 @@ class LsRestClient(Session):
66
69
  self._mocks = {}
67
70
  self.base_url = base_url
68
71
  self.base_headers = {"content-type": "application/json"}
72
+ self.base_kwargs = {}
69
73
  self.name = name
70
74
  super().__init__()
71
75
  self._clients[name] = self
@@ -164,6 +168,7 @@ class LsRestClient(Session):
164
168
 
165
169
  headers = self.base_headers | bearer_headers | kwargs.get("headers", {})
166
170
 
171
+ kwargs |= self.base_kwargs
167
172
  kwargs |= dict(headers=headers)
168
173
 
169
174
  # 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
- exceptions_by_class = {e.__name__: e for e in exceptions}
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()
@@ -6,7 +6,6 @@ import pydash
6
6
  from requests import Response
7
7
  from requests.structures import CaseInsensitiveDict
8
8
 
9
-
10
9
  @dataclass
11
10
  class LsRestClientResponse:
12
11
  """
@@ -36,9 +35,16 @@ class LsRestClientResponse:
36
35
  """
37
36
 
38
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
+
39
45
  return cls(
40
46
  status_code=response.status_code,
41
- content=response.content.decode("utf8" if encoding is None else encoding),
47
+ content=content,
42
48
  headers=response.headers,
43
49
  )
44
50
 
@@ -1,16 +1,16 @@
1
1
  [tool.poetry]
2
2
  name = "lsrestclient"
3
- version = "1.5.2"
3
+ version = "1.7.1"
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.11"
9
+ python = "^3.9"
10
10
  requests = "^2.31.0"
11
- lsjsonclasses = "^2.0.0"
12
- pydash = "^7.0.6"
13
- webexception = "^1.0.4"
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