Skip to main content

To handle API request retries due to timeouts

Project description

Hello, I'm Ryo 👋

Welcome To ApiRetrys 🔥

Version

Are you annoyed because your request has an error due to internet problems? I think this could be the answer

Feature ✨

  • automatically repeats if the request times out

  • The number of repetitions and waiting time can be adjusted and default values ​​are provided

  • maintain flexibility by returning all responses received

  • provides logs to make monitoring easier, which can be turned on or not

  • provides all the methods in the request

  • can handle to may requests

  • You can handle forbidden (403) by refreshing the session with a request to the main URL

Tech 💻

  • requests is an easy-to-use Python library for interacting with APIs and making HTTP requests

Requirement ⚙️

Installation 🛠️

pip install ApiRetrys

How To Usage 🤔

Basic use

from ApiRetrys import ApiRetry



api = ApiRetry(show_logs=True)

response = api.get(url='https://github.com/')



# or you can define data types



from ApiRetrys import ApiRetry

from requests import Response



api = ApiRetry(show_logs=True)

response: Response = api.get(url='https://github.com/',)



# or you want to manually set the max_retries

# by defaulth (max_retries) is 5

response: Response = api.get(url='https://github.com/', max_retries=10)

if you set param (show_logs=True) then a log will come out like this



If the error exceeds max_retries then it will raise the exception MaxRetryExceptions



With other parameters

from ApiRetrys import ApiRetry

from requests import Response



headers = {

   "PUBLIC-CSRF-TOKEN": '7giS6UPv3Rf2l9fclp2wFzCEI',

   "User-Agent": user_agent.random

}



payload = {

    "query":"anime",

    "page":1,

    "per_page":50,

    "sorting":"likes",

    "pro_first":"1",

    "filters":[],

    "additional_fields":[]

    }



cookies = {

    'PRIVATE-CSRF-TOKEN': '8Mc45%2BAa3Vd',

    'cf_clearance': 'rpQn91kojLw_ZCzrgjP',

    '__stripe_mid': '0adecf01-7471-434e-aa12-6c2d33cbb216fd19aa',

    'g_state': '{"i_p":1702902016560,"i_l":1}',

    'visitor-uuid': 'fbf18513-e803-',

    '__cf_bm': '_ukyfnf1.He..wDhIxF',

    '__stripe_sid': 'c7956d65-b70f-4e',

    'referrer-host': 'www.google.com'

}



api = ApiRetry(show_logs=False)

response: Response = api.post(url='https://github.com/', data=payload, cookies=cookies)

examples of more complex usage as well as the use of handlers for many requests and forbidden

from ApiRetrys import ApiRetry

from requests import Response



class Main:

    def __init__(self) -> None:



        self.api = ApiRetry(



            # parameters to display logs or not

            show_logs=True,



            # parameter whether you want to use the default header,

            # but only contain the user agent

            defaulth_headers=True,



            # param to handle responses 403 and 429,

            # if false it will be returned even though the responses are 403 and 429

            handle_forbidden=True,



            # whatever response you want to handle

            # This is the default value, if it is not entered there is no problem

            codes_handler=[403, 429],



            # URL to refresh the session if you get a 403 response

            # (it is recommended to use the main URL of the destination website)

            redirect_url='https://github.com/')

        ...





    def execute(self, url: str):

        response: Response = self.api.get(url)



        print(response)

        ...



if __name__ == '__main__':

    main = Main()

    main.execute('https://github.com/ryyos/ApiRetrys')

explanation

when you try to make a request to the url https://github.com/ryyos/ApiRetrys

but you get a 403 response because your session does not have CSRF-TOKEN, or because there are too many requests, then the code will handle it by trying to refresh the session by making requests to the main URL in the redirect_url parameter

🚀Structure


│   LICENSE

│   README.md

│   setup.py

│

└───ApiRetrys

        ApiRetrys.py

        Logs.py

        MaxRetryExceptions.py

        __init__.py

Author

👤 Rio Dwi Saputra

Ryo's LinkedIn Ryo's Instagram

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ApiRetrys-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

ApiRetrys-0.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file ApiRetrys-0.0.1.tar.gz.

File metadata

  • Download URL: ApiRetrys-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ApiRetrys-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e032bb89bd25b0b8f07869b756d891290a57b919690a5b6d9f4842a349bc06fb
MD5 a5c9a40068ec66bc3dd2f13be83280eb
BLAKE2b-256 fc8e5c2e78fa5331d46a1742d9bada527b8251c659bf10bcee00b81697a11e37

See more details on using hashes here.

File details

Details for the file ApiRetrys-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ApiRetrys-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ApiRetrys-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5aedfe81a6341c62df4361465b2c4dfcec7d867e0971f43d0844e4c72e98a37a
MD5 bd46e270d1e94e19c9c07a04454739dc
BLAKE2b-256 35856f5f8d97a0032b5f3b30bc4674d55008408ba7d72bec257eeb6a9c44368f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page