lsrestclient 1.0.1__tar.gz → 1.2.0__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.
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/PKG-INFO +1 -1
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/client.py +25 -6
- lsrestclient-1.2.0/lsrestclient/fixtures.py +12 -0
- lsrestclient-1.2.0/lsrestclient/mock.py +36 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/pyproject.toml +4 -1
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/README.md +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/__init__.py +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/auth.py +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/contexts/__init__.py +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/contexts/bearer_token.py +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/exceptions.py +0 -0
- {lsrestclient-1.0.1 → lsrestclient-1.2.0}/lsrestclient/response.py +0 -0
@@ -1,5 +1,6 @@
|
|
1
1
|
import os
|
2
2
|
from typing import Optional, Dict, Any
|
3
|
+
from unittest.mock import MagicMock
|
3
4
|
|
4
5
|
import lsjsonclasses
|
5
6
|
import requests
|
@@ -62,12 +63,17 @@ class LsRestClient(Session):
|
|
62
63
|
) -> None:
|
63
64
|
"""Class representing a REST client for JSON API."""
|
64
65
|
|
66
|
+
self._mocks = {}
|
65
67
|
self.base_url = base_url
|
66
68
|
self.base_headers = {"content-type": "application/json"}
|
67
69
|
self.name = name
|
68
70
|
super().__init__()
|
69
71
|
self._clients[name] = self
|
70
72
|
|
73
|
+
@staticmethod
|
74
|
+
def mock_name(client_name: str, method: str, url: str) -> str:
|
75
|
+
return f"{client_name}_{method.upper()}_{url}"
|
76
|
+
|
71
77
|
def full_url(
|
72
78
|
self,
|
73
79
|
url: str,
|
@@ -91,6 +97,12 @@ class LsRestClient(Session):
|
|
91
97
|
del params[p]
|
92
98
|
return full_url.format(**url_params)
|
93
99
|
|
100
|
+
def caller(self, method: str, url: str, *args, **kwargs) -> LsRestClientResponse:
|
101
|
+
# check mocks
|
102
|
+
mock = self._mocks.get(self.mock_name(self.name, method, url), None)
|
103
|
+
func = mock if mock is not None else self.request
|
104
|
+
return func(method, url, *args, **kwargs)
|
105
|
+
|
94
106
|
def request(
|
95
107
|
self,
|
96
108
|
method: str,
|
@@ -173,7 +185,7 @@ class LsRestClient(Session):
|
|
173
185
|
:param kwargs: Additional keyword arguments for the request.
|
174
186
|
:return: The response object of type LsRestClientResponse.
|
175
187
|
"""
|
176
|
-
return self.
|
188
|
+
return self.caller("GET", *args, **kwargs)
|
177
189
|
|
178
190
|
def post(self, *args, **kwargs) -> LsRestClientResponse:
|
179
191
|
"""
|
@@ -183,7 +195,7 @@ class LsRestClient(Session):
|
|
183
195
|
:param kwargs: The keyword arguments for the POST request, including headers, body, and any other parameters.
|
184
196
|
:return: An instance of LsRestClientResponse, representing the response from the POST request.
|
185
197
|
"""
|
186
|
-
return self.
|
198
|
+
return self.caller("POST", *args, **kwargs)
|
187
199
|
|
188
200
|
def put(self, *args, **kwargs) -> LsRestClientResponse:
|
189
201
|
"""
|
@@ -193,7 +205,7 @@ class LsRestClient(Session):
|
|
193
205
|
:param kwargs: The keyword arguments to be passed to the request.
|
194
206
|
:return: An instance of LsRestClientResponse representing the response from the server.
|
195
207
|
"""
|
196
|
-
return self.
|
208
|
+
return self.caller("PUT", *args, **kwargs)
|
197
209
|
|
198
210
|
def patch(self, *args, **kwargs) -> LsRestClientResponse:
|
199
211
|
"""
|
@@ -203,7 +215,7 @@ class LsRestClient(Session):
|
|
203
215
|
:param kwargs: The keyword arguments for the PATCH request.
|
204
216
|
:return: The response from the PATCH request as an `LsRestClientResponse` object.
|
205
217
|
"""
|
206
|
-
return self.
|
218
|
+
return self.caller("PATCH", *args, **kwargs)
|
207
219
|
|
208
220
|
def delete(self, *args, **kwargs) -> LsRestClientResponse:
|
209
221
|
"""
|
@@ -213,7 +225,7 @@ class LsRestClient(Session):
|
|
213
225
|
:param kwargs: Arbitrary keyword arguments.
|
214
226
|
:return: An instance of LsRestClientResponse.
|
215
227
|
"""
|
216
|
-
return self.
|
228
|
+
return self.caller("DELETE", *args, **kwargs)
|
217
229
|
|
218
230
|
def options(self, *args, **kwargs) -> LsRestClientResponse:
|
219
231
|
"""
|
@@ -223,7 +235,7 @@ class LsRestClient(Session):
|
|
223
235
|
:param kwargs: Additional keyword arguments to be passed to the request.
|
224
236
|
:return: LsRestClientResponse object containing the response of the OPTIONS request.
|
225
237
|
"""
|
226
|
-
return self.
|
238
|
+
return self.caller("OPTIONS", *args, **kwargs)
|
227
239
|
|
228
240
|
def head(
|
229
241
|
self,
|
@@ -238,3 +250,10 @@ class LsRestClient(Session):
|
|
238
250
|
:return: The response object from the request.
|
239
251
|
"""
|
240
252
|
return self.request("HEAD", *args, **kwargs)
|
253
|
+
|
254
|
+
def mock(self, mock_name: str, mock: MagicMock):
|
255
|
+
self._mocks[mock_name] = mock
|
256
|
+
|
257
|
+
def unmock(self, mock_name: str):
|
258
|
+
if mock_name in self._mocks:
|
259
|
+
del self._mocks[mock_name]
|
@@ -0,0 +1,36 @@
|
|
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
|
+
|
31
|
+
|
32
|
+
@contextmanager
|
33
|
+
def lsrestclient_mock_context():
|
34
|
+
mocker = LsRestClientMocker()
|
35
|
+
yield mocker
|
36
|
+
mocker.unmock_all()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "lsrestclient"
|
3
|
-
version = "1.0
|
3
|
+
version = "1.2.0"
|
4
4
|
description = "REST Api Client"
|
5
5
|
authors = ["mba <bartel@electronic-shop.lu>"]
|
6
6
|
readme = "README.md"
|
@@ -32,3 +32,6 @@ log_cli_format = "[%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
|
|
32
32
|
|
33
33
|
[tool.black]
|
34
34
|
line-length = 120
|
35
|
+
|
36
|
+
[tool.poetry.plugins.pytest11]
|
37
|
+
"lsrestclient" = "lsrestclient"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|