From 204f05478a7c96be89aa0ca3e4907f143e254c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Kub=C3=AD=C4=8Dek?= Date: Mon, 13 Jan 2025 19:17:07 +0100 Subject: [PATCH] Add basic API and Client structure --- .env.example | 3 ++ requirements.txt | 2 ++ superfaktura/api.py | 28 +++++++++++++++++ superfaktura/client_contacts.py | 54 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 .env.example create mode 100644 requirements.txt create mode 100644 superfaktura/api.py create mode 100644 superfaktura/client_contacts.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..a72a067 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +SUPERFAKTURA_API_KEY= +SUPERFAKTURA_API_URL= +SUPERFAKTURA_API_EMAIL= \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ca6d6b6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests~=2.32.3 +python-dotenv~=1.0.1 \ No newline at end of file diff --git a/superfaktura/api.py b/superfaktura/api.py new file mode 100644 index 0000000..9b06925 --- /dev/null +++ b/superfaktura/api.py @@ -0,0 +1,28 @@ +from typing import Dict + +import requests +import os +from dotenv import load_dotenv # type: ignore + + +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") + self._auth_header = { + "Authorization": f"SFAPI email={_api_mail}&apikey={_api_key}" + } + + def get(self, endpoint: str) -> Dict: + url = f"{self._api_url}/{endpoint}" + req = requests.get(url=url, headers=self._auth_header) + return req.json() + + def post(self, endpoint: str, data: dict) -> Dict: + url = f"{self._api_url}/{endpoint}" + print(url) + print(data) + req = requests.post(url=url, headers=self._auth_header, data=data) + return req.json() diff --git a/superfaktura/client_contacts.py b/superfaktura/client_contacts.py new file mode 100644 index 0000000..a7603fa --- /dev/null +++ b/superfaktura/client_contacts.py @@ -0,0 +1,54 @@ +import dataclasses +from pprint import pprint + +from superfaktura.api import SuperFakturaAPI + + +@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 + + +class ClientContact(SuperFakturaAPI): + def __init__(self): + super().__init__() + + def add_contact(self, contact: ClientContactModel): + url = "clients/create" + return self.post(endpoint=url, data=dataclasses.asdict(contact)) + +if __name__ == "__main__": + client = ClientContact() + resp = client.add_contact(ClientContactModel(name="John Doe")) + pprint(resp)