bapp-api-client 0.4.3__tar.gz → 0.5.0__tar.gz
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.
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/CHANGELOG.md +1 -1
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/PKG-INFO +4 -1
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/README.md +3 -0
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/bapp_api_client/client.py +73 -17
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/pyproject.toml +1 -1
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/.github/workflows/publish.yml +0 -0
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/LICENSE +0 -0
- {bapp_api_client-0.4.3 → bapp_api_client-0.5.0}/bapp_api_client/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bapp-api-client
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: BAPP Auto API Client for Python
|
|
5
5
|
Project-URL: Homepage, https://www.bapp.ro
|
|
6
6
|
Project-URL: Repository, https://github.com/bapp-open/sdk-python
|
|
@@ -87,6 +87,8 @@ client.app = "wms"
|
|
|
87
87
|
| `host` | API base URL | `https://panel.bapp.ro/api` |
|
|
88
88
|
| `tenant` | Tenant ID (`x-tenant-id` header) | `None` |
|
|
89
89
|
| `app` | App slug (`x-app-slug` header) | `"account"` |
|
|
90
|
+
| `timeout` | HTTP request timeout (seconds) | `30` |
|
|
91
|
+
| `max_retries` | Max retries on transient errors (5xx, 429, connection) | `3` |
|
|
90
92
|
|
|
91
93
|
### Methods
|
|
92
94
|
|
|
@@ -105,6 +107,7 @@ client.app = "wms"
|
|
|
105
107
|
| `get_document_views(record)` | Extract available views from a record |
|
|
106
108
|
| `get_document_url(record, output?, label?, variation?)` | Build a render/download URL |
|
|
107
109
|
| `get_document_content(record, output?, label?, variation?)` | Fetch document bytes (PDF, HTML, JPG) |
|
|
110
|
+
| `download_document(record, dest, output?, label?, variation?)` | Stream document to file (memory-efficient) |
|
|
108
111
|
| `list_tasks()` | List available task codes |
|
|
109
112
|
| `detail_task(code)` | Get task configuration |
|
|
110
113
|
| `run_task(code, payload?)` | Execute a task |
|
|
@@ -74,6 +74,8 @@ client.app = "wms"
|
|
|
74
74
|
| `host` | API base URL | `https://panel.bapp.ro/api` |
|
|
75
75
|
| `tenant` | Tenant ID (`x-tenant-id` header) | `None` |
|
|
76
76
|
| `app` | App slug (`x-app-slug` header) | `"account"` |
|
|
77
|
+
| `timeout` | HTTP request timeout (seconds) | `30` |
|
|
78
|
+
| `max_retries` | Max retries on transient errors (5xx, 429, connection) | `3` |
|
|
77
79
|
|
|
78
80
|
### Methods
|
|
79
81
|
|
|
@@ -92,6 +94,7 @@ client.app = "wms"
|
|
|
92
94
|
| `get_document_views(record)` | Extract available views from a record |
|
|
93
95
|
| `get_document_url(record, output?, label?, variation?)` | Build a render/download URL |
|
|
94
96
|
| `get_document_content(record, output?, label?, variation?)` | Fetch document bytes (PDF, HTML, JPG) |
|
|
97
|
+
| `download_document(record, dest, output?, label?, variation?)` | Stream document to file (memory-efficient) |
|
|
95
98
|
| `list_tasks()` | List available task codes |
|
|
96
99
|
| `detail_task(code)` | Get task configuration |
|
|
97
100
|
| `run_task(code, payload?)` | Execute a task |
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"""BAPP Auto API Client for Python."""
|
|
2
2
|
|
|
3
3
|
import io
|
|
4
|
+
import random
|
|
4
5
|
import time
|
|
6
|
+
import urllib.parse
|
|
7
|
+
|
|
5
8
|
import requests
|
|
6
9
|
|
|
7
10
|
|
|
@@ -55,11 +58,15 @@ class BappApiClient:
|
|
|
55
58
|
tenant=None,
|
|
56
59
|
app="account",
|
|
57
60
|
user_agent=None,
|
|
61
|
+
timeout=30,
|
|
62
|
+
max_retries=3,
|
|
58
63
|
):
|
|
59
64
|
self.host = host.rstrip("/")
|
|
60
65
|
self.tenant = tenant
|
|
61
66
|
self.app = app
|
|
62
67
|
self.user_agent = user_agent
|
|
68
|
+
self._timeout = timeout
|
|
69
|
+
self._max_retries = max_retries
|
|
63
70
|
self._session = requests.Session()
|
|
64
71
|
if bearer:
|
|
65
72
|
self._session.headers["Authorization"] = f"Bearer {bearer}"
|
|
@@ -96,17 +103,29 @@ class BappApiClient:
|
|
|
96
103
|
kwargs["data"] = data
|
|
97
104
|
else:
|
|
98
105
|
kwargs["json"] = json
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
|
|
107
|
+
for attempt in range(self._max_retries + 1):
|
|
108
|
+
try:
|
|
109
|
+
resp = self._session.request(
|
|
110
|
+
method,
|
|
111
|
+
f"{self.host}{path}",
|
|
112
|
+
params=params,
|
|
113
|
+
headers=self._headers(headers),
|
|
114
|
+
timeout=self._timeout,
|
|
115
|
+
**kwargs,
|
|
116
|
+
)
|
|
117
|
+
if resp.status_code in (429, 502, 503, 504) and attempt < self._max_retries:
|
|
118
|
+
time.sleep(min(2 ** attempt + random.random(), 10))
|
|
119
|
+
continue
|
|
120
|
+
resp.raise_for_status()
|
|
121
|
+
if resp.status_code == 204:
|
|
122
|
+
return None
|
|
123
|
+
return resp.json()
|
|
124
|
+
except requests.exceptions.ConnectionError:
|
|
125
|
+
if attempt >= self._max_retries:
|
|
126
|
+
raise
|
|
127
|
+
time.sleep(min(2 ** attempt + random.random(), 10))
|
|
128
|
+
return None # unreachable
|
|
110
129
|
|
|
111
130
|
# -- user ----------------------------------------------------------------
|
|
112
131
|
|
|
@@ -249,13 +268,13 @@ class BappApiClient:
|
|
|
249
268
|
return None
|
|
250
269
|
|
|
251
270
|
if view["type"] == "public_view":
|
|
252
|
-
|
|
271
|
+
params = {"output": output}
|
|
253
272
|
v = variation or view.get("default_variation")
|
|
254
273
|
if v:
|
|
255
|
-
|
|
274
|
+
params["variation"] = v
|
|
256
275
|
if download:
|
|
257
|
-
|
|
258
|
-
return
|
|
276
|
+
params["download"] = "true"
|
|
277
|
+
return f"{self.host}/render/{token}?{urllib.parse.urlencode(params)}"
|
|
259
278
|
|
|
260
279
|
# Legacy view_token
|
|
261
280
|
if output == "pdf":
|
|
@@ -264,7 +283,7 @@ class BappApiClient:
|
|
|
264
283
|
action = "pdf.context"
|
|
265
284
|
else:
|
|
266
285
|
action = "pdf.preview"
|
|
267
|
-
return f"{self.host}/documents/{action}?
|
|
286
|
+
return f"{self.host}/documents/{action}?{urllib.parse.urlencode({'token': token})}"
|
|
268
287
|
|
|
269
288
|
def get_document_content(self, record, output="html", label=None,
|
|
270
289
|
variation=None, download=False):
|
|
@@ -294,10 +313,47 @@ class BappApiClient:
|
|
|
294
313
|
)
|
|
295
314
|
if url is None:
|
|
296
315
|
return None
|
|
297
|
-
resp = self._session.get(url)
|
|
316
|
+
resp = self._session.get(url, timeout=self._timeout)
|
|
298
317
|
resp.raise_for_status()
|
|
299
318
|
return resp.content
|
|
300
319
|
|
|
320
|
+
def download_document(self, record, dest, output="html", label=None,
|
|
321
|
+
variation=None, download=False, chunk_size=8192):
|
|
322
|
+
"""Stream document content directly to a file.
|
|
323
|
+
|
|
324
|
+
Like :meth:`get_document_content` but writes to *dest* in chunks,
|
|
325
|
+
avoiding loading the entire document into memory.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
record: Entity dict from :meth:`list` or :meth:`get`.
|
|
329
|
+
dest: File path to write to.
|
|
330
|
+
output: Desired format — ``"html"``, ``"pdf"``, ``"jpg"``, or
|
|
331
|
+
``"context"``.
|
|
332
|
+
label: Select a specific view by label.
|
|
333
|
+
variation: Variation code for ``public_view`` entries.
|
|
334
|
+
download: When *True* the server sends the response as an
|
|
335
|
+
attachment.
|
|
336
|
+
chunk_size: Size of chunks to stream (default 8192 bytes).
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
*True* if the document was saved, *False* if no view tokens.
|
|
340
|
+
|
|
341
|
+
Raises:
|
|
342
|
+
requests.HTTPError: On non-2xx responses.
|
|
343
|
+
"""
|
|
344
|
+
url = self.get_document_url(
|
|
345
|
+
record, output=output, label=label, variation=variation,
|
|
346
|
+
download=download,
|
|
347
|
+
)
|
|
348
|
+
if url is None:
|
|
349
|
+
return False
|
|
350
|
+
with self._session.get(url, stream=True, timeout=self._timeout) as resp:
|
|
351
|
+
resp.raise_for_status()
|
|
352
|
+
with open(dest, "wb") as f:
|
|
353
|
+
for chunk in resp.iter_content(chunk_size=chunk_size):
|
|
354
|
+
f.write(chunk)
|
|
355
|
+
return True
|
|
356
|
+
|
|
301
357
|
# -- tasks ---------------------------------------------------------------
|
|
302
358
|
|
|
303
359
|
def list_tasks(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|