"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from .basesdk import BaseSDK
from documenso_sdk import models, utils
from documenso_sdk._hooks import HookContext
from documenso_sdk.types import OptionalNullable, UNSET
from documenso_sdk.utils import get_security_from_env
from documenso_sdk.utils.unmarshal_json_response import unmarshal_json_response
from typing import Any, List, Mapping, Optional, Union


class TemplateSDK(BaseSDK):
    def template_create_template_temporary(
        self,
        *,
        title: str,
        folder_id: Optional[str] = None,
        external_id: OptionalNullable[str] = UNSET,
        visibility: Optional[
            models.TemplateCreateTemplateTemporaryVisibilityRequest
        ] = None,
        global_access_auth: Optional[
            List[models.TemplateCreateTemplateTemporaryGlobalAccessAuthRequest]
        ] = None,
        global_action_auth: Optional[
            List[models.TemplateCreateTemplateTemporaryGlobalActionAuthRequest]
        ] = None,
        public_title: Optional[str] = None,
        public_description: Optional[str] = None,
        type_: Optional[models.TemplateCreateTemplateTemporaryTypeRequest] = None,
        meta: Optional[
            Union[
                models.TemplateCreateTemplateTemporaryMeta,
                models.TemplateCreateTemplateTemporaryMetaTypedDict,
            ]
        ] = None,
        attachments: Optional[
            Union[
                List[models.TemplateCreateTemplateTemporaryAttachment],
                List[models.TemplateCreateTemplateTemporaryAttachmentTypedDict],
            ]
        ] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.TemplateCreateTemplateTemporaryResponse:
        r"""Create template

        You will need to upload the PDF to the provided URL returned. Note: Once V2 API is released, this will be removed since we will allow direct uploads, instead of using an upload URL.

        :param title:
        :param folder_id:
        :param external_id:
        :param visibility:
        :param global_access_auth:
        :param global_action_auth:
        :param public_title:
        :param public_description:
        :param type:
        :param meta:
        :param attachments:
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.TemplateCreateTemplateTemporaryRequest(
            title=title,
            folder_id=folder_id,
            external_id=external_id,
            visibility=visibility,
            global_access_auth=global_access_auth,
            global_action_auth=global_action_auth,
            public_title=public_title,
            public_description=public_description,
            type=type_,
            meta=utils.get_pydantic_model(
                meta, Optional[models.TemplateCreateTemplateTemporaryMeta]
            ),
            attachments=utils.get_pydantic_model(
                attachments,
                Optional[List[models.TemplateCreateTemplateTemporaryAttachment]],
            ),
        )

        req = self._build_request(
            method="POST",
            path="/template/create/beta",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request,
                False,
                False,
                "json",
                models.TemplateCreateTemplateTemporaryRequest,
            ),
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="template-createTemplateTemporary",
                oauth2_scopes=None,
                security_source=get_security_from_env(
                    self.sdk_configuration.security, models.Security
                ),
            ),
            request=req,
            error_status_codes=["400", "401", "403", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryResponse, http_res
            )
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryBadRequestErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryBadRequestError(
                response_data, http_res
            )
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryUnauthorizedErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryUnauthorizedError(
                response_data, http_res
            )
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryForbiddenErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryForbiddenError(
                response_data, http_res
            )
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryInternalServerErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryInternalServerError(
                response_data, http_res
            )
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise models.APIError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise models.APIError("API error occurred", http_res, http_res_text)

        raise models.APIError("Unexpected response received", http_res)

    async def template_create_template_temporary_async(
        self,
        *,
        title: str,
        folder_id: Optional[str] = None,
        external_id: OptionalNullable[str] = UNSET,
        visibility: Optional[
            models.TemplateCreateTemplateTemporaryVisibilityRequest
        ] = None,
        global_access_auth: Optional[
            List[models.TemplateCreateTemplateTemporaryGlobalAccessAuthRequest]
        ] = None,
        global_action_auth: Optional[
            List[models.TemplateCreateTemplateTemporaryGlobalActionAuthRequest]
        ] = None,
        public_title: Optional[str] = None,
        public_description: Optional[str] = None,
        type_: Optional[models.TemplateCreateTemplateTemporaryTypeRequest] = None,
        meta: Optional[
            Union[
                models.TemplateCreateTemplateTemporaryMeta,
                models.TemplateCreateTemplateTemporaryMetaTypedDict,
            ]
        ] = None,
        attachments: Optional[
            Union[
                List[models.TemplateCreateTemplateTemporaryAttachment],
                List[models.TemplateCreateTemplateTemporaryAttachmentTypedDict],
            ]
        ] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.TemplateCreateTemplateTemporaryResponse:
        r"""Create template

        You will need to upload the PDF to the provided URL returned. Note: Once V2 API is released, this will be removed since we will allow direct uploads, instead of using an upload URL.

        :param title:
        :param folder_id:
        :param external_id:
        :param visibility:
        :param global_access_auth:
        :param global_action_auth:
        :param public_title:
        :param public_description:
        :param type:
        :param meta:
        :param attachments:
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.TemplateCreateTemplateTemporaryRequest(
            title=title,
            folder_id=folder_id,
            external_id=external_id,
            visibility=visibility,
            global_access_auth=global_access_auth,
            global_action_auth=global_action_auth,
            public_title=public_title,
            public_description=public_description,
            type=type_,
            meta=utils.get_pydantic_model(
                meta, Optional[models.TemplateCreateTemplateTemporaryMeta]
            ),
            attachments=utils.get_pydantic_model(
                attachments,
                Optional[List[models.TemplateCreateTemplateTemporaryAttachment]],
            ),
        )

        req = self._build_request_async(
            method="POST",
            path="/template/create/beta",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request,
                False,
                False,
                "json",
                models.TemplateCreateTemplateTemporaryRequest,
            ),
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="template-createTemplateTemporary",
                oauth2_scopes=None,
                security_source=get_security_from_env(
                    self.sdk_configuration.security, models.Security
                ),
            ),
            request=req,
            error_status_codes=["400", "401", "403", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryResponse, http_res
            )
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryBadRequestErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryBadRequestError(
                response_data, http_res
            )
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryUnauthorizedErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryUnauthorizedError(
                response_data, http_res
            )
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryForbiddenErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryForbiddenError(
                response_data, http_res
            )
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                models.TemplateCreateTemplateTemporaryInternalServerErrorData, http_res
            )
            raise models.TemplateCreateTemplateTemporaryInternalServerError(
                response_data, http_res
            )
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise models.APIError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise models.APIError("API error occurred", http_res, http_res_text)

        raise models.APIError("Unexpected response received", http_res)
