pinecone.openapi_support.deserializer

 1import json
 2import re
 3
 4from .model_utils import deserialize_file, file_type, validate_and_convert_types
 5
 6
 7class Deserializer:
 8    @staticmethod
 9    def decode_response(response_type, response):
10        if response_type != (file_type,):
11            encoding = "utf-8"
12            content_type = response.getheader("content-type")
13            if content_type is not None and "charset=" in content_type:
14                match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type)
15                if match:
16                    encoding = match.group(1)
17            response.data = response.data.decode(encoding)
18
19    @staticmethod
20    def deserialize(response, response_type, config, _check_type):
21        """Deserializes response into an object.
22
23        :param response: RESTResponse object to be deserialized.
24        :param response_type: For the response, a tuple containing:
25            valid classes
26            a list containing valid classes (for list schemas)
27            a dict containing a tuple of valid classes as the value
28            Example values:
29            (str,)
30            (Pet,)
31            (float, none_type)
32            ([int, none_type],)
33            ({str: (bool, str, int, float, date, datetime, str, none_type)},)
34        :param _check_type: boolean, whether to check the types of the data
35            received from the server
36        :type _check_type: bool
37
38        :return: deserialized object.
39        """
40        # handle file downloading
41        # save response body into a tmp file and return the instance
42        if response_type == (file_type,):
43            content_disposition = response.getheader("Content-Disposition")
44            return deserialize_file(response.data, config, content_disposition=content_disposition)
45
46        # fetch data from response object
47        try:
48            received_data = json.loads(response.data)
49        except ValueError:
50            received_data = response.data
51
52        # store our data under the key of 'received_data' so users have some
53        # context if they are deserializing a string and the data type is wrong
54
55        deserialized_data = validate_and_convert_types(
56            received_data, response_type, ["received_data"], True, _check_type, configuration=config
57        )
58        return deserialized_data
class Deserializer:
 8class Deserializer:
 9    @staticmethod
10    def decode_response(response_type, response):
11        if response_type != (file_type,):
12            encoding = "utf-8"
13            content_type = response.getheader("content-type")
14            if content_type is not None and "charset=" in content_type:
15                match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type)
16                if match:
17                    encoding = match.group(1)
18            response.data = response.data.decode(encoding)
19
20    @staticmethod
21    def deserialize(response, response_type, config, _check_type):
22        """Deserializes response into an object.
23
24        :param response: RESTResponse object to be deserialized.
25        :param response_type: For the response, a tuple containing:
26            valid classes
27            a list containing valid classes (for list schemas)
28            a dict containing a tuple of valid classes as the value
29            Example values:
30            (str,)
31            (Pet,)
32            (float, none_type)
33            ([int, none_type],)
34            ({str: (bool, str, int, float, date, datetime, str, none_type)},)
35        :param _check_type: boolean, whether to check the types of the data
36            received from the server
37        :type _check_type: bool
38
39        :return: deserialized object.
40        """
41        # handle file downloading
42        # save response body into a tmp file and return the instance
43        if response_type == (file_type,):
44            content_disposition = response.getheader("Content-Disposition")
45            return deserialize_file(response.data, config, content_disposition=content_disposition)
46
47        # fetch data from response object
48        try:
49            received_data = json.loads(response.data)
50        except ValueError:
51            received_data = response.data
52
53        # store our data under the key of 'received_data' so users have some
54        # context if they are deserializing a string and the data type is wrong
55
56        deserialized_data = validate_and_convert_types(
57            received_data, response_type, ["received_data"], True, _check_type, configuration=config
58        )
59        return deserialized_data
@staticmethod
def decode_response(response_type, response):
 9    @staticmethod
10    def decode_response(response_type, response):
11        if response_type != (file_type,):
12            encoding = "utf-8"
13            content_type = response.getheader("content-type")
14            if content_type is not None and "charset=" in content_type:
15                match = re.search(r"charset=([a-zA-Z\-\d]+)[\s\;]?", content_type)
16                if match:
17                    encoding = match.group(1)
18            response.data = response.data.decode(encoding)
@staticmethod
def deserialize(response, response_type, config, _check_type):
20    @staticmethod
21    def deserialize(response, response_type, config, _check_type):
22        """Deserializes response into an object.
23
24        :param response: RESTResponse object to be deserialized.
25        :param response_type: For the response, a tuple containing:
26            valid classes
27            a list containing valid classes (for list schemas)
28            a dict containing a tuple of valid classes as the value
29            Example values:
30            (str,)
31            (Pet,)
32            (float, none_type)
33            ([int, none_type],)
34            ({str: (bool, str, int, float, date, datetime, str, none_type)},)
35        :param _check_type: boolean, whether to check the types of the data
36            received from the server
37        :type _check_type: bool
38
39        :return: deserialized object.
40        """
41        # handle file downloading
42        # save response body into a tmp file and return the instance
43        if response_type == (file_type,):
44            content_disposition = response.getheader("Content-Disposition")
45            return deserialize_file(response.data, config, content_disposition=content_disposition)
46
47        # fetch data from response object
48        try:
49            received_data = json.loads(response.data)
50        except ValueError:
51            received_data = response.data
52
53        # store our data under the key of 'received_data' so users have some
54        # context if they are deserializing a string and the data type is wrong
55
56        deserialized_data = validate_and_convert_types(
57            received_data, response_type, ["received_data"], True, _check_type, configuration=config
58        )
59        return deserialized_data

Deserializes response into an object.

Parameters
  • response: RESTResponse object to be deserialized.
  • response_type: For the response, a tuple containing: valid classes a list containing valid classes (for list schemas) a dict containing a tuple of valid classes as the value Example values: (str,) (Pet,) (float, none_type) ([int, none_type],) ({str: (bool, str, int, float, date, datetime, str, none_type)},)
  • _check_type: boolean, whether to check the types of the data received from the server
Returns

deserialized object.