# coding: utf-8

"""
    RAGsys API

     RAGSys API is an innovative AI retrieval engine developed by Crossing Minds to enhance  Large Language Model (LLM) performance without the need for traditional fine-tuning.  By integrating Retrieval-Augmented Generation (RAG) techniques, RAGSys enables seamless adaptation  of LLMs to various tasks and domains, offering a cost-effective and scalable solution for  enterprises.   Key Features of RAGSys:  - **Seamless Integration**: Compatible with any LLM, including those from Anthropic, OpenAI, and open-source alternatives, allowing effortless switching between models without losing optimizations.  - **Enhanced Retrieval with RAG Embeddings**: Utilizes proprietary RAG embeddings to ensure better understanding and rapid information retrieval, even with massive datasets, resulting in more contextually relevant and accurate LLM outputs.  - **Groundbreaking Accuracy Improvements**: Consistently outperforms traditional fine-tuning, achieving up to a 76% improvement on key benchmarks, with significant enhancements in truthfulness, emotion detection, and commonsense reasoning across various LLMs.  - **Scalable Architecture**: Designed for enterprise-scale deployment, efficiently handling large datasets and complex retrieval tasks, ensuring consistent performance as AI needs grow.   By leveraging RAGSys, organizations can rapidly adapt LLMs to new tasks or domains, saving computational resources and accelerating deployment cycles, all while maintaining high levels of accuracy and relevance in AI outputs.  

    The version of the OpenAPI document: 1.0.0
    Contact: contact@crossingminds.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


import datetime
import time
from dateutil.parser import parse
from enum import Enum
import decimal
import json
import mimetypes
import os
import re
import tempfile

from urllib.parse import quote
from typing import Tuple, Optional, List, Dict, Union
from pydantic import SecretStr

from ragsys.configuration import Configuration
from ragsys.api_response import ApiResponse, T as ApiResponseT
import ragsys.models
from ragsys import rest
from ragsys.exceptions import (
    ApiValueError,
    ApiException,
    BadRequestException,
    UnauthorizedException,
    ForbiddenException,
    NotFoundException,
    ServiceException
)
# import apis into api package
from ragsys.api.account_api import AccountApi
from ragsys.api.completion_api import CompletionApi
from ragsys.api.database_api import DatabaseApi
from ragsys.api.few_shot_example_api import FewShotExampleApi
from ragsys.api.task_api import TaskApi



RequestSerialized = Tuple[str, str, Dict[str, str], Optional[str], List[str]]

class RAGSys:
    """RAGsys Python client


    """

    PRIMITIVE_TYPES = (float, bool, bytes, str, int)
    NATIVE_TYPES_MAPPING = {
        'int': int,
        'long': int, # TODO remove as only py3 is supported?
        'float': float,
        'str': str,
        'bool': bool,
        'date': datetime.date,
        'datetime': datetime.datetime,
        'decimal': decimal.Decimal,
        'object': object,
    }
    _pool = None

    def __init__(
        self,
        api_key: Optional[str] = None,
        base_url: str = "https://ragsys-api.crossingminds.com",
    ) -> None:
        self.configuration = Configuration(
            access_token=api_key,
            host=base_url,
        )
        self.rest_client = rest.RESTClientObject(self.configuration)

        self.default_headers = {}
        # Set default User-Agent
        self.user_agent = 'RAGsys/1.0.1/python'
        self.cookie = None
        self.client_side_validation = self.configuration.client_side_validation

        # Initialize API resources
        self._account_api = AccountApi(self)
        self._completion_api = CompletionApi(self)
        self._database_api = DatabaseApi(self)
        self._few_shot_example_api = FewShotExampleApi(self)
        self._task_api = TaskApi(self)


    # ================ API methods for Account ================

    def login(self, email, password):
        """
        Login using email and password. An API key will be given in the body of the response. The api key should be used with `Authorization: Bearer <api_key>` header to authenticate your following requests.
        
        :param str email: Email address
        :param str password: Password
        :return: LoginResponse
        """
        login_request = {   
            'email': email,
            'password': password,
        }

        return self._account_api.login(login_request)

    def reset_password(self, email, password, code, code_expiration_timestamp):
        """
        Reset password using a code.
        
        :param str email: Email address
        :param str password: New Password
        :param str code: Verification code sent to the email
        :param int code_expiration_timestamp: Timestamp when the verification code expires
        :return: ResetPasswordResponse
        """
        reset_password = {   
            'email': email,
            'password': password,
            'code': code,
            'code_expiration_timestamp': code_expiration_timestamp,
        }

        return self._account_api.reset_password(reset_password)

    def send_reset_password_email(self, email, email_redirect_url):
        """
        This endpoint will send a reset password code to the email.  All instruction to reset the password are inside the email.
        
        :param str email: Email address
        :param str email_redirect_url: Email redirect URL. It will send set password email. The email, code and the code expiration will be added to the URL as query parameter
        :return: 
        """
        send_reset_password_request = {   
            'email': email,
            'email_redirect_url': email_redirect_url,
        }

        return self._account_api.send_reset_password_email(send_reset_password_request)

    # ================ API methods for Completion ================

    def completion(self, db_name, messages, retrieval=None, model=None, max_tokens=None, temperature=None):
        """
        Chat completion with dynamic retrieval capabilities. It allows the enrichment of system prompts using Jinja-based templating to dynamically insert retrieval content.  Please ensure you have triggered a Train Model task for the retrieval before attempting the first completion.
        
        :param str db_name: Database name or ID
        :param List[MessageContent] messages: List of messages to be processed.                                The retrieval feature can be enabled when a message with a `system` role is placed in              the first position, and a prompt utilizing Jinja2 template syntax may be              incorporated within this message.               Note: The final message must have the ``user`` role.             
        :param Retrieval retrieval: 
        :param str model: 
        :param int max_tokens: 
        :param float temperature: 
        :return: CompletionResponse
        """
        completion_body = {   
            'messages': messages,
        }
        if retrieval is not None:
            completion_body['retrieval'] = retrieval
        if model is not None:
            completion_body['model'] = model
        if max_tokens is not None:
            completion_body['max_tokens'] = max_tokens
        if temperature is not None:
            completion_body['temperature'] = temperature

        return self._completion_api.completion(db_name, completion_body)

    def get_models(self, db_name):
        """
        List all LLM models available for use in chat completion.
        
        :param str db_name: Database name or ID
        :return: LlmListModelsResponse
        """

        return self._completion_api.get_models(db_name)

    # ================ API methods for Database ================

    def create_database(self, name, description):
        """
        Creates a database for RAGSys.
        
        :param str name: Database name
        :param str description: Database description
        :return: InitDatabaseResponse
        """
        create_database_request = {   
            'name': name,
            'description': description,
        }

        return self._database_api.create_database(create_database_request)

    def delete_database(self, db_name):
        """
        Delete database
        
        :param str db_name: Database name or ID
        :return: 
        """

        return self._database_api.delete_database(db_name)

    def get_database(self, db_name):
        """
        Retrieves database configuration.
        
        :param str db_name: Database name or ID
        :return: GetConfigurationResponse
        """

        return self._database_api.get_database(db_name)

    def get_databases(self, page=None, amt=None):
        """
        List all databases available in the organization.
        
        :param int page: Page to be listed
        :param int amt: Amount of databases to return
        :return: DatabaseListResponse
        """

        return self._database_api.get_databases(page, amt)

    def update_database(self, db_name, description=None):
        """
        Updates a database for RAGSys.
        
        :param str db_name: Database name or ID
        :param str description: 
        :return: 
        """
        update_database = {   
        }
        if description is not None:
            update_database['description'] = description

        return self._database_api.update_database(db_name, update_database)

    # ================ API methods for  ================

    def get_saved_examples(self, db_name, amt=None, cursor=None, filters=None):
        """
        Fetch examples saved. Use amt and cursor query parameters to control the output.
        
        :param str db_name: Database name or ID
        :param int amt: Maximum amount of example returned
        :param str cursor: Pagination cursor, typically from the next_cursor value from the previous response
        :param str filters: A comma-separated list of filters to apply to the query. Each filter follows the format `<PROPERTY_NAME>:<OPERATOR>` or `<PROPERTY_NAME>:<OPERATOR>:<VALUE>`. For numeric properties, available operators are `lt`, `lte`, `gt`, and `gte`, used with a value. For string properties, use `empty` or `notempty` without a value. Multiple filters can be combined using commas (e.g., `price:lt:100,status:notempty`).
        :return: ExamplesResponse
        """

        return self._few_shot_example_api.get_saved_examples(db_name, amt, cursor, filters)

    def retrieve_ranked_examples(self, db_name, query, amt=None, filters=None, diversity=None):
        """
        Get examples by leveraging the retrieval model to rank and return a list of examples most relevant to a given query.
        
        :param str db_name: Database name or ID
        :param str query: Text to be retrieved
        :param int amt: 
        :param str filters: 
        :param float diversity: Weight for diversity reranking rules (0-1.5 recommended)
        :return: RetrieveExamplesResponse
        """
        retrieve_examples = {   
            'query': query,
        }
        if amt is not None:
            retrieve_examples['amt'] = amt
        if filters is not None:
            retrieve_examples['filters'] = filters
        if diversity is not None:
            retrieve_examples['diversity'] = diversity

        return self._few_shot_example_api.retrieve_ranked_examples(db_name, retrieve_examples)

    def save_examples(self, db_name, examples):
        """
        Create or partial update examples. The `query` content will be used by our retrieval model to evaluate relevence and diversity. If `id` is not provided, it will be generated using the `query` content as hash. If an example with the same `id` already exists, the example will be updated.
        
        :param str db_name: Database name or ID
        :param List[Example] examples: List of examples to be processed, where each example is represented as an object with customizable properties. `type` and `item_id` are reserved and cannot be used as custom properties. If the list has many examples or if the examples are large, split it into multiple requests.
        :return: CreateOrPartialUpdateExamplesResponse
        """
        examples_body = {   
            'examples': examples,
        }

        return self._few_shot_example_api.save_examples(db_name, examples_body)

    # ================ API methods for Task ================

    def get_task(self, task_type, task_id, db_name):
        """
        Get status task
        
        :param TaskType task_type: Task type
        :param str task_id: Task ID
        :param str db_name: Database name or ID
        :return: TaskWithDetailsResponse
        """

        return self._task_api.get_task(task_type, task_id, db_name)

    def train_retrieval_model(self, db_name):
        """
        Triggers the retrieval model training process.  This step is essential to use the completion endpoints with the RAGsys retrieval capabilities, such as well-chosen few-shot examples. Please train the retrieval model after you first upload your examples.  After the first training, a retrain is not required to take new examples into account. However, if sizeable changes to the example set are made, a retrain should increase the quality of the retrieval.
        
        :param str db_name: Database name or ID
        :return: TaskResponse
        """

        return self._task_api.train_retrieval_model(db_name)


    def wait_for_training_tasks(self, db_name: str, tasks, timeout=600):
        """
        Wait for a list of background task to complete.

        :param db_name: database ID
        :param tasks:  list of tasks
        :param timeout: maximum time to wait (default: 10min)
        """
        tasks = [dict(task) for task in tasks]
        time_start = time.time()
        error_messages = []
        running_tasks = {f"{task['task_type']}-{task['task_id']}": task for task in tasks}
        while time.time() - time_start < timeout:
            finished = []
            for t_id, task in running_tasks.items():
                task_resp = self.get_task(task['task_type'], task['task_id'], db_name)
                print(f"[{t_id}] Task Status: {task_resp.status}. Task progress: {task_resp.progress}")
                if task_resp.status == 'COMPLETED':
                    print(f"[{t_id}] Task completed.")
                    finished.append(t_id)
                elif task_resp.status in ['REQUEST_SENT', 'RUNNING']:
                    pass
                else:
                    error_messages += [f"[{t_id}] Task failed with status {task_resp.status}"]
                    finished.append(t_id)

            # Remove completed tasks from tracking
            for t_id in finished:
                running_tasks.pop(t_id)

            if not running_tasks:
                print('All tasks completed successfully')
                break

            # Sleep to avoid excessive polling
            time.sleep(2)

        # timeout case
        if running_tasks:
            remaining = ", ".join(running_tasks.keys())
            raise RuntimeError(
                f"The following tasks did not complete within {timeout} seconds: {remaining}")

        # Raise any collected exceptions for failed tasks
        if error_messages:
            raise Exception(f"One or more tasks failed: {error_messages}")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

    @property
    def user_agent(self):
        """User agent for this API client"""
        return self.default_headers['User-Agent']

    @user_agent.setter
    def user_agent(self, value):
        self.default_headers['User-Agent'] = value

    def set_default_header(self, header_name, header_value):
        self.default_headers[header_name] = header_value


    _default = None

    @classmethod
    def get_default(cls):
        """Return new instance of ApiClient.

        This method returns newly created, based on default constructor,
        object of ApiClient class or returns a copy of default
        ApiClient.

        :return: The ApiClient object.
        """
        if cls._default is None:
            cls._default = RAGSys()
        return cls._default

    @classmethod
    def set_default(cls, default):
        """Set default instance of ApiClient.

        It stores default ApiClient.

        :param default: object of ApiClient.
        """
        cls._default = default

    def param_serialize(
        self,
        method,
        resource_path,
        path_params=None,
        query_params=None,
        header_params=None,
        body=None,
        post_params=None,
        files=None, auth_settings=None,
        collection_formats=None,
        _host=None,
        _request_auth=None
    ) -> RequestSerialized:

        """Builds the HTTP request params needed by the request.
        :param method: Method to call.
        :param resource_path: Path to method endpoint.
        :param path_params: Path parameters in the url.
        :param query_params: Query parameters in the url.
        :param header_params: Header parameters to be
            placed in the request header.
        :param body: Request body.
        :param post_params dict: Request post form parameters,
            for `application/x-www-form-urlencoded`, `multipart/form-data`.
        :param auth_settings list: Auth Settings names for the request.
        :param files dict: key -> filename, value -> filepath,
            for `multipart/form-data`.
        :param collection_formats: dict of collection formats for path, query,
            header, and post parameters.
        :param _request_auth: set to override the auth_settings for an a single
                              request; this effectively ignores the authentication
                              in the spec for a single request.
        :return: tuple of form (path, http_method, query_params, header_params,
            body, post_params, files)
        """

        config = self.configuration

        # header parameters
        header_params = header_params or {}
        header_params.update(self.default_headers)
        if self.cookie:
            header_params['Cookie'] = self.cookie
        if header_params:
            header_params = self.sanitize_for_serialization(header_params)
            header_params = dict(
                self.parameters_to_tuples(header_params,collection_formats)
            )

        # path parameters
        if path_params:
            path_params = self.sanitize_for_serialization(path_params)
            path_params = self.parameters_to_tuples(
                path_params,
                collection_formats
            )
            for k, v in path_params:
                # specified safe chars, encode everything
                resource_path = resource_path.replace(
                    '{%s}' % k,
                    quote(str(v), safe=config.safe_chars_for_path_param)
                )

        # post parameters
        if post_params or files:
            post_params = post_params if post_params else []
            post_params = self.sanitize_for_serialization(post_params)
            post_params = self.parameters_to_tuples(
                post_params,
                collection_formats
            )
            if files:
                post_params.extend(self.files_parameters(files))

        # auth setting
        self.update_params_for_auth(
            header_params,
            query_params,
            auth_settings,
            resource_path,
            method,
            body,
            request_auth=_request_auth
        )

        # body
        if body:
            body = self.sanitize_for_serialization(body)

        # request url
        if _host is None or self.configuration.ignore_operation_servers:
            url = self.configuration.host + resource_path
        else:
            # use server/host defined in path or operation instead
            url = _host + resource_path

        # query parameters
        if query_params:
            query_params = self.sanitize_for_serialization(query_params)
            url_query = self.parameters_to_url_query(
                query_params,
                collection_formats
            )
            url += "?" + url_query

        return method, url, header_params, body, post_params


    def call_api(
        self,
        method,
        url,
        header_params=None,
        body=None,
        post_params=None,
        _request_timeout=None
    ) -> rest.RESTResponse:
        """Makes the HTTP request (synchronous)
        :param method: Method to call.
        :param url: Path to method endpoint.
        :param header_params: Header parameters to be
            placed in the request header.
        :param body: Request body.
        :param post_params dict: Request post form parameters,
            for `application/x-www-form-urlencoded`, `multipart/form-data`.
        :param _request_timeout: timeout setting for this request.
        :return: RESTResponse
        """

        try:
            # perform request and return response
            response_data = self.rest_client.request(
                method, url,
                headers=header_params,
                body=body, post_params=post_params,
                _request_timeout=_request_timeout
            )

        except ApiException as e:
            raise e

        return response_data

    def response_deserialize(
        self,
        response_data: rest.RESTResponse,
        response_types_map: Optional[Dict[str, ApiResponseT]]=None
    ) -> ApiResponse[ApiResponseT]:
        """Deserializes response into an object.
        :param response_data: RESTResponse object to be deserialized.
        :param response_types_map: dict of response types.
        :return: ApiResponse
        """

        msg = "RESTResponse.read() must be called before passing it to response_deserialize()"
        assert response_data.data is not None, msg

        response_type = response_types_map.get(str(response_data.status), None)
        if not response_type and isinstance(response_data.status, int) and 100 <= response_data.status <= 599:
            # if not found, look for '1XX', '2XX', etc.
            response_type = response_types_map.get(str(response_data.status)[0] + "XX", None)

        # deserialize response data
        response_text = None
        return_data = None
        try:
            if response_type == "bytearray":
                return_data = response_data.data
            elif response_type == "file":
                return_data = self.__deserialize_file(response_data)
            elif response_type is not None:
                match = None
                content_type = response_data.getheader('content-type')
                if content_type is not None:
                    match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
                encoding = match.group(1) if match else "utf-8"
                response_text = response_data.data.decode(encoding)
                return_data = self.deserialize(response_text, response_type, content_type)
        finally:
            if not 200 <= response_data.status <= 299:
                raise ApiException.from_response(
                    http_resp=response_data,
                    body=response_text,
                    data=return_data,
                )

        return ApiResponse(
            status_code = response_data.status,
            data = return_data,
            headers = response_data.getheaders(),
            raw_data = response_data.data
        )

    def sanitize_for_serialization(self, obj):
        """Builds a JSON POST object.

        If obj is None, return None.
        If obj is SecretStr, return obj.get_secret_value()
        If obj is str, int, long, float, bool, return directly.
        If obj is datetime.datetime, datetime.date
            convert to string in iso8601 format.
        If obj is decimal.Decimal return string representation.
        If obj is list, sanitize each element in the list.
        If obj is dict, return the dict.
        If obj is OpenAPI model, return the properties dict.

        :param obj: The data to serialize.
        :return: The serialized form of data.
        """
        if obj is None:
            return None
        elif isinstance(obj, Enum):
            return obj.value
        elif isinstance(obj, SecretStr):
            return obj.get_secret_value()
        elif isinstance(obj, self.PRIMITIVE_TYPES):
            return obj
        elif isinstance(obj, list):
            return [
                self.sanitize_for_serialization(sub_obj) for sub_obj in obj
            ]
        elif isinstance(obj, tuple):
            return tuple(
                self.sanitize_for_serialization(sub_obj) for sub_obj in obj
            )
        elif isinstance(obj, (datetime.datetime, datetime.date)):
            return obj.isoformat()
        elif isinstance(obj, decimal.Decimal):
            return str(obj)

        elif isinstance(obj, dict):
            obj_dict = obj
        else:
            # Convert model obj to dict except
            # attributes `openapi_types`, `attribute_map`
            # and attributes which value is not None.
            # Convert attribute name to json key in
            # model definition for request.
            if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
                obj_dict = obj.to_dict()
            else:
                obj_dict = obj.__dict__

        return {
            key: self.sanitize_for_serialization(val)
            for key, val in obj_dict.items()
        }

    def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
        """Deserializes response into an object.

        :param response: RESTResponse object to be deserialized.
        :param response_type: class literal for
            deserialized object, or string of class name.
        :param content_type: content type of response.

        :return: deserialized object.
        """

        # fetch data from response object
        if content_type is None:
            try:
                data = json.loads(response_text)
            except ValueError:
                data = response_text
        elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
            if response_text == "":
                data = ""
            else:
                data = json.loads(response_text)
        elif re.match(r'^text\/[a-z.+-]+\s*(;|$)', content_type, re.IGNORECASE):
            data = response_text
        else:
            raise ApiException(
                status=0,
                reason="Unsupported content type: {0}".format(content_type)
            )

        return self.__deserialize(data, response_type)

    def __deserialize(self, data, klass):
        """Deserializes dict, list, str into an object.

        :param data: dict, list or str.
        :param klass: class literal, or string of class name.

        :return: object.
        """
        if data is None:
            return None

        if isinstance(klass, str):
            if klass.startswith('List['):
                m = re.match(r'List\[(.*)]', klass)
                assert m is not None, "Malformed List type definition"
                sub_kls = m.group(1)
                return [self.__deserialize(sub_data, sub_kls)
                        for sub_data in data]

            if klass.startswith('Dict['):
                m = re.match(r'Dict\[([^,]*), (.*)]', klass)
                assert m is not None, "Malformed Dict type definition"
                sub_kls = m.group(2)
                return {k: self.__deserialize(v, sub_kls)
                        for k, v in data.items()}

            # convert str to class
            if klass in self.NATIVE_TYPES_MAPPING:
                klass = self.NATIVE_TYPES_MAPPING[klass]
            else:
                klass = getattr(ragsys.models, klass)

        if klass in self.PRIMITIVE_TYPES:
            return self.__deserialize_primitive(data, klass)
        elif klass == object:
            return self.__deserialize_object(data)
        elif klass == datetime.date:
            return self.__deserialize_date(data)
        elif klass == datetime.datetime:
            return self.__deserialize_datetime(data)
        elif klass == decimal.Decimal:
            return decimal.Decimal(data)
        elif issubclass(klass, Enum):
            return self.__deserialize_enum(data, klass)
        else:
            return self.__deserialize_model(data, klass)

    def parameters_to_tuples(self, params, collection_formats):
        """Get parameters as list of tuples, formatting collections.

        :param params: Parameters as dict or list of two-tuples
        :param dict collection_formats: Parameter collection formats
        :return: Parameters as list of tuples, collections formatted
        """
        new_params: List[Tuple[str, str]] = []
        if collection_formats is None:
            collection_formats = {}
        for k, v in params.items() if isinstance(params, dict) else params:
            if k in collection_formats:
                collection_format = collection_formats[k]
                if collection_format == 'multi':
                    new_params.extend((k, value) for value in v)
                else:
                    if collection_format == 'ssv':
                        delimiter = ' '
                    elif collection_format == 'tsv':
                        delimiter = '\t'
                    elif collection_format == 'pipes':
                        delimiter = '|'
                    else:  # csv is the default
                        delimiter = ','
                    new_params.append(
                        (k, delimiter.join(str(value) for value in v)))
            else:
                new_params.append((k, v))
        return new_params

    def parameters_to_url_query(self, params, collection_formats):
        """Get parameters as list of tuples, formatting collections.

        :param params: Parameters as dict or list of two-tuples
        :param dict collection_formats: Parameter collection formats
        :return: URL query string (e.g. a=Hello%20World&b=123)
        """
        new_params: List[Tuple[str, str]] = []
        if collection_formats is None:
            collection_formats = {}
        for k, v in params.items() if isinstance(params, dict) else params:
            if isinstance(v, bool):
                v = str(v).lower()
            if isinstance(v, (int, float)):
                v = str(v)
            if isinstance(v, dict):
                v = json.dumps(v)

            if k in collection_formats:
                collection_format = collection_formats[k]
                if collection_format == 'multi':
                    new_params.extend((k, quote(str(value))) for value in v)
                else:
                    if collection_format == 'ssv':
                        delimiter = ' '
                    elif collection_format == 'tsv':
                        delimiter = '\t'
                    elif collection_format == 'pipes':
                        delimiter = '|'
                    else:  # csv is the default
                        delimiter = ','
                    new_params.append(
                        (k, delimiter.join(quote(str(value)) for value in v))
                    )
            else:
                new_params.append((k, quote(str(v))))

        return "&".join(["=".join(map(str, item)) for item in new_params])

    def files_parameters(
        self,
        files: Dict[str, Union[str, bytes, List[str], List[bytes], Tuple[str, bytes]]],
    ):
        """Builds form parameters.

        :param files: File parameters.
        :return: Form parameters with files.
        """
        params = []
        for k, v in files.items():
            if isinstance(v, str):
                with open(v, 'rb') as f:
                    filename = os.path.basename(f.name)
                    filedata = f.read()
            elif isinstance(v, bytes):
                filename = k
                filedata = v
            elif isinstance(v, tuple):
                filename, filedata = v
            elif isinstance(v, list):
                for file_param in v:
                    params.extend(self.files_parameters({k: file_param}))
                continue
            else:
                raise ValueError("Unsupported file value")
            mimetype = (
                mimetypes.guess_type(filename)[0]
                or 'application/octet-stream'
            )
            params.append(
                tuple([k, tuple([filename, filedata, mimetype])])
            )
        return params

    def select_header_accept(self, accepts: List[str]) -> Optional[str]:
        """Returns `Accept` based on an array of accepts provided.

        :param accepts: List of headers.
        :return: Accept (e.g. application/json).
        """
        if not accepts:
            return None

        for accept in accepts:
            if re.search('json', accept, re.IGNORECASE):
                return accept

        return accepts[0]

    def select_header_content_type(self, content_types):
        """Returns `Content-Type` based on an array of content_types provided.

        :param content_types: List of content-types.
        :return: Content-Type (e.g. application/json).
        """
        if not content_types:
            return None

        for content_type in content_types:
            if re.search('json', content_type, re.IGNORECASE):
                return content_type

        return content_types[0]

    def update_params_for_auth(
        self,
        headers,
        queries,
        auth_settings,
        resource_path,
        method,
        body,
        request_auth=None
    ) -> None:
        """Updates header and query params based on authentication setting.

        :param headers: Header parameters dict to be updated.
        :param queries: Query parameters tuple list to be updated.
        :param auth_settings: Authentication setting identifiers list.
        :resource_path: A string representation of the HTTP request resource path.
        :method: A string representation of the HTTP request method.
        :body: A object representing the body of the HTTP request.
        The object type is the return value of sanitize_for_serialization().
        :param request_auth: if set, the provided settings will
                             override the token in the configuration.
        """
        if not auth_settings:
            return

        if request_auth:
            self._apply_auth_params(
                headers,
                queries,
                resource_path,
                method,
                body,
                request_auth
            )
        else:
            for auth in auth_settings:
                auth_setting = self.configuration.auth_settings().get(auth)
                if auth_setting:
                    self._apply_auth_params(
                        headers,
                        queries,
                        resource_path,
                        method,
                        body,
                        auth_setting
                    )

    def _apply_auth_params(
        self,
        headers,
        queries,
        resource_path,
        method,
        body,
        auth_setting
    ) -> None:
        """Updates the request parameters based on a single auth_setting

        :param headers: Header parameters dict to be updated.
        :param queries: Query parameters tuple list to be updated.
        :resource_path: A string representation of the HTTP request resource path.
        :method: A string representation of the HTTP request method.
        :body: A object representing the body of the HTTP request.
        The object type is the return value of sanitize_for_serialization().
        :param auth_setting: auth settings for the endpoint
        """
        if auth_setting['in'] == 'cookie':
            headers['Cookie'] = auth_setting['value']
        elif auth_setting['in'] == 'header':
            if auth_setting['type'] != 'http-signature':
                headers[auth_setting['key']] = auth_setting['value']
        elif auth_setting['in'] == 'query':
            queries.append((auth_setting['key'], auth_setting['value']))
        else:
            raise ApiValueError(
                'Authentication token must be in `query` or `header`'
            )

    def __deserialize_file(self, response):
        """Deserializes body to file

        Saves response body into a file in a temporary folder,
        using the filename from the `Content-Disposition` header if provided.

        handle file downloading
        save response body into a tmp file and return the instance

        :param response:  RESTResponse.
        :return: file path.
        """
        fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
        os.close(fd)
        os.remove(path)

        content_disposition = response.getheader("Content-Disposition")
        if content_disposition:
            m = re.search(
                r'filename=[\'"]?([^\'"\s]+)[\'"]?',
                content_disposition
            )
            assert m is not None, "Unexpected 'content-disposition' header value"
            filename = m.group(1)
            path = os.path.join(os.path.dirname(path), filename)

        with open(path, "wb") as f:
            f.write(response.data)

        return path

    def __deserialize_primitive(self, data, klass):
        """Deserializes string to primitive type.

        :param data: str.
        :param klass: class literal.

        :return: int, long, float, str, bool.
        """
        try:
            return klass(data)
        except UnicodeEncodeError:
            return str(data)
        except TypeError:
            return data

    def __deserialize_object(self, value):
        """Return an original value.

        :return: object.
        """
        return value

    def __deserialize_date(self, string):
        """Deserializes string to date.

        :param string: str.
        :return: date.
        """
        try:
            return parse(string).date()
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason="Failed to parse `{0}` as date object".format(string)
            )

    def __deserialize_datetime(self, string):
        """Deserializes string to datetime.

        The string should be in iso8601 datetime format.

        :param string: str.
        :return: datetime.
        """
        try:
            return parse(string)
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason=(
                    "Failed to parse `{0}` as datetime object"
                    .format(string)
                )
            )

    def __deserialize_enum(self, data, klass):
        """Deserializes primitive type to enum.

        :param data: primitive type.
        :param klass: class literal.
        :return: enum value.
        """
        try:
            return klass(data)
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason=(
                    "Failed to parse `{0}` as `{1}`"
                    .format(data, klass)
                )
            )

    def __deserialize_model(self, data, klass):
        """Deserializes list or dict to model.

        :param data: dict, list.
        :param klass: class literal.
        :return: model object.
        """

        return klass.from_dict(data)
