Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
  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)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 def get_address(self):
     """https://developers.coinbase.com/api#get-account39s-bitcoin-address"""
     response = self.api_client._get('accounts', self.id, 'address')
     api_obj = self.load(response.json())
     if not api_obj.get('success', False):
         raise build_api_error(APIError, response,
                               'Failed to fetch account address')
     if not isinstance(api_obj, Address):
         raise build_api_error(UnexpectedDataFormatError, response,
                               'Could not parse API response')
     return api_obj
Ejemplo n.º 7
0
 def create_order(self):
     """https://developers.coinbase.com/api#create-an-order-for-a-button"""
     response = self.api_client._post('buttons', self.code, 'create_order')
     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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
 def get_address(self):
   """https://developers.coinbase.com/api#get-account39s-bitcoin-address"""
   response = self.api_client._get('accounts', self.id, 'address')
   api_obj = self.load(response.json())
   if not api_obj.get('success', False):
     raise build_api_error(
         APIError,
         response,
         'Failed to fetch account address')
   if not isinstance(api_obj, Address):
     raise build_api_error(
         UnexpectedDataFormatError,
         response,
         'Could not parse API response')
   return api_obj
Ejemplo n.º 10
0
  def _handle_response(self, response):
    """Internal helper for handling API responses from the Coinbase server.

    Raises the appropriate exceptions when necessary; otherwise, returns the
    response.
    """
    if response.status_code == 200:
      return response
    # If the API response was not 200, an error occurred. Raise an exception
    # with the details of the error and references to the full response and,
    # when possible, request. These exceptions are intended to bubble up to the
    # consuming user. If the error is authentication related, raise a more
    # specific exception.
    if response.status_code == 401:
      raise build_api_error(AuthenticationError, response)
    raise build_api_error(APIError, response)
Ejemplo n.º 11
0
 def delete(self):
     """https://developers.coinbase.com/api#delete-an-account"""
     response = self.api_client._delete('accounts', self.id)
     api_obj = self.load(response.json())
     if not api_obj.get('success', False):
         raise build_api_error(APIError, response,
                               'Account deletion failed')
Ejemplo n.º 12
0
 def create_order(self):
   """https://developers.coinbase.com/api#create-an-order-for-a-button"""
   response = self.api_client._post('buttons', self.code, 'create_order')
   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
Ejemplo n.º 13
0
 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
Ejemplo n.º 14
0
  def refresh(self):
    """Attempt to refresh the current access token / refresh token pair.

    If successful, the relevant attributes of this client will be updated
    automatically and the dict of token values and information given  by the
    Coinbase OAuth server will be returned to the caller.

    If unsuccessful, raises a TokenRefreshError.
    """
    params = {
      'grant_type': 'refresh_token',
      'client_id': self.client_id,
      'client_secret': self.client_secret,
      'refresh_token': self.refresh_token
    }
    response = self.session.post(
        self.TOKEN_ENDPOINT_URI, params=params, verify=self.VERIFY_SSL)

    if not response.status_code == 200:
      raise build_api_error(TokenRefreshError, response)

    data = response.json()
    self.access_token = data.get('access_token')
    self.refresh_token = data.get('refresh_token')
    return data
Ejemplo n.º 15
0
  def _handle_response(self, response):
    # 402 will only be returned if the API endpoint requires that the oauth
    # client include the user's 2FA token as a parameter on the request.
    if response.status_code == 402:
      raise build_api_error(TwoFactorTokenRequired, response)

    # Non-authentication errors should be handled by the standard Client
    # response processing logic.
    if response.status_code != 401:
      return super(OAuthClient, self)._handle_response(response)

    error_details = _parse_authentication_error(response)
    if error_details and error_details.get('id') == 'invalid_token':
      if 'expired' in error_details.get('error', ''):
        raise build_api_error(ExpiredAccessToken, response)
      raise build_api_error(InvalidAccessToken, response)
    raise build_api_error(AuthenticationError, response)
