Utilities
Copyright (C) 2024 TheOnlyWayUp
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Utility functions for the wattpad package.
build_url(path, fields=None, limit=None, offset=None)
#
Build an API Request URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The API Endpoint to request. |
required |
fields |
Optional[dict]
|
Fields Data, processed by |
None
|
limit |
Optional[int]
|
Number of records to limit the response to. Defaults to None. |
None
|
offset |
Optional[int]
|
Number of records to skip before beginning the response. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The built URL. |
Source code in src/wattpad/utils.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
construct_fields(fields)
#
Constructs a field query string from a dictionary representing the same.
Example:
>>> d: StoryModelFieldsType = {
"tags": True,
"id": True,
"parts": {"id": True},
"tagRankings": True
}
>>> construct_fields(d)
'tags,id,parts(id),tagRankings'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
dict
|
Field Data. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Field Query String. |
Source code in src/wattpad/utils.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
convert_from_aliases(data, model)
#
Convert a dictionary's keys from a model's aliased fields to their original names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
The dictionary whose keys must be changed. Nested dictionaries are not supported. |
required |
model |
BaseModel
|
The BaseModel to derive aliases from. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Updated dictionary with keys that are aliases replaced for their non-aliased variants. |
Source code in src/wattpad/utils.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
create_singleton()
#
Make a class a singleton using the first argument as the key.
Thanks https://medium.com/@pavankumarmasters/exploring-the-singleton-design-pattern-in-python-a34efa5e8cfa#:~:text=Code%20Magic%3A%20Conjuring%20Singletons%20with%20Metaclasses.
Returns:
Name | Type | Description |
---|---|---|
SingletonMeta |
Any
|
The Singleton metaclass. |
Source code in src/wattpad/utils.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
fetch_url(url, headers={})
async
#
Perform a GET Request to the provided URL, merging the provided headers with base_headers
.
Note: API Responses are cached using the URL as a key. Set the WPPY_SKIP_CACHE
Environment Variable to True to bypass the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The URL to request. |
required |
headers |
dict
|
Additional headers to merge atop of |
{}
|
Returns:
Type | Description |
---|---|
dict | list
|
dict | list: The JSON-Decoded Response. |
Source code in src/wattpad/utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
get_fields(model, prefer_alias=True)
#
Retrieve the fields of a Pydantic Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseModel
|
The model to retrieve fields from. |
required |
prefer_alias |
bool | optional
|
Whether to prefer field aliases if present. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of fields. |
Source code in src/wattpad/utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|