本文整理汇总了Python中munch.Munch.open_block方法的典型用法代码示例。如果您正苦于以下问题:Python Munch.open_block方法的具体用法?Python Munch.open_block怎么用?Python Munch.open_block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类munch.Munch
的用法示例。
在下文中一共展示了Munch.open_block方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_static_price
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
def test_static_price(
empty_proxy: PaywalledProxy,
api_endpoint_address: str,
client: Client,
wait_for_blocks
):
proxy = empty_proxy
endpoint_url = "http://" + api_endpoint_address
proxy.add_paywalled_resource(StaticPriceResource, '/resource', 3)
# test GET
response = requests.get(endpoint_url + '/resource')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 3
channel = client.get_suitable_channel(headers.receiver_address, int(headers.price) * 4)
wait_for_blocks(6)
channel.update_balance(int(headers.price))
headers = Munch()
headers.balance = str(channel.balance)
headers.balance_signature = encode_hex(channel.balance_sig)
headers.sender_address = channel.sender
headers.open_block = str(channel.block)
headers = HTTPHeaders.serialize(headers)
response = requests.get(endpoint_url + '/resource', headers=headers)
assert response.status_code == 200
assert response.text.strip() == 'GET'
assert_method(requests.post, endpoint_url + '/resource', headers, channel, 'POST')
assert_method(requests.put, endpoint_url + '/resource', headers, channel, 'PUT')
assert_method(requests.delete, endpoint_url + '/resource', headers, channel, 'DEL')
示例2: test_explicit_json
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
def test_explicit_json(
empty_proxy: PaywalledProxy,
api_endpoint_address: str,
client: Client,
wait_for_blocks
):
proxy = empty_proxy
endpoint_url = "http://" + api_endpoint_address
proxy.add_paywalled_resource(JSONResource, '/resource', 3)
# test GET
response = requests.get(endpoint_url + '/resource')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 3
channel = client.get_suitable_channel(headers.receiver_address, int(headers.price) * 4)
wait_for_blocks(6)
channel.update_balance(int(headers.price))
headers = Munch()
headers.balance = str(channel.balance)
headers.balance_signature = encode_hex(channel.balance_sig)
headers.sender_address = channel.sender
headers.open_block = str(channel.block)
headers = HTTPHeaders.serialize(headers)
response = requests.get(endpoint_url + '/resource', headers=headers)
assert response.status_code == 200
# If headers don't merge properly, this results in 'application/json,application/json'.
assert response.headers['Content-Type'] == 'application/json'
assert response.json() == {'GET': 1}
示例3: test_method_price
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
def test_method_price(
empty_proxy: PaywalledProxy,
api_endpoint_address: str,
client: Client,
wait_for_blocks
):
proxy = empty_proxy
endpoint_url = "http://" + api_endpoint_address
proxy.add_paywalled_resource(
DynamicMethodResource,
'/resource',
resource_class_args=(42,),
resource_class_kwargs={'bar': 9814072356})
# test GET
response = requests.get(endpoint_url + '/resource')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 1
channel = client.get_suitable_channel(headers.receiver_address, 1 + 2 + 3 + 4)
wait_for_blocks(6)
channel.update_balance(int(headers.price))
headers = Munch()
headers.balance = str(channel.balance)
headers.balance_signature = encode_hex(channel.balance_sig)
headers.sender_address = channel.sender
headers.open_block = str(channel.block)
headers = HTTPHeaders.serialize(headers)
response = requests.get(endpoint_url + '/resource', headers=headers)
assert response.status_code == 200
assert response.text.strip() == 'GET'
assert_method(requests.post,
endpoint_url + '/resource',
headers, channel, 'POST',
expected_price=2)
assert_method(requests.put,
endpoint_url + '/resource',
headers, channel, 'PUT',
expected_price=3)
assert_method(requests.delete,
endpoint_url + '/resource',
headers, channel, 'DEL',
expected_price=4)
示例4: assert_method
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
def assert_method(method, url, headers, channel, expected_reply, expected_price=3):
# test POST
response = method(url, headers=headers)
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == expected_price
channel.update_balance(int(headers.sender_balance) + int(headers.price))
headers = Munch()
headers.balance = str(channel.balance)
headers.balance_signature = encode_hex(channel.balance_sig)
headers.sender_address = channel.sender
headers.open_block = str(channel.block)
headers = HTTPHeaders.serialize(headers)
response = method(url, headers=headers)
assert response.status_code == 200
assert response.text.strip() == expected_reply
示例5: _request_resource
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
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)
示例6: test_dynamic_price
# 需要导入模块: from munch import Munch [as 别名]
# 或者: from munch.Munch import open_block [as 别名]
def test_dynamic_price(
empty_proxy: PaywalledProxy,
api_endpoint_address: str,
client: Client,
wait_for_blocks
):
proxy = empty_proxy
endpoint_url = "http://" + api_endpoint_address
price_cycle = cycle([1, 2, 3, 4, 5])
url_to_price = {} # type: Dict
def price_fn():
url = request.path
if int(url.split('_')[-1]) == 0:
return 0
if url in url_to_price:
price = url_to_price[url]
else:
price = next(price_cycle)
url_to_price[url] = price
return price
proxy.add_paywalled_resource(
DynamicPriceResource,
'/resource_<int:res_id>',
price=price_fn
)
response = requests.get(endpoint_url + '/resource_3')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 1
response = requests.get(endpoint_url + '/resource_5')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 2
response = requests.get(endpoint_url + '/resource_2')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 3
response = requests.get(endpoint_url + '/resource_3')
assert response.status_code == 402
headers = HTTPHeaders.deserialize(response.headers)
assert int(headers.price) == 1
response = requests.get(endpoint_url + '/resource_0')
assert response.status_code == 200
assert response.text.strip() == '0'
channel = client.get_suitable_channel(headers.receiver_address, 2)
wait_for_blocks(6)
channel.update_balance(2)
headers = Munch()
headers.balance = str(channel.balance)
headers.balance_signature = encode_hex(channel.balance_sig)
headers.sender_address = channel.sender
headers.open_block = str(channel.block)
headers = HTTPHeaders.serialize(headers)
response = requests.get(endpoint_url + '/resource_5', headers=headers)
assert response.status_code == 200
assert response.text.strip() == '5'