Add HTTP 429 error handling
This commit is contained in:
parent
c3a06e7255
commit
fa419980f5
@ -3,6 +3,7 @@ from types import WrapperDescriptorType
|
||||
from pytz.tzinfo import memorized_timedelta
|
||||
#import requests
|
||||
import socket
|
||||
import time
|
||||
from request_wrapper import requests_wrapper as requests
|
||||
import datetime
|
||||
import iso8601
|
||||
@ -18,9 +19,14 @@ if debug == True:
|
||||
requests_log.propagate = True
|
||||
timeoutTime = 1
|
||||
base_api_url = "https://api.modrinth.com:443/api/v1"
|
||||
def failCheck(response):
|
||||
def failCheck(response, functOrigin):
|
||||
print("Status Code is: "+str(response.status_code))
|
||||
if response.status_code != 200:
|
||||
if response.status_code == 429:
|
||||
print("Too many requests!")
|
||||
time.sleep(int(response.headers["Retry-After"]))
|
||||
print("Retrying "+functOrigin"...")
|
||||
return True
|
||||
elif response.status_code != 200:
|
||||
raise error
|
||||
# Data Caching
|
||||
dataCache = {}
|
||||
@ -35,7 +41,8 @@ def modInfo(project):
|
||||
print("Returning cached data!")
|
||||
return dataCache["modInfo"]
|
||||
response = requests.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||
failCheck(response)
|
||||
if failCheck(response, "modInfo") == True: #Attempt to requery API
|
||||
response = requests.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
cacheData("modInfo", api_response)
|
||||
return api_response
|
||||
@ -60,7 +67,8 @@ def getAllModVersionInfo(project):
|
||||
numberOfVersions = len(versions)
|
||||
for item in range(numberOfVersions):
|
||||
response = requests.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||
failCheck(response)
|
||||
if failCheck(response, "getAllModVersionInfo") == True: #Attempt to requery API
|
||||
response = requests.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
responseList.append(api_response)
|
||||
cacheData("getAllMinecraftVersionInfo", responseList)
|
||||
|
Loading…
x
Reference in New Issue
Block a user