def valid_sign(self, connect_response: WykopConnectLoginInfo) -> bool: secret_bytes = force_bytes(self.credentials.secretkey) app_key_bytes = force_bytes(connect_response.app_key) login_bytes = force_bytes(connect_response.login) token_bytes = force_bytes(connect_response.token) to_hash = secret_bytes + app_key_bytes + login_bytes + token_bytes return hashlib.md5(to_hash).hexdigest() == connect_response.sign
def get_api_sign(self, url, **post_params): """ Gets request api sign. """ post_params_values = self.get_post_params_values(**post_params) post_params_values_str = ",".join(post_params_values) post_params_values_bytes = force_bytes(post_params_values_str) url_bytes = force_bytes(url) secretkey_bytes = force_bytes(self.secretkey) return hashlib.md5(secretkey_bytes + url_bytes + post_params_values_bytes).hexdigest()
def get_api_sign(self, url, **post_params): """ Gets request api sign. """ post_params_values = self.get_post_params_values(**post_params) post_params_values_str = ",".join(post_params_values) post_params_values_bytes = force_bytes(post_params_values_str) url_bytes = force_bytes(url) secretkey_bytes = force_bytes(self.secretkey) return hashlib.md5( secretkey_bytes + url_bytes + post_params_values_bytes).hexdigest()
def test_exception(self): error_msg = u('cze\u015b\u0107') exc = ValueError(error_msg) result = force_bytes(exc) assert result == error_msg.encode('utf8')
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 get_connect_named_params(self, redirect_url=None): """ Gets request api parameters for wykop connect. """ apisign = self.get_api_sign(redirect_url) named_params = { 'secure': apisign, } if redirect_url is not None: redirect_url_bytes = force_bytes(redirect_url) redirect_url_encoded = quote_plus( base64.b64encode(redirect_url_bytes)) named_params.update({ 'redirect': redirect_url_encoded, }) return named_params
def get_connect_api_params(self, redirect_url=None): """ Gets request api parameters for wykop connect. """ apisign = self.get_api_sign(redirect_url) api_params = { 'secure': apisign, } if redirect_url is not None: redirect_url_bytes = force_bytes(redirect_url) redirect_url_encoded = quote_plus( base64.b64encode(redirect_url_bytes)) api_params.update({ 'redirect': redirect_url_encoded, }) return api_params
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_message(self, message): encoding = getattr(sys.stdout, 'encoding', 'utf-8') return force_bytes(message, encoding)
def test_encoding_ignore(self): msg = u('cze\u015b\u0107') result = force_bytes(msg, encoding='ascii', errors='ignore') assert result == b('cze')
def test_encoding(self): msg = u('cze\u015b\u0107') result = force_bytes(msg) assert result == b('cze\xc5\x9b\xc4\x87')
def test_date(self): date = datetime.date(2017, 9, 5) result = force_bytes(date) assert result == b'2017-09-05'
def parse_wykop_connect_response(self, response) -> WykopConnectLoginInfo: decoded_response = base64.b64decode( force_bytes(response)).decode('utf-8') field_dict = json.loads(decoded_response) return WykopConnectLoginInfo(**field_dict)