Exemple #1
0
    async def get_sms_template(self, templateId):
        """
        Fetches a specific template by `id`
        Args:
            template_id {str}
        Returns:
            SmsTemplate
        """
        http_method = "get".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/templates/sms/{templateId}
            """)

        body = {}
        headers = {}

        request, error = await self._request_executor.create_request(
            http_method, api_url, body, headers)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, SmsTemplate)

        if error:
            return (None, response, error)

        try:
            result = SmsTemplate(self.form_response_body(response.get_body()))
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
    async def list_sms_templates(self,
                                 query_params={},
                                 keep_empty_params=False):
        """
        Enumerates custom SMS templates in your organization. A
        subset of templates can be returned that match a templ
        ate type.
        Args:
            query_params {dict}: Map of query parameters for request
            [query_params.templateType] {str}
        Returns:
            list: Collection of SmsTemplate instances.
        """
        http_method = "get".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/templates/sms
            """)
        if query_params:
            encoded_query_params = urlencode(query_params)
            api_url += f"/?{encoded_query_params}"

        body = {}
        headers = {}
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, SmsTemplate)

        if error:
            return (None, response, error)

        try:
            result = []
            for item in response.get_body():
                result.append(SmsTemplate(self.form_response_body(item)))
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
    async def update_sms_template(self,
                                  templateId,
                                  sms_template,
                                  keep_empty_params=False):
        """
        Updates the SMS template.
        Args:
            template_id {str}
            {sms_template}
        Returns:
            SmsTemplate
        """
        http_method = "put".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/templates/sms/{templateId}
            """)

        if isinstance(sms_template, dict):
            body = sms_template
        else:
            body = sms_template.as_dict()
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, SmsTemplate)

        if error:
            return (None, response, error)

        try:
            result = SmsTemplate(self.form_response_body(response.get_body()))
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
    async def create_sms_template(
            self, sms_template
    ):
        """
        Adds a new custom SMS template to your organization.
        Args:
            {sms_template}
        Returns:
            SmsTemplate
        """
        http_method = "post".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/templates/sms
            """)

        if isinstance(sms_template, dict):
            body = sms_template
        else:
            body = sms_template.as_dict()
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }

        request, error = await self._request_executor.create_request(
            http_method, api_url, body, headers
        )

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, SmsTemplate)

        if error:
            return (None, response, error)

        try:
            result = SmsTemplate(
                self.form_response_body(response.get_body())
            )
        except Exception as error:
            return (None, response, error)
        return (result, response, None)