from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_access_token_response import GetAccessTokenResponse
from ...types import Response


def _get_kwargs(
    *,
    authorization: str,
    accept: str,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    headers["Authorization"] = authorization

    headers["Accept"] = accept

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/dpi/v1/auth/accesstoken",
    }

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> GetAccessTokenResponse | None:
    if response.status_code == 200:
        response_200 = GetAccessTokenResponse.from_dict(response.json())

        return response_200

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[GetAccessTokenResponse]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    *,
    client: AuthenticatedClient | Client,
    authorization: str,
    accept: str,
) -> Response[GetAccessTokenResponse]:
    r"""Get Access New Token


    The Get access_token API call provides OAuth 2.0 Bearer token, assigned to your Consumer_key and
    Consumer_secret, which will grants you an access to the Deutsche Post International Shipping
    APIs.<br/><br/> If you already received credentials for the old gateway these remain valid until
    further notice.<br/><br/> The API operation is secured by HTTP Basic authentication, therefore you
    have to provide `Consumer_key` and `Consumer_secret` as an username and password, when calling the
    API..<br/><br/> **Example:**
    1. You will receive `Consumer_key` and `Consumer_secret` from Deutsche Post International
    representative in following format.<br/>
      Credentials below are exemplary and can _NOT_ be used in this Sandbox environment to get the
    access_token.

        ````
        Consumer_key: 5qsFCFLzeoz4C6PKJ7yH3NDQHgBEJLt7
        Consumer_secret: P6mEGGaAZ2TdkLpD
        ````

    2. When passing the `Consumer_key` and `Consumer_secret` via HTTPS request, using HTTP Basic
    authentication, you have to
        populate HTTP Header Authorization in following format.
        > **Note:** *`Consumer_key` and `Consumer_secret` has to be encoded in base64-encoding. There is
    a `space` characeter between Basic and the base64-encoded string. There is a `:` character between
    the Consumer_key and Consumer_secret, when encoded in base64-encoding.*

        ````
        Authorization: Basic base64-encoded(Consumer_key:Consumer_secret)
        Authorization: Basic NXFzRkNGTHplb3o0QzZQS0o3eUgzTkRRSGdCRUpMdDc6UDZtRUdHYUFaMlRka0xwRA==
        ````

        > **Note:** *You can test the Get access_token API directly from the documentation page. The
    Authorization HTTP Header was populated for you in the Console View on the right side of the
    screen.*

    3. You will receive following response in the JSON format.

        ````
          {
                \"access_token\": \"8CyAkmSppbfG5KAQ4AinTZ8RVJnD\",
                \"token_type\": \"Bearer\",
                \"expires_in\": 17999
           }
        ````

    Understanding of the response fields:
    `access_token:` Contains access_token string for the Shipping / Tracking API authentication and
    authorization.<br/><br/>
    `token_type:` Type of the OAuth 2.0 access_token. Default value is \"Bearer\".<br/><br/>
    `expires_in:` Time to live of the access_token. Default value is 18000 seconds / 5 hours. After this
    time the token expires. <br/><br/>

    Args:
        authorization (str):
        accept (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetAccessTokenResponse]
    """

    kwargs = _get_kwargs(
        authorization=authorization,
        accept=accept,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    authorization: str,
    accept: str,
) -> GetAccessTokenResponse | None:
    r"""Get Access New Token


    The Get access_token API call provides OAuth 2.0 Bearer token, assigned to your Consumer_key and
    Consumer_secret, which will grants you an access to the Deutsche Post International Shipping
    APIs.<br/><br/> If you already received credentials for the old gateway these remain valid until
    further notice.<br/><br/> The API operation is secured by HTTP Basic authentication, therefore you
    have to provide `Consumer_key` and `Consumer_secret` as an username and password, when calling the
    API..<br/><br/> **Example:**
    1. You will receive `Consumer_key` and `Consumer_secret` from Deutsche Post International
    representative in following format.<br/>
      Credentials below are exemplary and can _NOT_ be used in this Sandbox environment to get the
    access_token.

        ````
        Consumer_key: 5qsFCFLzeoz4C6PKJ7yH3NDQHgBEJLt7
        Consumer_secret: P6mEGGaAZ2TdkLpD
        ````

    2. When passing the `Consumer_key` and `Consumer_secret` via HTTPS request, using HTTP Basic
    authentication, you have to
        populate HTTP Header Authorization in following format.
        > **Note:** *`Consumer_key` and `Consumer_secret` has to be encoded in base64-encoding. There is
    a `space` characeter between Basic and the base64-encoded string. There is a `:` character between
    the Consumer_key and Consumer_secret, when encoded in base64-encoding.*

        ````
        Authorization: Basic base64-encoded(Consumer_key:Consumer_secret)
        Authorization: Basic NXFzRkNGTHplb3o0QzZQS0o3eUgzTkRRSGdCRUpMdDc6UDZtRUdHYUFaMlRka0xwRA==
        ````

        > **Note:** *You can test the Get access_token API directly from the documentation page. The
    Authorization HTTP Header was populated for you in the Console View on the right side of the
    screen.*

    3. You will receive following response in the JSON format.

        ````
          {
                \"access_token\": \"8CyAkmSppbfG5KAQ4AinTZ8RVJnD\",
                \"token_type\": \"Bearer\",
                \"expires_in\": 17999
           }
        ````

    Understanding of the response fields:
    `access_token:` Contains access_token string for the Shipping / Tracking API authentication and
    authorization.<br/><br/>
    `token_type:` Type of the OAuth 2.0 access_token. Default value is \"Bearer\".<br/><br/>
    `expires_in:` Time to live of the access_token. Default value is 18000 seconds / 5 hours. After this
    time the token expires. <br/><br/>

    Args:
        authorization (str):
        accept (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetAccessTokenResponse
    """

    return sync_detailed(
        client=client,
        authorization=authorization,
        accept=accept,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    authorization: str,
    accept: str,
) -> Response[GetAccessTokenResponse]:
    r"""Get Access New Token


    The Get access_token API call provides OAuth 2.0 Bearer token, assigned to your Consumer_key and
    Consumer_secret, which will grants you an access to the Deutsche Post International Shipping
    APIs.<br/><br/> If you already received credentials for the old gateway these remain valid until
    further notice.<br/><br/> The API operation is secured by HTTP Basic authentication, therefore you
    have to provide `Consumer_key` and `Consumer_secret` as an username and password, when calling the
    API..<br/><br/> **Example:**
    1. You will receive `Consumer_key` and `Consumer_secret` from Deutsche Post International
    representative in following format.<br/>
      Credentials below are exemplary and can _NOT_ be used in this Sandbox environment to get the
    access_token.

        ````
        Consumer_key: 5qsFCFLzeoz4C6PKJ7yH3NDQHgBEJLt7
        Consumer_secret: P6mEGGaAZ2TdkLpD
        ````

    2. When passing the `Consumer_key` and `Consumer_secret` via HTTPS request, using HTTP Basic
    authentication, you have to
        populate HTTP Header Authorization in following format.
        > **Note:** *`Consumer_key` and `Consumer_secret` has to be encoded in base64-encoding. There is
    a `space` characeter between Basic and the base64-encoded string. There is a `:` character between
    the Consumer_key and Consumer_secret, when encoded in base64-encoding.*

        ````
        Authorization: Basic base64-encoded(Consumer_key:Consumer_secret)
        Authorization: Basic NXFzRkNGTHplb3o0QzZQS0o3eUgzTkRRSGdCRUpMdDc6UDZtRUdHYUFaMlRka0xwRA==
        ````

        > **Note:** *You can test the Get access_token API directly from the documentation page. The
    Authorization HTTP Header was populated for you in the Console View on the right side of the
    screen.*

    3. You will receive following response in the JSON format.

        ````
          {
                \"access_token\": \"8CyAkmSppbfG5KAQ4AinTZ8RVJnD\",
                \"token_type\": \"Bearer\",
                \"expires_in\": 17999
           }
        ````

    Understanding of the response fields:
    `access_token:` Contains access_token string for the Shipping / Tracking API authentication and
    authorization.<br/><br/>
    `token_type:` Type of the OAuth 2.0 access_token. Default value is \"Bearer\".<br/><br/>
    `expires_in:` Time to live of the access_token. Default value is 18000 seconds / 5 hours. After this
    time the token expires. <br/><br/>

    Args:
        authorization (str):
        accept (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetAccessTokenResponse]
    """

    kwargs = _get_kwargs(
        authorization=authorization,
        accept=accept,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    authorization: str,
    accept: str,
) -> GetAccessTokenResponse | None:
    r"""Get Access New Token


    The Get access_token API call provides OAuth 2.0 Bearer token, assigned to your Consumer_key and
    Consumer_secret, which will grants you an access to the Deutsche Post International Shipping
    APIs.<br/><br/> If you already received credentials for the old gateway these remain valid until
    further notice.<br/><br/> The API operation is secured by HTTP Basic authentication, therefore you
    have to provide `Consumer_key` and `Consumer_secret` as an username and password, when calling the
    API..<br/><br/> **Example:**
    1. You will receive `Consumer_key` and `Consumer_secret` from Deutsche Post International
    representative in following format.<br/>
      Credentials below are exemplary and can _NOT_ be used in this Sandbox environment to get the
    access_token.

        ````
        Consumer_key: 5qsFCFLzeoz4C6PKJ7yH3NDQHgBEJLt7
        Consumer_secret: P6mEGGaAZ2TdkLpD
        ````

    2. When passing the `Consumer_key` and `Consumer_secret` via HTTPS request, using HTTP Basic
    authentication, you have to
        populate HTTP Header Authorization in following format.
        > **Note:** *`Consumer_key` and `Consumer_secret` has to be encoded in base64-encoding. There is
    a `space` characeter between Basic and the base64-encoded string. There is a `:` character between
    the Consumer_key and Consumer_secret, when encoded in base64-encoding.*

        ````
        Authorization: Basic base64-encoded(Consumer_key:Consumer_secret)
        Authorization: Basic NXFzRkNGTHplb3o0QzZQS0o3eUgzTkRRSGdCRUpMdDc6UDZtRUdHYUFaMlRka0xwRA==
        ````

        > **Note:** *You can test the Get access_token API directly from the documentation page. The
    Authorization HTTP Header was populated for you in the Console View on the right side of the
    screen.*

    3. You will receive following response in the JSON format.

        ````
          {
                \"access_token\": \"8CyAkmSppbfG5KAQ4AinTZ8RVJnD\",
                \"token_type\": \"Bearer\",
                \"expires_in\": 17999
           }
        ````

    Understanding of the response fields:
    `access_token:` Contains access_token string for the Shipping / Tracking API authentication and
    authorization.<br/><br/>
    `token_type:` Type of the OAuth 2.0 access_token. Default value is \"Bearer\".<br/><br/>
    `expires_in:` Time to live of the access_token. Default value is 18000 seconds / 5 hours. After this
    time the token expires. <br/><br/>

    Args:
        authorization (str):
        accept (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetAccessTokenResponse
    """

    return (
        await asyncio_detailed(
            client=client,
            authorization=authorization,
            accept=accept,
        )
    ).parsed
