# coding: utf-8

"""
    Klavis AI (https://www.klavis.ai)

    Klavis AI - Open Source MCP Integrations for AI Applications

    The version of the OpenAPI document: 0.1.0
    Contact: connect@klavis.ai
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501

import warnings
from pydantic import validate_call, Field, StrictFloat, StrictStr, StrictInt
from typing import Any, Dict, List, Optional, Tuple, Union
from typing_extensions import Annotated

from pydantic import Field, StrictStr
from typing import Any, Optional
from typing_extensions import Annotated
from klavis.models.github_oauth_success_response import GithubOauthSuccessResponse

from klavis.api_client import ApiClient, RequestSerialized
from klavis.api_response import ApiResponse
from klavis.rest import RESTResponseType


class GithubOauthApi:
    """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) -> None:
        if api_client is None:
            api_client = ApiClient.get_default()
        self.api_client = api_client


    @validate_call
    def authorize_github(
        self,
        instance_id: Annotated[StrictStr, Field(description="Unique identifier for the client instance requesting authorization")],
        client_id: Annotated[Optional[StrictStr], Field(description="Client ID for white labeling, if not provided will use default credentials")] = None,
        scope: Annotated[Optional[StrictStr], Field(description="Optional OAuth scopes to request (comma-separated string)")] = None,
        redirect_url: Annotated[Optional[StrictStr], Field(description="Optional URL to redirect to after authorization completes")] = None,
        _request_timeout: Union[
            None,
            Annotated[StrictFloat, Field(gt=0)],
            Tuple[
                Annotated[StrictFloat, Field(gt=0)],
                Annotated[StrictFloat, Field(gt=0)]
            ]
        ] = None,
        _request_auth: Optional[Dict[StrictStr, Any]] = None,
        _content_type: Optional[StrictStr] = None,
        _headers: Optional[Dict[StrictStr, Any]] = None,
        _host_index: Annotated[StrictInt, Field(ge=0, le=0)] = 0,
    ) -> object:
        """Authorize Github

        Start GitHub OAuth flow  Parameters: - instance_id: Identifier for the instance requesting authorization - client_id: Optional client ID for white labeling - scope: Optional scopes to request (comma-separated) - redirect_url: Optional URL to redirect to after authorization completes

        :param instance_id: Unique identifier for the client instance requesting authorization (required)
        :type instance_id: str
        :param client_id: Client ID for white labeling, if not provided will use default credentials
        :type client_id: str
        :param scope: Optional OAuth scopes to request (comma-separated string)
        :type scope: str
        :param redirect_url: Optional URL to redirect to after authorization completes
        :type redirect_url: str
        :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.
        :type _request_timeout: int, tuple(int, int), optional
        :param _request_auth: set to override the auth_settings for an a single
                              request; this effectively ignores the
                              authentication in the spec for a single request.
        :type _request_auth: dict, optional
        :param _content_type: force content-type for the request.
        :type _content_type: str, Optional
        :param _headers: set to override the headers for a single
                         request; this effectively ignores the headers
                         in the spec for a single request.
        :type _headers: dict, optional
        :param _host_index: set to override the host_index for a single
                            request; this effectively ignores the host_index
                            in the spec for a single request.
        :type _host_index: int, optional
        :return: Returns the result object.
        """ # noqa: E501

        _param = self._authorize_github_serialize(
            instance_id=instance_id,
            client_id=client_id,
            scope=scope,
            redirect_url=redirect_url,
            _request_auth=_request_auth,
            _content_type=_content_type,
            _headers=_headers,
            _host_index=_host_index
        )

        _response_types_map: Dict[str, Optional[str]] = {
            '200': "object",
            '422': "HTTPValidationError",
        }
        response_data = self.api_client.call_api(
            *_param,
            _request_timeout=_request_timeout
        )
        response_data.read()
        return self.api_client.response_deserialize(
            response_data=response_data,
            response_types_map=_response_types_map,
        ).data

    def _authorize_github_serialize(
        self,
        instance_id,
        client_id,
        scope,
        redirect_url,
        _request_auth,
        _content_type,
        _headers,
        _host_index,
    ) -> RequestSerialized:

        _host = None

        _collection_formats: Dict[str, str] = {
        }

        _path_params: Dict[str, str] = {}
        _query_params: List[Tuple[str, str]] = []
        _header_params: Dict[str, Optional[str]] = _headers or {}
        _form_params: List[Tuple[str, str]] = []
        _files: Dict[
            str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
        ] = {}
        _body_params: Optional[bytes] = None

        # process the path parameters
        # process the query parameters
        if instance_id is not None:
            
            _query_params.append(('instance_id', instance_id))
            
        if client_id is not None:
            
            _query_params.append(('client_id', client_id))
            
        if scope is not None:
            
            _query_params.append(('scope', scope))
            
        if redirect_url is not None:
            
            _query_params.append(('redirect_url', redirect_url))
            
        # process the header parameters
        # process the form parameters
        # process the body parameter


        # set the HTTP header `Accept`
        if 'Accept' not in _header_params:
            _header_params['Accept'] = self.api_client.select_header_accept(
                [
                    'application/json'
                ]
            )


        # authentication setting
        _auth_settings: List[str] = [
        ]

        return self.api_client.param_serialize(
            method='GET',
            resource_path='/oauth/github/authorize',
            path_params=_path_params,
            query_params=_query_params,
            header_params=_header_params,
            body=_body_params,
            post_params=_form_params,
            files=_files,
            auth_settings=_auth_settings,
            collection_formats=_collection_formats,
            _host=_host,
            _request_auth=_request_auth
        )




    @validate_call
    def github_o_auth_callback(
        self,
        code: Annotated[Optional[StrictStr], Field(description="Authorization code returned by GitHub")] = None,
        state: Annotated[Optional[StrictStr], Field(description="State parameter containing encoded authorization data")] = None,
        error: Annotated[Optional[StrictStr], Field(description="Error code returned by GitHub, if any")] = None,
        error_description: Annotated[Optional[StrictStr], Field(description="Detailed error description from GitHub, if any")] = None,
        _request_timeout: Union[
            None,
            Annotated[StrictFloat, Field(gt=0)],
            Tuple[
                Annotated[StrictFloat, Field(gt=0)],
                Annotated[StrictFloat, Field(gt=0)]
            ]
        ] = None,
        _request_auth: Optional[Dict[StrictStr, Any]] = None,
        _content_type: Optional[StrictStr] = None,
        _headers: Optional[Dict[StrictStr, Any]] = None,
        _host_index: Annotated[StrictInt, Field(ge=0, le=0)] = 0,
    ) -> GithubOauthSuccessResponse:
        """Github Callback

        Handles the callback from GitHub OAuth authorization.

        :param code: Authorization code returned by GitHub
        :type code: str
        :param state: State parameter containing encoded authorization data
        :type state: str
        :param error: Error code returned by GitHub, if any
        :type error: str
        :param error_description: Detailed error description from GitHub, if any
        :type error_description: str
        :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.
        :type _request_timeout: int, tuple(int, int), optional
        :param _request_auth: set to override the auth_settings for an a single
                              request; this effectively ignores the
                              authentication in the spec for a single request.
        :type _request_auth: dict, optional
        :param _content_type: force content-type for the request.
        :type _content_type: str, Optional
        :param _headers: set to override the headers for a single
                         request; this effectively ignores the headers
                         in the spec for a single request.
        :type _headers: dict, optional
        :param _host_index: set to override the host_index for a single
                            request; this effectively ignores the host_index
                            in the spec for a single request.
        :type _host_index: int, optional
        :return: Returns the result object.
        """ # noqa: E501

        _param = self._github_o_auth_callback_serialize(
            code=code,
            state=state,
            error=error,
            error_description=error_description,
            _request_auth=_request_auth,
            _content_type=_content_type,
            _headers=_headers,
            _host_index=_host_index
        )

        _response_types_map: Dict[str, Optional[str]] = {
            '200': "GithubOauthSuccessResponse",
            '400': "GithubOauthErrorResponse",
            '422': "HTTPValidationError",
        }
        response_data = self.api_client.call_api(
            *_param,
            _request_timeout=_request_timeout
        )
        response_data.read()
        return self.api_client.response_deserialize(
            response_data=response_data,
            response_types_map=_response_types_map,
        ).data

    def _github_o_auth_callback_serialize(
        self,
        code,
        state,
        error,
        error_description,
        _request_auth,
        _content_type,
        _headers,
        _host_index,
    ) -> RequestSerialized:

        _host = None

        _collection_formats: Dict[str, str] = {
        }

        _path_params: Dict[str, str] = {}
        _query_params: List[Tuple[str, str]] = []
        _header_params: Dict[str, Optional[str]] = _headers or {}
        _form_params: List[Tuple[str, str]] = []
        _files: Dict[
            str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
        ] = {}
        _body_params: Optional[bytes] = None

        # process the path parameters
        # process the query parameters
        if code is not None:
            
            _query_params.append(('code', code))
            
        if state is not None:
            
            _query_params.append(('state', state))
            
        if error is not None:
            
            _query_params.append(('error', error))
            
        if error_description is not None:
            
            _query_params.append(('error_description', error_description))
            
        # process the header parameters
        # process the form parameters
        # process the body parameter


        # set the HTTP header `Accept`
        if 'Accept' not in _header_params:
            _header_params['Accept'] = self.api_client.select_header_accept(
                [
                    'application/json'
                ]
            )


        # authentication setting
        _auth_settings: List[str] = [
        ]

        return self.api_client.param_serialize(
            method='GET',
            resource_path='/oauth/github/callback',
            path_params=_path_params,
            query_params=_query_params,
            header_params=_header_params,
            body=_body_params,
            post_params=_form_params,
            files=_files,
            auth_settings=_auth_settings,
            collection_formats=_collection_formats,
            _host=_host,
            _request_auth=_request_auth
        )


