Habiticalib 0.1.0a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ """Modern asynchronous Python client library for the Habitica API."""
2
+
3
+ from .exceptions import HabiticaException, NotAuthorizedError, NotFoundError
4
+ from .lib import Habitica
5
+
6
+ __version__ = "0.1.0a0"
7
+ __all__ = [
8
+ "Habitica",
9
+ "HabiticaException",
10
+ "NotAuthorizedError",
11
+ "NotFoundError",
12
+ ]
habiticalib/const.py ADDED
@@ -0,0 +1,14 @@
1
+ """Constants for Habiticalib."""
2
+
3
+ DEFAULT_URL = "https://habitica.com/"
4
+ ASSETS_URL = "https://habitica-assets.s3.amazonaws.com/mobileApp/images/"
5
+
6
+ DEVELOPER_ID = "4c4ca53f-c059-4ffa-966e-9d29dd405daf"
7
+
8
+ BACKER_ONLY_GEAR = {
9
+ "armor_special_ks2019": "BackerOnly-Equip-MythicGryphonArmor.gif",
10
+ "eyewear_special_ks2019": "BackerOnly-Equip-MythicGryphonVisor.gif",
11
+ "head_special_ks2019": "BackerOnly-Equip-MythicGryphonHelm.gif",
12
+ "shield_special_ks2019": "BackerOnly-Equip-MythicGryphonShield.gif",
13
+ "weapon_special_ks2019": "BackerOnly-Equip-MythicGryphonGlaive.gif",
14
+ }
@@ -0,0 +1,27 @@
1
+ """Exceptions for Habiticalib."""
2
+
3
+ from typing import Self
4
+
5
+ from habiticalib.types import HabiticaErrorResponse
6
+
7
+
8
+ class HabiticaException(Exception): # noqa: N818
9
+ """Base class for Habitica errors."""
10
+
11
+ def __init__(self: Self, error: HabiticaErrorResponse) -> None:
12
+ """Initialize the Exception."""
13
+ self.error = error
14
+
15
+ super().__init__(error.message)
16
+
17
+
18
+ class NotAuthorizedError(HabiticaException):
19
+ """NotAuthorized error."""
20
+
21
+
22
+ class NotFoundError(HabiticaException):
23
+ """NotFound error."""
24
+
25
+
26
+ class BadRequestError(HabiticaException):
27
+ """BadRequest error."""
habiticalib/helpers.py ADDED
@@ -0,0 +1,115 @@
1
+ """Helper functions for Habiticalib."""
2
+
3
+ from dataclasses import asdict
4
+ from importlib.metadata import version
5
+ import platform
6
+ import uuid
7
+
8
+ import aiohttp
9
+
10
+ from .const import DEVELOPER_ID
11
+ from .types import HabiticaUserResponse, UserData, UserStyles
12
+
13
+
14
+ def join_fields(user_fields: list[str] | str) -> str:
15
+ """Join user fields into a comma-separated string.
16
+
17
+ This method takes a list of user fields or a single string and
18
+ returns a string. If a list is provided, it joins the elements
19
+ with a comma separator. If a string is provided, it returns
20
+ the string as-is.
21
+
22
+ Parameters
23
+ ----------
24
+ user_fields : list of str or str
25
+ A list of user field strings or a single user field string.
26
+
27
+ Returns
28
+ -------
29
+ str
30
+ A comma-separated string of user fields if `user_fields` is a list,
31
+ or the original string if `user_fields` is a single string.
32
+ """
33
+ return ",".join(user_fields) if isinstance(user_fields, list) else str(user_fields)
34
+
35
+
36
+ def get_user_agent() -> str:
37
+ """Generate User-Agent string.
38
+
39
+ The User-Agent string contains details about the operating system,
40
+ its version, architecture, the habiticalib version, aiohttp version,
41
+ and Python version.
42
+
43
+ Returns
44
+ -------
45
+ str
46
+ A User-Agent string with OS details, library versions, and a project URL.
47
+
48
+ Examples
49
+ --------
50
+ >>> client.get_user_agent()
51
+ 'Habiticalib/0.0.0 (Windows 11 (10.0.22000); 64bit)
52
+ aiohttp/3.10.9 Python/3.12.7 +https://github.com/tr4nt0r/habiticalib)'
53
+ """
54
+ os_name = platform.system()
55
+ os_version = platform.version()
56
+ os_release = platform.release()
57
+ arch, _ = platform.architecture()
58
+ os_info = f"{os_name} {os_release} ({os_version}); {arch}"
59
+
60
+ return (
61
+ f"Habiticalib/{version("habiticalib")} ({os_info}) "
62
+ f"aiohttp/{aiohttp.__version__} Python/{platform.python_version()} "
63
+ " +https://github.com/tr4nt0r/habiticalib)"
64
+ )
65
+
66
+
67
+ def get_x_client(x_client: str | None = None) -> str:
68
+ """Generate the x-client header string.
69
+
70
+ If a valid `x_client` is provided, it validates the User ID (UUID format).
71
+ If no `x_client` is provided, it generates a default x-client header string.
72
+
73
+ Parameters
74
+ ----------
75
+ x_client : str or None, optional
76
+ A client identifier containing a UUID (first 36 characters) or None. If
77
+ None, a default x-client header will be generated.
78
+
79
+ Returns
80
+ -------
81
+ str
82
+ The validated x-client header string or habiticalib's default x-client header.
83
+
84
+ Raises
85
+ ------
86
+ ValueError
87
+ If the provided `x_client` is not in a valid UUID format.
88
+
89
+ Examples
90
+ --------
91
+ >>> get_x_client("123e4567-e89b-12d3-a456-426614174000 - MyHabiticaApp")
92
+ '123e4567-e89b-12d3-a456-426614174000 - MyHabiticaApp'
93
+
94
+ >>> get_x_client()
95
+ '4c4ca53f-c059-4ffa-966e-9d29dd405daf - Habiticalib/0.0.0'
96
+ """
97
+ if x_client:
98
+ try:
99
+ uuid.UUID(x_client[:36])
100
+ except ValueError as e:
101
+ msg = (
102
+ "Invalid User ID provided in x-client. Expected a valid "
103
+ "UUID format. Please ensure the User ID is correct"
104
+ )
105
+ raise ValueError(msg) from e
106
+
107
+ return x_client
108
+
109
+ return f"{DEVELOPER_ID} - Habiticalib/{version("habiticalib")}"
110
+
111
+
112
+ def extract_user_styles(user_data: HabiticaUserResponse) -> UserStyles:
113
+ """Extract user styles from a user data object."""
114
+ data: UserData = user_data.data
115
+ return UserStyles.from_dict(asdict(data))