"""cellpointapi.

This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
"""

# ruff: noqa: D410, E501, E101, D206
from apimatic_core.authentication.multiple.single_auth import (
    Single,
)
from apimatic_core.request_builder import RequestBuilder
from apimatic_core.response_handler import ResponseHandler
from apimatic_core.types.parameter import Parameter

from cellpointapi.api_helper import APIHelper
from cellpointapi.apis.base_api import BaseApi
from cellpointapi.configuration import Server
from cellpointapi.exceptions.api_exception import (
    ApiException,
)
from cellpointapi.exceptions.problem_exception import (
    ProblemException,
)
from cellpointapi.http.http_method_enum import (
    HttpMethodEnum,
)
from cellpointapi.models.token_post_200_response import (
    TokenPost200Response,
)


class AuthenticationApi(BaseApi):
    """A Controller to access Endpoints in the cellpointapi API."""

    def __init__(self, config):
        """Initialize AuthenticationApi object."""
        super(AuthenticationApi, self).__init__(config)

    def get_access_token(self,
                         grant_type=None,
                         client_assertion_type=None,
                         client_id=None,
                         audience=None,
                         scope=None,
                         client_assertion=None):
        """Perform a POST request to /oauth2/token.

        Retrieve an Access Token for desired scope using Private Key JWT Flow.
        Prerequisites for Private Key JWT Flow:
        1. CellPoint must be configured to use merchant's `jwks` (JSON Web Key Set)
        endpoint to validate Private JWT.
        ***Flow Diagram***
        ![diagram_001](media/diagram_001.png)
        For more details, see Authorizations for this endpoint below:

        Args:
            grant_type (str, optional): The type of grant requested. This must be
                `client_credentials`.
            client_assertion_type (str, optional): The type of client assertion. This
                must be `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.
            client_id (str, optional): The client ID, as provided by Cellpoint.
            audience (Audience, optional): The intended audience for the client
                assertion.
            scope (str, optional): The requested permissions for the client. A space
                separated list of permissions.
            client_assertion (str, optional): A JWT signed by the caller. The JWT
                must contain the following claims: - `iss`: The client ID, as
                provided by Cellpoint. - `sub`: The client ID, as provided by
                Cellpoint. - `aud`: The appropriate auth server, example:
                https://auth.api.test.cellpoint.app/

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. OK

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/oauth2/token")
            .http_method(HttpMethodEnum.POST)
            .form_param(Parameter()
                .key("grant_type")
                .value(grant_type))
            .form_param(Parameter()
                .key("client_assertion_type")
                .value(client_assertion_type))
            .form_param(Parameter()
                .key("client_id")
                .value(client_id))
            .form_param(Parameter()
                .key("audience")
                .value(audience))
            .form_param(Parameter()
                .key("scope")
                .value(scope))
            .form_param(Parameter()
                .key("client_assertion")
                .value(client_assertion))
            .header_param(Parameter()
                            .key("content-type")
                            .value("application/x-www-form-urlencoded"))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .auth(Single("merchantIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(TokenPost200Response.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("404", "Resource Not Found", ApiException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()
