Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- class APIRequester:
- def get(self, url, params=None, headers=None):
- r"""
- Sends a GET request.
- Args:
- url (str): The URL to send the GET request to.
- params (dict, optional): The URL parameters to append to the URL. Defaults to None.
- headers (dict, optional): The headers to send with the request. Defaults to None.
- Returns:
- response (`requests.Response`): The response of the GET request. See [requests.Response](https://requests.readthedocs.io/en/latest/api/#requests.Response).
- Example:
- ```python
- from api_requester import APIRequester
- requester = APIRequester()
- response = requester.get('https://api.example.com/data', params={'key': 'value'})
- print(response.status_code)
- ```
- """
- response = requests.get(url, params=params, headers=headers)
- return response
- def post(self, url, data=None, json=None, headers=None):
- """
- Sends a POST request.
- Args:
- url (str): The URL to send the POST request to.
- data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
- json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
- headers (dict, optional): The headers to send with the request. Defaults to None.
- Returns:
- requests.Response: The response of the POST request.
- Example:
- ```python
- requester = APIRequester()
- response = requester.post('https://api.example.com/data', json={'key': 'value'})
- print(response.status_code)
- ```
- """
- response = requests.post(url, data=data, json=json, headers=headers)
- return response
- def put(self, url, data=None, json=None, headers=None):
- """
- Sends a PUT request.
- Args:
- url (str): The URL to send the PUT request to.
- data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
- json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
- headers (dict, optional): The headers to send with the request. Defaults to None.
- Returns:
- requests.Response: The response of the PUT request.
- Example:
- ```python
- requester = APIRequester()
- response = requester.put('https://api.example.com/data', json={'key': 'value'})
- print(response.status_code)
- ```
- """
- response = requests.put(url, data=data, json=json, headers=headers)
- return response
- def patch(self, url, data=None, json=None, headers=None):
- """
- Sends a PATCH request.
- Args:
- url (str): The URL to send the PATCH request to.
- data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
- json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
- headers (dict, optional): The headers to send with the request. Defaults to None.
- Returns:
- requests.Response: The response of the PATCH request.
- Example:
- ```python
- requester = APIRequester()
- response = requester.patch('https://api.example.com/data', json={'key': 'value'})
- print(response.status_code)
- ```
- """
- response = requests.patch(url, data=data, json=json, headers=headers)
- return response
- def trace(self, url, headers=None):
- """
- Sends a TRACE request.
- Args:
- url (str): The URL to send the TRACE request to.
- headers (dict, optional): The headers to send with the request. Defaults to None.
- Returns:
- requests.Response: The response of the TRACE request.
- Example:
- ```python
- requester = APIRequester()
- response = requester.trace('https://api.example.com/trace')
- print(response.status_code)
- ```
- """
- response = requests.request('TRACE', url, headers=headers)
- return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement