Skip to content

Commit

Permalink
[tutorial] update python tutorial with get_resource and get_table_ite…
Browse files Browse the repository at this point in the history
…m APIs
  • Loading branch information
msmouse authored and gregnazario committed Apr 27, 2022
1 parent f8c5d63 commit c3f7cec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
39 changes: 25 additions & 14 deletions developer-docs-site/static/examples/python/first_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) Aptos
# SPDX-License-Identifier: Apache-2.0

import requests
from typing import Any, Dict
import sys

Expand Down Expand Up @@ -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


Expand Down
15 changes: 5 additions & 10 deletions developer-docs-site/static/examples/python/first_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3f7cec

Please sign in to comment.