Ejemplo n.º 1
0
    def _make_api_object(self, response, model_type=None):
        blob = response.json()
        data = blob.get('data', None)
        # All valid responses have a "data" key.
        if data is None:
            raise build_api_error(response, blob)
        # Warn the user about each warning that was returned.
        warnings_data = blob.get('warnings', None)
        for warning_blob in warnings_data or []:
            message = "%s (%s)" % (warning_blob.get(
                'message', ''), warning_blob.get('url', ''))
            warnings.warn(message, UserWarning)

        pagination = blob.get('pagination', None)
        kwargs = {
            'response':
            response,
            'pagination':
            pagination and new_api_object(None, pagination, APIObject),
            'warnings':
            warnings_data and new_api_object(None, warnings_data, APIObject),
        }
        if isinstance(data, dict):
            obj = new_api_object(self, data, model_type, **kwargs)
        else:
            obj = APIObject(self, **kwargs)
            obj.data = new_api_object(self, data, model_type)
        return obj
Ejemplo n.º 2
0
    def _make_api_object(self, response, model_type=None):
        blob = response.json()
        data = blob.get('data', None)
        # All valid responses have a "data" key.
        if data is None:
            raise build_api_error(response, blob)
        # Warn the user about each warning that was returned.
        warnings_data = blob.get('warnings', None)
        for warning_blob in warnings_data or []:
            message = "%s (%s)" % (
                warning_blob.get('message', ''),
                warning_blob.get('url', ''))
            warnings.warn(message, UserWarning)

        pagination = blob.get('pagination', None)
        kwargs = {
            'response': response,
            'pagination': pagination and new_api_object(None, pagination, APIObject),
            'warnings': warnings_data and new_api_object(None, warnings_data, APIObject)
        }
        if isinstance(data, dict):
            obj = new_api_object(self, data, model_type, **kwargs)
        else:
            obj = APIObject(self, **kwargs)
            obj.data = new_api_object(self, data, model_type)
        return obj
Ejemplo n.º 3
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 not str(response.status_code).startswith('2'):
            raise build_api_error(response)
        return response
Ejemplo n.º 4
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 not str(response.status_code).startswith('2'):
            raise build_api_error(response)
        return response
Ejemplo n.º 5
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.
    """
        params = {"grant_type": "refresh_token", "refresh_token": self.refresh_token}
        response = self._post("oauth", "token", params=params)
        response = self._handle_response(response)
        blob = response.json()
        self.access_token = blob.get("access_token", None)
        self.refresh_token = blob.get("refresh_token", None)
        if not (self.access_token and self.refresh_token):
            raise build_api_error(response, blob)
        return blob
Ejemplo n.º 6
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.
    """
        params = {
            'grant_type': 'refresh_token',
            'refresh_token': self.refresh_token
        }
        response = self._post('oauth', 'token', params=params)
        response = self._handle_response(response)
        blob = response.json()
        self.access_token = blob.get('access_token', None)
        self.refresh_token = blob.get('refresh_token', None)
        if not (self.access_token and self.refresh_token):
            raise build_api_error(response, blob)
        return blob