def request(self, rtype, rmethod, rmethod_params=None, api_params=None, post_params=None, file_params=None, parser=default_parser, requester=default_requester): """ Makes request. """ log.debug('Making request') rmethod_params = rmethod_params or [] api_params = api_params or {} post_params = post_params or {} file_params = file_params or {} # sanitize data rtype = force_text(rtype) rmethod = force_text(rmethod) post_params = dictmap(force_bytes, post_params) api_params = dictmap(force_text, api_params) url = self.construct_url(rtype, rmethod, *rmethod_params, **api_params) headers = self.get_headers(url, **post_params) response = requester.make_request(url, post_params, headers, file_params) if parser is None: return response return parser.parse(response)
def request(self, rtype, rmethod=None, named_params=None, api_params=None, post_params=None, file_params=None): log.debug('Making request') post_params = sort_and_remove_none_values(post_params) file_params = file_params or {} rtype = force_text(rtype) rmethod = rmethod and force_text(rmethod) post_params = dictmap(force_bytes, post_params) url = self.construct_url(rtype=rtype, rmethod=rmethod, api_params=api_params, named_params=named_params) headers = self.headers(url, **post_params) response = self.requester.make_request(url, post_params, headers, file_params) if self.parser is None: return response return self.parser.parse_response(response)
def request(self, rtype, rmethod=None, api_params=None, post_params=None, file_params=None, parser=default_parser, requester=default_requester): """ Makes request. """ log.debug('Making request') api_params = api_params or {} post_params = post_params or {} file_params = file_params or {} # sanitize data rtype = force_text(rtype) rmethod = rmethod and force_text(rmethod) post_params = dictmap(force_bytes, post_params) api_params = dictmap(force_text, api_params) url = self.construct_url(rtype, rmethod, **api_params) headers = self.get_headers(url, **post_params) response = requester.make_request( url, post_params, headers, file_params) if parser is None: return response return parser.parse(response)
def get_post_params_values(self, **post_params): """ Gets post parameters values list. Required to api sign. """ return [ force_text(post_params[key]) for key in sorted(post_params.keys()) ]
def make_request(self, url, data=None, headers=None, files=None): log.debug( " Fetching url: `%s` (data: %s, headers: `%s`)", str(url), str(data), str(headers), ) if files: raise NotImplementedError( "Install requests package to send files.") if headers is None: headers = {} data_bytes = force_bytes(urlencode(data)) if data else None req = Request(url, data=data_bytes, headers=headers) try: with contextlib.closing(urlopen(req)) as resp: return force_text(resp.read()) except HTTPError as ex: raise WykopAPIError(0, str(ex.code)) except URLError as ex: raise WykopAPIError(0, str(ex.reason))
def get_connect_data(self, data, parser=default_parser): """ Gets decoded data from wykop connect. """ data_bytes = force_bytes(data) decoded = base64.decodestring(data_bytes) decoded_str = force_text(decoded) parsed = parser.parse(decoded_str) return parsed['appkey'], parsed['login'], parsed['token']
def make_request(self, url, data=None, headers=None, files=None): log.debug(" Fetching url: `%s` (data: %s, headers: `%s`)" % (str(url), str(data), str(headers))) try: files = self._get_files(files) method = self._get_method(data, files) resp = request(method, url, data=data, headers=headers, files=files) resp.raise_for_status() return force_text(resp.content) except RequestException as e: raise WykopAPIError(0, str(e))
def request(self, rtype, rmethod=None, named_params=None, api_params=None, post_params=None, file_params=None): log.debug('Making request') named_params = named_params or {} api_params = api_params or [] post_params = OrderedDict( {k: v for k, v in sorted(post_params.items()) if v} if post_params else {}) file_params = file_params or {} rtype = force_text(rtype) rmethod = rmethod and force_text(rmethod) post_params = dictmap(force_bytes, post_params) named_params = dictmap(force_text, named_params) url = self.construct_url(rtype=rtype, rmethod=rmethod, api_params=api_params, named_params=named_params) headers = self.headers(url, **post_params) response = self.requester.make_request(url, post_params, headers, file_params) if self.parser is None: return response return self.parser.parse(response)
def make_request(self, url, data=None, headers=None, files=None): log.debug( " Fetching url: `%s` (data: %s, headers: `%s`)", str(url), str(data), str(headers), ) try: files = self._get_files(files) method = self._get_method(data, files) resp = request(method, url, data=data, headers=headers, files=files) resp.raise_for_status() return force_text(resp.content) except RequestException as ex: raise WykopAPIError(0, str(ex))
def make_request(self, url, data=None, headers=None, files=None): log.debug(" Fetching url: `%s` (data: %s, headers: `%s`)" % (str(url), str(data), str(headers))) if files: raise NotImplementedError( "Install requests package to send files.") if headers is None: headers = {} data_bytes = force_bytes(urlencode(data)) if data else None req = Request(url, data=data_bytes, headers=headers) try: with contextlib.closing(urlopen(req)) as f: return force_text(f.read()) except HTTPError as e: raise WykopAPIError(0, str(e.code)) except URLError as e: raise WykopAPIError(0, str(e.reason))
def get_post_params_values(self, **post_params): """ Gets post parameters values list. Required to api sign. """ return [force_text(post_params[key]) for key in sorted(post_params.keys())]
def test_decoding_ignore(self): msg = b('cze\xc5\x9b\xc4\x87') result = force_text(msg, encoding='ascii', errors='ignore') assert result == u('cze')
def test_decoding(self): msg = b('cze\xc5\x9b\xc4\x87') result = force_text(msg) assert result == u('cze\u015b\u0107')
def test_text(self): result = force_text('test') assert result == 'test'