def request_money(self, from_email_address, amount=None, amount_string=None, amount_currency_iso=None, notes=None): """https://developers.coinbase.com/api#request-bitcoin""" if (not (amount or (amount_string and amount_currency_iso)) or (amount and (amount_string or amount_currency_iso))): raise ValueError( 'Must supply either `amount` OR `amount_string` and ' '`amount_currency_iso`') data = encode_params({ 'account_id': self.id, 'transaction': { 'amount': amount, 'amount_currency_iso': amount_currency_iso, 'amount_string': amount_string, 'notes': notes, 'from': from_email_address, }, }) response = self.api_client._post('transactions', 'request_money', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to request money') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def modify(self, name=None, native_currency=None, time_zone=None): """https://developers.coinbase.com/api#modify-current-user""" if not (name or native_currency or time_zone): raise ValueError('Must supply at least one field to be modified.') data = encode_params({ 'user': { 'name': name, 'native_currency': native_currency, 'time_zone': time_zone, }, }) response = self.api_client._put('users', self.id, data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'User update failed') user = api_obj.get('user', None) if not isinstance(user, User): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') self.update(user)
def request_money( self, from_email_address, amount=None, amount_string=None, amount_currency_iso=None, notes=None): """https://developers.coinbase.com/api#request-bitcoin""" if (not (amount or (amount_string and amount_currency_iso)) or (amount and (amount_string or amount_currency_iso))): raise ValueError( 'Must supply either `amount` OR `amount_string` and ' '`amount_currency_iso`') data = encode_params({ 'account_id': self.id, 'transaction': { 'amount': amount, 'amount_currency_iso': amount_currency_iso, 'amount_string': amount_string, 'notes': notes, 'from': from_email_address, }, }) response = self.api_client._post( 'transactions', 'request_money', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Failed to request money') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def get_spot_price(self, currency=None): """https://developers.coinbase.com/api#get-the-spot-price-of-bitcoin""" data = encode_params({ 'currency': currency, }) response = self._get('prices','spot_rate', data=data) return self._make_api_object(response.json())
def sell(self, qty, currency=None, agree_btc_amount_varies=None, commit=None, payment_method_id=None): """https://developers.coinbase.com/api#sells""" data = encode_params({ 'account_id': self.id, 'qty': qty, 'currency': currency, 'agree_btc_amount_varies': agree_btc_amount_varies, 'commit': commit, 'payment_method_id': payment_method_id, }) response = self.api_client._post('sells', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Failed to buy bitcoin') transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def get_sell_price(self, qty=None, currency=None): """https://developers.coinbase.com/api#get-the-sell-price""" data = encode_params({ 'qty': qty, 'currency': currency, }) response = self._get('prices','sell', data=data) return self._make_api_object(response.json())
def get_accounts(self, page=None, limit=None, all_accounts=None): """https://developers.coinbase.com/api#list-accounts""" data = encode_params({ 'page': page, 'limit': limit, 'all_accounts': all_accounts, }, bools_to_ints=True) response = self._get('accounts', data=data) return self._make_api_object(value=response.json(), paged_key='accounts')
def get_orders(self, page=None, limit=None): """https://developers.coinbase.com/api#list-orders""" data = encode_params({ 'account_id': self.id, 'limit': limit, 'page': page, }) response = self.api_client._get('orders', data=data) return self.load(response.json(), paged_key='orders')
def get_contacts(self, page=None, limit=None, all_accounts=None): """https://developers.coinbase.com/api#contacts""" data = encode_params({ 'page': page, 'limit': limit, 'all_accounts': all_accounts, }) response = self._get('contacts', data=data) return self._make_api_object(response.json(), paged_key='contacts')
def get_addresses(self, page=None, limit=None, query=None): """https://developers.coinbase.com/api#list-bitcoin-addresses""" data = encode_params({ 'account_id': self.id, 'limit': limit, 'page': page, 'query': query, }) response = self.api_client._get('addresses', data=data) return self.load(response.json(), paged_key='addresses')
def get_order(self, id_or_custom): """https://developers.coinbase.com/api#show-an-order""" data = encode_params({'account_id': self.id}) response = self.api_client._get('orders', id_or_custom, data=data) api_obj = self.load(response.json()) order = api_obj.get('order', None) if not isinstance(order, Order): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return order
def get_transfer(self, transfer_id): """https://developers.coinbase.com/api#show-a-transfer""" data = encode_params({'account_id': self.id}) response = self.api_client._get('transfers', transfer_id, data=data) api_obj = self.load(response.json()) transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def create_button(self, name, price_string, price_currency_iso, type=None, subscription=None, repeat=None, style=None, text=None, description=None, custom=None, custom_secure=None, callback_url=None, success_url=None, cancel_url=None, info_url=None, auto_redirect=None, auto_redirect_success=None, auto_redirect_cancel=None, variable_price=None, include_address=None, include_email=None, choose_price=None, price_1=None, price_2=None, price_3=None, price_4=None, price_5=None): """https://developers.coinbase.com/api#create-a-new-payment-button-page-or-iframe""" data = encode_params({ 'account_id': self.id, 'button': { 'name': name, 'price_string': price_string, 'price_currency_iso': price_currency_iso, 'type': type, 'subscription': subscription, 'repeat': repeat, 'style': style, 'text': text, 'description': description, 'custom': custom, 'custom_secure': custom_secure, 'callback_url': callback_url, 'success_url': success_url, 'cancel_url': cancel_url, 'info_url': info_url, 'auto_redirect': auto_redirect, 'auto_redirect_success': auto_redirect_success, 'auto_redirect_cancel': auto_redirect_cancel, 'variable_price': variable_price, 'include_address': include_address, 'include_email': include_email, 'choose_price': choose_price, 'price_1': price_1, 'price_2': price_2, 'price_3': price_3, 'price_4': price_4, 'price_5': price_5, }, }) response = self.api_client._post('buttons', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Failed to create button') button = api_obj.get('button', None) if not isinstance(button, Button): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return button
def deposit(self, amount, payment_method_id): """https://developers.coinbase.com/api/v1#deposit-usd""" data = encode_params({'account_id': self.id, 'amount': amount, 'payment_method_id': payment_method_id}) response = self.api_client._post('deposits', data = data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, ', '.join(api_object.get('errors', ['Failed to deposit money']))) return api_obj
def get_transfer(self, transfer_id): """https://developers.coinbase.com/api#show-a-transfer""" data = encode_params({'account_id': self.id}) response = self.api_client._get('transfers', transfer_id, data=data) api_obj = self.load(response.json()) transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def get_order(self, id_or_custom): """https://developers.coinbase.com/api#show-an-order""" data = encode_params({'account_id': self.id}) response = self.api_client._get('orders', id_or_custom, data=data) api_obj = self.load(response.json()) order = api_obj.get('order', None) if not isinstance(order, Order): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return order
def cancel(self): """https://developers.coinbase.com/api#cancel-bitcoin-request""" data = encode_params({'account_id': self.account and self.account.id}) response = self.api_client._delete('transactions', self.id, 'cancel_request', data=data) api_obj = self.load(response.json()) success = api_obj.get('success', False) if not success: raise build_api_error(APIError, response, 'Failed to resend transaction') return success
def cancel(self): """https://developers.coinbase.com/api#cancel-bitcoin-request""" data = encode_params({'account_id': self.account and self.account.id}) response = self.api_client._delete( 'transactions', self.id, 'cancel_request', data=data) api_obj = self.load(response.json()) success = api_obj.get('success', False) if not success: raise build_api_error( APIError, response, 'Failed to resend transaction') return success
def get_orders(self, page=None, limit=None): """https://developers.coinbase.com/api#list-orders-for-a-button""" data = encode_params({ 'account_id': self.account and self.account.id, 'page': page, 'limit': limit, }) # Buttons included as attributes on Order objects have an 'id' attribute, # but not a 'code' attribute. Buttons fetched directly from the # `get_button` API have a 'code' attribute, but not a 'id' attribute. identifier = getattr(self, 'id', None) or getattr(self, 'code', None) response = self.api_client._get('buttons', identifier, 'orders', data=data) return self.load(response.json(), paged_key='orders')
def send_money(self, to_btc_address, amount=None, amount_string=None, amount_currency_iso=None, notes=None, user_fee=None, referrer_id=None, idem=None, instant_buy=None, order_id=None, two_factor_token=None): """https://developers.coinbase.com/api#send-money""" if (not (amount or (amount_string and amount_currency_iso)) or (amount and (amount_string or amount_currency_iso))): raise ValueError( 'Must supply either `amount` OR `amount_string` and ' '`amount_currency_iso`') data = encode_params({ 'account_id': self.id, 'transaction': { 'amount': amount, 'amount_currency_iso': amount_currency_iso, 'amount_string': amount_string, 'idem': idem, 'instant_buy': instant_buy, 'notes': notes, 'order_id': order_id, 'referrer_id': referrer_id, 'to': to_btc_address, 'user_fee': user_fee, }, }) headers = { 'CB-2FA-Token': two_factor_token } if two_factor_token else None response = self.api_client._post('transactions', 'send_money', data=data, headers=headers) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to send money') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def complete(self): """https://developers.coinbase.com/api#complete-bitcoin-request""" data = encode_params({'account_id': self.account and self.account.id}) response = self.api_client._put('transactions', self.id, 'complete_request', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to complete transaction') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def modify(self, new_name=None): """https://developers.coinbase.com/api#modify-an-account""" data = encode_params({ 'account': { 'name': new_name, }, }) response = self.api_client._put('accounts', self.id, data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Account update failed') account = api_obj.get('account', None) if not isinstance(account, Account): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') self.update(account)
def complete(self): """https://developers.coinbase.com/api#complete-bitcoin-request""" data = encode_params({'account_id': self.account and self.account.id}) response = self.api_client._put( 'transactions', self.id, 'complete_request', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Failed to complete transaction') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def create_user(self, email, password, referrer_id=None, client_id=None, scopes=None): """https://developers.coinbase.com/api#create-a-new-user""" data = encode_params({ 'user': { 'email': email, 'password': password, 'referrer_id': referrer_id, 'client_id': client_id, 'scopes': ( ' '.join(scopes) if isinstance(scopes, (list, tuple)) else scopes), }, }) response = self._post('users', data=data) api_obj = self._make_api_object(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to create a user') return api_obj
def create_account(self, name): """https://developers.coinbase.com/api#create-an-account""" data = encode_params({ 'account': { 'name': name, }, }) response = self._post('accounts', data=data) api_obj = self._make_api_object(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to create an account') account = api_obj.get('account', None) if not isinstance(account, Account): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return account
def commit(self): """https://developers.coinbase.com/api#start-a-transfer-that-is-in-the-created-state""" data = encode_params({ 'account_id': self.account and self.account.id, }) response = self.api_client._post('transfers', self.id, 'commit', data=data) api_obj = self.load(response.json()) success = api_obj.get('success', False) if not success: raise build_api_error(APIError, response, 'Failed to commit transfer') transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def send_money( self, to_btc_address, amount=None, amount_string=None, amount_currency_iso=None, notes=None, user_fee=None, referrer_id=None, idem=None, instant_buy=None, order_id=None, two_factor_token=None): """https://developers.coinbase.com/api#send-money""" if (not (amount or (amount_string and amount_currency_iso)) or (amount and (amount_string or amount_currency_iso))): raise ValueError( 'Must supply either `amount` OR `amount_string` and ' '`amount_currency_iso`') data = encode_params({ 'account_id': self.id, 'transaction': { 'amount': amount, 'amount_currency_iso': amount_currency_iso, 'amount_string': amount_string, 'idem': idem, 'instant_buy': instant_buy, 'notes': notes, 'order_id': order_id, 'referrer_id': referrer_id, 'to': to_btc_address, 'user_fee': user_fee, }, }) headers = {'CB-2FA-Token': two_factor_token} if two_factor_token else None response = self.api_client._post( 'transactions', 'send_money', data=data, headers=headers) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Failed to send money') transaction = api_obj.get('transaction', None) if not isinstance(transaction, Transaction): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transaction
def create_address(self, label=None, callback_url=None): """https://developers.coinbase.com/api#create-a-new-bitcoin-address-for-an-account""" data = encode_params({ 'address': { 'label': label, 'callback_url': callback_url, }, }) response = self.api_client._post('accounts', self.id, 'address', data=data) address = self.load(response.json()) if not address.get('success', False): raise build_api_error(APIError, response, 'Address creation failed') if not isinstance(address, Address): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return address
def commit(self): """https://developers.coinbase.com/api#start-a-transfer-that-is-in-the-created-state""" data = encode_params({ 'account_id': self.account and self.account.id, }) response = self.api_client._post('transfers', self.id, 'commit', data=data) api_obj = self.load(response.json()) success = api_obj.get('success', False) if not success: raise build_api_error( APIError, response, 'Failed to commit transfer') transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def refund(self, refund_iso_code, mispayment_id=None, external_refund_address=None, instant_buy=None): """https://developers.coinbase.com/api#refund-an-order""" data = encode_params({ 'order': { 'refund_iso_code': refund_iso_code, 'external_refund_address': external_refund_address, 'mispayment_id': mispayment_id, 'instant_buy': instant_buy, }, }) response = self.api_client._post('orders', self.id, 'refund', data=data) api_obj = self.load(response.json()) order = api_obj.get('order', None) if not isinstance(order, Order): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return order
def modify(self, name=None, native_currency=None, time_zone=None): """https://developers.coinbase.com/api#modify-current-user""" if not (name or native_currency or time_zone): raise ValueError('Must supply at least one field to be modified.') data = encode_params({ 'user': { 'name': name, 'native_currency': native_currency, 'time_zone': time_zone, }, }) response = self.api_client._put('users', self.id, data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'User update failed') user = api_obj.get('user', None) if not isinstance(user, User): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') self.update(user)
def modify(self, new_name=None): """https://developers.coinbase.com/api#modify-an-account""" data = encode_params({ 'account': { 'name': new_name, }, }) response = self.api_client._put('accounts', self.id, data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error( APIError, response, 'Account update failed') account = api_obj.get('account', None) if not isinstance(account, Account): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') self.update(account)
def create_address(self, label=None, callback_url=None): """https://developers.coinbase.com/api#create-a-new-bitcoin-address-for-an-account""" data = encode_params({ 'address': { 'label': label, 'callback_url': callback_url, }, }) response = self.api_client._post( 'accounts', self.id, 'address', data=data) address = self.load(response.json()) if not address.get('success', False): raise build_api_error( APIError, response, 'Address creation failed') if not isinstance(address, Address): raise build_api_error( UnexpectedDataFormatError, response, 'Could not parse API response') return address
def sell(self, qty, currency=None, agree_btc_amount_varies=None, commit=None, payment_method_id=None): """https://developers.coinbase.com/api#sells""" data = encode_params({ 'account_id': self.id, 'qty': qty, 'currency': currency, 'agree_btc_amount_varies': agree_btc_amount_varies, 'commit': commit, 'payment_method_id': payment_method_id, }) response = self.api_client._post('sells', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to buy bitcoin') transfer = api_obj.get('transfer', None) if not isinstance(transfer, Transfer): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return transfer
def refund(self, refund_iso_code, mispayment_id=None, external_refund_address=None, instant_buy=None): """https://developers.coinbase.com/api#refund-an-order""" data = encode_params({ 'order': { 'refund_iso_code': refund_iso_code, 'external_refund_address': external_refund_address, 'mispayment_id': mispayment_id, 'instant_buy': instant_buy, }, }) response = self.api_client._post('orders', self.id, 'refund', data=data) api_obj = self.load(response.json()) order = api_obj.get('order', None) if not isinstance(order, Order): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return order
def create_order(self, name, price_string, price_currency_iso, type=None, subscription=None, repeat=None, style=None, text=None, description=None, custom=None, custom_secure=None, callback_url=None, success_url=None, cancel_url=None, info_url=None, auto_redirect=None, auto_redirect_success=None, auto_redirect_cancel=None, variable_price=None, include_address=None, include_email=None, choose_price=None, price_1=None, price_2=None, price_3=None, price_4=None, price_5=None): data = encode_params({ 'account_id': self.id, 'button': { 'name': name, 'price_string': price_string, 'price_currency_iso': price_currency_iso, 'type': type, 'subscription': subscription, 'repeat': repeat, 'style': style, 'text': text, 'description': description, 'custom': custom, 'custom_secure': custom_secure, 'callback_url': callback_url, 'success_url': success_url, 'cancel_url': cancel_url, 'info_url': info_url, 'auto_redirect': auto_redirect, 'auto_redirect_success': auto_redirect_success, 'auto_redirect_cancel': auto_redirect_cancel, 'variable_price': variable_price, 'include_address': include_address, 'include_email': include_email, 'choose_price': choose_price, 'price_1': price_1, 'price_2': price_2, 'price_3': price_3, 'price_4': price_4, 'price_5': price_5, }, }) response = self.api_client._post('orders', data=data) api_obj = self.load(response.json()) if not api_obj.get('success', False): raise build_api_error(APIError, response, 'Failed to create order') order = api_obj.get('order', None) if not isinstance(order, Order): raise build_api_error(UnexpectedDataFormatError, response, 'Could not parse API response') return order
def redeem_token(self, token_id): data = encode_params({ 'token_id': token_id}) response = self._post('tokens/redeem', data=data) api_obj = self._make_api_object(response.json()) return api_obj.get('success', False)