|
| 1 | +"""This class takes a base URL as an argument when it's initialized, |
| 2 | +which is the endpoint for the RESTFUL API that we'll be interacting with. |
| 3 | +The create(), read(), update(), and delete() methods each correspond to |
| 4 | +the CRUD operations that can be performed on the API """ |
| 5 | + |
| 6 | +import requests |
| 7 | +import json |
| 8 | + |
| 9 | + |
| 10 | +class HttpClient: |
| 11 | + """ |
| 12 | + This class takes a base URL as an argument when it's initialized, |
| 13 | + which is the endpoint for the RESTFUL API that |
| 14 | + we'll be interacting with. The create(), read(), update(), and delete() |
| 15 | + methods each correspond to the CRUD |
| 16 | + operations that can be performed on the API """ |
| 17 | + |
| 18 | + def __init__(self, endpoint): |
| 19 | + #init method |
| 20 | + self.url="user" |
| 21 | + self.endpoint = endpoint |
| 22 | + self.failure_retry = 0 |
| 23 | + self.exceptions = True |
| 24 | + self.errors = True |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + def get(self, url, headers=None, params=None): |
| 29 | + """ |
| 30 | + Perform an HTTP GET request with the specified URL and parameters. |
| 31 | +
|
| 32 | + :param url: The URL to send the request to. |
| 33 | + :param headers: Optional dictionary of headers to include in the request. |
| 34 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 35 | + :return: The response from the server. |
| 36 | + """ |
| 37 | + return self._call_request('GET', url, headers=headers, params=params) |
| 38 | + |
| 39 | + def put(self, url, headers=None, params=None, data=None, json=None): |
| 40 | + """ |
| 41 | + Perform an HTTP PUT request with the specified URL and parameters. |
| 42 | +
|
| 43 | + :param url: The URL to send the request to. |
| 44 | + :param headers: Optional dictionary of headers to include in the request. |
| 45 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 46 | + :param data: Optional dictionary, list of tuples, or bytes to include in the body of the request. |
| 47 | + :param json: Optional JSON data to include in the body of the request. |
| 48 | + :return: The response from the server. |
| 49 | + """ |
| 50 | + return self._call_request('PUT', url, headers=headers, params=params, data=data, json=json) |
| 51 | + |
| 52 | + def post(self, url, headers=None, params=None, data=None, json=None): |
| 53 | + """ |
| 54 | + Perform an HTTP POST request with the specified URL and parameters. |
| 55 | +
|
| 56 | + :param url: The URL to send the request to. |
| 57 | + :param headers: Optional dictionary of headers to include in the request. |
| 58 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 59 | + :param data: Optional dictionary, list of tuples, or bytes to include in the body of the request. |
| 60 | + :param json: Optional JSON data to include in the body of the request. |
| 61 | + :return: The response from the server. |
| 62 | + """ |
| 63 | + return self._call_request('POST', url, headers=headers, params=params, data=data, json=json) |
| 64 | + |
| 65 | + def delete(self, url, headers=None, params=None): |
| 66 | + """ |
| 67 | + Perform an HTTP DELETE request with the specified URL and parameters. |
| 68 | +
|
| 69 | + :param url: The URL to send the request to. |
| 70 | + :param headers: Optional dictionary of headers to include in the request. |
| 71 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 72 | + :return: The response from the server. |
| 73 | + """ |
| 74 | + return self._call_request('DELETE', url, headers=headers, params=params) |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + def _call_request(self, method, url_path, headers=None, params=None, data=None, json=None): |
| 79 | + url = f"{self.endpoint}/{url_path}" |
| 80 | + retries = self.failure_retry + 1 |
| 81 | + |
| 82 | + while retries > 0: |
| 83 | + try: |
| 84 | + response = requests.request(method, url, data=data, headers=headers, params=params, json=json) |
| 85 | + |
| 86 | + if response.status_code >= 400: |
| 87 | + if self.errors: |
| 88 | + raise Exception(f"API returned an error: {response.text}") |
| 89 | + elif retries > 1: |
| 90 | + retries -= 1 |
| 91 | + else: |
| 92 | + return None |
| 93 | + else: |
| 94 | + return response.json() |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + if self.exceptions: |
| 98 | + raise e |
| 99 | + elif retries > 1: |
| 100 | + retries -= 1 |
| 101 | + else: |
| 102 | + return None |
| 103 | + |
| 104 | + |
0 commit comments