bapp-api-client 0.4.4__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.
@@ -1,6 +1,6 @@
1
1
  # Changelog
2
2
 
3
- ## 0.4.4
3
+ ## 0.5.0
4
4
 
5
5
  - Initial release.
6
6
  - Authentication: Bearer (JWT/OAuth) and Token (API key).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bapp-api-client
3
- Version: 0.4.4
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
- resp = self._session.request(
100
- method,
101
- f"{self.host}{path}",
102
- params=params,
103
- headers=self._headers(headers),
104
- **kwargs,
105
- )
106
- resp.raise_for_status()
107
- if resp.status_code == 204:
108
- return None
109
- return resp.json()
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
- url = f"{self.host}/render/{token}?output={output}"
271
+ params = {"output": output}
253
272
  v = variation or view.get("default_variation")
254
273
  if v:
255
- url += f"&variation={v}"
274
+ params["variation"] = v
256
275
  if download:
257
- url += "&download=true"
258
- return url
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}?token={token}"
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):
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "bapp-api-client"
7
- version = "0.4.4"
7
+ version = "0.5.0"
8
8
  description = "BAPP Auto API Client for Python"
9
9
  requires-python = ">=3.9"
10
10
  license = "MIT"
File without changes