forked from justoneapi/justoneapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
38 lines (31 loc) · 1.13 KB
/
client.py
File metadata and controls
38 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import annotations
from justoneapi._transport import Transport
from justoneapi.config import DEFAULT_BASE_URL
from justoneapi.generated.resources import RESOURCE_CLASSES
class JustOneAPIClient:
def __init__(
self,
token: str,
base_url: str = DEFAULT_BASE_URL,
timeout: int = 60,
raise_on_business_error: bool = False,
):
if not token:
raise ValueError("Token is required. Please contact us to obtain one.")
self._transport = Transport(
token=token,
base_url=base_url,
timeout=timeout,
raise_on_business_error=raise_on_business_error,
)
self._resources: dict[str, object] = {}
for namespace, resource_class in RESOURCE_CLASSES.items():
resource = resource_class(self._transport)
setattr(self, namespace, resource)
self._resources[namespace] = resource
def close(self) -> None:
self._transport.close()
def __enter__(self) -> "JustOneAPIClient":
return self
def __exit__(self, exc_type, exc, tb) -> None:
self.close()