From 710acdfc2d08d9925e29b9c150989169002d76b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Wed, 15 Jan 2025 16:26:46 +0100 Subject: [PATCH] Client: obtain specific contact detail --- superfaktura/__init__.py | 0 superfaktura/client_contacts.py | 115 +++++++++++++------ superfaktura/{api.py => superfaktura_api.py} | 25 +++- 3 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 superfaktura/__init__.py rename superfaktura/{api.py => superfaktura_api.py} (52%) diff --git a/superfaktura/__init__.py b/superfaktura/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/superfaktura/client_contacts.py b/superfaktura/client_contacts.py index a7603fa..89ae69c 100644 --- a/superfaktura/client_contacts.py +++ b/superfaktura/client_contacts.py @@ -1,54 +1,99 @@ import dataclasses +import json from pprint import pprint +from typing import Optional -from superfaktura.api import SuperFakturaAPI +from superfaktura.superfaktura_api import SuperFakturaAPI + + +class ClientException(Exception): + pass @dataclasses.dataclass class ClientContactModel: name: str - address: str = None - bank_account: str = None - bank_code: str = None - city: str = None - comment: str = None - country: str = None - country_id: int = None - currency: str = None - default_variable: str = None - delivery_address: str = None - delivery_city: str = None - delivery_country: str = None - delivery_country_id: int = None - delivery_name: str = None - delivery_phone: str = None - delivery_zip: str = None - dic: str = None - discount: float = None - due_date: int = None - email: str = None - fax: str = None - iban: str = None - ic_dph: str = None - ico: str = None - match_address: int = None - phone: str = None - swift: str = None - tags: str = None - uuid: str = None - zip: str = None - update: bool = None + address: Optional[str] = None + bank_account: Optional[str] = None + bank_code: Optional[str] = None + city: Optional[str] = None + comment: Optional[str] = None + country: Optional[str] = None + country_id: Optional[int] = None + currency: Optional[str] = None + default_variable: Optional[str] = None + delivery_address: Optional[str] = None + delivery_city: Optional[str] = None + delivery_country: Optional[str] = None + delivery_country_id: Optional[int] = None + delivery_name: Optional[str] = None + delivery_phone: Optional[str] = None + delivery_zip: Optional[str] = None + dic: Optional[str] = None + discount: Optional[float] = None + due_date: Optional[int] = None + email: Optional[str] = None + fax: Optional[str] = None + iban: Optional[str] = None + ic_dph: Optional[str] = None + ico: Optional[str] = None + match_address: Optional[int] = None + phone: Optional[str] = None + swift: Optional[str] = None + tags: Optional[str] = None + uuid: Optional[str] = None + zip: Optional[str] = None + update: Optional[bool] = None + + id: Optional[int] = None + + def as_dict(self) -> dict: + data = dataclasses.asdict(self) + for key in list(data.keys()): + if data[key] is None: + del data[key] + return data + + @staticmethod + def from_dict(data: dict) -> "ClientContactModel": + return ClientContactModel( + **{ + k: v + for k, v in data.items() + if k in ClientContactModel.__dataclass_fields__ + } + ) class ClientContact(SuperFakturaAPI): def __init__(self): super().__init__() - def add_contact(self, contact: ClientContactModel): + def add_contact(self, contact: ClientContactModel) -> bool: url = "clients/create" - return self.post(endpoint=url, data=dataclasses.asdict(contact)) + data = {"Client": contact.as_dict()} + resp = self.post(endpoint=url, data=json.dumps(data)) + if resp["error_message"] == "Client created": + return True + return False + + def list(self) -> dict: + url = "clients/index.json" + return self.get(endpoint=url) + + def get_client(self, client_id: int) -> ClientContactModel: + url = f"clients/view/{client_id}" + data = self.get(endpoint=url) + if "Client" not in data: + raise ClientException("Client not found") + data = data["Client"] + return ClientContactModel.from_dict(data) + if __name__ == "__main__": client = ClientContact() - resp = client.add_contact(ClientContactModel(name="John Doe")) + resp = client.list() + pprint(resp) + + pprint(client.get_client(40019)) diff --git a/superfaktura/api.py b/superfaktura/superfaktura_api.py similarity index 52% rename from superfaktura/api.py rename to superfaktura/superfaktura_api.py index d48227e..789b156 100644 --- a/superfaktura/api.py +++ b/superfaktura/superfaktura_api.py @@ -5,22 +5,37 @@ import os from dotenv import load_dotenv # type: ignore +class SuperFakturaAPIException(Exception): + pass + + class SuperFakturaAPI: def __init__(self) -> None: load_dotenv() _api_key = os.getenv("SUPERFAKTURA_API_KEY") self._api_url = os.getenv("SUPERFAKTURA_API_URL") _api_mail = os.getenv("SUPERFAKTURA_API_EMAIL") + _api_company_id = os.getenv("SUPERFAKTURA_API_COMPANY_ID") self._auth_header = { - "Authorization": f"SFAPI email={_api_mail}&apikey={_api_key}" + "Authorization": f"SFAPI email={_api_mail}&apikey={_api_key}&company_id={_api_company_id}" } def get(self, endpoint: str) -> Dict: url = f"{self._api_url}/{endpoint}" req = requests.get(url=url, headers=self._auth_header) - return req.json() + if req.status_code == 200: + return req.json() + else: + raise SuperFakturaAPIException( + f"Get status code: {req.status_code}; {req.json()}" + ) - def post(self, endpoint: str, data: dict) -> Dict: + def post(self, endpoint: str, data: str) -> Dict: url = f"{self._api_url}/{endpoint}" - req = requests.post(url=url, headers=self._auth_header, data=data) - return req.json() + req = requests.post(url=url, headers=self._auth_header, data={"data": data}) + if req.status_code == 200: + return req.json() + else: + raise SuperFakturaAPIException( + f"Post status code: {req.status_code}; {req.json()}" + )