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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pagination import AsyncPager, BaseHttpResponse, SyncPager
from ..core.request_options import RequestOptions
from ..core.unchecked_base_model import construct_type
from ..types.bank_account import BankAccount
from ..types.get_bank_account_by_v1id_response import GetBankAccountByV1IdResponse
from ..types.get_bank_account_response import GetBankAccountResponse
from ..types.list_bank_accounts_response import ListBankAccountsResponse


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

    def list(
        self,
        *,
        cursor: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        location_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[BankAccount]:
        """
        Returns a list of [BankAccount](entity:BankAccount) objects linked to a Square account.

        Parameters
        ----------
        cursor : typing.Optional[str]
            The pagination cursor returned by a previous call to this endpoint.
            Use it in the next `ListBankAccounts` request to retrieve the next set
            of results.

            See the [Pagination](https://developer.squareup.com/docs/working-with-apis/pagination) guide for more information.

        limit : typing.Optional[int]
            Upper limit on the number of bank accounts to return in the response.
            Currently, 1000 is the largest supported limit. You can specify a limit
            of up to 1000 bank accounts. This is also the default limit.

        location_id : typing.Optional[str]
            Location ID. You can specify this optional filter
            to retrieve only the linked bank accounts belonging to a specific location.

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

        Returns
        -------
        SyncPager[BankAccount]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/bank-accounts",
            method="GET",
            params={
                "cursor": cursor,
                "limit": limit,
                "location_id": location_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListBankAccountsResponse,
                    construct_type(
                        type_=ListBankAccountsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.bank_accounts
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    cursor=_parsed_next,
                    limit=limit,
                    location_id=location_id,
                    request_options=request_options,
                )
                return SyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_by_v1id(
        self, v1bank_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetBankAccountByV1IdResponse]:
        """
        Returns details of a [BankAccount](entity:BankAccount) identified by V1 bank account ID.

        Parameters
        ----------
        v1bank_account_id : str
            Connect V1 ID of the desired `BankAccount`. For more information, see
            [Retrieve a bank account by using an ID issued by V1 Bank Accounts API](https://developer.squareup.com/docs/bank-accounts-api#retrieve-a-bank-account-by-using-an-id-issued-by-v1-bank-accounts-api).

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

        Returns
        -------
        HttpResponse[GetBankAccountByV1IdResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/bank-accounts/by-v1-id/{jsonable_encoder(v1bank_account_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBankAccountByV1IdResponse,
                    construct_type(
                        type_=GetBankAccountByV1IdResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get(
        self, bank_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetBankAccountResponse]:
        """
        Returns details of a [BankAccount](entity:BankAccount)
        linked to a Square account.

        Parameters
        ----------
        bank_account_id : str
            Square-issued ID of the desired `BankAccount`.

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

        Returns
        -------
        HttpResponse[GetBankAccountResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/bank-accounts/{jsonable_encoder(bank_account_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBankAccountResponse,
                    construct_type(
                        type_=GetBankAccountResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def list(
        self,
        *,
        cursor: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        location_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[BankAccount]:
        """
        Returns a list of [BankAccount](entity:BankAccount) objects linked to a Square account.

        Parameters
        ----------
        cursor : typing.Optional[str]
            The pagination cursor returned by a previous call to this endpoint.
            Use it in the next `ListBankAccounts` request to retrieve the next set
            of results.

            See the [Pagination](https://developer.squareup.com/docs/working-with-apis/pagination) guide for more information.

        limit : typing.Optional[int]
            Upper limit on the number of bank accounts to return in the response.
            Currently, 1000 is the largest supported limit. You can specify a limit
            of up to 1000 bank accounts. This is also the default limit.

        location_id : typing.Optional[str]
            Location ID. You can specify this optional filter
            to retrieve only the linked bank accounts belonging to a specific location.

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

        Returns
        -------
        AsyncPager[BankAccount]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/bank-accounts",
            method="GET",
            params={
                "cursor": cursor,
                "limit": limit,
                "location_id": location_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListBankAccountsResponse,
                    construct_type(
                        type_=ListBankAccountsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.bank_accounts
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""

                async def _get_next():
                    return await self.list(
                        cursor=_parsed_next,
                        limit=limit,
                        location_id=location_id,
                        request_options=request_options,
                    )

                return AsyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_by_v1id(
        self, v1bank_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetBankAccountByV1IdResponse]:
        """
        Returns details of a [BankAccount](entity:BankAccount) identified by V1 bank account ID.

        Parameters
        ----------
        v1bank_account_id : str
            Connect V1 ID of the desired `BankAccount`. For more information, see
            [Retrieve a bank account by using an ID issued by V1 Bank Accounts API](https://developer.squareup.com/docs/bank-accounts-api#retrieve-a-bank-account-by-using-an-id-issued-by-v1-bank-accounts-api).

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

        Returns
        -------
        AsyncHttpResponse[GetBankAccountByV1IdResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/bank-accounts/by-v1-id/{jsonable_encoder(v1bank_account_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBankAccountByV1IdResponse,
                    construct_type(
                        type_=GetBankAccountByV1IdResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get(
        self, bank_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetBankAccountResponse]:
        """
        Returns details of a [BankAccount](entity:BankAccount)
        linked to a Square account.

        Parameters
        ----------
        bank_account_id : str
            Square-issued ID of the desired `BankAccount`.

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

        Returns
        -------
        AsyncHttpResponse[GetBankAccountResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/bank-accounts/{jsonable_encoder(bank_account_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBankAccountResponse,
                    construct_type(
                        type_=GetBankAccountResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
