|
1 | 1 | """ |
2 | 2 | lokalise.base_client |
3 | 3 | ~~~~~~~~~~~~~~~~~~~~ |
4 | | -This module contains base API client definition. |
| 4 | +This module contains the base Lokalise API client definition. |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | from .types import FullClientProto |
|
10 | 10 |
|
11 | 11 |
|
12 | 12 | class BaseClient(FullClientProto): |
13 | | - """Base client used to send API requests.""" |
| 13 | + """Base client used to configure and send Lokalise API requests.""" |
14 | 14 |
|
15 | 15 | def __init__( |
16 | 16 | self, |
17 | 17 | token: str, |
18 | | - connect_timeout: int | float | None = None, |
19 | | - read_timeout: int | float | None = None, |
20 | | - enable_compression: bool | None = False, |
| 18 | + connect_timeout: Number | None = None, |
| 19 | + read_timeout: Number | None = None, |
| 20 | + enable_compression: bool = False, |
21 | 21 | api_host: str | None = None, |
22 | 22 | ) -> None: |
23 | | - """Instantiate a new Lokalise API client. |
24 | | -
|
25 | | - :param str token: Your Lokalise API token. |
26 | | - :param connect_timeout: (optional) Server connection timeout |
27 | | - (the value is in seconds). By default, the client will wait indefinitely. |
28 | | - :type connect_timeout: int or float |
29 | | - :param read_timeout: (optional) Server read timeout |
30 | | - (the value is in seconds). By default, the client will wait indefinitely. |
31 | | - :type read_timeout: int or float |
32 | | - :param enable_compression: (optional) Whether to enable gzip compression. |
33 | | - :param api_host: (optional) Custom API host to send requests to. |
34 | | - By default it's off. |
35 | | - :type enable_compression: bool |
| 23 | + """Create a new Lokalise API client instance. |
| 24 | +
|
| 25 | + Args: |
| 26 | + token: Your Lokalise API token (non-empty string). |
| 27 | + connect_timeout: Optional connection timeout in seconds. ``None`` means |
| 28 | + wait indefinitely. |
| 29 | + read_timeout: Optional read timeout in seconds. ``None`` means |
| 30 | + wait indefinitely. |
| 31 | + enable_compression: Whether to enable gzip compression (default: False). |
| 32 | + api_host: Optional custom API host. Defaults to the official Lokalise API. |
36 | 33 | """ |
37 | | - self._token = token |
38 | | - self._connect_timeout = connect_timeout |
39 | | - self._read_timeout = read_timeout |
40 | | - self._enable_compression = enable_compression |
41 | | - self._api_host = api_host |
42 | | - self._token_header = "X-Api-Token" |
| 34 | + if not token: |
| 35 | + raise ValueError("token must be a non-empty string") |
| 36 | + |
| 37 | + self._token: str | None = token |
| 38 | + self._connect_timeout: Number | None = connect_timeout |
| 39 | + self._read_timeout: Number | None = read_timeout |
| 40 | + self._enable_compression: bool = bool(enable_compression) |
| 41 | + self._api_host: str | None = api_host.strip() if api_host and api_host.strip() else None |
| 42 | + self._token_header: str = "X-Api-Token" |
| 43 | + |
| 44 | + # --------------------------------------------------------------------- |
| 45 | + # Properties |
| 46 | + # --------------------------------------------------------------------- |
43 | 47 |
|
44 | 48 | @property |
45 | | - def token(self) -> str: |
| 49 | + def token(self) -> str | None: |
| 50 | + """Return the current API token, or None if not set.""" |
46 | 51 | return self._token |
47 | 52 |
|
48 | 53 | @token.setter |
49 | | - def token(self, value: str) -> None: |
| 54 | + def token(self, value: str | None) -> None: |
50 | 55 | if not value: |
51 | 56 | raise ValueError("token must be a non-empty string") |
52 | 57 | self._token = value |
53 | 58 |
|
54 | 59 | @property |
55 | 60 | def connect_timeout(self) -> Number | None: |
| 61 | + """Connection timeout in seconds, or ``None`` for no limit.""" |
56 | 62 | return self._connect_timeout |
57 | 63 |
|
58 | 64 | @connect_timeout.setter |
59 | 65 | def connect_timeout(self, value: Number | None) -> None: |
60 | 66 | if value is not None and value < 0: |
61 | | - raise ValueError("connect_timeout must be a non-negative number or None") |
| 67 | + raise ValueError("connect_timeout must be non-negative or None") |
62 | 68 | self._connect_timeout = value |
63 | 69 |
|
64 | 70 | @property |
65 | 71 | def read_timeout(self) -> Number | None: |
| 72 | + """Read timeout in seconds, or ``None`` for no limit.""" |
66 | 73 | return self._read_timeout |
67 | 74 |
|
68 | 75 | @read_timeout.setter |
69 | 76 | def read_timeout(self, value: Number | None) -> None: |
70 | 77 | if value is not None and value < 0: |
71 | | - raise ValueError("read_timeout must be a non-negative number or None") |
| 78 | + raise ValueError("read_timeout must be non-negative or None") |
72 | 79 | self._read_timeout = value |
73 | 80 |
|
74 | 81 | @property |
75 | | - def enable_compression(self) -> bool | None: |
| 82 | + def enable_compression(self) -> bool: |
| 83 | + """Whether gzip compression is enabled.""" |
76 | 84 | return self._enable_compression |
77 | 85 |
|
78 | 86 | @enable_compression.setter |
79 | 87 | def enable_compression(self, value: bool | None) -> None: |
80 | | - self._enable_compression = bool(value) if value is not None else False |
| 88 | + self._enable_compression = bool(value) |
81 | 89 |
|
82 | 90 | @property |
83 | 91 | def api_host(self) -> str | None: |
| 92 | + """Custom API host, or ``None`` to use the default Lokalise API.""" |
84 | 93 | return self._api_host |
85 | 94 |
|
86 | 95 | @api_host.setter |
87 | 96 | def api_host(self, value: str | None) -> None: |
88 | | - if value is not None: |
89 | | - v = value.strip() |
90 | | - if not v: |
91 | | - value = None |
92 | | - self._api_host = value |
| 97 | + self._api_host = value.strip() if value and value.strip() else None |
93 | 98 |
|
94 | 99 | @property |
95 | 100 | def token_header(self) -> str: |
| 101 | + """HTTP header key used for the API token.""" |
96 | 102 | return self._token_header |
0 commit comments