Bind Nova Poshta API key to cash register (#3)
Each cash register stores its own encrypted NP API key. Status polling uses register keys and binds an order to the register whose key sees the TTN as its own (PhoneSender present); ETTN receipts are created from that register. Migration 0008 moves the old NOVA_POSHTA_API_KEY into the default register. Closes #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,4 +15,8 @@ class NovaPoshtaError(Exception):
|
||||
|
||||
|
||||
class NovaPoshtaClient(Protocol):
|
||||
async def get_statuses(self, *, waybill_numbers: list[str]) -> list[TrackingStatusOut]: ...
|
||||
"""Ключ API передаётся в каждый вызов: у каждой кассы свой кабинет отправителя."""
|
||||
|
||||
async def get_statuses(
|
||||
self, *, api_key: str, waybill_numbers: list[str]
|
||||
) -> list[TrackingStatusOut]: ...
|
||||
|
||||
@@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
from app.core.config import Settings
|
||||
from app.schemas.tracking import TrackingStatusOut
|
||||
from app.services.nova_poshta.client import NovaPoshtaError
|
||||
|
||||
@@ -15,10 +14,9 @@ _MAX_DOCUMENTS_PER_REQUEST = 100
|
||||
|
||||
|
||||
class NpTrackingClient:
|
||||
def __init__(self, settings: Settings) -> None:
|
||||
self._api_key = settings.nova_poshta_api_key
|
||||
|
||||
async def get_statuses(self, *, waybill_numbers: list[str]) -> list[TrackingStatusOut]:
|
||||
async def get_statuses(
|
||||
self, *, api_key: str, waybill_numbers: list[str]
|
||||
) -> list[TrackingStatusOut]:
|
||||
if not waybill_numbers:
|
||||
return []
|
||||
if len(waybill_numbers) > _MAX_DOCUMENTS_PER_REQUEST:
|
||||
@@ -28,7 +26,7 @@ class NpTrackingClient:
|
||||
)
|
||||
|
||||
body = {
|
||||
"apiKey": self._api_key,
|
||||
"apiKey": api_key,
|
||||
"modelName": "TrackingDocument",
|
||||
"calledMethod": "getStatusDocuments",
|
||||
"methodProperties": {
|
||||
|
||||
@@ -15,17 +15,32 @@ _FIXTURE_STATUSES: dict[str, dict] = {
|
||||
"AmountToPay": "1200.00",
|
||||
"AfterpaymentOnGoodsCost": 1200,
|
||||
"PaymentStatus": "Paid",
|
||||
"PhoneSender": "380501112233",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class StubNovaPoshtaClient:
|
||||
def __init__(self, statuses: dict[str, dict] | None = None) -> None:
|
||||
self._statuses = statuses if statuses is not None else _FIXTURE_STATUSES
|
||||
"""`statuses` — ответы NP по ТТН; `by_key` — отдельные ответы для конкретных
|
||||
ключей API (кабинет отправителя видит свои ТТН полностью, чужие — урезанно)."""
|
||||
|
||||
async def get_statuses(self, *, waybill_numbers: list[str]) -> list[TrackingStatusOut]:
|
||||
def __init__(
|
||||
self,
|
||||
statuses: dict[str, dict] | None = None,
|
||||
*,
|
||||
by_key: dict[str, dict[str, dict]] | None = None,
|
||||
) -> None:
|
||||
self._statuses = statuses if statuses is not None else _FIXTURE_STATUSES
|
||||
self._by_key = by_key or {}
|
||||
self.calls: list[tuple[str, list[str]]] = []
|
||||
|
||||
async def get_statuses(
|
||||
self, *, api_key: str, waybill_numbers: list[str]
|
||||
) -> list[TrackingStatusOut]:
|
||||
self.calls.append((api_key, list(waybill_numbers)))
|
||||
statuses = self._by_key.get(api_key, self._statuses)
|
||||
return [
|
||||
TrackingStatusOut.model_validate(self._statuses[number])
|
||||
TrackingStatusOut.model_validate(statuses[number])
|
||||
for number in waybill_numbers
|
||||
if number in self._statuses
|
||||
if number in statuses
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user