# This file was auto-generated by Fern from our API Definition.

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..types.v_1_vault_schema_config import V1VaultSchemaConfig
from ..core.request_options import RequestOptions
from ..types.v_1_bin_list_response import V1BinListResponse
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.pydantic_utilities import parse_obj_as
from ..errors.not_found_error import NotFoundError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class BinLookupClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def list_cards(
        self,
        *,
        fields: typing.Optional[typing.Sequence[str]] = OMIT,
        bin: typing.Optional[str] = OMIT,
        vault_schema_config: typing.Optional[V1VaultSchemaConfig] = OMIT,
        skyflow_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> V1BinListResponse:
        """
        <b>Note</b>: This endpoint is in beta and subject to change. <br><br> Returns the specified card metadata.

        Parameters
        ----------
        fields : typing.Optional[typing.Sequence[str]]
            Fields to return. If not specified, all fields are returned.

        bin : typing.Optional[str]
            BIN of the card.

        vault_schema_config : typing.Optional[V1VaultSchemaConfig]

        skyflow_id : typing.Optional[str]
            <code>skyflow_id</code> of the record.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        V1BinListResponse
            A successful response.

        Examples
        --------
        from skyflow import Skyflow

        client = Skyflow(
            api_key="YOUR_API_KEY",
        )
        client.bin_lookup.list_cards(
            bin="012345",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/card_lookup",
            method="POST",
            json={
                "fields": fields,
                "BIN": bin,
                "vault_schema_config": convert_and_respect_annotation_metadata(
                    object_=vault_schema_config, annotation=V1VaultSchemaConfig, direction="write"
                ),
                "skyflow_id": skyflow_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    V1BinListResponse,
                    parse_obj_as(
                        type_=V1BinListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Dict[str, typing.Optional[typing.Any]],
                        parse_obj_as(
                            type_=typing.Dict[str, typing.Optional[typing.Any]],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncBinLookupClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list_cards(
        self,
        *,
        fields: typing.Optional[typing.Sequence[str]] = OMIT,
        bin: typing.Optional[str] = OMIT,
        vault_schema_config: typing.Optional[V1VaultSchemaConfig] = OMIT,
        skyflow_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> V1BinListResponse:
        """
        <b>Note</b>: This endpoint is in beta and subject to change. <br><br> Returns the specified card metadata.

        Parameters
        ----------
        fields : typing.Optional[typing.Sequence[str]]
            Fields to return. If not specified, all fields are returned.

        bin : typing.Optional[str]
            BIN of the card.

        vault_schema_config : typing.Optional[V1VaultSchemaConfig]

        skyflow_id : typing.Optional[str]
            <code>skyflow_id</code> of the record.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        V1BinListResponse
            A successful response.

        Examples
        --------
        import asyncio

        from skyflow import AsyncSkyflow

        client = AsyncSkyflow(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.bin_lookup.list_cards(
                bin="012345",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/card_lookup",
            method="POST",
            json={
                "fields": fields,
                "BIN": bin,
                "vault_schema_config": convert_and_respect_annotation_metadata(
                    object_=vault_schema_config, annotation=V1VaultSchemaConfig, direction="write"
                ),
                "skyflow_id": skyflow_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    V1BinListResponse,
                    parse_obj_as(
                        type_=V1BinListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Dict[str, typing.Optional[typing.Any]],
                        parse_obj_as(
                            type_=typing.Dict[str, typing.Optional[typing.Any]],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
