nlbone 0.1.31__tar.gz → 0.1.33__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.
Files changed (67) hide show
  1. {nlbone-0.1.31 → nlbone-0.1.33}/PKG-INFO +1 -1
  2. {nlbone-0.1.31 → nlbone-0.1.33}/pyproject.toml +1 -1
  3. nlbone-0.1.33/src/nlbone/adapters/http_clients/uploadchi.py +92 -0
  4. nlbone-0.1.33/src/nlbone/adapters/http_clients/uploadchi_async.py +72 -0
  5. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/config/settings.py +8 -1
  6. {nlbone-0.1.31/src/nlbone/di → nlbone-0.1.33/src/nlbone}/container.py +3 -1
  7. nlbone-0.1.33/src/nlbone/core/ports/files.py +20 -0
  8. nlbone-0.1.31/src/nlbone/adapters/http_clients/uploadchi.py +0 -177
  9. nlbone-0.1.31/src/nlbone/core/ports/files.py +0 -47
  10. nlbone-0.1.31/src/nlbone/utils/__init__.py +0 -0
  11. {nlbone-0.1.31 → nlbone-0.1.33}/.gitignore +0 -0
  12. {nlbone-0.1.31 → nlbone-0.1.33}/LICENSE +0 -0
  13. {nlbone-0.1.31 → nlbone-0.1.33}/README.md +0 -0
  14. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/__init__.py +0 -0
  15. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/__init__.py +0 -0
  16. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/auth/__init__.py +0 -0
  17. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/auth/keycloak.py +0 -0
  18. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/__init__.py +0 -0
  19. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/memory.py +0 -0
  20. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/postgres.py +0 -0
  21. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/query_builder.py +0 -0
  22. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/__init__.py +0 -0
  23. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/base.py +0 -0
  24. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/engine.py +0 -0
  25. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/__init__.py +0 -0
  26. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/builder.py +0 -0
  27. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/coercion.py +0 -0
  28. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/filters.py +0 -0
  29. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/ordering.py +0 -0
  30. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/query/types.py +0 -0
  31. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/db/sqlalchemy/schema.py +0 -0
  32. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/http_clients/__init__.py +0 -0
  33. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/http_clients/email_gateway.py +0 -0
  34. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/messaging/__init__.py +0 -0
  35. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/adapters/messaging/redis.py +0 -0
  36. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/config/__init__.py +0 -0
  37. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/config/logging.py +0 -0
  38. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/__init__.py +0 -0
  39. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/application/__init__.py +0 -0
  40. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/application/services/__init__.py +0 -0
  41. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/application/services.py +0 -0
  42. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/application/use_cases/__init__.py +0 -0
  43. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/application/use_cases/register_user.py +0 -0
  44. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/domain/__init__.py +0 -0
  45. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/domain/events.py +0 -0
  46. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/domain/models.py +0 -0
  47. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/ports/__init__.py +0 -0
  48. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/ports/auth.py +0 -0
  49. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/ports/messaging.py +0 -0
  50. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/core/ports/repo.py +0 -0
  51. {nlbone-0.1.31/src/nlbone/di → nlbone-0.1.33/src/nlbone/interfaces}/__init__.py +0 -0
  52. {nlbone-0.1.31/src/nlbone/interfaces → nlbone-0.1.33/src/nlbone/interfaces/api}/__init__.py +0 -0
  53. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/dependencies/__init__.py +0 -0
  54. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/dependencies/db.py +0 -0
  55. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/exceptions.py +0 -0
  56. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/pagination/__init__.py +0 -0
  57. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/pagination/offset_base.py +0 -0
  58. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/routers.py +0 -0
  59. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/api/schemas.py +0 -0
  60. {nlbone-0.1.31/src/nlbone/interfaces/api → nlbone-0.1.33/src/nlbone/interfaces/cli}/__init__.py +0 -0
  61. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/cli/init_db.py +0 -0
  62. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/cli/main.py +0 -0
  63. {nlbone-0.1.31/src/nlbone/interfaces/cli → nlbone-0.1.33/src/nlbone/interfaces/jobs}/__init__.py +0 -0
  64. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/interfaces/jobs/sync_tokens.py +0 -0
  65. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/types.py +0 -0
  66. {nlbone-0.1.31/src/nlbone/interfaces/jobs → nlbone-0.1.33/src/nlbone/utils}/__init__.py +0 -0
  67. {nlbone-0.1.31 → nlbone-0.1.33}/src/nlbone/utils/time.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nlbone
