-1

enter image description hereSo i have this bit of code

statssession = requests.Session()
getstats = statssession.get('https://api.hypixel.net/player',
                                                params={'key': random.choice([key, key2]), 'uuid': playeruuid},
                                                timeout=10).json()

I'm completely new to python with no OOP experience and this bit of code throws a random time out exception for read randomly. What I want is when it throws that exception to not just break my code but retry the request instead preferably with the library from requests itself but I have no idea how to do it so here I'm asking, any help appreciated.

Mathias
  • 1
  • 2
  • 11
  • 2
    Does this answer your question? [Can I set max\_retries for requests.request?](https://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request) – aaossa Feb 14 '22 at 00:58
  • 1
    yeah it did to an extent thank you. – Mathias Feb 14 '22 at 02:48

1 Answers1

0

It looks like you're using the API incorrectly: https://api.hypixel.net/#section/Authentication/ApiKey

Try setting the key into the requests.Session headers:

statssession = requests.Session()
statssession.headers["API-Key"] = random.choice([key, key2])

To try again after timeout, you can use a try except block:

for _ in range(5):
    try:
        getstats = statssession.get(
            'https://api.hypixel.net/player',
            params = {'uuid': playeruuid},
            timeout=10).json()
        break
    except requests.exceptions.ReadTimeout:
        continue

Or you can set retries within urllib3, used by requests, as outlined here: Can I set max_retries for requests.request?

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
  • Thank you for the answer, but i'm using more than just 2 api keys and i've made it so if those 2 api keys hit a rate limit it switches to other 2 – Mathias Feb 14 '22 at 01:15