Advertisement
evstratbg

Untitled

May 17th, 2024
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. import requests
  2.  
  3. class APIRequester:
  4.     def get(self, url, params=None, headers=None):
  5.         r"""
  6.        Sends a GET request.
  7.  
  8.        Args:
  9.            url (str): The URL to send the GET request to.
  10.            params (dict, optional): The URL parameters to append to the URL. Defaults to None.
  11.            headers (dict, optional): The headers to send with the request. Defaults to None.
  12.  
  13.        Returns:
  14.            response (`requests.Response`): The response of the GET request. See [requests.Response](https://requests.readthedocs.io/en/latest/api/#requests.Response).
  15.        Example:
  16.            ```python
  17.            from api_requester import APIRequester
  18.  
  19.            requester = APIRequester()
  20.            response = requester.get('https://api.example.com/data', params={'key': 'value'})
  21.            print(response.status_code)
  22.            ```
  23.        """
  24.         response = requests.get(url, params=params, headers=headers)
  25.         return response
  26.  
  27.     def post(self, url, data=None, json=None, headers=None):
  28.         """
  29.        Sends a POST request.
  30.  
  31.        Args:
  32.            url (str): The URL to send the POST request to.
  33.            data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
  34.            json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
  35.            headers (dict, optional): The headers to send with the request. Defaults to None.
  36.  
  37.        Returns:
  38.            requests.Response: The response of the POST request.
  39.  
  40.        Example:
  41.            ```python
  42.            requester = APIRequester()
  43.            response = requester.post('https://api.example.com/data', json={'key': 'value'})
  44.            print(response.status_code)
  45.            ```
  46.        """
  47.         response = requests.post(url, data=data, json=json, headers=headers)
  48.         return response
  49.  
  50.     def put(self, url, data=None, json=None, headers=None):
  51.         """
  52.        Sends a PUT request.
  53.  
  54.        Args:
  55.            url (str): The URL to send the PUT request to.
  56.            data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
  57.            json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
  58.            headers (dict, optional): The headers to send with the request. Defaults to None.
  59.  
  60.        Returns:
  61.            requests.Response: The response of the PUT request.
  62.  
  63.        Example:
  64.            ```python
  65.            requester = APIRequester()
  66.            response = requester.put('https://api.example.com/data', json={'key': 'value'})
  67.            print(response.status_code)
  68.            ```
  69.        """
  70.         response = requests.put(url, data=data, json=json, headers=headers)
  71.         return response
  72.  
  73.     def patch(self, url, data=None, json=None, headers=None):
  74.         """
  75.        Sends a PATCH request.
  76.  
  77.        Args:
  78.            url (str): The URL to send the PATCH request to.
  79.            data (dict or bytes, optional): The form data to send in the body of the request. Defaults to None.
  80.            json (dict, optional): The JSON data to send in the body of the request. Defaults to None.
  81.            headers (dict, optional): The headers to send with the request. Defaults to None.
  82.  
  83.        Returns:
  84.            requests.Response: The response of the PATCH request.
  85.  
  86.        Example:
  87.            ```python
  88.            requester = APIRequester()
  89.            response = requester.patch('https://api.example.com/data', json={'key': 'value'})
  90.            print(response.status_code)
  91.            ```
  92.        """
  93.         response = requests.patch(url, data=data, json=json, headers=headers)
  94.         return response
  95.  
  96.     def trace(self, url, headers=None):
  97.         """
  98.        Sends a TRACE request.
  99.  
  100.        Args:
  101.            url (str): The URL to send the TRACE request to.
  102.            headers (dict, optional): The headers to send with the request. Defaults to None.
  103.  
  104.        Returns:
  105.            requests.Response: The response of the TRACE request.
  106.  
  107.        Example:
  108.            ```python
  109.            requester = APIRequester()
  110.            response = requester.trace('https://api.example.com/trace')
  111.            print(response.status_code)
  112.            ```
  113.        """
  114.         response = requests.request('TRACE', url, headers=headers)
  115.         return response
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement