emitfy 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
emitfy/__init__.py
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
import urllib.error
|
|
6
|
+
import urllib.parse
|
|
7
|
+
import urllib.request
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class EmitfyError(Exception):
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
message: str,
|
|
15
|
+
code: str | None = None,
|
|
16
|
+
details: Any = None,
|
|
17
|
+
status_code: int = 0,
|
|
18
|
+
) -> None:
|
|
19
|
+
super().__init__(message)
|
|
20
|
+
self.code = code
|
|
21
|
+
self.details = details
|
|
22
|
+
self.status_code = status_code
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _HttpClient:
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
api_key: str,
|
|
29
|
+
api_secret: str,
|
|
30
|
+
base_url: str = "https://api.emitfy.com/v1",
|
|
31
|
+
max_retries: int = 2,
|
|
32
|
+
) -> None:
|
|
33
|
+
self.api_key = api_key
|
|
34
|
+
self.api_secret = api_secret
|
|
35
|
+
self.base_url = base_url.rstrip("/")
|
|
36
|
+
self.max_retries = max_retries
|
|
37
|
+
|
|
38
|
+
def request(
|
|
39
|
+
self,
|
|
40
|
+
method: str,
|
|
41
|
+
path: str,
|
|
42
|
+
body: dict[str, Any] | None = None,
|
|
43
|
+
extra_headers: dict[str, str] | None = None,
|
|
44
|
+
) -> Any:
|
|
45
|
+
url = f"{self.base_url}/{path.lstrip('/')}"
|
|
46
|
+
attempt = 0
|
|
47
|
+
|
|
48
|
+
while True:
|
|
49
|
+
attempt += 1
|
|
50
|
+
headers = {
|
|
51
|
+
"X-Api-Key": self.api_key,
|
|
52
|
+
"X-Api-Secret": self.api_secret,
|
|
53
|
+
"Accept": "application/json",
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
}
|
|
56
|
+
if extra_headers:
|
|
57
|
+
headers.update(extra_headers)
|
|
58
|
+
|
|
59
|
+
data = None if body is None else json.dumps(body).encode("utf-8")
|
|
60
|
+
req = urllib.request.Request(url, data=data, headers=headers, method=method.upper())
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
with urllib.request.urlopen(req) as response:
|
|
64
|
+
raw = response.read().decode("utf-8")
|
|
65
|
+
status = response.status
|
|
66
|
+
retry_after = response.headers.get("Retry-After", "1")
|
|
67
|
+
except urllib.error.HTTPError as exc:
|
|
68
|
+
raw = exc.read().decode("utf-8")
|
|
69
|
+
status = exc.code
|
|
70
|
+
retry_after = exc.headers.get("Retry-After", "1") if exc.headers else "1"
|
|
71
|
+
|
|
72
|
+
if status == 429 and attempt <= self.max_retries + 1:
|
|
73
|
+
time.sleep(max(1, int(retry_after or "1")))
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
decoded = json.loads(raw) if raw else None
|
|
77
|
+
|
|
78
|
+
if status >= 400:
|
|
79
|
+
error = (decoded or {}).get("error") if isinstance(decoded, dict) else None
|
|
80
|
+
message = (
|
|
81
|
+
error.get("message")
|
|
82
|
+
if isinstance(error, dict)
|
|
83
|
+
else "Request failed."
|
|
84
|
+
)
|
|
85
|
+
raise EmitfyError(
|
|
86
|
+
str(message),
|
|
87
|
+
error.get("code") if isinstance(error, dict) else None,
|
|
88
|
+
error.get("details") if isinstance(error, dict) else None,
|
|
89
|
+
status,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if isinstance(decoded, dict) and "data" in decoded:
|
|
93
|
+
return decoded["data"]
|
|
94
|
+
|
|
95
|
+
return decoded
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class CompanyResource:
|
|
99
|
+
def __init__(self, http: _HttpClient, base_path: str) -> None:
|
|
100
|
+
self._http = http
|
|
101
|
+
self._base_path = base_path
|
|
102
|
+
|
|
103
|
+
def list(self, **query: Any) -> Any:
|
|
104
|
+
path = self._base_path
|
|
105
|
+
if query:
|
|
106
|
+
path = f"{path}?{urllib.parse.urlencode(query)}"
|
|
107
|
+
return self._http.request("GET", path)
|
|
108
|
+
|
|
109
|
+
def create(self, payload: dict[str, Any], idempotency_key: str | None = None) -> Any:
|
|
110
|
+
headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
|
|
111
|
+
return self._http.request("POST", self._base_path, payload, headers)
|
|
112
|
+
|
|
113
|
+
def get(self, id: str) -> Any:
|
|
114
|
+
return self._http.request("GET", f"{self._base_path}/{urllib.parse.quote(id)}")
|
|
115
|
+
|
|
116
|
+
def update(self, id: str, payload: dict[str, Any]) -> Any:
|
|
117
|
+
return self._http.request("PUT", f"{self._base_path}/{urllib.parse.quote(id)}", payload)
|
|
118
|
+
|
|
119
|
+
def delete(self, id: str) -> Any:
|
|
120
|
+
return self._http.request("DELETE", f"{self._base_path}/{urllib.parse.quote(id)}")
|
|
121
|
+
|
|
122
|
+
def post(
|
|
123
|
+
self,
|
|
124
|
+
suffix: str,
|
|
125
|
+
payload: dict[str, Any] | None = None,
|
|
126
|
+
idempotency_key: str | None = None,
|
|
127
|
+
) -> Any:
|
|
128
|
+
headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
|
|
129
|
+
return self._http.request(
|
|
130
|
+
"POST",
|
|
131
|
+
f"{self._base_path.rstrip('/')}/{suffix.lstrip('/')}",
|
|
132
|
+
payload,
|
|
133
|
+
headers,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class CompanyContext:
|
|
138
|
+
def __init__(self, http: _HttpClient, company_id: str) -> None:
|
|
139
|
+
self._http = http
|
|
140
|
+
self._company_id = company_id
|
|
141
|
+
prefix = f"/companies/{urllib.parse.quote(company_id)}"
|
|
142
|
+
self.nfse = CompanyResource(http, f"{prefix}/nfse")
|
|
143
|
+
self.nfe = CompanyResource(http, f"{prefix}/nfe")
|
|
144
|
+
self.nfce = CompanyResource(http, f"{prefix}/nfce")
|
|
145
|
+
self.cte = CompanyResource(http, f"{prefix}/cte")
|
|
146
|
+
self.customers = CompanyResource(http, f"{prefix}/customers")
|
|
147
|
+
self.products = CompanyResource(http, f"{prefix}/products")
|
|
148
|
+
self.sales = CompanyResource(http, f"{prefix}/sales")
|
|
149
|
+
self.invoices = CompanyResource(http, f"{prefix}/invoices")
|
|
150
|
+
self.received_nfes = CompanyResource(http, f"{prefix}/received-nfes")
|
|
151
|
+
|
|
152
|
+
def id(self) -> str:
|
|
153
|
+
return self._company_id
|
|
154
|
+
|
|
155
|
+
def create_cte_os(
|
|
156
|
+
self, payload: dict[str, Any], idempotency_key: str | None = None
|
|
157
|
+
) -> Any:
|
|
158
|
+
headers = {"Idempotency-Key": idempotency_key} if idempotency_key else None
|
|
159
|
+
return self._http.request(
|
|
160
|
+
"POST",
|
|
161
|
+
f"/companies/{urllib.parse.quote(self._company_id)}/cte-os",
|
|
162
|
+
payload,
|
|
163
|
+
headers,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class Emitfy:
|
|
168
|
+
def __init__(
|
|
169
|
+
self,
|
|
170
|
+
api_key: str,
|
|
171
|
+
api_secret: str,
|
|
172
|
+
base_url: str = "https://api.emitfy.com/v1",
|
|
173
|
+
max_retries: int = 2,
|
|
174
|
+
) -> None:
|
|
175
|
+
api_key = (api_key or "").strip()
|
|
176
|
+
api_secret = (api_secret or "").strip()
|
|
177
|
+
if not api_key or not api_secret:
|
|
178
|
+
raise EmitfyError("api_key and api_secret are required.")
|
|
179
|
+
self._http = _HttpClient(api_key, api_secret, base_url, max_retries)
|
|
180
|
+
self.webhooks = _Webhooks(self._http)
|
|
181
|
+
self.companies = _Companies(self._http)
|
|
182
|
+
|
|
183
|
+
def company(self, company_id: str) -> CompanyContext:
|
|
184
|
+
company_id = (company_id or "").strip()
|
|
185
|
+
if not company_id:
|
|
186
|
+
raise EmitfyError("company_id is required.")
|
|
187
|
+
return CompanyContext(self._http, company_id)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class _Webhooks:
|
|
191
|
+
def __init__(self, http: _HttpClient) -> None:
|
|
192
|
+
self._http = http
|
|
193
|
+
|
|
194
|
+
def list(self) -> Any:
|
|
195
|
+
return self._http.request("GET", "/webhooks")
|
|
196
|
+
|
|
197
|
+
def create(self, payload: dict[str, Any]) -> Any:
|
|
198
|
+
return self._http.request("POST", "/webhooks", payload)
|
|
199
|
+
|
|
200
|
+
def update(self, id: str, payload: dict[str, Any]) -> Any:
|
|
201
|
+
return self._http.request("PUT", f"/webhooks/{urllib.parse.quote(id)}", payload)
|
|
202
|
+
|
|
203
|
+
def set_active(self, id: str, active: bool) -> Any:
|
|
204
|
+
return self._http.request(
|
|
205
|
+
"PATCH",
|
|
206
|
+
f"/webhooks/{urllib.parse.quote(id)}/active",
|
|
207
|
+
{"active": active},
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
def delete(self, id: str) -> Any:
|
|
211
|
+
return self._http.request("DELETE", f"/webhooks/{urllib.parse.quote(id)}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class _Companies:
|
|
215
|
+
def __init__(self, http: _HttpClient) -> None:
|
|
216
|
+
self._http = http
|
|
217
|
+
|
|
218
|
+
def list(self) -> Any:
|
|
219
|
+
return self._http.request("GET", "/companies")
|
|
220
|
+
|
|
221
|
+
def create(self, payload: dict[str, Any]) -> Any:
|
|
222
|
+
return self._http.request("POST", "/companies", payload)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
__all__ = ["Emitfy", "EmitfyError", "CompanyContext"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: emitfy
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Official Emitfy API SDK for Python
|
|
5
|
+
Author: Emitfy
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://api.emitfy.com/docs/sdks
|
|
8
|
+
Project-URL: Documentation, https://api.emitfy.com/docs
|
|
9
|
+
Project-URL: Repository, https://github.com/emitfy/emitfy-python
|
|
10
|
+
Keywords: emitfy,nfe,nfse,fiscal,brazil,sdk
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# emitfy (Python)
|
|
17
|
+
|
|
18
|
+
Official Emitfy API SDK for Python.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install emitfy
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from emitfy import Emitfy
|
|
26
|
+
import os
|
|
27
|
+
|
|
28
|
+
emitfy = Emitfy(os.environ["EMITFY_API_KEY"], os.environ["EMITFY_API_SECRET"])
|
|
29
|
+
emitfy.webhooks.create({
|
|
30
|
+
"url": "https://seu-sistema.com/webhooks/emitfy",
|
|
31
|
+
"events": {"invoice": ["nfse.authorized"], "cte": []},
|
|
32
|
+
})
|
|
33
|
+
company = emitfy.company(os.environ["EMITFY_COMPANY_ID"])
|
|
34
|
+
company.nfse.create({"serviceDescription": "Serviço", "amount": 100})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Docs: https://api.emitfy.com/docs/sdks
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
emitfy/__init__.py,sha256=4R8WlyEkAhJU20-Qcr2oEI1az8FkhRf1aklSocT8b2I,7835
|
|
2
|
+
emitfy-0.2.2.dist-info/licenses/LICENSE,sha256=Poay4YENtADdklNZmz_64CqKKQd1oCggkt5QeHnluBE,1079
|
|
3
|
+
emitfy-0.2.2.dist-info/METADATA,sha256=Y9zsnw4DyMEPDXBCGtyXGouJ5TjgnPsTwWrTb-7awDU,1017
|
|
4
|
+
emitfy-0.2.2.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
5
|
+
emitfy-0.2.2.dist-info/top_level.txt,sha256=0EVvuYkqkMwIkOAFFob8AKc1sxbEp-JH2aZyopYDEV0,7
|
|
6
|
+
emitfy-0.2.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Emitfy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
emitfy
|