3
- Version: 0.1.31
3
+ Version: 0.1.33
4
4
  Summary: Backbone package for interfaces and infrastructure in Python projects
5
5
  Author-email: Amir Hosein Kahkbazzadeh <a.khakbazzadeh@gmail.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "nlbone"
7
- version = "0.1.31"
7
+ version = "0.1.33"
8
8
  description = "Backbone package for interfaces and infrastructure in Python projects"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,92 @@
1
+ from __future__ import annotations
2
+ import json
3
+ from typing import Any, Optional
4
+ import httpx
5
+
6
+ from nlbone.core.ports.files import FileServicePort
7
+ from nlbone.config.settings import get_settings
8
+
9
+ class UploadchiError(RuntimeError):
10
+ def __init__(self, status: int, detail: Any | None = None):
11
+ super().__init__(f"Uploadchi HTTP {status}: {detail}")
12
+ self.status = status
13
+ self.detail = detail
14
+
15
+ def _resolve_token(explicit: str | None) -> str | None:
16
+ if explicit is not None:
17
+ return explicit
18
+ s = get_settings()
19
+ return s.UPLOADCHI_TOKEN.get_secret_value() if s.UPLOADCHI_TOKEN else None
20
+
21
+ def _auth_headers(token: str | None) -> dict[str, str]:
22
+ return {"Authorization": f"Bearer {token}"} if token else {}
23
+
24
+ def _build_list_query(limit: int, offset: int, filters: dict[str, Any] | None, sort: list[tuple[str, str]] | None) -> dict[str, Any]:
25
+ q: dict[str, Any] = {"limit": limit, "offset": offset}
26
+ if filters:
27
+ q["filters"] = json.dumps(filters)
28
+ if sort:
29
+ q["sort"] = ",".join([f"{f}:{o}" for f, o in sort])
30
+ return q
31
+
32
+ def _filename_from_cd(cd: str | None, fallback: str) -> str:
33
+ if not cd:
34
+ return fallback
35
+ if "filename=" in cd:
36
+ return cd.split("filename=", 1)[1].strip("\"'")
37
+ return fallback
38
+
39
+ class UploadchiClient(FileServicePort):
40
+ def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None, client: httpx.Client | None = None) -> None:
41
+ s = get_settings()
42
+ self._base_url = base_url or str(s.UPLOADCHI_BASE_URL)
43
+ self._timeout = timeout_seconds or float(s.HTTP_TIMEOUT_SECONDS)
44
+ self._client = client or httpx.Client(base_url=self._base_url, timeout=self._timeout, follow_redirects=True)
45
+
46
+ def close(self) -> None:
47
+ self._client.close()
48
+
49
+ def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict:
50
+ tok = _resolve_token(token)
51
+ files = {"file": (filename, file_bytes)}
52
+ data = (params or {}).copy()
53
+ r = self._client.post("", files=files, data=data, headers=_auth_headers(tok))
54
+ if r.status_code >= 400:
55
+ raise UploadchiError(r.status_code, r.text)
56
+ return r.json()
57
+
58
+ def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None:
59
+ tok = _resolve_token(token)
60
+ r = self._client.post(f"/{file_id}/commit", headers=_auth_headers(tok), params={"client_id": client_id} if client_id else None)
61
+ if r.status_code not in (204, 200):
62
+ raise UploadchiError(r.status_code, r.text)
63
+
64
+ 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:
65
+ tok = _resolve_token(token)
66
+ q = _build_list_query(limit, offset, filters, sort)
67
+ r = self._client.get("", params=q, headers=_auth_headers(tok))
68
+ if r.status_code >= 400:
69
+ raise UploadchiError(r.status_code, r.text)
70
+ return r.json()
71
+
72
+ def get_file(self, file_id: int, token: str | None = None) -> dict:
73
+ tok = _resolve_token(token)
74
+ r = self._client.get(f"/{file_id}", headers=_auth_headers(tok))
75
+ if r.status_code >= 400:
76
+ raise UploadchiError(r.status_code, r.text)
77
+ return r.json()
78
+
79
+ def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]:
80
+ tok = _resolve_token(token)
81
+ r = self._client.get(f"/{file_id}/download", headers=_auth_headers(tok))
82
+ if r.status_code >= 400:
83
+ raise UploadchiError(r.status_code, r.text)
84
+ filename = _filename_from_cd(r.headers.get("content-disposition"), fallback=f"file-{file_id}")
85
+ media_type = r.headers.get("content-type", "application/octet-stream")
86
+ return r.content, filename, media_type
87
+
88
+ def delete_file(self, file_id: int, token: str | None = None) -> None:
89
+ tok = _resolve_token(token)
90
+ r = self._client.delete(f"/{file_id}", headers=_auth_headers(tok))
91
+ if r.status_code not in (204, 200):
92
+ raise UploadchiError(r.status_code, r.text)
@@ -0,0 +1,72 @@
1
+ from __future__ import annotations
2
+ from typing import Any, Optional, AsyncIterator
3
+ import httpx
4
+
5
+ from nlbone.core.ports.files import AsyncFileServicePort
6
+ from nlbone.config.settings import get_settings
7
+ from .uploadchi import UploadchiError, _auth_headers, _build_list_query, _filename_from_cd, _resolve_token
8
+
9
+ class UploadchiAsyncClient(AsyncFileServicePort):
10
+ def __init__(self, base_url: Optional[str] = None, timeout_seconds: Optional[float] = None, client: httpx.AsyncClient | None = None) -> None:
11
+ s = get_settings()
12
+ self._base_url = base_url or str(s.UPLOADCHI_BASE_URL)
13
+ self._timeout = timeout_seconds or float(s.HTTP_TIMEOUT_SECONDS)
14
+ self._client = client or httpx.AsyncClient(base_url=self._base_url, timeout=self._timeout, follow_redirects=True)
15
+
16
+ async def aclose(self) -> None:
17
+ await self._client.aclose()
18
+
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)
21
+ files = {"file": (filename, file_bytes)}
22
+ data = (params or {}).copy()
23
+ r = await self._client.post("", files=files, data=data, headers=_auth_headers(tok))
24
+ if r.status_code >= 400:
25
+ raise UploadchiError(r.status_code, await r.aread())
26
+ return r.json()
27
+
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
+ if r.status_code not in (204, 200):
32
+ raise UploadchiError(r.status_code, await r.aread())
33
+
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)
36
+ q = _build_list_query(limit, offset, filters, sort)
37
+ r = await self._client.get("", params=q, headers=_auth_headers(tok))
38
+ if r.status_code >= 400:
39
+ raise UploadchiError(r.status_code, await r.aread())
40
+ return r.json()
41
+
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))
45
+ if r.status_code >= 400:
46
+ raise UploadchiError(r.status_code, await r.aread())
47
+ return r.json()
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)
52
+ if r.status_code >= 400:
53
+ body = await r.aread()
54
+ raise UploadchiError(r.status_code, body.decode(errors="ignore"))
55
+ filename = _filename_from_cd(r.headers.get("content-disposition"), fallback=f"file-{file_id}")
56
+ media_type = r.headers.get("content-type", "application/octet-stream")
57
+
58
+ async def _aiter() -> AsyncIterator[bytes]:
59
+ try:
60
+ async for chunk in r.aiter_bytes():
61
+ yield chunk
62
+ finally:
63
+ await r.aclose()
64
+
65
+ return _aiter(), filename, media_type
66
+
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))
70
+ if r.status_code not in (204, 200):
71
+ body = await r.aread()
72
+ raise UploadchiError(r.status_code, body.decode(errors="ignore"))
@@ -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_",
@@ -1,7 +1,8 @@
1
1
  from nlbone.adapters.http_clients.uploadchi import UploadchiClient
