pinecone.openapi_support.rest_urllib3

  1import json
  2import logging
  3import ssl
  4import os
  5from typing import Optional
  6from urllib.parse import urlencode, quote
  7from .configuration import Configuration
  8from .rest_utils import raise_exceptions_or_return, RESTResponse, RestClientInterface
  9
 10import urllib3
 11
 12from .exceptions import PineconeApiException, PineconeApiValueError
 13
 14
 15class bcolors:
 16    HEADER = "\033[95m"
 17    OKBLUE = "\033[94m"
 18    OKCYAN = "\033[96m"
 19    OKGREEN = "\033[92m"
 20    WARNING = "\033[93m"
 21    FAIL = "\033[91m"
 22    ENDC = "\033[0m"
 23    BOLD = "\033[1m"
 24    UNDERLINE = "\033[4m"
 25
 26
 27logger = logging.getLogger(__name__)
 28""" @private """
 29
 30
 31class Urllib3RestClient(RestClientInterface):
 32    pool_manager: urllib3.PoolManager
 33
 34    def __init__(
 35        self, configuration: Configuration, pools_size: int = 4, maxsize: Optional[int] = None
 36    ) -> None:
 37        # urllib3.PoolManager will pass all kw parameters to connectionpool
 38        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
 39        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
 40        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
 41        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501
 42
 43        # cert_reqs
 44        if configuration.verify_ssl:
 45            cert_reqs = ssl.CERT_REQUIRED
 46        else:
 47            cert_reqs = ssl.CERT_NONE
 48
 49        addition_pool_args = {}
 50        if configuration.assert_hostname is not None:
 51            addition_pool_args["assert_hostname"] = configuration.assert_hostname  # noqa: E501
 52
 53        if configuration.retries is not None:
 54            addition_pool_args["retries"] = configuration.retries
 55
 56        if configuration.socket_options is not None:
 57            addition_pool_args["socket_options"] = configuration.socket_options
 58
 59        if maxsize is None:
 60            if configuration.connection_pool_maxsize is not None:
 61                maxsize = configuration.connection_pool_maxsize
 62            else:
 63                maxsize = 4
 64
 65        # https pool manager
 66        if configuration.proxy:
 67            self.pool_manager = urllib3.ProxyManager(
 68                num_pools=pools_size,
 69                maxsize=maxsize,
 70                cert_reqs=cert_reqs,
 71                ca_certs=configuration.ssl_ca_cert,
 72                cert_file=configuration.cert_file,
 73                key_file=configuration.key_file,
 74                proxy_url=configuration.proxy,
 75                proxy_headers=configuration.proxy_headers,
 76                **addition_pool_args,
 77            )
 78        else:
 79            self.pool_manager = urllib3.PoolManager(
 80                num_pools=pools_size,
 81                maxsize=maxsize,
 82                cert_reqs=cert_reqs,
 83                ca_certs=configuration.ssl_ca_cert,
 84                cert_file=configuration.cert_file,
 85                key_file=configuration.key_file,
 86                **addition_pool_args,
 87            )
 88
 89    def request(
 90        self,
 91        method,
 92        url,
 93        query_params=None,
 94        headers=None,
 95        body=None,
 96        post_params=None,
 97        _preload_content=True,
 98        _request_timeout=None,
 99    ):
