mirror of
https://github.com/Eledio/superfaktura-client.git
synced 2025-11-01 00:18:25 +01:00
Client: obtain specific contact detail
This commit is contained in:
0
superfaktura/__init__.py
Normal file
0
superfaktura/__init__.py
Normal file
@@ -1,54 +1,99 @@
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
|
import json
|
||||||
from pprint import pprint
|
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
|
@dataclasses.dataclass
|
||||||
class ClientContactModel:
|
class ClientContactModel:
|
||||||
name: str
|
name: str
|
||||||
address: str = None
|
address: Optional[str] = None
|
||||||
bank_account: str = None
|
bank_account: Optional[str] = None
|
||||||
bank_code: str = None
|
bank_code: Optional[str] = None
|
||||||
city: str = None
|
city: Optional[str] = None
|
||||||
comment: str = None
|
comment: Optional[str] = None
|
||||||
country: str = None
|
country: Optional[str] = None
|
||||||
country_id: int = None
|
country_id: Optional[int] = None
|
||||||
currency: str = None
|
currency: Optional[str] = None
|
||||||
default_variable: str = None
|
default_variable: Optional[str] = None
|
||||||
delivery_address: str = None
|
delivery_address: Optional[str] = None
|
||||||
delivery_city: str = None
|
delivery_city: Optional[str] = None
|
||||||
delivery_country: str = None
|
delivery_country: Optional[str] = None
|
||||||
delivery_country_id: int = None
|
delivery_country_id: Optional[int] = None
|
||||||
delivery_name: str = None
|
delivery_name: Optional[str] = None
|
||||||
delivery_phone: str = None
|
delivery_phone: Optional[str] = None
|
||||||
delivery_zip: str = None
|
delivery_zip: Optional[str] = None
|
||||||
dic: str = None
|
dic: Optional[str] = None
|
||||||
discount: float = None
|
discount: Optional[float] = None
|
||||||
due_date: int = None
|
due_date: Optional[int] = None
|
||||||
email: str = None
|
email: Optional[str] = None
|
||||||
fax: str = None
|
fax: Optional[str] = None
|
||||||
iban: str = None
|
iban: Optional[str] = None
|
||||||
ic_dph: str = None
|
ic_dph: Optional[str] = None
|
||||||
ico: str = None
|
ico: Optional[str] = None
|
||||||
match_address: int = None
|
match_address: Optional[int] = None
|
||||||
phone: str = None
|
phone: Optional[str] = None
|
||||||
swift: str = None
|
swift: Optional[str] = None
|
||||||
tags: str = None
|
tags: Optional[str] = None
|
||||||
uuid: str = None
|
uuid: Optional[str] = None
|
||||||
zip: str = None
|
zip: Optional[str] = None
|
||||||
update: bool = 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):
|
class ClientContact(SuperFakturaAPI):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def add_contact(self, contact: ClientContactModel):
|
def add_contact(self, contact: ClientContactModel) -> bool:
|
||||||
url = "clients/create"
|
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__":
|
if __name__ == "__main__":
|
||||||
client = ClientContact()
|
client = ClientContact()
|
||||||
resp = client.add_contact(ClientContactModel(name="John Doe"))
|
resp = client.list()
|
||||||
|
|
||||||
pprint(resp)
|
pprint(resp)
|
||||||
|
|
||||||
|
pprint(client.get_client(40019))
|
||||||
|
|||||||
@@ -5,22 +5,37 @@ import os
|
|||||||
from dotenv import load_dotenv # type: ignore
|
from dotenv import load_dotenv # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class SuperFakturaAPIException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SuperFakturaAPI:
|
class SuperFakturaAPI:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
_api_key = os.getenv("SUPERFAKTURA_API_KEY")
|
_api_key = os.getenv("SUPERFAKTURA_API_KEY")
|
||||||
self._api_url = os.getenv("SUPERFAKTURA_API_URL")
|
self._api_url = os.getenv("SUPERFAKTURA_API_URL")
|
||||||
_api_mail = os.getenv("SUPERFAKTURA_API_EMAIL")
|
_api_mail = os.getenv("SUPERFAKTURA_API_EMAIL")
|
||||||
|
_api_company_id = os.getenv("SUPERFAKTURA_API_COMPANY_ID")
|
||||||
self._auth_header = {
|
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:
|
def get(self, endpoint: str) -> Dict:
|
||||||
url = f"{self._api_url}/{endpoint}"
|
url = f"{self._api_url}/{endpoint}"
|
||||||
req = requests.get(url=url, headers=self._auth_header)
|
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}"
|
url = f"{self._api_url}/{endpoint}"
|
||||||
req = requests.post(url=url, headers=self._auth_header, data=data)
|
req = requests.post(url=url, headers=self._auth_header, data={"data": data})
|
||||||
return req.json()
|
if req.status_code == 200:
|
||||||
|
return req.json()
|
||||||
|
else:
|
||||||
|
raise SuperFakturaAPIException(
|
||||||
|
f"Post status code: {req.status_code}; {req.json()}"
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user