Ejemplo n.º 16
0
 def set_primary(self):
     """https://developers.coinbase.com/api#set-account-as-primary"""
     response = self.api_client._post('accounts', self.id, 'primary')
     api_obj = self.load(response.json())
     if not api_obj.get('success', False):
         raise build_api_error(APIError, response,
                               'Account could not be set as primary')
     self.primary = True
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
 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
Ejemplo n.º 19
0
 def delete(self):
   """https://developers.coinbase.com/api#delete-an-account"""
   response = self.api_client._delete('accounts', self.id)
   api_obj = self.load(response.json())
   if not api_obj.get('success', False):
     raise build_api_error(
         APIError,
         response,
         'Account deletion failed')
Ejemplo n.º 20
0
 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
Ejemplo n.º 21
0
 def get_button(self, code_or_custom):
     """https://developers.coinbase.com/api#show-a-button"""
     response = self.api_client._get('buttons', code_or_custom)
     api_obj = self.load(response.json())
     button = api_obj.get('button', None)
     if not isinstance(button, Button):
         raise build_api_error(UnexpectedDataFormatError, response,
                               'Could not parse API response')
     return button
Ejemplo n.º 22
0
 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
Ejemplo n.º 23
0
 def set_primary(self):
   """https://developers.coinbase.com/api#set-account-as-primary"""
   response = self.api_client._post('accounts', self.id, 'primary')
   api_obj = self.load(response.json())
   if not api_obj.get('success', False):
     raise build_api_error(
         APIError,
         response,
         'Account could not be set as primary')
   self.primary = True
Ejemplo n.º 24
0
 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
Ejemplo n.º 25
0
 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
Ejemplo n.º 26
0
  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
Ejemplo n.º 27
0
 def get_current_user(self):
   """https://developers.coinbase.com/api#get-current-user"""
   response = self._get('users', 'self')
   api_obj = self._make_api_object(response.json())
   user = api_obj.get('user', None)
   if not isinstance(user, User):
     raise build_api_error(
         UnexpectedDataFormatError,
         response,
         'Could not parse API response')
   return user
Ejemplo n.º 28
0
 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
Ejemplo n.º 29
0
 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
Ejemplo n.º 30
0
 def get_payment_method(self, payment_method_id):
   """https://developers.coinbase.com/api#show-a-payment-method"""
   response = self._get('payment_methods', payment_method_id)
   api_obj = self._make_api_object(response.json())
   payment_method = api_obj.get('payment_method', None)
   if not isinstance(payment_method, PaymentMethod):
     raise build_api_error(
         UnexpectedDataFormatError,
         response,
         'Could not parse API response')
   return payment_method
Ejemplo n.º 31
0
 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
Ejemplo n.º 32
0
 def get_button(self, code_or_custom):
   """https://developers.coinbase.com/api#show-a-button"""
   response = self.api_client._get('buttons', code_or_custom)
   api_obj = self.load(response.json())
   button = api_obj.get('button', None)
   if not isinstance(button, Button):
     raise build_api_error(
         UnexpectedDataFormatError,
         response,
         'Could not parse API response')
   return button
Ejemplo n.º 33
0
    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)
Ejemplo n.º 34
0
 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
Ejemplo n.º 35
0
 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
Ejemplo n.º 36
0
 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
Ejemplo n.º 37
0
 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
Ejemplo n.º 38
0
  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)
Ejemplo n.º 39
0
 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
Ejemplo n.º 40
0
 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
Ejemplo n.º 41
0
  def get_account(self, account_id=None):
    """https://developers.coinbase.com/api#show-an-account

    If the `account_id` parameter is omitted, this method will fetch details
    on the primary account.
    """
    if account_id is None:
      account_id = 'primary'
    response = self._get('accounts', account_id)
    api_obj = self._make_api_object(response.json())
    account = api_obj.get('account', None)
    if not isinstance(account, Account):
      raise build_api_error(
          UnexpectedDataFormatError,
          response,
          'Could not parse API response')
    return account
Ejemplo n.º 42
0
 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
Ejemplo n.º 43
0
 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
Ejemplo n.º 44
0
 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
Ejemplo n.º 45
0
 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