Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleaned button, added some error handling
  • Loading branch information
sdobz committed Jan 7, 2014
commit a608a14fc84ab7dd914782f60ab8a0b9527f01bc
80 changes: 41 additions & 39 deletions coinbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def balance(self):

url = COINBASE_ENDPOINT + '/account/balance'
response = self.session.get(url, params=self.global_request_params)
results = response.json()
return CoinbaseAmount(results['amount'], results['currency'])
response_parsed = response.json()
return CoinbaseAmount(response_parsed['amount'], response_parsed['currency'])

@property
def receive_address(self):
Expand All @@ -180,7 +180,8 @@ def receive_address(self):
"""
url = COINBASE_ENDPOINT + '/account/receive_address'
response = self.session.get(url, params=self.global_request_params)
return response.json()['address']
response_parsed = response.json()
return response_parsed['address']

@property
def contacts(self):
Expand Down Expand Up @@ -220,8 +221,8 @@ def sell_price(self, qty=1):
params = {'qty': qty}
params.update(self.global_request_params)
response = self.session.get(url, params=params)
results = response.json()
return CoinbaseAmount(results['amount'], results['currency'])
response_parsed = response.json()
return CoinbaseAmount(response_parsed['amount'], response_parsed['currency'])

# @property
# def user(self):
Expand All @@ -246,9 +247,8 @@ def buy_btc(self, qty, pricevaries=False):
}
response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
response_parsed = response.json()
if response_parsed['success'] == False:
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])

return CoinbaseTransfer(response_parsed['transfer'])


Expand All @@ -265,9 +265,8 @@ def sell_btc(self, qty):
}
response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
response_parsed = response.json()
if response_parsed['success'] == False:
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])

return CoinbaseTransfer(response_parsed['transfer'])


Expand Down Expand Up @@ -302,10 +301,8 @@ def request(self, from_email, amount, notes='', currency='BTC'):

response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
response_parsed = response.json()
if response_parsed['success'] == False:
pass
#DO ERROR HANDLING and raise something

if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])
return CoinbaseTransaction(response_parsed['transaction'])

def send(self, to_address, amount, notes='', currency='BTC'):
Expand Down Expand Up @@ -340,10 +337,8 @@ def send(self, to_address, amount, notes='', currency='BTC'):

response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
response_parsed = response.json()

if response_parsed['success'] == False:
raise RuntimeError('Transaction Failed')

if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])
return CoinbaseTransaction(response_parsed['transaction'])


Expand All @@ -365,12 +360,12 @@ def transactions(self, count=30):
params = {'page': page}
params.update(self.global_request_params)
response = self.session.get(url=url, params=params)
parsed_transactions = response.json()
response_parsed = response.json()

if parsed_transactions['num_pages'] == page:
if response_parsed['num_pages'] == page:
reached_final_page = True

for transaction in parsed_transactions['transactions']:
for transaction in response_parsed['transactions']:
transactions.append(CoinbaseTransaction(transaction['transaction']))

return transactions
Expand All @@ -393,12 +388,14 @@ def transfers(self, count=30):
params = {'page': page}
params.update(self.global_request_params)
response = self.session.get(url=url, params=params)
parsed_transfers = response.json()
response_parsed = response.json()
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])

if parsed_transfers['num_pages'] == page:
if response_parsed['num_pages'] == page:
reached_final_page = True

for transfer in parsed_transfers['transfers']:
for transfer in response_parsed['transfers']:
transfers.append(CoinbaseTransfer(transfer['transfer']))

return transfers
Expand All @@ -411,13 +408,8 @@ def get_transaction(self, transaction_id):
"""
url = COINBASE_ENDPOINT + '/transactions/' + str(transaction_id)
response = self.session.get(url, params=self.global_request_params)
results = response.json()

if results.get('success', True) == False:
pass
#TODO: Add error handling

return CoinbaseTransaction(results['transaction'])
response_parsed = response.json()
return CoinbaseTransaction(response_parsed['transaction'])

def get_user_details(self):
"""
Expand All @@ -427,9 +419,9 @@ def get_user_details(self):
"""
url = COINBASE_ENDPOINT + '/users'
response = self.session.get(url, params=self.global_request_params)
results = response.json()
response_parsed = response.json()

user_details = results['users'][0]['user']
user_details = response_parsed['users'][0]['user']

#Convert our balance and limits to proper amounts
balance = CoinbaseAmount(user_details['balance']['amount'], user_details['balance']['currency'])
Expand Down Expand Up @@ -462,7 +454,10 @@ def generate_receive_address(self, callback_url=None):
}
}
response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
return response.json()['address']
response_parsed = response.json()
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])
return response_parsed['address']

def create_button(self, name,
price,
Expand Down Expand Up @@ -539,7 +534,10 @@ def create_button(self, name,
del request_data['button'][key]

response = self.session.post(url=url, data=json.dumps(request_data), params=self.global_request_params)
return CoinbaseButton(response.json()['button'])
response_parsed = response.json()
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])
return CoinbaseButton(response_parsed['button'])

def create_order(self, code):
"""
Expand All @@ -550,7 +548,10 @@ def create_order(self, code):
url = COINBASE_ENDPOINT + '/buttons/{code}/create_order'.format(code=code)

response = self.session.post(url=url, params=self.global_request_params)
return CoinbaseOrder(response.json()['order'])
response_parsed = response.json()
if response_parsed['success'] is False:
return CoinbaseError(response_parsed['errors'])
return CoinbaseOrder(response_parsed['order'])

def get_order(self, order_id):
"""
Expand All @@ -561,7 +562,8 @@ def get_order(self, order_id):
url = COINBASE_ENDPOINT + '/orders/{id}'.format(id=order_id)

response = self.session.get(url=url, params=self.global_request_params)
return CoinbaseOrder(response.json()['order'])
response_parsed = response.json()
return CoinbaseOrder(response_parsed['order'])

def orders(self, count=30):
"""
Expand All @@ -581,12 +583,12 @@ def orders(self, count=30):
params = {'page': page}
params.update(self.global_request_params)
response = self.session.get(url=url, params=params)
parsed_orders = response.json()
response_parsed = response.json()

if parsed_orders['num_pages'] == page:
if response_parsed['num_pages'] == page:
reached_final_page = True

for order in parsed_orders['orders']:
for order in response_parsed['orders']:
orders.append(CoinbaseOrder(order['order']))

return orders
4 changes: 3 additions & 1 deletion coinbase/models/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ def __init__(self, button):
# so we map them together
if 'id' in button:
self.button_id = button['id']
if 'code' in button:
elif 'code' in button:
self.button_id = button['code']
else:
self.button_id = None
self.code = self.button_id

self.name = button['name']
Expand Down