# 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.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.bad_request_error import BadRequestError
from ..errors.forbidden_error import ForbiddenError
from ..errors.internal_server_error import InternalServerError
from ..errors.too_many_requests_error import TooManyRequestsError
from .types.create_free_key_developers_response import CreateFreeKeyDevelopersResponse
from .types.verify_free_key_developers_response import VerifyFreeKeyDevelopersResponse

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


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

    def create_free_key(
        self, *, email: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CreateFreeKeyDevelopersResponse]:
        """
        Step 1 of 2: Request a verification code to get a free API key.

        - Sends a 6-digit code to your email (expires in 30 minutes)
        - Max 5 codes per email per hour (anti-spam)
        - If email already has a verified key, returns it immediately
        - Use POST /api/developers/keys/free/verify with the code to receive your key

        Parameters
        ----------
        email : str
            Your email address

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

        Returns
        -------
        HttpResponse[CreateFreeKeyDevelopersResponse]
            Verification code sent or existing key returned
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/developers/keys/free",
            method="POST",
            json={
                "email": email,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateFreeKeyDevelopersResponse,
                    parse_obj_as(
                        type_=CreateFreeKeyDevelopersResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 verify_free_key(
        self, *, email: str, code: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[VerifyFreeKeyDevelopersResponse]:
        """
        Step 2 of 2: Verify your email with the 6-digit code and receive your API key.

        - Code expires after 30 minutes
        - Max 5 wrong attempts per code (then must request new code)
        - On success: issues sfx_free_ key (persistent, 1 per email)
        - Free tier: 20 requests/minute, 60 requests/day

        Parameters
        ----------
        email : str
            The email you used in step 1

        code : str
            6-digit verification code from email

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

        Returns
        -------
        HttpResponse[VerifyFreeKeyDevelopersResponse]
            Email verified, API key issued
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/developers/keys/free/verify",
            method="POST",
            json={
                "email": email,
                "code": code,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    VerifyFreeKeyDevelopersResponse,
                    parse_obj_as(
                        type_=VerifyFreeKeyDevelopersResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 AsyncRawDevelopersClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_free_key(
        self, *, email: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CreateFreeKeyDevelopersResponse]:
        """
        Step 1 of 2: Request a verification code to get a free API key.

        - Sends a 6-digit code to your email (expires in 30 minutes)
        - Max 5 codes per email per hour (anti-spam)
        - If email already has a verified key, returns it immediately
        - Use POST /api/developers/keys/free/verify with the code to receive your key

        Parameters
        ----------
        email : str
            Your email address

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

        Returns
        -------
        AsyncHttpResponse[CreateFreeKeyDevelopersResponse]
            Verification code sent or existing key returned
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/developers/keys/free",
            method="POST",
            json={
                "email": email,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateFreeKeyDevelopersResponse,
                    parse_obj_as(
                        type_=CreateFreeKeyDevelopersResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 verify_free_key(
        self, *, email: str, code: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[VerifyFreeKeyDevelopersResponse]:
        """
        Step 2 of 2: Verify your email with the 6-digit code and receive your API key.

        - Code expires after 30 minutes
        - Max 5 wrong attempts per code (then must request new code)
        - On success: issues sfx_free_ key (persistent, 1 per email)
        - Free tier: 20 requests/minute, 60 requests/day

        Parameters
        ----------
        email : str
            The email you used in step 1

        code : str
            6-digit verification code from email

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

        Returns
        -------
        AsyncHttpResponse[VerifyFreeKeyDevelopersResponse]
            Email verified, API key issued
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/developers/keys/free/verify",
            method="POST",
            json={
                "email": email,
                "code": code,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    VerifyFreeKeyDevelopersResponse,
                    parse_obj_as(
                        type_=VerifyFreeKeyDevelopersResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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)
