PyFlowery/pyflowery/models.py
cswimr 33b63fecc4
Some checks failed
Actions / lint (push) Failing after 16s
Actions / build (push) Successful in 19s
(1.0.3) retry handlers and more
added automatic retry handlers for 429 and 5xx error codes, as well as custom exceptions for ratelimiting, client errors, and server errors
also added a more advanced user agent string constructor. previously, setting FloweryAPIConfig.user_agent would override the default user_agent. so, if you set user_agent to `foobar`, the user agent string would be `foobar` in requests. now, the user agent string sent by requests would be the following on my development machine:
`'User-Agent': 'PyFlowery/1.0.3 PyFloweryTests (Python 3.12.6 (main, Sep  8 2024, 13:18:56) [GCC 14.2.1 20240805])'`
2024-09-17 23:24:14 -04:00

74 lines
2.2 KiB
Python

from dataclasses import dataclass, field
from logging import Logger, getLogger
from sys import version as pyversion
from typing import Dict, List, Union
from pyflowery.version import VERSION
@dataclass
class Voice:
"""Voice object returned from the Flowery API
Attributes:
id (str): UUID of the voice
name (str): Name of the voice
gender (str): Gender of the voice
source (str): Source of the voice
language (Language): Language object
"""
id: str
name: str
gender: str
source: str
language: 'Language'
@dataclass
class Language:
"""Language object returned from the Flowery API
Attributes:
name (str): Name of the language
code (str): Code of the language
"""
name: str
code: str
@dataclass
class Result:
"""Result returned from low-level RestAdapter
Attributes:
success (bool): Boolean of whether the request was successful
status_code (int): Standard HTTP Status code
message (str = ''): Human readable result
data (Union[List[Dict], Dict]): Python List of Dictionaries (or maybe just a single Dictionary on error)
"""
success: bool
status_code: int
message: str = ''
data: Union[List[Dict], Dict] = field(default_factory=dict)
@dataclass
class FloweryAPIConfig:
"""Configuration for the Flowery API
Attributes:
user_agent (str | None): User-Agent string to use for the HTTP requests
logger (Logger): Logger to use for logging messages
allow_truncation (bool): Whether to allow truncation of text that is too long
retry_limit (int): Number of times to retry a request before giving up
interval (int): Seconds to wait between each retried request, multiplied by how many attempted requests have been made
"""
user_agent: str | None = None
logger: Logger = getLogger('pyflowery')
allow_truncation: bool = False
retry_limit: int = 3
interval: int = 5
def prepended_user_agent(self) -> str:
"""Return the user_agent with the PyFlowery module version prepended"""
if not self.user_agent:
return f"PyFlowery/{VERSION} (Python {pyversion})"
return f"PyFlowery/{VERSION} {self.user_agent} (Python {pyversion})"