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

"""
    pepipost.controllers.email_controller

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

from .base_controller import BaseController
from ..api_helper import APIHelper
from ..configuration import Configuration
from ..models.send_email_response import SendEmailResponse
from ..exceptions.api_exception import APIException

class EmailController(BaseController):

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


    def create_send_email(self,
                          api_key=None,
                          body=None):
        """Does a POST request to /v2/sendEmail.

        This Endpoint sends emails with the credentials passed.

        Args:
            api_key (string, optional): Generated header parameter. Example
                value ='5ce7096ed4bf2b39dfa932ff5fa84ed9ed8'
            body (EmailBody, optional): The body passed will be json format.

        Returns:
            SendEmailResponse: 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
        _query_builder = Configuration.base_uri
        _query_builder += '/v2/sendEmail'
        _query_url = APIHelper.clean_url(_query_builder)

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

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

        # Endpoint and global error handling using HTTP status codes.
        if _context.response.status_code == 405:
            raise APIException('Method not allowed', _context)
        self.validate_response(_context)

        # Return appropriate type
        return APIHelper.json_deserialize(_context.response.raw_body, SendEmailResponse.from_dictionary)
