# -*- coding: utf-8 -*-

"""
zypev1

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

from zypev1.api_helper import APIHelper
from zypev1.configuration import Server
from zypev1.controllers.base_controller import BaseController
from zypev1.models.o_auth import OAuth
from zypev1.models.o_auth_status import OAuthStatus


class OAuthController(BaseController):

    """A Controller to access Endpoints in the zypev1 API."""

    def __init__(self, config, call_back=None):
        super(OAuthController, self).__init__(config, call_back)

    def oauth_token_post_2(self,
                           client_id,
                           client_secret,
                           refresh_token,
                           grant_type,
                           body):
        """Does a POST request to /oauth/token.

        Refreshing Access Token

        Args:
            client_id (string): The client ID for your Zype application
            client_secret (string): The client secret for your Zype
                application
            refresh_token (string): Token to use to refresh
            grant_type (string): Must use `refresh_token`
            body (OAuthRefreshRequest): TODO: type description here.

        Returns:
            OAuth: Response from the API.

        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.

        """

        # Prepare query URL
        _url_path = '/oauth/token'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'client_id': client_id,
            'client_secret': client_secret,
            'refresh_token': refresh_token,
            'grant_type': grant_type
        }
        _query_builder = APIHelper.append_url_with_query_parameters(
            _query_builder,
            _query_parameters
        )
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'content-type': 'application/json; charset=utf-8'
        }

        # Prepare and execute request
        _request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, OAuth.from_dictionary)

        return decoded

    def oauth_token_status_post(self,
                                access_token):
        """Does a POST request to /oauth/token/status.

        Access Token Status

        Args:
            access_token (string): Access Token created via OAuth

        Returns:
            OAuthStatus: Response from the API.

        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.

        """

        # Prepare query URL
        _url_path = '/oauth/token/status'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'access_token': access_token
        }
        _query_builder = APIHelper.append_url_with_query_parameters(
            _query_builder,
            _query_parameters
        )
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json'
        }

        # Prepare and execute request
        _request = self.config.http_client.post(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, OAuthStatus.from_dictionary)

        return decoded
