|
| 1 | +from dataclasses import asdict |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from elorus.auth import ElorusAuthentication |
| 7 | +from elorus.exceptions import ( |
| 8 | + AuthenticationError, |
| 9 | + AuthorizationError, |
| 10 | + BadRequestError, |
| 11 | + Error, |
| 12 | + ThrottlingError, |
| 13 | +) |
| 14 | +from elorus.models import Contact, EmailBody, Invoice |
| 15 | + |
| 16 | + |
| 17 | +class Client: |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + api_key: str, |
| 21 | + elorus_organization_id: str, |
| 22 | + is_demo: bool = False, |
| 23 | + base_url: Optional[str] = "https://api.elorus.com", |
| 24 | + api_version: Optional[str] = "v1.1", |
| 25 | + ): |
| 26 | + self.base_url = base_url |
| 27 | + self.api_key = api_key |
| 28 | + self.elorus_organization_id = elorus_organization_id |
| 29 | + self.is_demo = is_demo |
| 30 | + self.api_version = api_version |
| 31 | + |
| 32 | + self.contacts = Contacts(self) |
| 33 | + self.invoices = Invoices(self) |
| 34 | + |
| 35 | + def _get_auth(self): |
| 36 | + return ElorusAuthentication( |
| 37 | + token=self.api_key, |
| 38 | + elorus_organization_id=self.elorus_organization_id, |
| 39 | + is_demo=self.is_demo, |
| 40 | + ) |
| 41 | + |
| 42 | + def handle_file_download(self, response): |
| 43 | + content_disposition = response.headers.get("Content-Disposition", "") |
| 44 | + if "filename=" not in content_disposition: |
| 45 | + raise ValueError("No filename found in Content-Disposition header") |
| 46 | + filename_kv = content_disposition.split("filename=") |
| 47 | + if len(filename_kv) < 2: |
| 48 | + raise ValueError("Invalid Content-Disposition header format") |
| 49 | + filename = filename_kv[1] |
| 50 | + return filename, response.content |
| 51 | + |
| 52 | + def _handle_response(self, response): |
| 53 | + if response.headers.get("Content-Type", "").lower() == "application/pdf": |
| 54 | + return self.handle_file_download(response) |
| 55 | + |
| 56 | + if response.status_code == 204: |
| 57 | + return response.text |
| 58 | + |
| 59 | + message = response.json() |
| 60 | + |
| 61 | + if response.status_code == 401: |
| 62 | + raise AuthenticationError( |
| 63 | + message=message, |
| 64 | + response=response, |
| 65 | + ) |
| 66 | + |
| 67 | + if response.status_code == 403: |
| 68 | + raise AuthorizationError( |
| 69 | + message=message, |
| 70 | + response=response, |
| 71 | + ) |
| 72 | + |
| 73 | + if response.status_code == 429: |
| 74 | + raise ThrottlingError( |
| 75 | + message=message, |
| 76 | + response=response, |
| 77 | + ) |
| 78 | + |
| 79 | + if response.status_code == 400: |
| 80 | + raise BadRequestError( |
| 81 | + message=message, |
| 82 | + response=response, |
| 83 | + ) |
| 84 | + |
| 85 | + if response.status_code >= 500: |
| 86 | + raise Error( |
| 87 | + message=message, |
| 88 | + response=response, |
| 89 | + ) |
| 90 | + |
| 91 | + try: |
| 92 | + response.raise_for_status() |
| 93 | + return message |
| 94 | + except: |
| 95 | + error = f"{response.text}" |
| 96 | + content_type = response.headers.get("Content-Type", "").lower() |
| 97 | + if content_type == "application/json": |
| 98 | + resp = response.json() |
| 99 | + error_message_keys = ("message", "msg", "detail") |
| 100 | + error_message = next( |
| 101 | + (resp[key] for key in error_message_keys if key in resp), None |
| 102 | + ) |
| 103 | + if error_message: |
| 104 | + error = f"Message: {error_message} , Error details: {resp.get('errors')}, {resp.get('data')}" |
| 105 | + raise Error(error, response) |
| 106 | + |
| 107 | + raise Error(error, response) |
| 108 | + |
| 109 | + def _handle_request( |
| 110 | + self, method: str, path: str, payload: Optional[dict] = None, **kwargs |
| 111 | + ): |
| 112 | + auth = self._get_auth() |
| 113 | + url = f"{self.base_url}/{self.api_version}/{path}" |
| 114 | + with httpx.Client(auth=auth) as client: |
| 115 | + response = client.request(method, url, json=payload, **kwargs) |
| 116 | + return self._handle_response(response) |
| 117 | + |
| 118 | + |
| 119 | +class SubClient: |
| 120 | + client = Client |
| 121 | + |
| 122 | + def __init__(self, client: Client): |
| 123 | + self.client = client |
| 124 | + |
| 125 | + |
| 126 | +class Contacts(SubClient): |
| 127 | + |
| 128 | + def list(self): |
| 129 | + return self.client._handle_request("GET", "contacts/") |
| 130 | + |
| 131 | + def create(self, contact: Contact): |
| 132 | + payload = asdict(contact) |
| 133 | + return self.client._handle_request("POST", "contacts/", payload=payload) |
| 134 | + |
| 135 | + def get(self, contact_id: str): |
| 136 | + return self.client._handle_request("GET", f"contacts/{contact_id}/") |
| 137 | + |
| 138 | + def update(self, contact_id: str, contact: Contact): |
| 139 | + payload = asdict(contact) |
| 140 | + return self.client._handle_request( |
| 141 | + "PUT", f"contacts/{contact_id}/", payload=payload |
| 142 | + ) |
| 143 | + |
| 144 | + def delete(self, contact_id: str): |
| 145 | + return self.client._handle_request("DELETE", f"contacts/{contact_id}/") |
| 146 | + |
| 147 | + |
| 148 | +class Invoices(SubClient): |
| 149 | + |
| 150 | + def list(self): |
| 151 | + return self.client._handle_request("GET", "invoices/") |
| 152 | + |
| 153 | + def create(self, invoice: Invoice): |
| 154 | + payload = invoice.clean_dict() |
| 155 | + return self.client._handle_request("POST", "invoices/", payload=payload) |
| 156 | + |
| 157 | + def get(self, invoice_id: str): |
| 158 | + return self.client._handle_request("GET", f"invoices/{invoice_id}/") |
| 159 | + |
| 160 | + def update(self, invoice_id: str, invoice: Invoice): |
| 161 | + payload = invoice.clean_dict() |
| 162 | + return self.client._handle_request( |
| 163 | + "PUT", f"invoices/{invoice_id}/", payload=payload |
| 164 | + ) |
| 165 | + |
| 166 | + def partial_update(self, invoice_id: str, invoice: Invoice): |
| 167 | + payload = invoice.clean_dict() |
| 168 | + return self.client._handle_request( |
| 169 | + "PATCH", f"invoices/{invoice_id}/", payload=payload |
| 170 | + ) |
| 171 | + |
| 172 | + def delete(self, invoice_id: str): |
| 173 | + return self.client._handle_request("DELETE", f"invoices/{invoice_id}/") |
| 174 | + |
| 175 | + def post_email(self, invoice_id: str, email_body: EmailBody): |
| 176 | + payload = email_body.clean_dict() |
| 177 | + return self.client._handle_request( |
| 178 | + "POST", f"invoices/{invoice_id}/email/", payload=payload |
| 179 | + ) |
| 180 | + |
| 181 | + def get_email(self, invoice_id: str): |
| 182 | + return self.client._handle_request("GET", f"invoices/{invoice_id}/email/") |
| 183 | + |
| 184 | + def get_pdf(self, invoice_id: str): |
| 185 | + return self.client._handle_request("GET", f"invoices/{invoice_id}/pdf/") |
| 186 | + |
| 187 | + def mark_void(self, invoice_id: str, is_void: bool = True): |
| 188 | + payload = {"is_void": is_void} |
| 189 | + return self.client._handle_request( |
| 190 | + "POST", f"invoices/{invoice_id}/void/", payload=payload |
| 191 | + ) |
0 commit comments