def get_normalized_params(params): """ Given a list of (k, v) parameters, returns a sorted, encoded normalized param """ return urlparse.urlencode(sorted(params))
def _request(self, endpoint=None, **kwargs): if endpoint is not None: # Handle undefined interfaces resource = self.interfaces.get(endpoint, {}) endpoint = endpoint.replace('.', '/') else: resource = self.interfaces endpoint = '/'.join(self.tree) for k in resource.get('required', []): if k not in (x.split(':')[0] for x in compat.iterkeys(kwargs)): raise ValueError('Missing required argument: %s' % k) method = kwargs.pop('method', resource.get('method')) if not method: raise InterfaceNotDefined( 'Interface is not defined, you must pass ``method`` (HTTP Method).') method = method.upper() if method not in ('GET', 'POST'): raise InvalidHTTPMethod(method) api = self.api version = kwargs.pop('version', api.version) format = kwargs.pop('format', api.format) formatter, formatter_error = api.formats[format] path = '/api/%s/%s.%s' % (version, endpoint, format) if 'api_secret' not in kwargs and api.secret_key: kwargs['api_secret'] = api.secret_key if 'api_public' not in kwargs and api.public_key: kwargs['api_key'] = api.public_key # We need to ensure this is a list so that # multiple values for a key work params = [] for k, v in compat.iteritems(kwargs): if isinstance(v, (list, tuple)): for val in v: params.append((k, val)) else: params.append((k, v)) headers = { 'User-Agent': 'disqus-python/%s' % __version__, 'Accept-Encoding': 'gzip', } if method == 'GET': path = '%s?%s' % (path, urllib.urlencode(params)) data = '' else: data = urllib.urlencode(params) headers['Content-Type'] = 'application/x-www-form-urlencoded' conn = httplib.HTTPSConnection(HOST, timeout=api.timeout) conn.request(method, path, data, headers) response = conn.getresponse() try: body = response.read() finally: # Close connection conn.close() if response.getheader('Content-Encoding') == 'gzip': # See: http://stackoverflow.com/a/2424549 body = zlib.decompress(body, 16 + zlib.MAX_WBITS) # Determine the encoding of the response and respect # the Content-Type header, but default back to utf-8 content_type = response.getheader('Content-Type') if content_type is None: encoding = DEFAULT_ENCODING else: try: encoding = CHARSET_RE.search(content_type).group(1) except AttributeError: encoding = DEFAULT_ENCODING body = body.decode(encoding) try: # Coerce response to Python data = formatter(body) except formatter_error: raise FormattingError(body) if response.status != 200: raise ERROR_MAP.get(data['code'], APIError)(data['code'], data['response']) if isinstance(data['response'], list): return Result(data['response'], data.get('cursor')) return data['response']
def _request(self, **kwargs): # Handle undefined interfaces resource = self.interface for k in resource.get('required', []): if k not in (x.split(':')[0] for x in compat.iterkeys(kwargs)): raise ValueError('Missing required argument: %s' % k) method = kwargs.pop('method', resource.get('method')) if not method: raise InterfaceNotDefined( 'Interface is not defined, you must pass ``method`` (HTTP Method).') api = self.api version = kwargs.pop('version', api.version) format = kwargs.pop('format', api.format) conn = httplib.HTTPSConnection(HOST, timeout=api.timeout) path = '/api/%s/%s.%s' % (version, '/'.join(self.tree), format) if 'api_secret' not in kwargs and api.secret_key: kwargs['api_secret'] = api.secret_key if 'api_public' not in kwargs and api.public_key: kwargs['api_key'] = api.public_key # We need to ensure this is a list so that # multiple values for a key work params = [] for k, v in compat.iteritems(kwargs): if isinstance(v, (list, tuple)): for val in v: params.append((k, val)) else: params.append((k, v)) headers = { 'User-Agent': 'disqus-python/%s' % __version__ } if method == 'GET': path = '%s?%s' % (path, urllib.urlencode(params)) data = '' else: data = urllib.urlencode(params) conn.request(method, path, data, headers) response = conn.getresponse() # Coerce response to Python data = api.formats[format](response.read()) # Close connection conn.close() if response.status != 200: raise ERROR_MAP.get(data['code'], APIError)(data['code'], data['response']) if isinstance(data['response'], list): return Result(data['response'], data.get('cursor')) return data['response']
def _request(self, endpoint=None, **kwargs): if endpoint is not None: # Handle undefined interfaces resource = self.interfaces.get(endpoint, {}) endpoint = endpoint.replace('.', '/') else: resource = self.interfaces endpoint = '/'.join(self.tree) for k in resource.get('required', []): if k not in (x.split(':')[0] for x in compat.iterkeys(kwargs)): raise ValueError('Missing required argument: %s' % k) method = kwargs.pop('method', resource.get('method')) if not method: raise InterfaceNotDefined( "Interface is not defined, you must pass ``method`` (GET or POST). Ex: disqus.get('trends.listThreads', method='GET')") method = method.upper() if method not in ('GET', 'POST'): raise InvalidHTTPMethod(method) api = self.api version = kwargs.pop('version', api.version) format = kwargs.pop('format', api.format) formatter, formatter_error = api.formats[format] path = '/api/%s/%s.%s' % (version, endpoint, format) if 'api_secret' not in kwargs and api.secret_key: kwargs['api_secret'] = api.secret_key if 'api_public' not in kwargs and api.public_key: kwargs['api_key'] = api.public_key # We need to ensure this is a list so that # multiple values for a key work params = [] for k, v in compat.iteritems(kwargs): if isinstance(v, (list, tuple)): for val in v: params.append((k, val)) else: params.append((k, v)) headers = { 'User-Agent': 'disqus-python/%s' % __version__, 'Accept-Encoding': 'gzip', } if method == 'GET': path = '%s?%s' % (path, urllib.urlencode(params)) data = '' else: data = urllib.urlencode(params) headers['Content-Type'] = 'application/x-www-form-urlencoded' conn = httplib.HTTPSConnection(HOST, timeout=api.timeout) conn.request(method, path, data, headers) response = conn.getresponse() try: body = response.read() finally: # Close connection conn.close() if response.getheader('Content-Encoding') == 'gzip': # See: http://stackoverflow.com/a/2424549 body = zlib.decompress(body, 16 + zlib.MAX_WBITS) # Determine the encoding of the response and respect # the Content-Type header, but default back to utf-8 content_type = response.getheader('Content-Type') if content_type is None: encoding = DEFAULT_ENCODING else: try: encoding = CHARSET_RE.search(content_type).group(1) except AttributeError: encoding = DEFAULT_ENCODING body = body.decode(encoding) try: # Coerce response to Python data = formatter(body) except formatter_error: raise FormattingError(body) if response.status != 200: raise ERROR_MAP.get(data['code'], APIError)(data['code'], data['response']) if isinstance(data['response'], list): return Result(data['response'], data.get('cursor')) return data['response']