# coding: utf-8

"""
    TrueLayer Authtentication

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)  # noqa: E501

    The version of the OpenAPI document: v1.0
    Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

import re  # noqa: F401

# python 2 and python 3 compatibility library
import six

from truelayer.auth.api_client import ApiClient
from truelayer.auth.exceptions import (
    ApiTypeError,
    ApiValueError
)


class DefaultApi(object):
    """NOTE: This class is auto generated by OpenAPI Generator
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """

    def __init__(self, api_client=None):
        if api_client is None:
            api_client = ApiClient()
        self.api_client = api_client

    def exchange_code(self, client_id, client_secret, redirect_uri, code, grant_type, **kwargs):  # noqa: E501
        """exchange_code  # noqa: E501

        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.exchange_code(client_id, client_secret, redirect_uri, code, grant_type, async_req=True)
        >>> result = thread.get()

        :param async_req bool: execute request asynchronously
        :param str client_id: (required)
        :param str client_secret: (required)
        :param str redirect_uri: (required)
        :param str code: (required)
        :param str grant_type: (required)
        :param str code_verifier:
        :param _preload_content: if False, the urllib3.HTTPResponse object will
                                 be returned without reading/decoding response
                                 data. Default is True.
        :param _request_timeout: timeout setting for this request. If one
                                 number provided, it will be total request
                                 timeout. It can also be a pair (tuple) of
                                 (connection, read) timeouts.
        :return: TokenResponse
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        return self.exchange_code_with_http_info(client_id, client_secret, redirect_uri, code, grant_type, **kwargs)  # noqa: E501

    def exchange_code_with_http_info(self, client_id, client_secret, redirect_uri, code, grant_type, **kwargs):  # noqa: E501
        """exchange_code  # noqa: E501

        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please pass async_req=True
        >>> thread = api.exchange_code_with_http_info(client_id, client_secret, redirect_uri, code, grant_type, async_req=True)
        >>> result = thread.get()

        :param async_req bool: execute request asynchronously
        :param str client_id: (required)
        :param str client_secret: (required)
        :param str redirect_uri: (required)
        :param str code: (required)
        :param str grant_type: (required)
        :param str code_verifier:
        :param _return_http_data_only: response data without head status code
                                       and headers
        :param _preload_content: if False, the urllib3.HTTPResponse object will
                                 be returned without reading/decoding response
                                 data. Default is True.
        :param _request_timeout: timeout setting for this request. If one
                                 number provided, it will be total request
                                 timeout. It can also be a pair (tuple) of
                                 (connection, read) timeouts.
        :return: tuple(TokenResponse, status_code(int), headers(HTTPHeaderDict))
                 If the method is called asynchronously,
                 returns the request thread.
        """

        local_var_params = locals()

        all_params = ['client_id', 'client_secret', 'redirect_uri', 'code', 'grant_type', 'code_verifier']  # noqa: E501
        all_params.append('async_req')
        all_params.append('_return_http_data_only')
        all_params.append('_preload_content')
        all_params.append('_request_timeout')

        for key, val in six.iteritems(local_var_params['kwargs']):
            if key not in all_params:
                raise ApiTypeError(
                    "Got an unexpected keyword argument '%s'"
                    " to method exchange_code" % key
                )
            local_var_params[key] = val
        del local_var_params['kwargs']
        # verify the required parameter 'client_id' is set
        if self.api_client.client_side_validation and ('client_id' not in local_var_params or  # noqa: E501
                                                        local_var_params['client_id'] is None):  # noqa: E501
            raise ApiValueError("Missing the required parameter `client_id` when calling `exchange_code`")  # noqa: E501
        # verify the required parameter 'client_secret' is set
        if self.api_client.client_side_validation and ('client_secret' not in local_var_params or  # noqa: E501
                                                        local_var_params['client_secret'] is None):  # noqa: E501
            raise ApiValueError("Missing the required parameter `client_secret` when calling `exchange_code`")  # noqa: E501
        # verify the required parameter 'redirect_uri' is set
        if self.api_client.client_side_validation and ('redirect_uri' not in local_var_params or  # noqa: E501
                                                        local_var_params['redirect_uri'] is None):  # noqa: E501
            raise ApiValueError("Missing the required parameter `redirect_uri` when calling `exchange_code`")  # noqa: E501
        # verify the required parameter 'code' is set
        if self.api_client.client_side_validation and ('code' not in local_var_params or  # noqa: E501
                                                        local_var_params['code'] is None):  # noqa: E501
            raise ApiValueError("Missing the required parameter `code` when calling `exchange_code`")  # noqa: E501
        # verify the required parameter 'grant_type' is set
        if self.api_client.client_side_validation and ('grant_type' not in local_var_params or  # noqa: E501
                                                        local_var_params['grant_type'] is None):  # noqa: E501
            raise ApiValueError("Missing the required parameter `grant_type` when calling `exchange_code`")  # noqa: E501

        collection_formats = {}

        path_params = {}

        query_params = []

        header_params = {}

        form_params = []
        local_var_files = {}
        if 'client_id' in local_var_params:
            form_params.append(('client_id', local_var_params['client_id']))  # noqa: E501
        if 'client_secret' in local_var_params:
            form_params.append(('client_secret', local_var_params['client_secret']))  # noqa: E501
        if 'redirect_uri' in local_var_params:
            form_params.append(('redirect_uri', local_var_params['redirect_uri']))  # noqa: E501
        if 'code' in local_var_params:
            form_params.append(('code', local_var_params['code']))  # noqa: E501
        if 'grant_type' in local_var_params:
            form_params.append(('grant_type', local_var_params['grant_type']))  # noqa: E501
        if 'code_verifier' in local_var_params:
            form_params.append(('code_verifier', local_var_params['code_verifier']))  # noqa: E501

        body_params = None
        # HTTP header `Accept`
        header_params['Accept'] = self.api_client.select_header_accept(
            ['application/json'])  # noqa: E501

        # HTTP header `Content-Type`
        header_params['Content-Type'] = self.api_client.select_header_content_type(  # noqa: E501
            ['application/x-www-form-urlencoded'])  # noqa: E501

        # Authentication setting
        auth_settings = []  # noqa: E501

        return self.api_client.call_api(
            '/connect/token', 'POST',
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type='TokenResponse',  # noqa: E501
            auth_settings=auth_settings,
            async_req=local_var_params.get('async_req'),
            _return_http_data_only=local_var_params.get('_return_http_data_only'),  # noqa: E501
            _preload_content=local_var_params.get('_preload_content', True),
            _request_timeout=local_var_params.get('_request_timeout'),
            collection_formats=collection_formats)
