|
| 1 | +"""PIN validation client for KRA checkers.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from pydantic import BaseModel, ConfigDict, Field |
| 6 | + |
| 7 | +from gavaconnect.auth.bearer import BearerAuthPolicy |
| 8 | +from gavaconnect.helpers.idempotency import idempotency_headers |
| 9 | +from gavaconnect.http.transport import AsyncTransport |
| 10 | + |
| 11 | + |
| 12 | +class PinCheckResult(BaseModel): |
| 13 | + """Result of PIN validation check.""" |
| 14 | + |
| 15 | + pin: str | None = Field(default=None, alias="PIN") |
| 16 | + taxpayer_name: str | None = Field(default=None, alias="TaxPayerName") |
| 17 | + status: str | None = None |
| 18 | + valid: bool | None = None |
| 19 | + |
| 20 | + model_config = ConfigDict(populate_by_name=True, extra="allow") |
| 21 | + |
| 22 | + |
| 23 | +class CheckersClient: |
| 24 | + """Client for KRA PIN validation endpoints.""" |
| 25 | + |
| 26 | + def __init__(self, tr: AsyncTransport, auth: BearerAuthPolicy) -> None: |
| 27 | + """Initialize the checkers client. |
| 28 | +
|
| 29 | + Args: |
| 30 | + tr: Transport instance for HTTP requests. |
| 31 | + auth: Bearer authentication policy. |
| 32 | +
|
| 33 | + """ |
| 34 | + self._tr = tr |
| 35 | + self._auth = auth |
| 36 | + |
| 37 | + async def validate_pin(self, *, pin: str, pin_key: str = "PIN") -> PinCheckResult: |
| 38 | + """Validate a KRA PIN using POST with JSON payload. |
| 39 | +
|
| 40 | + Args: |
| 41 | + pin: The PIN to validate. |
| 42 | + pin_key: The JSON key name for the PIN field. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + PIN validation result. |
| 46 | +
|
| 47 | + """ |
| 48 | + payload = {pin_key: pin} |
| 49 | + return await self.validate_pin_raw(payload) |
| 50 | + |
| 51 | + async def validate_pin_get(self, *, pin: str, query_key: str = "PIN") -> PinCheckResult: |
| 52 | + """Validate a KRA PIN using GET with query parameters. |
| 53 | +
|
| 54 | + Args: |
| 55 | + pin: The PIN to validate. |
| 56 | + query_key: The query parameter name for the PIN field. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + PIN validation result. |
| 60 | +
|
| 61 | + """ |
| 62 | + resp = await self._tr.request( |
| 63 | + "GET", |
| 64 | + "/checker/v1/pinbypin", |
| 65 | + auth=self._auth, |
| 66 | + params={query_key: pin}, |
| 67 | + ) |
| 68 | + self._tr.raise_for_api_error(resp) |
| 69 | + return PinCheckResult.model_validate(resp.json()) |
| 70 | + |
| 71 | + async def validate_pin_raw(self, payload: dict[str, Any]) -> PinCheckResult: |
| 72 | + """Validate a PIN using raw payload. |
| 73 | +
|
| 74 | + Args: |
| 75 | + payload: Raw JSON payload to send. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + PIN validation result. |
| 79 | +
|
| 80 | + """ |
| 81 | + headers = idempotency_headers() # Make POST requests retryable |
| 82 | + resp = await self._tr.request( |
| 83 | + "POST", |
| 84 | + "/checker/v1/pinbypin", |
| 85 | + auth=self._auth, |
| 86 | + json=payload, |
| 87 | + headers=headers, |
| 88 | + ) |
| 89 | + self._tr.raise_for_api_error(resp) |
| 90 | + return PinCheckResult.model_validate(resp.json()) |
0 commit comments