def _request_resource(self): """ Performs a simple GET request to the HTTP server with headers representing the given channel state. """ headers = Munch() headers.contract_address = self.client.channel_manager_address if self.channel: headers.balance = str(self.channel.balance) headers.balance_signature = encode_hex(self.channel.balance_sig) headers.sender_address = self.channel.sender headers.receiver_address = self.channel.receiver headers.open_block = str(self.channel.block) url = self.make_url(self.requested_resource) response = requests.get(url, headers=HTTPHeaders.serialize(headers)) headers = HTTPHeaders.deserialize(response.headers) if response.status_code == requests.codes.OK: self.on_success(response.content, headers.get('cost')) return response.content elif response.status_code == requests.codes.PAYMENT_REQUIRED: if 'insuf_confs' in headers: self.on_insufficient_confirmations() elif 'insuf_funds' in headers: self.on_insufficient_funds() else: balance = int(headers.sender_balance ) if 'sender_balance' in headers else None balance_sig = decode_hex(headers.balance_signature) \ if 'balance_signature' in headers else None self.on_payment_requested(headers.receiver_address, int(headers.price), balance, balance_sig, headers.get('contract_address'))
def _request_resource(self, method: str, url: str, **kwargs) -> Tuple[Any, bool]: """ Performs a simple GET request to the HTTP server with headers representing the given channel state. """ headers = Munch() headers.contract_address = self.client.channel_manager_address channel = self.get_channel(url) if channel: headers.balance = str(channel.balance) headers.balance_signature = encode_hex(channel.balance_sig) headers.sender_address = channel.sender headers.receiver_address = channel.receiver headers.open_block = str(channel.block) headers = HTTPHeaders.serialize(headers) if 'headers' in kwargs: headers.update(kwargs['headers']) kwargs['headers'] = headers else: kwargs['headers'] = headers response = requests.request(method, url, **kwargs) response_headers = HTTPHeaders.deserialize(response.headers) if self.on_http_response(method, url, response, **kwargs) is False: return None, False # user requested abort if response.status_code == requests.codes.OK: return response, self.on_success(method, url, response, **kwargs) elif response.status_code == requests.codes.PAYMENT_REQUIRED: if 'insuf_confs' in response_headers: return None, self.on_insufficient_confirmations( method, url, response, **kwargs) elif 'insuf_funds' in response_headers: return None, self.on_insufficient_funds( method, url, response, **kwargs) elif 'contract_address' not in response_headers or not is_same_address( response_headers.contract_address, self.client.channel_manager_address): return None, self.on_invalid_contract_address( method, url, response, **kwargs) elif 'invalid_amount' in response_headers: return None, self.on_invalid_amount(method, url, response, **kwargs) else: return None, self.on_payment_requested(method, url, response, **kwargs) else: return None, self.on_http_error(method, url, response, **kwargs)
def _request_resource(self, method: str, url: str, **kwargs) -> Tuple[Union[None, Response], bool]: """ Performs a simple GET request to the HTTP server with headers representing the given channel state. """ headers = Munch() headers.contract_address = self.client.context.channel_manager.address if self.channel is not None: headers.balance = str(self.channel.balance) headers.balance_signature = encode_hex(self.channel.balance_sig) headers.sender_address = self.channel.sender headers.receiver_address = self.channel.receiver headers.open_block = str(self.channel.block) headers.round_number = str(self.channel.last_round_signed) headers = HTTPHeaders.serialize(headers) if 'headers' in kwargs: headers.update(kwargs['headers']) kwargs['headers'] = headers else: kwargs['headers'] = headers response = requests.Session.request(self, method, url, **kwargs) if self.on_http_response(method, url, response, **kwargs) is False: return response, False # user requested abort if response.status_code == requests.codes.OK: return response, self.on_success(method, url, response, **kwargs) elif response.status_code == requests.codes.PAYMENT_REQUIRED: if HTTPHeaders.NONEXISTING_CHANNEL in response.headers: return response, self.on_nonexisting_channel( method, url, response, **kwargs) elif HTTPHeaders.INSUF_CONFS in response.headers: return response, self.on_insufficient_confirmations( method, url, response, **kwargs) elif HTTPHeaders.INVALID_PROOF in response.headers: return response, self.on_invalid_balance_proof( method, url, response, **kwargs) elif HTTPHeaders.CONTRACT_ADDRESS not in response.headers or not is_same_address( response.headers.get(HTTPHeaders.CONTRACT_ADDRESS), self.client.context.channel_manager.address): return response, self.on_invalid_contract_address( method, url, response, **kwargs) elif HTTPHeaders.INVALID_AMOUNT in response.headers: return response, self.on_invalid_amount( method, url, response, **kwargs) else: return response, self.on_payment_requested( method, url, response, **kwargs) else: return response, self.on_http_error(method, url, response, **kwargs)
def _request_resource( self, method: str, url: str, **kwargs ) -> Tuple[Union[None, Response], bool]: """ Performs a simple GET request to the HTTP server with headers representing the given channel state. """ headers = Munch() headers.contract_address = self.client.context.channel_manager.address if self.channel is not None: headers.balance = str(self.channel.balance) headers.balance_signature = encode_hex(self.channel.balance_sig) headers.sender_address = self.channel.sender headers.receiver_address = self.channel.receiver headers.open_block = str(self.channel.block) headers = HTTPHeaders.serialize(headers) if 'headers' in kwargs: headers.update(kwargs['headers']) kwargs['headers'] = headers else: kwargs['headers'] = headers response = requests.Session.request(self, method, url, **kwargs) if self.on_http_response(method, url, response, **kwargs) is False: return response, False # user requested abort if response.status_code == requests.codes.OK: return response, self.on_success(method, url, response, **kwargs) elif response.status_code == requests.codes.PAYMENT_REQUIRED: if HTTPHeaders.NONEXISTING_CHANNEL in response.headers: return response, self.on_nonexisting_channel(method, url, response, **kwargs) elif HTTPHeaders.INSUF_CONFS in response.headers: return response, self.on_insufficient_confirmations( method, url, response, **kwargs ) elif HTTPHeaders.INVALID_PROOF in response.headers: return response, self.on_invalid_balance_proof(method, url, response, **kwargs) elif HTTPHeaders.CONTRACT_ADDRESS not in response.headers or not is_same_address( response.headers.get(HTTPHeaders.CONTRACT_ADDRESS), self.client.context.channel_manager.address ): return response, self.on_invalid_contract_address(method, url, response, **kwargs) elif HTTPHeaders.INVALID_AMOUNT in response.headers: return response, self.on_invalid_amount(method, url, response, **kwargs) else: return response, self.on_payment_requested(method, url, response, **kwargs) else: return response, self.on_http_error(method, url, response, **kwargs)