100        """Perform requests.
101
102        :param method: http request method
103        :param url: http request url
104        :param query_params: query parameters in the url
105        :param headers: http request headers
106        :param body: request json body, for `application/json`
107        :param post_params: request post parameters,
108                            `application/x-www-form-urlencoded`
109                            and `multipart/form-data`
110        :param _preload_content: if False, the urllib3.HTTPResponse object will
111                                 be returned without reading/decoding response
112                                 data. Default is True.
113        :param _request_timeout: timeout setting for this request. If one
114                                 number provided, it will be total request
115                                 timeout. It can also be a pair (tuple) of
116                                 (connection, read) timeouts.
117        """
118        logger.debug("Calling urllib3 request()")
119        method = method.upper()
120        assert method in ["GET", "HEAD", "DELETE", "POST", "PUT", "PATCH", "OPTIONS"]
121
122        if os.environ.get("PINECONE_DEBUG_CURL"):
123            formatted_headers = " ".join(["-H '{0}: {1}'".format(k, v) for k, v in headers.items()])
124            formatted_query = urlencode(query_params)
125            if formatted_query:
126                formatted_url = f"{url}?{formatted_query}"
127            else:
128                formatted_url = url
129            if body is None:
130                print(
131                    bcolors.OKBLUE
132                    + "curl -X {method} '{url}' {formatted_headers}".format(
133                        method=method, url=formatted_url, formatted_headers=formatted_headers
134                    )
135                    + bcolors.ENDC
136                )
137            else:
138                formatted_body = json.dumps(body)
139                print(
140                    bcolors.OKBLUE
141                    + "curl -X {method} '{url}' {formatted_headers} -d '{data}'".format(
142                        method=method,
143                        url=formatted_url,
144                        formatted_headers=formatted_headers,
145                        data=formatted_body,
146                    )
147                    + bcolors.ENDC
148                )
149
150        if post_params and body:
151            raise PineconeApiValueError("body parameter cannot be used with post_params parameter.")
152
153        post_params = post_params or {}
154        headers = headers or {}
155
156        timeout = None
157        if _request_timeout:
158            if isinstance(_request_timeout, (int, float)):  # noqa: E501,F821
159                timeout = urllib3.Timeout(total=_request_timeout)
160            elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2:
161                timeout = urllib3.Timeout(connect=_request_timeout[0], read=_request_timeout[1])
162
163        try:
164            # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
165            if method in ["POST", "PUT", "PATCH", "OPTIONS", "DELETE"]:
166                # Only set a default Content-Type for POST, PUT, PATCH and OPTIONS requests
167                if (method != "DELETE") and ("Content-Type" not in headers):
168                    headers["Content-Type"] = "application/json"
169                if query_params:
170                    url += "?" + urlencode(query_params, quote_via=quote)
171
172                content_type = headers.get("Content-Type", "").lower()
173                if content_type == "" or ("json" in content_type):
174                    if body is None:
175                        request_body = None
176                    else:
177                        if content_type == "application/x-ndjson":
178                            # for x-ndjson requests, we are expecting an array of elements
179                            # that need to be converted to a newline separated string
180                            request_body = "\n".join(json.dumps(element) for element in body)
181                        else:  # content_type == "application/json":
182                            request_body = json.dumps(body)
183                    r = self.pool_manager.request(
184                        method,
185                        url,
186                        body=request_body,
187                        preload_content=_preload_content,
188                        timeout=timeout,
189                        headers=headers,
190                    )
191
192                elif content_type == "application/x-www-form-urlencoded":  # noqa: E501
193                    r = self.pool_manager.request(
194                        method,
195                        url,
196                        fields=post_params,
197                        encode_multipart=False,
198                        preload_content=_preload_content,
199                        timeout=timeout,
200                        headers=headers,
201                    )
202                elif content_type == "multipart/form-data":
203                    # must del headers['Content-Type'], or the correct
204                    # Content-Type which generated by urllib3 will be
205                    # overwritten.
206                    del headers["Content-Type"]
207                    r = self.pool_manager.request(
208                        method,
209                        url,
210                        fields=post_params,
211                        encode_multipart=True,
212                        preload_content=_preload_content,
213                        timeout=timeout,
214                        headers=headers,
215                    )
216                # Pass a `string` parameter directly in the body to support
217                # other content types than Json when `body` argument is
218                # provided in serialized form
219                elif isinstance(body, str) or isinstance(body, bytes):
220                    request_body = body
221                    r = self.pool_manager.request(
222                        method,
223                        url,
224                        body=request_body,
225                        preload_content=_preload_content,
226                        timeout=timeout,
227                        headers=headers,
228                    )
229                else:
230                    # Cannot generate the request from given parameters
231                    msg = """Cannot prepare a request message for provided
232                             arguments. Please check that your arguments match
233                             declared content type."""
234                    raise PineconeApiException(status=0, reason=msg)
235            # For `GET`, `HEAD`
236            else:
237                if query_params:
238                    url += "?" + urlencode(query_params, quote_via=quote)
239                r = self.pool_manager.request(
240                    method, url, preload_content=_preload_content, timeout=timeout, headers=headers
241                )
242        except urllib3.exceptions.SSLError as e:
243            msg = "{0}\n{1}".format(type(e).__name__, str(e))
244            raise PineconeApiException(status=0, reason=msg)
245
246        if os.environ.get("PINECONE_DEBUG_CURL"):
247            o = RESTResponse(r.status, r.data, r.headers, r.reason)
248
249            if o.status <= 300:
250                print(bcolors.OKGREEN + o.data.decode("utf-8") + bcolors.ENDC)
251            else:
252                print(bcolors.FAIL + o.data.decode("utf-8") + bcolors.ENDC)
253
254        if _preload_content:
255            r = RESTResponse(r.status, r.data, r.headers, r.reason)
256
257            # log response body
258            logger.debug("response body: %s", r.data)
259
260        return raise_exceptions_or_return(r)
class bcolors:
16class bcolors:
17    HEADER = "\033[95m"
18    OKBLUE = "\033[94m"
19    OKCYAN = "\033[96m"
20    OKGREEN = "\033[92m"
21    WARNING = "\033[93m"
22    FAIL = "\033[91m"
23    ENDC = "\033[0m"
24    BOLD = "\033[1m"
25    UNDERLINE = "\033[4m"
HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKCYAN = '\x1b[96m'
OKGREEN = '\x1b[92m'
WARNING = '\x1b[93m'
FAIL = '\x1b[91m'
ENDC = '\x1b[0m'
BOLD = '\x1b[1m'
UNDERLINE = '\x1b[4m'
class Urllib3RestClient(pinecone.openapi_support.rest_utils.RestClientInterface):
 32class Urllib3RestClient(RestClientInterface):
 33    pool_manager: urllib3.PoolManager
 34
 35    def __init__(
 36        self, configuration: Configuration, pools_size: int = 4, maxsize: Optional[int] = None
 37    ) -> None:
 38        # urllib3.PoolManager will pass all kw parameters to connectionpool
 39        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
 40        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
 41        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
 42        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501
 43
 44        # cert_reqs
 45        if configuration.verify_ssl:
 46            cert_reqs = ssl.CERT_REQUIRED
 47        else:
 48            cert_reqs = ssl.CERT_NONE
 49
 50        addition_pool_args = {}
 51        if configuration.assert_hostname is not None:
 52            addition_pool_args["assert_hostname"] = configuration.assert_hostname  # noqa: E501
 53
 54        if configuration.retries is not None:
 55            addition_pool_args["retries"] = configuration.retries
 56
 57        if configuration.socket_options is not None:
 58            addition_pool_args["socket_options"] = configuration.socket_options
 59
 60        if maxsize is None:
 61            if configuration.connection_pool_maxsize is not None:
 62                maxsize = configuration.connection_pool_maxsize
 63            else:
 64                maxsize = 4
 65
 66        # https pool manager
 67        if configuration.proxy:
 68            self.pool_manager = urllib3.ProxyManager(
 69                num_pools=pools_size,
 70                maxsize=maxsize,
 71                cert_reqs=cert_reqs,
 72                ca_certs=configuration.ssl_ca_cert,
 73                cert_file=configuration.cert_file,
 74                key_file=configuration.key_file,
 75                proxy_url=configuration.proxy,
 76                proxy_headers=configuration.proxy_headers,
 77                **addition_pool_args,
 78            )
 79        else:
 80            self.pool_manager = urllib3.PoolManager(
 81                num_pools=pools_size,
 82                maxsize=maxsize,
 83                cert_reqs=cert_reqs,
 84                ca_certs=configuration.ssl_ca_cert,
 85                cert_file=configuration.cert_file,
 86                key_file=configuration.key_file,
 87                **addition_pool_args,
 88            )
 89
 90    def request(
 91        self,
 92        method,
 93        url,
 94        query_params=None,
 95        headers=None,
 96        body=None,
 97        post_params=None,
 98        _preload_content=True,
 99        _request_timeout=None,
100    ):
101        """Perform requests.
102
103        :param method: http request method
104        :param url: http request url
105        :param query_params: query parameters in the url
106        :param headers: http request headers
107        :param body: request json body, for `application/json`
108        :param post_params: request post parameters,
109                            `application/x-www-form-urlencoded`
110                            and `multipart/form-data`
111        :param _preload_content: if False, the urllib3.HTTPResponse object will
112                                 be returned without reading/decoding response
113                                 data. Default is True.
114        :param _request_timeout: timeout setting for this request. If one
115                                 number provided, it will be total request
116                                 timeout. It can also be a pair (tuple) of
117                                 (connection, read) timeouts.
118        """
119        logger.debug("Calling urllib3 request()")
120        method = method.upper()
121        assert method in ["GET", "HEAD", "DELETE", "POST", "PUT", "PATCH", "OPTIONS"]
122
123        if os.environ.get("PINECONE_DEBUG_CURL"):
124            formatted_headers = " ".join(["-H '{0}: {1}'".format(k, v) for k, v in headers.items()])
125            formatted_query = urlencode(query_params)
126            if formatted_query:
127                formatted_url = f"{url}?{formatted_query}"
128            else:
129                formatted_url = url
130            if body is None:
131                print(
132                    bcolors.OKBLUE
133                    + "curl -X {method} '{url}' {formatted_headers}".format(
134                        method=method, url=formatted_url, formatted_headers=formatted_headers
135                    )
136                    + bcolors.ENDC
137                )
138            else:
139                formatted_body = json.dumps(body)
140                print(
141                    bcolors.OKBLUE
142                    + "curl -X {method} '{url}' {formatted_headers} -d '{data}'".format(
143                        method=method,
144                        url=formatted_url,
145                        formatted_headers=formatted_headers,
146                        data=formatted_body,
147                    )
148                    + bcolors.ENDC
149                )
150
151        if post_params and body:
152            raise PineconeApiValueError("body parameter cannot be used with post_params parameter.")
153
154        post_params = post_params or {}
155        headers = headers or {}
156
157        timeout = None
158        if _request_timeout:
159            if isinstance(_request_timeout, (int, float)):  # noqa: E501,F821
160                timeout = urllib3.Timeout(total=_request_timeout)
161            elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2:
162                timeout = urllib3.Timeout(connect=_request_timeout[0], read=_request_timeout[1])
163
164        try:
165            # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
166            if method in ["POST", "PUT", "PATCH", "OPTIONS", "DELETE"]:
167                # Only set a default Content-Type for POST, PUT, PATCH and OPTIONS requests
168                if (method != "DELETE") and ("Content-Type" not in headers):
169                    headers["Content-Type"] = "application/json"
170                if query_params:
171                    url += "?" + urlencode(query_params, quote_via=quote)
172
173                content_type = headers.get("Content-Type", "").lower()
174                if content_type == "" or ("json" in content_type):
175                    if body is None:
176                        request_body = None
177                    else:
178                        if content_type == "application/x-ndjson":
179                            # for x-ndjson requests, we are expecting an array of elements
180                            # that need to be converted to a newline separated string
181                            request_body = "\n".join(json.dumps(element) for element in body)
182                        else:  # content_type == "application/json":
183                            request_body = json.dumps(body)
184                    r = self.pool_manager.request(
185                        method,
186                        url,
187                        body=request_body,
188                        preload_content=_preload_content,
189                        timeout=timeout,
190                        headers=headers,
191                    )
192
193                elif content_type == "application/x-www-form-urlencoded":  # noqa: E501
194                    r = self.pool_manager.request(
195                        method,
196                        url,
197                        fields=post_params,
198                        encode_multipart=False,
199                        preload_content=_preload_content,
200                        timeout=timeout,
201                        headers=headers,
202                    )
203                elif content_type == "multipart/form-data":
204                    # must del headers['Content-Type'], or the correct
205                    # Content-Type which generated by urllib3 will be
206                    # overwritten.
207                    del headers["Content-Type"]
208                    r = self.pool_manager.request(
209                        method,
210                        url,
211                        fields=post_params,
212                        encode_multipart=True,
213                        preload_content=_preload_content,
214                        timeout=timeout,
215                        headers=headers,
216                    )
217                # Pass a `string` parameter directly in the body to support
218                # other content types than Json when `body` argument is
219                # provided in serialized form
220                elif isinstance(body, str) or isinstance(body, bytes):
221                    request_body = body
222                    r = self.pool_manager.request(
223                        method,
224                        url,
225                        body=request_body,
226                        preload_content=_preload_content,
227                        timeout=timeout,
228                        headers=headers,
229                    )
230                else:
231                    # Cannot generate the request from given parameters
232                    msg = """Cannot prepare a request message for provided
233                             arguments. Please check that your arguments match
234                             declared content type."""
235                    raise PineconeApiException(status=0, reason=msg)
236            # For `GET`, `HEAD`
237            else:
238                if query_params:
239                    url += "?" + urlencode(query_params, quote_via=quote)
240                r = self.pool_manager.request(
241                    method, url, preload_content=_preload_content, timeout=timeout, headers=headers
242                )
243        except urllib3.exceptions.SSLError as e:
244            msg = "{0}\n{1}".format(type(e).__name__, str(e))
245            raise PineconeApiException(status=0, reason=msg)
246
247        if os.environ.get("PINECONE_DEBUG_CURL"):
248            o = RESTResponse(r.status, r.data, r.headers, r.reason)
249
250            if o.status <= 300:
251                print(bcolors.OKGREEN + o.data.decode("utf-8") + bcolors.ENDC)
252            else:
253                print(bcolors.FAIL + o.data.decode("utf-8") + bcolors.ENDC)
254
255        if _preload_content:
256            r = RESTResponse(r.status, r.data, r.headers, r.reason)
257
258            # log response body
259            logger.debug("response body: %s", r.data)
260
261        return raise_exceptions_or_return(r)