2
+ from nlbone.adapters.http_clients.uploadchi_async import UploadchiAsyncClient
2
3
  from nlbone.config.settings import Settings
3
4
  from nlbone.adapters.auth.keycloak import KeycloakAuthService
4
- from nlbone.core.ports.files import FileServicePort
5
+ from nlbone.core.ports.files import FileServicePort, AsyncFileServicePort
5
6
 
6
7
 
7
8
  class Container:
@@ -9,3 +10,4 @@ class Container:
9
10
  self.settings = settings or Settings()
10
11
  self.auth: KeycloakAuthService = KeycloakAuthService(self.settings)
11
12
  self.file_service: FileServicePort = UploadchiClient()
13
+ self.afiles_service: AsyncFileServicePort = UploadchiAsyncClient()
@@ -0,0 +1,20 @@
1
+ from __future__ import annotations
2
+ from typing import Protocol, runtime_checkable, AsyncIterator, Any
3
+
4
+ @runtime_checkable
5
+ class FileServicePort(Protocol):
6
+ def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict: ...
7
+ def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None: ...
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: ...
9
+ def get_file(self, file_id: int, token: str | None = None) -> dict: ...
10
+ def download_file(self, file_id: int, token: str | None = None) -> tuple[bytes, str, str]: ...
11
+ def delete_file(self, file_id: int, token: str | None = None) -> None: ...
12
+
13
+ @runtime_checkable
14
+ class AsyncFileServicePort(Protocol):
15
+ async def upload_file(self, file_bytes: bytes, filename: str, params: dict[str, Any] | None = None, token: str | None = None) -> dict: ...
16
+ async def commit_file(self, file_id: int, client_id: str, token: str | None = None) -> None: ...
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: ...
18
+ async def get_file(self, file_id: int, token: str | None = None) -> dict: ...
19
+ async def download_file(self, file_id: int, token: str | None = None) -> tuple[AsyncIterator[bytes], str, str]: ...
20
+ async def delete_file(self, file_id: int, token: str | None = None) -> None: ...
@@ -1,177 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import json
4
- from typing import Any, AsyncIterator, Optional
5
-
6
- import httpx
7
-
8
- from nlbone.core.ports.files import FileServicePort
9
- from nlbone.config.settings import get_settings
10
-
11
-
12
- class FileServiceException(RuntimeError):
13
- def __init__(self, status: int, detail: Any | None = None):
14
- super().__init__(f"Uploadchi HTTP {status}: {detail}")
15
- self.status = status
16
- self.detail = detail
17
-
18
-
19
- def _auth_headers(token: str | None) -> dict[str, str]:
20
- return {"Authorization": f"Bearer {token}"} if token else {}
21
-
22
-
23
- def _build_list_query(
24
- limit: int,
25
- offset: int,
26
- filters: dict[str, Any] | None,
27
- sort: list[tuple[str, str]] | None,
28
- ) -> dict[str, Any]:
29
- q: dict[str, Any] = {"limit": limit, "offset": offset}
30
- if filters:
31
- # سرور شما `filters` را به صورت string می‌گیرد؛
32
- # اگر سمت سرور JSON هم قبول می‌کند، این بهتر است:
33
- q["filters"] = json.dumps(filters)
34
- if sort:
35
- # "created_at:desc,id:asc"
36
- q["sort"] = ",".join([f"{f}:{o}" for f, o in sort])
37
- return q
38
-
39
-
40
- class UploadchiClient(FileServicePort):
41
- """
42
- httpx-based client for the Uploadchi microservice.
43
-
44
- Base URL نمونه: http://uploadchi.internal/api/v1/files
45
- """
46
-
47
- def __init__(
48
- self,
49
- base_url: Optional[str] = None,
50
- timeout_seconds: Optional[float] = None,
51
- client: httpx.AsyncClient | None = None,
52
- ) -> None:
53
- s = get_settings()
54
- self._base_url = base_url or str(s.UPLOADCHI_BASE_URL)
55
- self._timeout = timeout_seconds or float(s.HTTP_TIMEOUT_SECONDS)
56
- # اگر کلاینت تزریق نشد، خودمان می‌سازیم
57
- self._client = client or httpx.AsyncClient(
58
- base_url=self._base_url,
59
- timeout=self._timeout,
60
- follow_redirects=True,
61
- )
62
-
63
- async def aclose(self) -> None:
64
- await self._client.aclose()
65
-
66
- # ---------- Endpoints ----------
67
-
68
- async def upload_file(
69
- self,
70
- file_bytes: bytes,
71
- filename: str,
72
- params: dict[str, Any] | None = None,
73
- token: str | None = None,
74
- ) -> dict:
75
- """
76
- POST "" → returns FileOut (dict)
77
- fields:
78
- - file: Upload (multipart)
79
- - other params (e.g., bucket, folder, content_type, ...)
80
- """
81
- files = {"file": (filename, file_bytes)}
82
- data = (params or {}).copy()
83
- r = await self._client.post(
84
- url="",
85
- files=files,
86
- data=data,
87
- headers=_auth_headers(token),
88
- )
89
- if r.status_code >= 400:
90
- raise FileServiceException(r.status_code, r.text)
91
- return r.json()
92
-
93
- async def commit_file(
94
- self,
95
- file_id: int,
96
- client_id: str,
97
- token: str | None = None,
98
- ) -> None:
99
- """
100
- POST "/{file_id}/commit" 204
101
- """
102
- r = await self._client.post(
103
- f"/{file_id}/commit",
104
- headers=_auth_headers(token),
105
- params={"client_id": client_id} if client_id else None,
106
- )
107
- if r.status_code not in (204, 200):
108
- raise FileServiceException(r.status_code, r.text)
109
-
110
- async def list_files(
111
- self,
112
- limit: int = 10,
113
- offset: int = 0,
114
- filters: dict[str, Any] | None = None,
115
- sort: list[tuple[str, str]] | None = None,
116
- token: str | None = None,
117
- ) -> dict:
118
- """
119
- GET "" → returns PaginateResponse-like dict
120
- { "data": [...], "total_count": int | null, "total_page": int | null }
121
- """
122
- q = _build_list_query(limit, offset, filters, sort)
123
- r = await self._client.get("", params=q, headers=_auth_headers(token))
124
- if r.status_code >= 400:
125
- raise FileServiceException(r.status_code, r.text)
126
- return r.json()
127
-
128
- async def get_file(
129
- self,
130
- file_id: int,
131
- token: str | None = None,
132
- ) -> dict:
133
- """
134
- GET "/{file_id}" → FileOut dict
135
- """
136
- r = await self._client.get(f"/{file_id}", headers=_auth_headers(token))
137
- if r.status_code >= 400:
138
- raise FileServiceException(r.status_code, r.text)
139
- return r.json()
140
-
141
- async def download_file(
142
- self,
143
- file_id: int,
144
- token: str | None = None,
145
- ) -> tuple[AsyncIterator[bytes], str, str]:
146
- """
147
- GET "/{file_id}/download" → stream + headers (filename, content-type)
148
- """
149
- r = await self._client.get(f"/{file_id}/download", headers=_auth_headers(token))
150
- if r.status_code >= 400:
151
- text = await r.aread()
152
- raise FileServiceException(r.status_code, text.decode(errors="ignore"))
153
-
154
- disp = r.headers.get("content-disposition", "")
155
- filename = (
156
- disp.split("filename=", 1)[1].strip("\"'") if "filename=" in disp else f"file-{file_id}"
157
- )
158
- media_type = r.headers.get("content-type", "application/octet-stream")
159
-
160
- async def _aiter() -> AsyncIterator[bytes]:
161
- async for chunk in r.aiter_bytes():
162
- yield chunk
163
- await r.aclose()
164
-
165
- return _aiter(), filename, media_type
166
-
167
- async def delete_file(
168
- self,
169
- file_id: int,
170
- token: str | None = None,
171
- ) -> None:
172
- """
173
- DELETE "/{file_id}" → 204
174
- """
175
- r = await self._client.delete(f"/{file_id}", headers=_auth_headers(token))
176
- if r.status_code not in (204, 200):
177
- raise FileServiceException(r.status_code, r.text)
@@ -1,47 +0,0 @@
1
- from __future__ import annotations
2
- from typing import Protocol, runtime_checkable, AsyncIterator, Any
3
-
4
- @runtime_checkable
5
- class FileServicePort(Protocol):
6
- async def upload_file(
7
- self,
8
- file_bytes: bytes,
9
- filename: str,
10
- params: dict[str, Any] | None = None,
11
- token: str | None = None,
12
- ) -> dict: ...
13
-
14
- async def commit_file(
15
- self,
16
- file_id: int,
17
- client_id: str,
18
- token: str | None = None,
19
- ) -> None: ...
20
-
21
- async def list_files(
22
- self,
23
- limit: int = 10,
24
- offset: int = 0,
25
- filters: dict[str, Any] | None = None,
26
- sort: list[tuple[str, str]] | None = None,
27
- token: str | None = None,
28
- ) -> dict: ...
29
-
30
- async def get_file(
31
- self,
32
- file_id: int,
33
- token: str | None = None,
34
- ) -> dict: ...
35
-
36
- async def download_file(
37
- self,
38
- file_id: int,
39
- token: str | None = None,
40
- ) -> tuple[AsyncIterator[bytes], str, str]:
41
- ...
42
-
43
- async def delete_file(
44
- self,
45
- file_id: int,
46
- token: str | None = None,
47
- ) -> None: ...
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes