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

"""
pqapiv2

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

from pqapiv2.api_helper import APIHelper
from pqapiv2.configuration import Server
from pqapiv2.controllers.base_controller import BaseController
from pqapiv2.models.webhook_collection_response import WebhookCollectionResponse
from pqapiv2.models.webhook_subscription_response import WebhookSubscriptionResponse


class WebhooksController(BaseController):

    """A Controller to access Endpoints in the pqapiv2 API."""
    def __init__(self, config):
        super(WebhooksController, self).__init__(config)

    def list_webhook_subscriptions(self,
                                   x_my_pay_quicker_version):
        """Does a GET request to /webhooks.

        Retrieve a list of all webhook subscriptions that supports filtering,
        sorting, and pagination through existing mechanisms.

        Args:
            x_my_pay_quicker_version (string): Date-based API Version
                specified in the header <i>required</i> on all calls.

        Returns:
            WebhookCollectionResponse: Response from the API. 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.

        """

        # Prepare query URL
        _url_path = '/webhooks'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'X-MyPayQuicker-Version': x_my_pay_quicker_version
        }

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

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

        return decoded

    def create_webhook_subscription(self,
                                    x_my_pay_quicker_version,
                                    body=None):
        """Does a POST request to /webhooks.

        Create a webhook subscription for a given URL and namespace. When this
        event fires, the webhook receives a call from the API.

        Args:
            x_my_pay_quicker_version (string): Date-based API Version
                specified in the header <i>required</i> on all calls.
            body (WebhookSubscription, optional): TODO: type description
                here.

        Returns:
            WebhookSubscriptionResponse: Response from the API. 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.

        """

        # Prepare query URL
        _url_path = '/webhooks'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'X-MyPayQuicker-Version': x_my_pay_quicker_version,
            'Content-Type': 'application/json'
        }

        # 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, WebhookSubscriptionResponse.from_dictionary)

        return decoded

    def retrieve_webhook_subscription(self,
                                      webh_token,
                                      x_my_pay_quicker_version):
        """Does a GET request to /webhooks/{webh-token}.

        Retrieve a single webhook subscription using the webhook token.

        Args:
            webh_token (string): TODO: type description here.
            x_my_pay_quicker_version (string): Date-based API Version
                specified in the header <i>required</i> on all calls.

        Returns:
            WebhookSubscriptionResponse: Response from the API. 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.

        """

        # Prepare query URL
        _url_path = '/webhooks/{webh-token}'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'webh-token': {'value': webh_token, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'X-MyPayQuicker-Version': x_my_pay_quicker_version
        }

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

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

        return decoded

    def delete_webhook_subscription(self,
                                    webh_token,
                                    x_my_pay_quicker_version):
        """Does a DELETE request to /webhooks/{webh-token}.

        Delete a webhook subscription. Deleted webhooks no longer receive
        notifications about events. Deleting an already deleted webhook will
        result in a successful 200 (OK) response code.

        Args:
            webh_token (string): TODO: type description here.
            x_my_pay_quicker_version (string): Date-based API Version
                specified in the header <i>required</i> on all calls.

        Returns:
            void: Response from the API. 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.

        """

        # Prepare query URL
        _url_path = '/webhooks/{webh-token}'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'webh-token': {'value': webh_token, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'X-MyPayQuicker-Version': x_my_pay_quicker_version
        }

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