From c3f7cecadf45ab60880d0cd2d235ee5bd20732a9 Mon Sep 17 00:00:00 2001 From: aldenhu Date: Tue, 26 Apr 2022 13:21:27 -0700 Subject: [PATCH] [tutorial] update python tutorial with get_resource and get_table_item APIs --- .../static/examples/python/first_nft.py | 39 ++++++++++++------- .../examples/python/first_transaction.py | 15 +++---- .../examples/python/hello_blockchain.py | 7 +--- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/developer-docs-site/static/examples/python/first_nft.py b/developer-docs-site/static/examples/python/first_nft.py index c735dedbb661f..089ec32bc4eb5 100644 --- a/developer-docs-site/static/examples/python/first_nft.py +++ b/developer-docs-site/static/examples/python/first_nft.py @@ -3,6 +3,7 @@ # Copyright (c) Aptos # SPDX-License-Identifier: Apache-2.0 +import requests from typing import Any, Dict import sys @@ -121,23 +122,33 @@ def cancel_token_offer( self.submit_transaction_helper(account, payload) #:!:>section_3 + def table_item(self, handle: str, key_type: str, value_type: str, key: Any) -> Any: + response = requests.post(f"{self.url}/tables/{handle}/item", json={ + "key_type": key_type, + "value_type": value_type, + "key": key, + }) + assert response.status_code == 200, response.text + return response.json() + def get_token_id(self, creator: str, collection_name: str, token_name: str) -> int: """ Retrieve the token's creation_num, which is useful for non-creator operations """ - resources = self.account_resources(creator) - collections = [] - tokens = [] - for resource in resources: - if resource["type"] == f"0x1::Token::Collections": - collections = resource["data"]["collections"]["data"] - for collection in collections: - if collection["key"] == collection_name: - tokens = collection["value"]["tokens"]["data"] - for token in tokens: - if token["key"] == token_name: - return int(token["value"]["id"]["creation_num"]) - - assert False + collections = self.account_resource(creator, "0x1::Token::Collections") + collection = self.table_item( + collections["data"]["collections"]["handle"], + "0x1::ASCII::String", + "0x1::Token::Collection", + collection_name, + ) + token_data = self.table_item( + collection["tokens"]["handle"], + "0x1::ASCII::String", + "0x1::Token::TokenData", + token_name, + ) + return token_data["id"]["creation_num"] + #<:!:section_3 diff --git a/developer-docs-site/static/examples/python/first_transaction.py b/developer-docs-site/static/examples/python/first_transaction.py index a823a80a7cc60..66568a7b2fb5c 100644 --- a/developer-docs-site/static/examples/python/first_transaction.py +++ b/developer-docs-site/static/examples/python/first_transaction.py @@ -56,10 +56,10 @@ def account(self, account_address: str) -> Dict[str, str]: assert response.status_code == 200, f"{response.text} - {account_address}" return response.json() - def account_resources(self, account_address: str) -> Dict[str, Any]: - """Returns all resources associated with the account""" - - response = requests.get(f"{self.url}/accounts/{account_address}/resources") + def account_resource(self, account_address: str, resource_type: str) -> Optional[Dict[str, Any]]: + response = requests.get(f"{self.url}/accounts/{account_address}/resource/{resource_type}") + if response.status_code == 404: + return None assert response.status_code == 200, response.text return response.json() #<:!:section_3 @@ -125,12 +125,7 @@ def wait_for_transaction(self, txn_hash: str) -> None: #:!:>section_5 def account_balance(self, account_address: str) -> Optional[int]: """Returns the test coin balance associated with the account""" - - resources = self.account_resources(account_address) - for resource in resources: - if resource["type"] == "0x1::TestCoin::Balance": - return int(resource["data"]["coin"]["value"]) - return None + return self.account_resource(account_address, "0x1::TestCoin::Balance") def transfer(self, account_from: Account, recipient: str, amount: int) -> str: """Transfer a given coin amount from a given Account to the recipient's account address. diff --git a/developer-docs-site/static/examples/python/hello_blockchain.py b/developer-docs-site/static/examples/python/hello_blockchain.py index a0e2572167565..8c54ef2468a82 100644 --- a/developer-docs-site/static/examples/python/hello_blockchain.py +++ b/developer-docs-site/static/examples/python/hello_blockchain.py @@ -28,12 +28,7 @@ def publish_module(self, account_from: Account, module_hex: str) -> str: #:!:>section_2 def get_message(self, contract_address: str, account_address: str) -> Optional[str]: """ Retrieve the resource Message::MessageHolder::message """ - - resources = self.account_resources(account_address) - for resource in resources: - if resource["type"] == f"0x{contract_address}::Message::MessageHolder": - return resource["data"]["message"] - return None + return self.account_resource(account_address, f"0x{contract_address}::Message::MessageHolder") #<:!:section_2 #:!:>section_3