Loading account balance with the 3commas API using python?

Viewed 538

I am trying to get my account balance with the python 3commas API unwrapper. I listed my code below. I think im getting stuck at the action_id part because i am not sure what to put there. With the current code i get all kinds of exceptions like response_json.get('error'), response_json.get('error_description'))}, None UnboundLocalError: local variable 'response_json' referenced before assignment.

This is the code:

from py3cw.request import Py3CW

p3cw = Py3CW(
    key='my key', 
    secret='my secret key',
    )

data = p3cw.request(
    entity='accounts',
    action='load_balances',
    action_id='POST /ver1/accounts/{number or name?}/'
    )

print (data);

Like i mentioned i also tried action_id='number or name?'. I also tried to change action_id to account_id.

I used this site btw: https://github.com/bogdanteodoru/py3cw

I really hope someone is able to help me :)

2 Answers

looks like your problem is your action_id parameter. You need to get your account id number. To get that number you can use a function like this:

def connected_accounts(p3cw):
error, data = p3cw.request(
    entity='accounts',
    action= ''
)
if error == {}:
    return data
else:
    raise(Exception(error['msg']))

That function will return data as a list of dictionary for every account. To look for the account id number just print data.

Then you can run your code with the action_id = account_id_number, i used a function like this:

def load_balance(p3cw, account_id):
error, data = p3cw.request(
    entity='accounts',
    action='load_balances',
    action_id=str(account_id),
)
if error == {}:
    return data
else:
    raise(Exception(error['msg']))

You just need to get the account id to use as the action_id parameter. Here's a working example:

from py3cw.request import Py3CW

p3cw = Py3CW(
    key='KEY', 
    secret='SECRET',
    )

def connected_accounts():
    error, accountData = p3cw.request(
        entity='accounts',
        action= ''
    )
    if error == {}:
        return accountData
    else:
        raise(Exception(error['msg']))

account_id = connected_accounts()[0]['id']

data = p3cw.request(
    entity='accounts',
    action='load_balances',
    action_id=str(account_id)
    )

print(data)
Related