|
| 1 | +from typing import Any, List, Optional |
| 2 | + |
| 3 | +from typing_extensions import TypedDict |
| 4 | + |
| 5 | +from ..._internal.api_client import InternalApiClient, InternalApiClientSync |
| 6 | +from ...types import BlindpayApiResponse, Currency, PaginationMetadata, VoucherStatus, VoucherType |
| 7 | + |
| 8 | + |
| 9 | +class CreateVoucherInput(TypedDict): |
| 10 | + code: str |
| 11 | + type: VoucherType |
| 12 | + currency: Currency |
| 13 | + amount: float |
| 14 | + description: Optional[str] |
| 15 | + max_redemptions: Optional[int] |
| 16 | + expires_at: Optional[str] |
| 17 | + receiver_id: Optional[str] |
| 18 | + metadata: Optional[Any] |
| 19 | + |
| 20 | + |
| 21 | +class CreateVoucherResponse(TypedDict): |
| 22 | + id: str |
| 23 | + code: str |
| 24 | + type: VoucherType |
| 25 | + status: VoucherStatus |
| 26 | + currency: Currency |
| 27 | + amount: float |
| 28 | + |
| 29 | + |
| 30 | +class UpdateVoucherInput(TypedDict, total=False): |
| 31 | + description: Optional[str] |
| 32 | + status: Optional[VoucherStatus] |
| 33 | + max_redemptions: Optional[int] |
| 34 | + expires_at: Optional[str] |
| 35 | + metadata: Optional[Any] |
| 36 | + |
| 37 | + |
| 38 | +class Voucher(TypedDict): |
| 39 | + id: str |
| 40 | + code: str |
| 41 | + description: Optional[str] |
| 42 | + type: VoucherType |
| 43 | + status: VoucherStatus |
| 44 | + currency: Currency |
| 45 | + amount: float |
| 46 | + max_redemptions: Optional[int] |
| 47 | + current_redemptions: int |
| 48 | + expires_at: Optional[str] |
| 49 | + receiver_id: Optional[str] |
| 50 | + metadata: Optional[Any] |
| 51 | + created_at: str |
| 52 | + updated_at: str |
| 53 | + |
| 54 | + |
| 55 | +class ListVouchersResponse(TypedDict): |
| 56 | + data: List[Voucher] |
| 57 | + pagination: PaginationMetadata |
| 58 | + |
| 59 | + |
| 60 | +class DeleteVoucherResponse(TypedDict): |
| 61 | + success: bool |
| 62 | + |
| 63 | + |
| 64 | +class VouchersResource: |
| 65 | + def __init__(self, instance_id: str, client: InternalApiClient): |
| 66 | + self._instance_id = instance_id |
| 67 | + self._client = client |
| 68 | + |
| 69 | + async def create(self, data: CreateVoucherInput) -> BlindpayApiResponse[CreateVoucherResponse]: |
| 70 | + return await self._client.post(f"/instances/{self._instance_id}/vouchers", data) |
| 71 | + |
| 72 | + async def list(self) -> BlindpayApiResponse[ListVouchersResponse]: |
| 73 | + return await self._client.get(f"/instances/{self._instance_id}/vouchers") |
| 74 | + |
| 75 | + async def get(self, voucher_id: str) -> BlindpayApiResponse[Voucher]: |
| 76 | + return await self._client.get(f"/instances/{self._instance_id}/vouchers/{voucher_id}") |
| 77 | + |
| 78 | + async def update(self, voucher_id: str, data: UpdateVoucherInput) -> BlindpayApiResponse[Voucher]: |
| 79 | + return await self._client.patch(f"/instances/{self._instance_id}/vouchers/{voucher_id}", data) |
| 80 | + |
| 81 | + async def delete(self, voucher_id: str) -> BlindpayApiResponse[DeleteVoucherResponse]: |
| 82 | + return await self._client.delete(f"/instances/{self._instance_id}/vouchers/{voucher_id}") |
| 83 | + |
| 84 | + |
| 85 | +class VouchersResourceSync: |
| 86 | + def __init__(self, instance_id: str, client: InternalApiClientSync): |
| 87 | + self._instance_id = instance_id |
| 88 | + self._client = client |
| 89 | + |
| 90 | + def create(self, data: CreateVoucherInput) -> BlindpayApiResponse[CreateVoucherResponse]: |
| 91 | + return self._client.post(f"/instances/{self._instance_id}/vouchers", data) |
| 92 | + |
| 93 | + def list(self) -> BlindpayApiResponse[ListVouchersResponse]: |
| 94 | + return self._client.get(f"/instances/{self._instance_id}/vouchers") |
| 95 | + |
| 96 | + def get(self, voucher_id: str) -> BlindpayApiResponse[Voucher]: |
| 97 | + return self._client.get(f"/instances/{self._instance_id}/vouchers/{voucher_id}") |
| 98 | + |
| 99 | + def update(self, voucher_id: str, data: UpdateVoucherInput) -> BlindpayApiResponse[Voucher]: |
| 100 | + return self._client.patch(f"/instances/{self._instance_id}/vouchers/{voucher_id}", data) |
| 101 | + |
| 102 | + def delete(self, voucher_id: str) -> BlindpayApiResponse[DeleteVoucherResponse]: |
| 103 | + return self._client.delete(f"/instances/{self._instance_id}/vouchers/{voucher_id}") |
| 104 | + |
| 105 | + |
| 106 | +def create_vouchers_resource(instance_id: str, client: InternalApiClient) -> VouchersResource: |
| 107 | + return VouchersResource(instance_id, client) |
| 108 | + |
| 109 | + |
| 110 | +def create_vouchers_resource_sync(instance_id: str, client: InternalApiClientSync) -> VouchersResourceSync: |
| 111 | + return VouchersResourceSync(instance_id, client) |
0 commit comments