nlbone 0.1.33__py3-none-any.whl → 0.1.34__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.
- nlbone/adapters/http_clients/uploadchi.py +34 -12
- {nlbone-0.1.33.dist-info → nlbone-0.1.34.dist-info}/METADATA +1 -1
- {nlbone-0.1.33.dist-info → nlbone-0.1.34.dist-info}/RECORD +5 -5
- {nlbone-0.1.33.dist-info → nlbone-0.1.34.dist-info}/WHEEL +0 -0
- {nlbone-0.1.33.dist-info → nlbone-0.1.34.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
import json
|
|
3
3
|
from typing import Any, Optional
|
|
4
|
+
from urllib.parse import urlparse, urlunparse
|
|
5
|
+
|
|
4
6
|
import httpx
|
|
7
|
+
import requests
|
|
5
8
|
|
|
6
9
|
from nlbone.core.ports.files import FileServicePort
|
|
7
10
|
from nlbone.config.settings import get_settings
|
|
8
11
|
|
|
12
|
+
|
|
9
13
|
class UploadchiError(RuntimeError):
|
|
10
14
|
def __init__(self, status: int, detail: Any | None = None):
|
|
11
15
|
super().__init__(f"Uploadchi HTTP {status}: {detail}")
|
|
12
16
|
self.status = status
|
|
13
17
|
self.detail = detail
|
|
14
18
|
|
|
19
|
+
|
|
15
20
|
def _resolve_token(explicit: str | None) -> str | None:
|
|
16
21
|
if explicit is not None:
|
|
17
22
|
return explicit
|
|
18
23
|
s = get_settings()
|
|
19
24
|
return s.UPLOADCHI_TOKEN.get_secret_value() if s.UPLOADCHI_TOKEN else None
|
|
20
25
|
|
|
26
|
+
|
|
21
27
|
def _auth_headers(token: str | None) -> dict[str, str]:
|
|
22
28
|
return {"Authorization": f"Bearer {token}"} if token else {}
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
|
|
31
|
+
def _build_list_query(limit: int, offset: int, filters: dict[str, Any] | None, sort: list[tuple[str, str]] | None) -> \
|
|
32
|
+
dict[str, Any]:
|
|
25
33
|
q: dict[str, Any] = {"limit": limit, "offset": offset}
|
|
26
34
|
if filters:
|
|
27
35
|
q["filters"] = json.dumps(filters)
|
|
@@ -29,6 +37,7 @@ def _build_list_query(limit: int, offset: int, filters: dict[str, Any] | None, s
|
|
|
29
37
|
q["sort"] = ",".join([f"{f}:{o}" for f, o in sort])
|
|
30
38
|
return q
|
|
31
39
|
|
|
40
|
+
|
|
32
41
|
def _filename_from_cd(cd: str | None, fallback: str) -> str:
|
|
33
42
|
if not cd:
|
|
34
43
|
return fallback
|
|
@@ -36,49 +45,62 @@ def _filename_from_cd(cd: str | None, fallback: str) -> str:
|
|
|
36
45
|
return cd.split("filename=", 1)[1].strip("\"'")
|
|
37
46
|
return fallback
|
|
38
47
|
|
|
48
|
+
|
|
49
|
+
def _normalize_https_base(url: str) -> str:
|
|
50
|
+
p = urlparse(url.strip())
|
|
51
|
+
p = p._replace(scheme="https") # enforce https
|
|
52
|
+
if p.path.endswith("/"):
|
|
53
|
+
p = p._replace(path=p.path.rstrip("/"))
|
|
54
|
+
return str(urlunparse(p))
|
|
55
|
+
|
|
56
|
+
|
|
39
57
|
class UploadchiClient(FileServicePort):
|
|
40
|
-
def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None,
|
|
58
|
+
def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None,
|
|
59
|
+
client: httpx.Client | None = None) -> None:
|
|
41
60
|
s = get_settings()
|
|
42
|
-
self._base_url = base_url or str(s.UPLOADCHI_BASE_URL)
|
|
61
|
+
self._base_url = _normalize_https_base(base_url or str(s.UPLOADCHI_BASE_URL))
|
|
43
62
|
self._timeout = timeout_seconds or float(s.HTTP_TIMEOUT_SECONDS)
|
|
44
|
-
self._client = client or
|
|
63
|
+
self._client = client or requests.session()
|
|
45
64
|
|
|
46
65
|
def close(self) -> None:
|
|
47
66
|
self._client.close()
|
|
48
67
|
|
|
49
|
-
def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None,
|
|
68
|
+
def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None,
|
|
69
|
+
token: str | None = None) -> dict:
|
|
50
70
|
tok = _resolve_token(token)
|
|
51
71
|
files = {"file": (filename, file_bytes)}
|
|
52
72
|
data = (params or {}).copy()
|
|
53
|
-
r = self._client.post(
|
|
73
|
+
r = self._client.post(self._base_url, files=files, data=data, headers=_auth_headers(tok))
|
|
54
74
|
if r.status_code >= 400:
|
|
55
75
|
raise UploadchiError(r.status_code, r.text)
|
|
56
76
|
return r.json()
|
|
57
77
|
|
|
58
78
|
def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None:
|
|
59
79
|
tok = _resolve_token(token)
|
|
60
|
-
r = self._client.post(f"/{file_id}/commit", headers=_auth_headers(tok),
|
|
80
|
+
r = self._client.post(f"{self._base_url}/{file_id}/commit", headers=_auth_headers(tok),
|
|
81
|
+
params={"client_id": client_id} if client_id else None)
|
|
61
82
|
if r.status_code not in (204, 200):
|
|
62
83
|
raise UploadchiError(r.status_code, r.text)
|
|
63
84
|
|
|
64
|
-
def list_files(self, limit: int = 10, offset: int = 0, filters: dict[str, Any] | None = None,
|
|
85
|
+
def list_files(self, limit: int = 10, offset: int = 0, filters: dict[str, Any] | None = None,
|
|
86
|
+
sort: list[tuple[str, str]] | None = None, token: str | None = None) -> dict:
|
|
65
87
|
tok = _resolve_token(token)
|
|
66
88
|
q = _build_list_query(limit, offset, filters, sort)
|
|
67
|
-
r = self._client.get(
|
|
89
|
+
r = self._client.get(self._base_url, params=q, headers=_auth_headers(tok))
|
|
68
90
|
if r.status_code >= 400:
|
|
69
91
|
raise UploadchiError(r.status_code, r.text)
|
|
70
92
|
return r.json()
|
|
71
93
|
|
|
72
94
|
def get_file(self, file_id: int, token: str | None = None) -> dict:
|
|
73
95
|
tok = _resolve_token(token)
|
|
74
|
-
r = self._client.get(f"/{file_id}", headers=_auth_headers(tok))
|
|
96
|
+
r = self._client.get(f"{self._base_url}/{file_id}", headers=_auth_headers(tok))
|
|
75
97
|
if r.status_code >= 400:
|
|
76
98
|
raise UploadchiError(r.status_code, r.text)
|
|
77
99
|
return r.json()
|
|
78
100
|
|
|
79
101
|
def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]:
|
|
80
102
|
tok = _resolve_token(token)
|
|
81
|
-
r = self._client.get(f"/{file_id}/download", headers=_auth_headers(tok))
|
|
103
|
+
r = self._client.get(f"{self._base_url}/{file_id}/download", headers=_auth_headers(tok))
|
|
82
104
|
if r.status_code >= 400:
|
|
83
105
|
raise UploadchiError(r.status_code, r.text)
|
|
84
106
|
filename = _filename_from_cd(r.headers.get("content-disposition"), fallback=f"file-{file_id}")
|
|
@@ -87,6 +109,6 @@ class UploadchiClient(FileServicePort):
|
|
|
87
109
|
|
|
88
110
|
def delete_file(self, file_id: int, token: str | None = None) -> None:
|
|
89
111
|
tok = _resolve_token(token)
|
|
90
|
-
r = self._client.delete(f"/{file_id}", headers=_auth_headers(tok))
|
|
112
|
+
r = self._client.delete(f"{self._base_url}/{file_id}", headers=_auth_headers(tok))
|
|
91
113
|
if r.status_code not in (204, 200):
|
|
92
114
|
raise UploadchiError(r.status_code, r.text)
|
|
@@ -20,7 +20,7 @@ nlbone/adapters/db/sqlalchemy/query/ordering.py,sha256=THbuxZmoFZzJIa28KfDQ6ioS8
|
|
|
20
20
|
nlbone/adapters/db/sqlalchemy/query/types.py,sha256=M2j6SOSyFkLuyXq4kvPYgZQYz5TKCBe3osoIIN1yfBI,287
|
|
21
21
|
nlbone/adapters/http_clients/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
22
|
nlbone/adapters/http_clients/email_gateway.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
|
-
nlbone/adapters/http_clients/uploadchi.py,sha256=
|
|
23
|
+
nlbone/adapters/http_clients/uploadchi.py,sha256=qguGDQ2NGNSN3BOFSFUINn8mKS4LI23UGURJNhDUH2E,4647
|
|
24
24
|
nlbone/adapters/http_clients/uploadchi_async.py,sha256=u-CHisQoTjuhyXb5h-TLDnfeUnN47e-VEbKamUnkaYI,3710
|
|
25
25
|
nlbone/adapters/messaging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
26
|
nlbone/adapters/messaging/redis.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -57,7 +57,7 @@ nlbone/interfaces/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
|
|
|
57
57
|
nlbone/interfaces/jobs/sync_tokens.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
58
58
|
nlbone/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
59
59
|
nlbone/utils/time.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
60
|
-
nlbone-0.1.
|
|
61
|
-
nlbone-0.1.
|
|
62
|
-
nlbone-0.1.
|
|
63
|
-
nlbone-0.1.
|
|
60
|
+
nlbone-0.1.34.dist-info/METADATA,sha256=5mgHF3w38N6ELApJK62fA5G_z_0eM_QSOwWpWkr-_vQ,2256
|
|
61
|
+
nlbone-0.1.34.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
62
|
+
nlbone-0.1.34.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
63
|
+
nlbone-0.1.34.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|