Skip to content

Commit 6fbe5a0

Browse files
committed
docs(functions): documentation for all functions + refine get()
1 parent 18e97e5 commit 6fbe5a0

6 files changed

Lines changed: 105 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ with the exception that all versions are currently `0.x.x` and may include break
77

88
---
99

10+
## [Unreleased]
11+
12+
### Added
13+
14+
- Use response headers to determine whether to parse HTTP responses into JSON
15+
- Added possibility for additional headers and proxies (handled by requests lib)
16+
- get() can also return `list` type as JSON can also be a list
17+
- Documentation for all functions
18+
19+
### Changed
20+
21+
`N/A`
22+
23+
### Fixed
24+
25+
`N/A`
26+
27+
---
28+
1029
## [1.0.1] - 2025-12-20
1130

1231
### Added

src/hytale/blogs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,40 @@
44

55

66
def get_blogs(limit=3) -> list[BlogPostExcerpt]:
7+
"""Get the most recent blogs with a snippet of the blog post's body.
8+
9+
Args:
10+
limit (int, optional): The number of blog posts to retrieve. Defaults to 3.
11+
12+
Returns:
13+
list[BlogPostExcerpt]: A list of blog post excerpts.
14+
"""
715
data = get("/blog/post/published", limit=limit)
816
return [BlogPostExcerpt.from_dict(item) for item in data]
917

1018

1119
def get_blog(slug: str) -> BlogPost:
20+
"""Get a blog post with the entire body HTML content
21+
22+
Args:
23+
slug (str): The slug provided by Hytale. For example https://hytale.com/news/2025/12/hytale-s-1st-faq has a slug of "hytale-s-1st-faq"
24+
25+
Returns:
26+
BlogPost: The blog post.
27+
"""
1228
data = get(f"/blog/post/slug/{slug}")
1329
return BlogPost.from_dict(data)
1430

1531

1632
def get_blogs_time(year: int, month: int) -> list[BlogPostExcerpt]:
33+
"""Get the most recent blogs within a year and month
34+
35+
Args:
36+
year (int): The year to filter blogs by.
37+
month (int): The month to filter blogs by.
38+
39+
Returns:
40+
list[BlogPostExcerpt]: A list of blog post excerpts.
41+
"""
1742
data = get(f"/blog/post/archive/{year}/{month}")
1843
return [BlogPostExcerpt.from_dict(item) for item in data]

src/hytale/http.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from typing import Union
32

43
import requests
@@ -20,12 +19,32 @@
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

src/hytale/media.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,50 @@
44

55

66
def get_screenshots() -> list[Image]:
7+
"""Get screenshots from the media API.
8+
9+
Returns:
10+
list[Image]: A list of images.
11+
"""
712
data = get("/media/category/screenshots")
813
return [Image.from_dict(item) for item in data]
914

1015

1116
def get_desktop_wallpapers() -> list[Image]:
17+
"""Get desktop wallpapers from the media API.
18+
19+
Returns:
20+
list[Image]: A list of images.
21+
"""
1222
data = get("/media/category/desktopWallpapers")
1323
return [Image.from_dict(item) for item in data]
1424

1525

1626
def get_mobile_wallpapers() -> list[Image]:
27+
"""Get mobile wallpapers from the media API.
28+
29+
Returns:
30+
list[Image]: A list of images.
31+
"""
1732
data = get("/media/category/mobileWallpapers")
1833
return [Image.from_dict(item) for item in data]
1934

2035

2136
def get_concept_arts() -> list[Image]:
37+
"""Get concept arts from the media API.
38+
39+
Returns:
40+
list[Image]: A list of images.
41+
"""
2242
data = get("/media/category/conceptArt")
2343
return [Image.from_dict(item) for item in data]
2444

2545

2646
def get_videos() -> list[Video]:
47+
"""Get videos from the media API.
48+
49+
Returns:
50+
list[Video]: A list of videos.
51+
"""
2752
data = get("/media/category/videos")
2853
return [Video.from_dict(item) for item in data]

src/hytale/packages.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44

55
def get_packages() -> dict[str, Package]:
6+
"""Get all the current store packages.
7+
8+
Returns:
9+
dict[str, Package]: A dictionary mapping package names to Package objects.
10+
"""
611
data = get("/packages", "store.")
712

813
for pkg_dict in data.values():

src/hytale/status.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33

44

55
def get_status() -> Status:
6+
"""Get the current status of the Hytale API.
7+
8+
Returns:
9+
Status: The current status.
10+
"""
611
data = get("/status")
712
return Status(data)

0 commit comments

Comments
 (0)