def retrieve(self, id, **params): base = self.get('url') id = util.utf8(id) extn = quote_plus(id) url = "%s/%s" % (base, extn) return self.request('get', url, params)
def _build_instance_url(cls, sid): if not sid: return "/v1/accounts/me/" sid = util.utf8(sid) base = cls.class_url() extn = quote_plus(sid) return "%s/%s" % (base, extn)
def request(self, method, url, headers, post_data=None): 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, os.path.join(os.path.dirname(__file__), 'data/ca-certificates.crt')) 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
def _build_instance_url(cls, sid): if not sid: return cls.class_url() base = cls.class_logs_url() sid = util.utf8(sid) extn = quote_plus(sid) if sid is not None: extn = extn + "/" return "%s/%s" % (base, extn)
def _build_instance_url(cls, sid): if not sid: return "/v1/accounts/customers/" base = "/v1/accounts/customer" sid = util.utf8(sid) extn = quote_plus(sid) if sid is not None: extn = extn + "/" return "%s/%s" % (base, extn)
def _build_instance_url(cls, sid): if not sid: return "/v1/plans/subscriptions/" base = cls.class_url() sid = util.utf8(sid) extn = quote_plus(sid) if sid is not None: extn = extn + "/" return "%s/%s" % (base, extn)
def delete(self, **params): base = '/v1/plans/un-subscribe' sid = self.get('subscription_id') sid = util.utf8(sid) extn = quote_plus(sid) if sid is not None: extn = extn + "/" url = "%s/%s" % (base, extn) self.refresh_from(self.request('delete', url, params)) return self
def _api_encode(data): for key, value in six.iteritems(data): key = util.utf8(key) if value is None: continue elif isinstance(value, list) or isinstance(value, tuple): for sv in value: if isinstance(sv, dict): subdict = _encode_nested_dict(key, sv, fmt='%s[][%s]') for k, v in _api_encode(subdict): yield (k, v) else: yield ("%s[]" % (key, ), 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) if id is not None: extn = extn + "/" return "%s%s" % (base, extn)