Helper class that provides a standard way to create an ABC using inheritance.

Urllib3RestClient( configuration: pinecone.openapi_support.configuration.Configuration, pools_size: int = 4, maxsize: Optional[int] = None)
35    def __init__(
36        self, configuration: Configuration, pools_size: int = 4, maxsize: Optional[int] = None
37    ) -> None:
38        # urllib3.PoolManager will pass all kw parameters to connectionpool
39        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
40        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
41        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
42        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501
43
44        # cert_reqs
45        if configuration.verify_ssl:
46            cert_reqs = ssl.CERT_REQUIRED
47        else:
48            cert_reqs = ssl.CERT_NONE
49
50        addition_pool_args = {}
51        if configuration.assert_hostname is not None:
52            addition_pool_args["assert_hostname"] = configuration.assert_hostname  # noqa: E501
53
54        if configuration.retries is not None:
55            addition_pool_args["retries"] = configuration.retries
56
57        if configuration.socket_options is not None:
58            addition_pool_args["socket_options"] = configuration.socket_options
59
60        if maxsize is None:
61            if configuration.connection_pool_maxsize is not None:
62                maxsize = configuration.connection_pool_maxsize
63            else:
64                maxsize = 4
65
66        # https pool manager
67        if configuration.proxy:
68            self.pool_manager = urllib3.ProxyManager(
69                num_pools=pools_size,
70                maxsize=maxsize,
71                cert_reqs=cert_reqs,
72                ca_certs=configuration.ssl_ca_cert,
73                cert_file=configuration.cert_file,
74                key_file=configuration.key_file,
75                proxy_url=configuration.proxy,
76                proxy_headers=configuration.proxy_headers,
77                **addition_pool_args,
78            )
79        else:
80            self.pool_manager = urllib3.PoolManager(
81                num_pools=pools_size,
82                maxsize=maxsize,
83                cert_reqs=cert_reqs,
84                ca_certs=configuration.ssl_ca_cert,
85                cert_file=configuration.cert_file,
86                key_file=configuration.key_file,
87                **addition_pool_args,
88            )
pool_manager: urllib3.poolmanager.PoolManager
def request( self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None):
 90    def request(
 91        self,
 92        method,
 93        url,
 94        query_params=None,
 95        headers=None,
 96        body=None,
 97        post_params=None,
 98        _preload_content=True,
 99        _request_timeout=None,
100    ):
101        """Perform requests.
102
103        :param method: http request method
104        :param url: http request url
105        :param query_params: query parameters in the url
106        :param headers: http request headers
107        :param body: request json body, for `application/json`
108        :param post_params: request post parameters,
109                            `application/x-www-form-urlencoded`
110                            and `multipart/form-data`
111        :param _preload_content: if False, the urllib3.HTTPResponse object will
112                                 be returned without reading/decoding response
113                                 data. Default is True.
114        :param _request_timeout: timeout setting for this request. If one
115                                 number provided, it will be total request
116                                 timeout. It can also be a pair (tuple) of
117                                 (connection, read) timeouts.
118        """
119        logger.debug("Calling urllib3 request()")
120        method = method.upper()
121        assert method in ["GET", "HEAD", "DELETE", "POST", "PUT", "PATCH", "OPTIONS"]
122
123        if os.environ.get("PINECONE_DEBUG_CURL"):
124            formatted_headers = " ".join(["-H '{0}: {1}'".format(k, v) for k, v in headers.items()])
125            formatted_query = urlencode(query_params)
126            if formatted_query:
127                formatted_url = f"{url}?{formatted_query}"
128            else:
129                formatted_url = url
130            if body is None:
131                print(
132                    bcolors.OKBLUE
133                    + "curl -X {method} '{url}' {formatted_headers}".format(
134                        method=method, url=formatted_url, formatted_headers=formatted_headers
135                    )
136                    + bcolors.ENDC
137                )
138            else:
139                formatted_body = json.dumps(body)
140                print(
141                    bcolors.OKBLUE
142                    + "curl -X {method} '{url}' {formatted_headers} -d '{data}'".format(
143                        method=method,
144                        url=formatted_url,
145                        formatted_headers=formatted_headers,
146                        data=formatted_body,
147                    )
148                    + bcolors.ENDC
149                )
150
151        if post_params and body:
152            raise PineconeApiValueError("body parameter cannot be used with post_params parameter.")
153
154        post_params = post_params or {}
155        headers = headers or {}
156
157        timeout = None
158        if _request_timeout:
159            if isinstance(_request_timeout, (int, float)):  # noqa: E501,F821
160                timeout = urllib3.Timeout(total=_request_timeout)
161            elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2:
162                timeout = urllib3.Timeout(connect=_request_timeout[0], read=_request_timeout[1])
163
164        try:
165            # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
166            if method in ["POST", "PUT", "PATCH", "OPTIONS", "DELETE"]:
167                # Only set a default Content-Type for POST, PUT, PATCH and OPTIONS requests
168                if (method != "DELETE") and ("Content-Type" not in headers):
169                    headers["Content-Type"] = "application/json"
170                if query_params:
171                    url += "?" + urlencode(query_params, quote_via=quote)
172
173                content_type = headers.get("Content-Type", "").lower()
174                if content_type == "" or ("json" in content_type):
175                    if body is None:
176                        request_body = None
177                    else:
178                        if content_type == "application/x-ndjson":
179                            # for x-ndjson requests, we are expecting an array of elements
180                            # that need to be converted to a newline separated string
181                            request_body = "\n".join(json.dumps(element) for element in body)
182                        else:  # content_type == "application/json":
183                            request_body = json.dumps(body)
184                    r = self.pool_manager.request(
185                        method,
186                        url,
187                        body=request_body,
188                        preload_content=_preload_content,
189                        timeout=timeout,
190                        headers=headers,
191                    )
192
193                elif content_type == "application/x-www-form-urlencoded":  # noqa: E501
194                    r = self.pool_manager.request(
195                        method,
196                        url,
197                        fields=post_params,
198                        encode_multipart=False,
199                        preload_content=_preload_content,
200                        timeout=timeout,
201                        headers=headers,
202                    )
203                elif content_type == "multipart/form-data":
204                    # must del headers['Content-Type'], or the correct
205                    # Content-Type which generated by urllib3 will be
206                    # overwritten.
207                    del headers["Content-Type"]
208                    r = self.pool_manager.request(
209                        method,
210                        url,
211                        fields=post_params,
212                        encode_multipart=True,
213                        preload_content=_preload_content,
214                        timeout=timeout,
215                        headers=headers,
216                    )
217                # Pass a `string` parameter directly in the body to support
218                # other content types than Json when `body` argument is
219                # provided in serialized form
220                elif isinstance(body, str) or isinstance(body, bytes):
221                    request_body = body
222                    r = self.pool_manager.request(
223                        method,
224                        url,
225                        body=request_body,
226                        preload_content=_preload_content,
227                        timeout=timeout,
228                        headers=headers,
229                    )
230                else:
231                    # Cannot generate the request from given parameters
232                    msg = """Cannot prepare a request message for provided
233                             arguments. Please check that your arguments match
234                             declared content type."""
235                    raise PineconeApiException(status=0, reason=msg)
236            # For `GET`, `HEAD`
237            else:
238                if query_params:
239                    url += "?" + urlencode(query_params, quote_via=quote)
240                r = self.pool_manager.request(
241                    method, url, preload_content=_preload_content, timeout=timeout, headers=headers
242                )
243        except urllib3.exceptions.SSLError as e:
244            msg = "{0}\n{1}".format(type(e).__name__, str(e))
245            raise PineconeApiException(status=0, reason=msg)
246
247        if os.environ.get("PINECONE_DEBUG_CURL"):
248            o = RESTResponse(r.status, r.data, r.headers, r.reason)
249
250            if o.status <= 300:
251                print(bcolors.OKGREEN + o.data.decode("utf-8") + bcolors.ENDC)
252            else:
253                print(bcolors.FAIL + o.data.decode("utf-8") + bcolors.ENDC)
254
255        if _preload_content:
256            r = RESTResponse(r.status, r.data, r.headers, r.reason)
257
258            # log response body
259            logger.debug("response body: %s", r.data)
260
261        return raise_exceptions_or_return(r)

Perform requests.

Parameters
  • method: http request method
  • url: http request url
  • query_params: query parameters in the url
  • headers: http request headers
  • body: request json body, for application/json
  • post_params: request post parameters, application/x-www-form-urlencoded and multipart/form-data
  • _preload_content: if False, the urllib3.HTTPResponse object will be returned without reading/decoding response data. Default is True.
  • _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts.