Skip to content

Commit 5a5c446

Browse files
authored
Add patch request (#104)
1 parent 6c24b54 commit 5a5c446

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

pyms/flask/services/requests.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,50 @@ def put_for_object(self, url, path_params=None, data=None, headers=None, **kwarg
271271
response = self.put(url, path_params=path_params, data=data, headers=headers, **kwargs)
272272
return self.parse_response(response)
273273

274+
@retry
275+
def patch(self, url, path_params=None, data=None, headers=None, **kwargs):
276+
"""Sends a PATCH request.
277+
278+
:param url: URL for the new :class:`Request` object. Could contain path parameters
279+
:param path_params: (optional) Dictionary, list of tuples with path parameters values to compose url
280+
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
281+
object to send in the body of the :class:`Request`.
282+
:param json: (optional) json data to send in the body of the :class:`Request`.
283+
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
284+
:param kwargs: Optional arguments that ``request`` takes.
285+
:return: :class:`Response <Response>` object
286+
:rtype: requests.Response
287+
"""
288+
289+
full_url = self._build_url(url, path_params)
290+
headers = self._get_headers(headers)
291+
headers = self.insert_trace_headers(headers)
292+
logger.debug("Patch with url {}, data {}, headers {}, kwargs {}".format(full_url, data, headers,
293+
kwargs))
294+
295+
session = requests.Session()
296+
response = self.requests(session=session).patch(full_url, data, headers=headers, **kwargs)
297+
logger.debug("Response {}".format(response))
298+
299+
return response
300+
301+
def patch_for_object(self, url, path_params=None, data=None, headers=None, **kwargs):
302+
"""Sends a PATCH request and returns the json representation found in response's content data node.
303+
304+
:param url: URL for the new :class:`Request` object. Could contain path parameters
305+
:param path_params: (optional) Dictionary, list of tuples with path parameters values to compose url
306+
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
307+
object to send in the body of the :class:`Request`.
308+
:param json: (optional) json data to send in the body of the :class:`Request`.
309+
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
310+
:param kwargs: Optional arguments that ``request`` takes.
311+
:return: :class:`Response <Response>` object
312+
:rtype: requests.Response
313+
"""
314+
315+
response = self.patch(url, path_params=path_params, data=data, headers=headers, **kwargs)
316+
return self.parse_response(response)
317+
274318
@retry
275319
def delete(self, url, path_params=None, headers=None, **kwargs):
276320
"""Sends a DELETE request.

tests/test_requests.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,65 @@ def test_put_for_object_with_valid_data(self, mock_request):
222222

223223
self.assertEqual(expected, response)
224224

225+
@requests_mock.Mocker()
226+
def test_patch(self, mock_request):
227+
url = "http://www.my-site.com/users/{user-id}"
228+
path_params = {'user-id': 123}
229+
full_url = "http://www.my-site.com/users/123"
230+
user = {'name': 'Peter', 'email': 'peter@my-site.com'}
231+
text = json.dumps({'data': {'id': 123, 'name': 'Peter', 'email': 'peter@my-site.com'}})
232+
233+
with self.app.app_context():
234+
mock_request.patch(full_url, text=text, status_code=200)
235+
response = self.request.patch(url, path_params, json=user)
236+
237+
self.assertEqual(200, response.status_code)
238+
self.assertEqual(text, response.text)
239+
240+
@requests_mock.Mocker()
241+
def test_patch_for_object_without_json(self, mock_request):
242+
url = "http://www.my-site.com/users/{user-id}"
243+
path_params = {'user-id': 123}
244+
full_url = "http://www.my-site.com/users/123"
245+
user = {'name': 'Peter', 'email': 'peter@my-site.com'}
246+
expected = {}
247+
248+
with self.app.app_context():
249+
mock_request.patch(full_url, status_code=200)
250+
response = self.request.patch_for_object(url, path_params, json=user)
251+
252+
self.assertEqual(expected, response)
253+
254+
@requests_mock.Mocker()
255+
def test_patch_for_object_without_valid_json_data(self, mock_request):
256+
url = "http://www.my-site.com/users/{user-id}"
257+
path_params = {'user-id': 123}
258+
full_url = "http://www.my-site.com/users/123"
259+
user = {'name': 'Peter', 'email': 'peter@my-site.com'}
260+
text = json.dumps({'another_data': {'id': 123, 'name': 'Peter', 'email': 'peter@my-site.com.com'}})
261+
expected = {}
262+
263+
with self.app.app_context():
264+
mock_request.patch(full_url, text=text, status_code=200)
265+
response = self.request.patch_for_object(url, path_params, json=user)
266+
267+
self.assertEqual(expected, response)
268+
269+
@requests_mock.Mocker()
270+
def test_patch_for_object_with_valid_data(self, mock_request):
271+
url = "http://www.my-site.com/users/{user-id}"
272+
path_params = {'user-id': 123}
273+
full_url = "http://www.my-site.com/users/123"
274+
user = {'name': 'Peter', 'email': 'peter@my-site.com'}
275+
text = json.dumps({'data': {'id': 123, 'name': 'Peter', 'email': 'peter@my-site.com.com'}})
276+
expected = {'id': 123, 'name': 'Peter', 'email': 'peter@my-site.com.com'}
277+
278+
with self.app.app_context():
279+
mock_request.patch(full_url, text=text, status_code=200)
280+
response = self.request.patch_for_object(url, path_params, json=user)
281+
282+
self.assertEqual(expected, response)
283+
225284
@requests_mock.Mocker()
226285
def test_delete(self, mock_request):
227286
url = "http://www.my-site.com/users/{user-id}"

0 commit comments

Comments
 (0)