lsrestclient 1.0.1__py3-none-any.whl → 1.2.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lsrestclient/client.py +25 -6
- lsrestclient/fixtures.py +12 -0
- lsrestclient/mock.py +36 -0
- {lsrestclient-1.0.1.dist-info → lsrestclient-1.2.0.dist-info}/METADATA +1 -1
- lsrestclient-1.2.0.dist-info/RECORD +13 -0
- lsrestclient-1.2.0.dist-info/entry_points.txt +3 -0
- lsrestclient-1.0.1.dist-info/RECORD +0 -10
- {lsrestclient-1.0.1.dist-info → lsrestclient-1.2.0.dist-info}/WHEEL +0 -0
lsrestclient/client.py
CHANGED
@@ -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]
|
lsrestclient/fixtures.py
ADDED
lsrestclient/mock.py
ADDED
@@ -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()
|
@@ -0,0 +1,13 @@
|
|
1
|
+
lsrestclient/__init__.py,sha256=lI62SHmG0m-iukB5UEwdN5dO4cKnDasOt-TmR1rgaWI,72
|
2
|
+
lsrestclient/auth.py,sha256=-XT9OajBudLDZJ7tZyM0E5EXF2R1Kc-jyeKMf24wyJA,403
|
3
|
+
lsrestclient/client.py,sha256=cQXgRZ7oktHR7u8i9sib9FhBV5JxdmA-QOz6UGkYzUc,9922
|
4
|
+
lsrestclient/contexts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
+
lsrestclient/contexts/bearer_token.py,sha256=GZZOzAI2Ng_9DvFCbhv1Wwb8wJ1AYCca3fQeNtt2NaU,753
|
6
|
+
lsrestclient/exceptions.py,sha256=2Nw3eISrpBCdhHWOZTBjttEtPkJmTpRoPis72VO-R_g,979
|
7
|
+
lsrestclient/fixtures.py,sha256=dFkAYQXL6xqTOwRofb03Nsu_cIjq1sG10Rh8dxWfz9s,239
|
8
|
+
lsrestclient/mock.py,sha256=T9QYFdkKidLLKOeQMhwvMYY5pANQpHLXmkcLOtxd5Q4,971
|
9
|
+
lsrestclient/response.py,sha256=IXjIgb7ySF-1FdAtnOKRyF3k9LJRB3OFyzIIZI1GNxM,1816
|
10
|
+
lsrestclient-1.2.0.dist-info/METADATA,sha256=dRRu2ymZD198uSbOuF3Fjmmn1Sd2z0nRCb6lj44KOqg,6672
|
11
|
+
lsrestclient-1.2.0.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
|
12
|
+
lsrestclient-1.2.0.dist-info/entry_points.txt,sha256=zyMRgI1XpL7R013uDs4KUVTa1XL7AfzJ_-LfRPgFCXM,38
|
13
|
+
lsrestclient-1.2.0.dist-info/RECORD,,
|
@@ -1,10 +0,0 @@
|
|
1
|
-
lsrestclient/__init__.py,sha256=lI62SHmG0m-iukB5UEwdN5dO4cKnDasOt-TmR1rgaWI,72
|
2
|
-
lsrestclient/auth.py,sha256=-XT9OajBudLDZJ7tZyM0E5EXF2R1Kc-jyeKMf24wyJA,403
|
3
|
-
lsrestclient/client.py,sha256=4shMxnMiLtJXpTScb9LCVsqHvbUnUWKlaetKTK5-OwE,9225
|
4
|
-
lsrestclient/contexts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
-
lsrestclient/contexts/bearer_token.py,sha256=GZZOzAI2Ng_9DvFCbhv1Wwb8wJ1AYCca3fQeNtt2NaU,753
|
6
|
-
lsrestclient/exceptions.py,sha256=2Nw3eISrpBCdhHWOZTBjttEtPkJmTpRoPis72VO-R_g,979
|
7
|
-
lsrestclient/response.py,sha256=IXjIgb7ySF-1FdAtnOKRyF3k9LJRB3OFyzIIZI1GNxM,1816
|
8
|
-
lsrestclient-1.0.1.dist-info/METADATA,sha256=8MTtWZE5YJ__Jme1PCR92MMEftaq9D76oJpexiAshmc,6672
|
9
|
-
lsrestclient-1.0.1.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
|
10
|
-
lsrestclient-1.0.1.dist-info/RECORD,,
|
File without changes
|