Доработка взаимодействия с API, новая процедура установки, определение установленных версий игры и начальная реализация БД

This commit is contained in:
Евгений Титаренко 2023-07-21 18:05:44 +03:00
parent 983d0f9e21
commit 5beb835d1e
4 changed files with 217 additions and 51 deletions

26
api.py
View file

@ -14,21 +14,22 @@ def download(file_url: str, file_size: int, path: str):
file.write(data)
def __method(method: str, api_version: str = API_VERSION):
def __method(method: str, sub_method = "", api_version: str = API_VERSION):
api_url = "https://api.modrinth.com"
def request(**args):
sub_method = ""
slug = ""
if "project" in args:
sub_method = "/" + args.pop("project")
slug = "/" + args.pop("project")
elif "version" in args:
sub_method = "/" + args.pop("version")
print(f"{api_url}/{api_version}{method}{sub_method}")
resp = requests.get(f"{api_url}/{api_version}{method}{sub_method}", params=args, headers=HEADERS)
print(resp.headers.get("X-Ratelimit-Remaining", -1))
slug = "/" + args.pop("version")
# print(f"{api_url}/{api_version}{method}{slug}{sub_method}")
#print(args)
resp = requests.get(f"{api_url}/{api_version}{method}{slug}{sub_method}", params=args, headers=HEADERS)
# print(resp.headers.get("X-Ratelimit-Remaining", -1))
match resp.status_code:
case 200:
print("200: OK")
# print("200: OK")
if type(resp.json()) == list:
return [type(method, (object,), obj) for obj in resp.json()]
return type(method, (object,), resp.json())
@ -40,10 +41,10 @@ def __method(method: str, api_version: str = API_VERSION):
print("401: ERROR")
case 404:
'''
The requested project was not found or
no authorization to see this project
The requested project was not found or no authorization to see this project
'''
print("404: ERROR")
# print("404: ERROR")
return None
resp.raise_for_status()
return request
@ -51,5 +52,6 @@ def __method(method: str, api_version: str = API_VERSION):
test = __method("", "")
search = __method("/search")
project = __method("/project")
check_project = __method("/project", sub_method="/check")
version = __method("/version")
versions = __method("/versions")
get_versions = __method("/project", sub_method="/version")