1- import json
21from typing import Union
32
43import requests
2019)
2120
2221
23- def get (path : str , sub_domain : str = "" , ** params ) -> Union [dict , str ]:
22+ def get (
23+ path : str , sub_domain : str = "" , headers = None , proxies = None , ** params
24+ ) -> Union [dict , list , str ]:
25+ """Send a get request to the Hytale API
26+
27+ Args:
28+ path (str): The path of the endpoint (eg. "/blog/post/archive")
29+ sub_domain (str, optional): The subdomain of the API (eg. "store."). Defaults to "".
30+ headers (dict, optional): Additional headers to include in the request. Defaults to None.
31+ proxies (dict, optional): Proxies to use for the request. Defaults to None.
32+
33+ Raises:
34+ HytaleAPIError: Generic error
35+ BlockedError: Error raised if cloudflare blocks the request (due to spam or headers misconfiguration)
36+ HytaleAPIError: Generic HTTP error
37+
38+ Returns:
39+ Union[dict, list, str]: Converted JSON response from the API if the response's Content-Type is `application/json`, otherwise a string.
40+ """
2441 url = "https://" + sub_domain + BASE_URL + path
2542 try :
2643 response = _session .get (
2744 url ,
2845 params = params ,
46+ headers = headers or {},
47+ proxies = proxies ,
2948 timeout = 3 ,
3049 )
3150 except requests .RequestException as exc :
@@ -38,7 +57,9 @@ def get(path: str, sub_domain: str = "", **params) -> Union[dict, str]:
3857 f"Request failed [{ response .status_code } ]: { response .text } "
3958 )
4059
41- try :
42- return json .loads (response .text )
43- except json .decoder .JSONDecodeError :
60+ content_type = response .headers .get ("Content-Type" , "" )
61+
62+ if content_type == "application/json" :
63+ return response .json ()
64+ else :
4465 return response .text
0 commit comments