rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
127 stars 26 forks source link

Retrieving structured data #29

Closed jbroudou closed 2 years ago

jbroudou commented 2 years ago

Maybe I'm doing this wrong, but when calling a method that returns a list such as below, the response comes back as an unstructured list

record = Types.Record({
    'include_reward_status':Types.Vec(Types.Int32),
    'before_proposal':Types.Null,
    'limit':Types.Nat32,
    'exclude_topic':Types.Vec(Types.Int32),
    'include_status':Types.Vec(Types.Int32),
})
params = [
    {
        'type': record, 
        'value':{
            'include_reward_status': [], 
            'before_proposal': None, 
            'limit': 100, 
            'exclude_topic': [], 
            'include_status': [1]
        }
}]

res = agent.query_raw("rrkah-fqaaa-aaaaa-aaaaq-cai", "list_proposals", encode(params))
pprint(res)

results in

[{'type': 3871088705,
  'value': {'_3720987355': [{'_100394802': 1,
                             '_1120297033': 1644281103,
                             '_1138543385': 0,
                             '_1380048431': [1644626703],
                             '_1453869204': 0,
                             '_1659864270': 100000000,
                             '_2084260468': [{'_24641': 83739340113,
                                              '_338842564': 40551870688681598,
                                              '_4174818006': 1644301268,
                                              '_6039847': 16091166445927}],
                             '_2139208002': 1,
                             '_23515': [{'_23515': 43574}],
                             '_2756235859': 0,
                             '_3000310834': [{'_2162756390': '## Proposal to '
                                                             'Upgrade the '
                                                             'Registry '
                                                             'Canister\n'
                                                             '\n'
                                                             '### Proposer: '
                                                             'Dfinity '
                                                             'Foundation\n'
                                                             '### Git Hash: '
                                                             'ed721ffdde4b1d4d982d0c843a0b9e293380e05b\n'
                                                             '### New Wasm '
                                                             'Hash: '
                                                             '3cc60f5bee9f555258a5bfb648ff16e7441bc4920383c1127c9bd564eccfc848\n'
                                                             '### Target '
                                                             'canister: '
                                                             'rwlgt-iiaaa-aaaaa-aaaaa-cai\n'
                                                             '\n'
                                                             '---\n'
                                                             '## Release '
                                                             'Notes\n'
                                                             '\n'
                                                             '2c560f3cf2c5e36bdbf5edbd148cb28f72db24b7 '
                                                             'Merge branch '
                                                             "'egeyar/2022-02-02/Add-removed-fields-back-to-BlessReplicaVersionPayload' "

Is there a way to get this library to return a dictionary with indexed items corresponding to what I see here?

https://bmht6-iiaaa-aaaad-qabeq-cai.raw.ic0.app/principal/rrkah-fqaaa-aaaaa-aaaaq-cai

ccyanxyz commented 2 years ago

This would require parsing the canister's candid interface, this feature will be added in the near future

Myse1f commented 2 years ago

Forn now, you can provide return type to query_raw in order to get corresponding return type. e.g.

retType = Types.Vec(Types.Nat64);
res = agent.query_raw("rrkah-fqaaa-aaaaa-aaaaq-cai", "get_neuron_ids", encode([]), retType)

and got

[{'type': 'vec (nat64)', 'value': []}]

Candid parsing is WIP.

ccyanxyz commented 2 years ago

Candid parsing already supported on the main branch