Skip to content

Commit 6870bc9

Browse files
author
Hugo Osvaldo Barrera
committed
Deduplicate some client functions
A few of these functions were pretty much a copy-paste of each other, so I've de-duplicated these and just re-use the same block for all of them. This improves readability a bit, and makes it a bit more obvious that all of these actually do the same. I've also deleted an initialiser that did nothing, since that mostly adds noise/complexity.
1 parent 642376f commit 6870bc9

1 file changed

Lines changed: 10 additions & 33 deletions

File tree

mercadopago/http/http_client.py

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class HttpClient():
1111
Default implementation to call all REST API's
1212
"""
1313

14-
def __init__(self):
15-
pass
16-
1714
@staticmethod
1815
def __get_session(max_retries):
1916
retry_strategy = Retry(
@@ -24,44 +21,24 @@ def __get_session(max_retries):
2421
http.mount("https://", HTTPAdapter(max_retries=retry_strategy))
2522
return http
2623

27-
def get(self, url, headers, params=None, timeout=None, maxretries=None): #pylint: disable=missing-function-docstring, too-many-arguments
24+
def request(self, method, url, headers, data=None, params=None, timeout=None, maxretries=None): # pylint: disable=missing-function-docstring, too-many-arguments
2825
with self.__get_session(maxretries) as session:
29-
api_result = session.get(url, params=params, headers=headers, timeout=timeout)
26+
api_result = session.request(method, url, params=params, headers=headers, timeout=timeout)
3027
response = {
3128
"status": api_result.status_code,
3229
"response": api_result.json()
3330
}
3431

3532
return response
3633

37-
def post(self, url, headers, data=None, params=None, timeout=None, maxretries=None): #pylint: disable=missing-function-docstring, too-many-arguments
38-
with self.__get_session(maxretries) as session:
39-
api_result = session.post(url, params=params, data=data,
40-
headers=headers, timeout=timeout)
41-
response = {
42-
"status": api_result.status_code,
43-
"response": api_result.json()
44-
}
34+
def get(self, url, headers, params=None, timeout=None, maxretries=None): # pylint: disable=missing-function-docstring, too-many-arguments
35+
return self.request("GET", url=url, headers=headers, params=params, timeout=timeout, maxretries=maxretries)
4536

46-
return response
37+
def post(self, url, headers, data=None, params=None, timeout=None, maxretries=None): # pylint: disable=missing-function-docstring, too-many-arguments
38+
return self.request("POST", url=url, headers=headers, data=data, params=params, timeout=timeout, maxretries=maxretries)
4739

48-
def put(self, url, headers, data=None, params=None, timeout=None, maxretries=None): #pylint: disable=missing-function-docstring, too-many-arguments
49-
with self.__get_session(maxretries) as session:
50-
api_result = session.put(url, params=params, data=data,
51-
headers=headers, timeout=timeout)
52-
response = {
53-
"status": api_result.status_code,
54-
"response": api_result.json()
55-
}
56-
57-
return response
58-
59-
def delete(self, url, headers, params=None, timeout=None, maxretries=None): #pylint: disable=missing-function-docstring, too-many-arguments
60-
with self.__get_session(maxretries) as session:
61-
api_result = session.delete(url, params=params, headers=headers, timeout=timeout)
62-
response = {
63-
"status": api_result.status_code,
64-
"response": api_result.json()
65-
}
40+
def put(self, url, headers, data=None, params=None, timeout=None, maxretries=None): # pylint: disable=missing-function-docstring, too-many-arguments
41+
return self.request("PUT", url=url, headers=headers, data=data, params=params, timeout=timeout, maxretries=maxretries)
6642

67-
return response
43+
def delete(self, url, headers, params=None, timeout=None, maxretries=None): # pylint: disable=missing-function-docstring, too-many-arguments
44+
return self.request("DELETE", url=url, headers=headers, params=params, timeout=timeout, maxretries=maxretries)

0 commit comments

Comments
 (0)