def custom_method_request(cls, sid, **params): url = "%s/%s/%s" % ( cls.class_url(), quote_plus(util.utf8(sid)), http_path, ) return cls._static_request(http_verb, url, **params)
def request(self, method, url, headers, post_data=None, stream=False): if stream: raise NotImplementedError("Stream not yet implemented for {}".format(self)) b = util.io.BytesIO() rheaders = util.io.BytesIO() # Pycurl's design is a little weird: although we set per-request # options on this object, it's also capable of maintaining established # connections. Here we call reset() between uses to make sure it's in a # pristine state, but notably reset() doesn't reset connections, so we # still get to take advantage of those by virtue of re-using the same # object. self._curl.reset() proxy = self._get_proxy(url) if proxy: if proxy.hostname: self._curl.setopt(pycurl.PROXY, proxy.hostname) if proxy.port: self._curl.setopt(pycurl.PROXYPORT, proxy.port) if proxy.username or proxy.password: self._curl.setopt( pycurl.PROXYUSERPWD, "%s:%s" % (proxy.username, proxy.password) ) if method == "get": self._curl.setopt(pycurl.HTTPGET, 1) elif method == "post": self._curl.setopt(pycurl.POST, 1) self._curl.setopt(pycurl.POSTFIELDS, post_data) else: self._curl.setopt(pycurl.CUSTOMREQUEST, method.upper()) # pycurl doesn't like unicode URLs self._curl.setopt(pycurl.URL, util.utf8(url)) self._curl.setopt(pycurl.WRITEFUNCTION, b.write) self._curl.setopt(pycurl.HEADERFUNCTION, rheaders.write) self._curl.setopt(pycurl.NOSIGNAL, 1) self._curl.setopt(pycurl.CONNECTTIMEOUT, 30) self._curl.setopt(pycurl.TIMEOUT, 80) self._curl.setopt( pycurl.HTTPHEADER, ["%s: %s" % (k, v) for k, v in six.iteritems(dict(headers))], ) if self._verify_ssl_certs: self._curl.setopt(pycurl.CAINFO, openai.ca_bundle_path) else: self._curl.setopt(pycurl.SSL_VERIFYHOST, False) try: self._curl.perform() except pycurl.error as e: self._handle_request_error(e) rbody = b.getvalue().decode("utf-8") rcode = self._curl.getinfo(pycurl.RESPONSE_CODE) headers = self.parse_headers(rheaders.getvalue().decode("utf-8")) return rbody, rcode, headers, stream
def class_url(cls, engine=None): # Namespaces are separated in object names with periods (.) and in URLs # with forward slashes (/), so replace the former with the latter. base = cls.OBJECT_NAME.replace(".", "/") if engine is None: return "/%s/%ss" % (cls.api_prefix, base) engine = util.utf8(engine) extn = quote_plus(engine) return "/%s/engines/%s/%ss" % (cls.api_prefix, extn, base)
def retrieve(self, id, api_key=None, openai_version=None, openai_account=None, **params): url = "%s/%s" % (self.get("url"), quote_plus(util.utf8(id))) return self._request("get", url, api_key=api_key, openai_version=openai_version, openai_account=openai_account, **params)
def _api_encode(data): for key, value in six.iteritems(data): key = util.utf8(key) if value is None: continue elif hasattr(value, "openai_id"): yield (key, value.openai_id) elif isinstance(value, list) or isinstance(value, tuple): for i, sv in enumerate(value): if isinstance(sv, dict): subdict = _encode_nested_dict("%s[%d]" % (key, i), sv) for k, v in _api_encode(subdict): yield (k, v) else: yield ("%s[%d]" % (key, i), util.utf8(sv)) elif isinstance(value, dict): subdict = _encode_nested_dict(key, value) for subkey, subvalue in _api_encode(subdict): yield (subkey, subvalue) elif isinstance(value, datetime.datetime): yield (key, _encode_datetime(value)) else: yield (key, util.utf8(value))
def instance_url(self): id = self.get("id") if not isinstance(id, six.string_types): raise error.InvalidRequestError( "Could not determine which URL to request: %s instance " "has invalid ID: %r, %s. ID should be of type `str` (or" " `unicode`)" % (type(self).__name__, id, type(id)), "id", ) id = util.utf8(id) base = self.class_url() extn = quote_plus(id) return "%s/%s" % (base, extn)
def instance_url(self): id = self.get("id") if not isinstance(id, six.string_types): raise error.InvalidRequestError( "Could not determine which URL to request: %s instance " "has invalid ID: %r, %s. ID should be of type `str` (or" " `unicode`)" % (type(self).__name__, id, type(id)), "id", ) id = util.utf8(id) base = self.class_url(self.engine) extn = quote_plus(id) url = "%s/%s" % (base, extn) timeout = self.get("timeout") if timeout is not None: timeout = quote_plus(str(timeout)) url += "?timeout={}".format(timeout) return url
def class_url(cls, engine): assert engine is not None engine = util.utf8(engine) extn = quote_plus(engine) return "/%s/engines/%s/branches" % (cls.api_prefix, extn)
def _cls_delete(cls, sid, **params): url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid))) return cls._static_request("delete", url, **params)
def modify(cls, sid, **params): url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid))) return cls._static_request("post", url, **params)