nlbone 0.1.32__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 +46 -13
- nlbone/adapters/http_clients/uploadchi_async.py +25 -20
- nlbone/config/settings.py +8 -1
- nlbone/core/ports/files.py +2 -4
- {nlbone-0.1.32.dist-info → nlbone-0.1.34.dist-info}/METADATA +1 -1
- {nlbone-0.1.32.dist-info → nlbone-0.1.34.dist-info}/RECORD +8 -8
- {nlbone-0.1.32.dist-info → nlbone-0.1.34.dist-info}/WHEEL +0 -0
- {nlbone-0.1.32.dist-info → nlbone-0.1.34.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,21 +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
|
+
|
|
20
|
+
def _resolve_token(explicit: str | None) -> str | None:
|
|
21
|
+
if explicit is not None:
|
|
22
|
+
return explicit
|
|
23
|
+
s = get_settings()
|
|
24
|
+
return s.UPLOADCHI_TOKEN.get_secret_value() if s.UPLOADCHI_TOKEN else None
|
|
25
|
+
|
|
26
|
+
|
|
15
27
|
def _auth_headers(token: str | None) -> dict[str, str]:
|
|
16
28
|
return {"Authorization": f"Bearer {token}"} if token else {}
|
|
17
29
|
|
|
18
|
-
|
|
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]:
|
|
19
33
|
q: dict[str, Any] = {"limit": limit, "offset": offset}
|
|
20
34
|
if filters:
|
|
21
35
|
q["filters"] = json.dumps(filters)
|
|
@@ -23,6 +37,7 @@ def _build_list_query(limit: int, offset: int, filters: dict[str, Any] | None, s
|
|
|
23
37
|
q["sort"] = ",".join([f"{f}:{o}" for f, o in sort])
|
|
24
38
|
return q
|
|
25
39
|
|
|
40
|
+
|
|
26
41
|
def _filename_from_cd(cd: str | None, fallback: str) -> str:
|
|
27
42
|
if not cd:
|
|
28
43
|
return fallback
|
|
@@ -30,45 +45,62 @@ def _filename_from_cd(cd: str | None, fallback: str) -> str:
|
|
|
30
45
|
return cd.split("filename=", 1)[1].strip("\"'")
|
|
31
46
|
return fallback
|
|
32
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
|
+
|
|
33
57
|
class UploadchiClient(FileServicePort):
|
|
34
|
-
|
|
35
|
-
|
|
58
|
+
def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None,
|
|
59
|
+
client: httpx.Client | None = None) -> None:
|
|
36
60
|
s = get_settings()
|
|
37
|
-
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))
|
|
38
62
|
self._timeout = timeout_seconds or float(s.HTTP_TIMEOUT_SECONDS)
|
|
39
|
-
self._client = client or
|
|
63
|
+
self._client = client or requests.session()
|
|
40
64
|
|
|
41
65
|
def close(self) -> None:
|
|
42
66
|
self._client.close()
|
|
43
67
|
|
|
44
|
-
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:
|
|
70
|
+
tok = _resolve_token(token)
|
|
45
71
|
files = {"file": (filename, file_bytes)}
|
|
46
72
|
data = (params or {}).copy()
|
|
47
|
-
r = self._client.post(
|
|
73
|
+
r = self._client.post(self._base_url, files=files, data=data, headers=_auth_headers(tok))
|
|
48
74
|
if r.status_code >= 400:
|
|
49
75
|
raise UploadchiError(r.status_code, r.text)
|
|
50
76
|
return r.json()
|
|
51
77
|
|
|
52
78
|
def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None:
|
|
53
|
-
|
|
79
|
+
tok = _resolve_token(token)
|
|
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)
|
|
54
82
|
if r.status_code not in (204, 200):
|
|
55
83
|
raise UploadchiError(r.status_code, r.text)
|
|
56
84
|
|
|
57
|
-
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:
|
|
87
|
+
tok = _resolve_token(token)
|
|
58
88
|
q = _build_list_query(limit, offset, filters, sort)
|
|
59
|
-
r = self._client.get(
|
|
89
|
+
r = self._client.get(self._base_url, params=q, headers=_auth_headers(tok))
|
|
60
90
|
if r.status_code >= 400:
|
|
61
91
|
raise UploadchiError(r.status_code, r.text)
|
|
62
92
|
return r.json()
|
|
63
93
|
|
|
64
94
|
def get_file(self, file_id: int, token: str | None = None) -> dict:
|
|
65
|
-
|
|
95
|
+
tok = _resolve_token(token)
|
|
96
|
+
r = self._client.get(f"{self._base_url}/{file_id}", headers=_auth_headers(tok))
|
|
66
97
|
if r.status_code >= 400:
|
|
67
98
|
raise UploadchiError(r.status_code, r.text)
|
|
68
99
|
return r.json()
|
|
69
100
|
|
|
70
101
|
def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]:
|
|
71
|
-
|
|
102
|
+
tok = _resolve_token(token)
|
|
103
|
+
r = self._client.get(f"{self._base_url}/{file_id}/download", headers=_auth_headers(tok))
|
|
72
104
|
if r.status_code >= 400:
|
|
73
105
|
raise UploadchiError(r.status_code, r.text)
|
|
74
106
|
filename = _filename_from_cd(r.headers.get("content-disposition"), fallback=f"file-{file_id}")
|
|
@@ -76,6 +108,7 @@ class UploadchiClient(FileServicePort):
|
|
|
76
108
|
return r.content, filename, media_type
|
|
77
109
|
|
|
78
110
|
def delete_file(self, file_id: int, token: str | None = None) -> None:
|
|
79
|
-
|
|
111
|
+
tok = _resolve_token(token)
|
|
112
|
+
r = self._client.delete(f"{self._base_url}/{file_id}", headers=_auth_headers(tok))
|
|
80
113
|
if r.status_code not in (204, 200):
|
|
81
114
|
raise UploadchiError(r.status_code, r.text)
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
import json
|
|
3
2
|
from typing import Any, Optional, AsyncIterator
|
|
4
3
|
import httpx
|
|
5
4
|
|
|
6
5
|
from nlbone.core.ports.files import AsyncFileServicePort
|
|
7
6
|
from nlbone.config.settings import get_settings
|
|
8
|
-
from .uploadchi import UploadchiError, _auth_headers, _build_list_query, _filename_from_cd
|
|
7
|
+
from .uploadchi import UploadchiError, _auth_headers, _build_list_query, _filename_from_cd, _resolve_token
|
|
9
8
|
|
|
10
9
|
class UploadchiAsyncClient(AsyncFileServicePort):
|
|
11
|
-
"""Async client using httpx.AsyncClient; method names prefixed with a_"""
|
|
12
10
|
def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None, client: httpx.AsyncClient | None = None) -> None:
|
|
13
11
|
s = get_settings()
|
|
14
12
|
self._base_url = base_url or str(s.UPLOADCHI_BASE_URL)
|
|
@@ -18,34 +16,39 @@ class UploadchiAsyncClient(AsyncFileServicePort):
|
|
|
18
16
|
async def aclose(self) -> None:
|
|
19
17
|
await self._client.aclose()
|
|
20
18
|
|
|
21
|
-
async def
|
|
19
|
+
async def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict:
|
|
20
|
+
tok = _resolve_token(token)
|
|
22
21
|
files = {"file": (filename, file_bytes)}
|
|
23
22
|
data = (params or {}).copy()
|
|
24
|
-
r = await self._client.post("", files=files, data=data, headers=_auth_headers(
|
|
23
|
+
r = await self._client.post("", files=files, data=data, headers=_auth_headers(tok))
|
|
25
24
|
if r.status_code >= 400:
|
|
26
|
-
raise UploadchiError(r.status_code, r.
|
|
25
|
+
raise UploadchiError(r.status_code, await r.aread())
|
|
27
26
|
return r.json()
|
|
28
27
|
|
|
29
|
-
async def
|
|
30
|
-
|
|
28
|
+
async def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None:
|
|
29
|
+
tok = _resolve_token(token)
|
|
30
|
+
r = await self._client.post(f"/{file_id}/commit", headers=_auth_headers(tok), params={"client_id": client_id} if client_id else None)
|
|
31
31
|
if r.status_code not in (204, 200):
|
|
32
|
-
raise UploadchiError(r.status_code, r.
|
|
32
|
+
raise UploadchiError(r.status_code, await r.aread())
|
|
33
33
|
|
|
34
|
-
async def
|
|
34
|
+
async def list_files(self, limit: int = 10, offset: int = 0, filters: dict[str, Any] | None = None, sort: list[tuple[str, str]] | None = None, token: str | None = None) -> dict:
|
|
35
|
+
tok = _resolve_token(token)
|
|
35
36
|
q = _build_list_query(limit, offset, filters, sort)
|
|
36
|
-
r = await self._client.get("", params=q, headers=_auth_headers(
|
|
37
|
+
r = await self._client.get("", params=q, headers=_auth_headers(tok))
|
|
37
38
|
if r.status_code >= 400:
|
|
38
|
-
raise UploadchiError(r.status_code, r.
|
|
39
|
+
raise UploadchiError(r.status_code, await r.aread())
|
|
39
40
|
return r.json()
|
|
40
41
|
|
|
41
|
-
async def
|
|
42
|
-
|
|
42
|
+
async def get_file(self, file_id: int, token: str | None = None) -> dict:
|
|
43
|
+
tok = _resolve_token(token)
|
|
44
|
+
r = await self._client.get(f"/{file_id}", headers=_auth_headers(tok))
|
|
43
45
|
if r.status_code >= 400:
|
|
44
|
-
raise UploadchiError(r.status_code, r.
|
|
46
|
+
raise UploadchiError(r.status_code, await r.aread())
|
|
45
47
|
return r.json()
|
|
46
48
|
|
|
47
|
-
async def
|
|
48
|
-
|
|
49
|
+
async def download_file(self, file_id: int, token: str | None = None) -> tuple[AsyncIterator[bytes], str, str]:
|
|
50
|
+
tok = _resolve_token(token)
|
|
51
|
+
r = await self._client.get(f"/{file_id}/download", headers=_auth_headers(tok), stream=True)
|
|
49
52
|
if r.status_code >= 400:
|
|
50
53
|
body = await r.aread()
|
|
51
54
|
raise UploadchiError(r.status_code, body.decode(errors="ignore"))
|
|
@@ -61,7 +64,9 @@ class UploadchiAsyncClient(AsyncFileServicePort):
|
|
|
61
64
|
|
|
62
65
|
return _aiter(), filename, media_type
|
|
63
66
|
|
|
64
|
-
async def
|
|
65
|
-
|
|
67
|
+
async def delete_file(self, file_id: int, token: str | None = None) -> None:
|
|
68
|
+
tok = _resolve_token(token)
|
|
69
|
+
r = await self._client.delete(f"/{file_id}", headers=_auth_headers(tok))
|
|
66
70
|
if r.status_code not in (204, 200):
|
|
67
|
-
|
|
71
|
+
body = await r.aread()
|
|
72
|
+
raise UploadchiError(r.status_code, body.decode(errors="ignore"))
|
nlbone/config/settings.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
|
2
2
|
from functools import lru_cache
|
|
3
3
|
from pathlib import Path
|
|
4
4
|
from typing import Literal
|
|
5
|
-
from pydantic import AnyHttpUrl, Field, SecretStr
|
|
5
|
+
from pydantic import AnyHttpUrl, Field, SecretStr, AliasChoices
|
|
6
6
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
7
7
|
|
|
8
8
|
|
|
@@ -54,7 +54,14 @@ class Settings(BaseSettings):
|
|
|
54
54
|
# ---------------------------
|
|
55
55
|
REDIS_URL: str = Field(default="redis://localhost:6379/0")
|
|
56
56
|
|
|
57
|
+
# ---------------------------
|
|
58
|
+
# UPLOADCHI
|
|
59
|
+
# ---------------------------
|
|
57
60
|
UPLOADCHI_BASE_URL: AnyHttpUrl = Field(default="https://uploadchi.numberland.ir/v1/files")
|
|
61
|
+
UPLOADCHI_TOKEN: SecretStr | None = Field(
|
|
62
|
+
default=None,
|
|
63
|
+
validation_alias=AliasChoices("NLBONE_UPLOADCHI_TOKEN", "UPLOADCHI_TOKEN"),
|
|
64
|
+
)
|
|
58
65
|
|
|
59
66
|
model_config = SettingsConfigDict(
|
|
60
67
|
env_prefix="NLBONE_",
|
nlbone/core/ports/files.py
CHANGED
|
@@ -3,20 +3,18 @@ from typing import Protocol, runtime_checkable, AsyncIterator, Any
|
|
|
3
3
|
|
|
4
4
|
@runtime_checkable
|
|
5
5
|
class FileServicePort(Protocol):
|
|
6
|
-
# ---- Sync API ----
|
|
7
6
|
def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict: ...
|
|
8
7
|
def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None: ...
|
|
9
8
|
def list_files(self, limit: int = 10, offset: int = 0, filters: dict[str, Any] | None = None, sort: list[tuple[str, str]] | None = None, token: str | None = None) -> dict: ...
|
|
10
9
|
def get_file(self, file_id: int, token: str | None = None) -> dict: ...
|
|
11
|
-
def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]: ...
|
|
10
|
+
def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]: ...
|
|
12
11
|
def delete_file(self, file_id: int, token: str | None = None) -> None: ...
|
|
13
12
|
|
|
14
13
|
@runtime_checkable
|
|
15
14
|
class AsyncFileServicePort(Protocol):
|
|
16
|
-
# ---- Async API (with a_-prefix) ----
|
|
17
15
|
async def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict: ...
|
|
18
16
|
async def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None: ...
|
|
19
17
|
async def list_files(self, limit: int = 10, offset: int = 0, filters: dict[str, Any] | None = None, sort: list[tuple[str, str]] | None = None, token: str | None = None) -> dict: ...
|
|
20
18
|
async def get_file(self, file_id: int, token: str | None = None) -> dict: ...
|
|
21
|
-
async def download_file(self, file_id: int, token: str | None = None) -> tuple[AsyncIterator[bytes], str, str]: ...
|
|
19
|
+
async def download_file(self, file_id: int, token: str | None = None) -> tuple[AsyncIterator[bytes], str, str]: ...
|
|
22
20
|
async def delete_file(self, file_id: int, token: str | None = None) -> None: ...
|
|
@@ -20,13 +20,13 @@ 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=
|
|
24
|
-
nlbone/adapters/http_clients/uploadchi_async.py,sha256=
|
|
23
|
+
nlbone/adapters/http_clients/uploadchi.py,sha256=qguGDQ2NGNSN3BOFSFUINn8mKS4LI23UGURJNhDUH2E,4647
|
|
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
|
|
27
27
|
nlbone/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
28
28
|
nlbone/config/logging.py,sha256=68WRQejEpL6eHEY_cOgdlOjndKc-RWth0n4YmXnceC8,5041
|
|
29
|
-
nlbone/config/settings.py,sha256=
|
|
29
|
+
nlbone/config/settings.py,sha256=IbjhevN_0TuO_on1-VxXH_aOaVwj3K7YBhU8X9tIWE4,2626
|
|
30
30
|
nlbone/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
31
31
|
nlbone/core/application/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
32
32
|
nlbone/core/application/services.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -38,7 +38,7 @@ nlbone/core/domain/events.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,
|
|
|
38
38
|
nlbone/core/domain/models.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
39
39
|
nlbone/core/ports/__init__.py,sha256=J24ldeLIQ_AWqKpdNbtaEWq4-G4cyWx8XEG0Dt6keao,29
|
|
40
40
|
nlbone/core/ports/auth.py,sha256=v2NiH8FzKXZ1MabzQxaK7AQvnt-GQindYIki90swTE4,492
|
|
41
|
-
nlbone/core/ports/files.py,sha256=
|
|
41
|
+
nlbone/core/ports/files.py,sha256=yJwW0kFWxkrT4ntfXDdJYyKSEWB6daEuaI6zgmeg0Gg,1596
|
|
42
42
|
nlbone/core/ports/messaging.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
43
43
|
nlbone/core/ports/repo.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
44
44
|
nlbone/interfaces/__init__.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
|