Exemplo n.º 1
0
    def _post(self,
              api_method,
              params=None,
              connection=None,
              error_handler=None):
        if params is None:
            params = {'nonce': datetime.now().microsecond}
        else:
            params["nonce"] = datetime.now().microsecond
        encoded_params = urllib.parse.urlencode(params).encode()

        # Hash the params string to produce the Sign header value
        H = hmac.new(self.secret.encode(), digestmod=hashlib.sha512)
        H.update(encoded_params)
        sign = H.hexdigest()

        if connection is None:
            connection = common.BTERConnection()

        headers = {"Key": self.key, "Sign": sign}
        result = connection.makeJSONRequest('/api/1/private/' + api_method,
                                            method='POST',
                                            extra_headers=headers,
                                            params=encoded_params)

        return common.validateResponse(result, error_handler=error_handler)
Exemplo n.º 2
0
def getTradeHistory(pair, connection=None, start_tid=None, count=None, error_handler=None):
    """
    Retrieve the trade history for the given pair. Returns a list of
    Trade instances. If count is not None, it should be an integer, and
    specifies the number of items from the trade history that will be
    processed and returned.
    """
    common.validatePair(pair)

    if connection is None:
        connection = common.BTERConnection()

    if start_tid is None:
        result = connection.makeJSONRequest('/api/1/trade/%s' % pair, method='GET')
    else:
        result = connection.makeJSONRequest('/api/1/trade/%s/%d' % (pair, start_tid), method='GET')

    result = common.validateResponse(result, error_handler=error_handler)

    history = result[u'data']
    if type(history) is not list:
        raise Exception('The response does not contain a history list.')

    result = []
    # Limit the number of items returned if requested.
    if count is not None:
        history = history[:count]

    for h in history:
        h["pair"] = pair
        t = Trade(**h)
        result.append(t)
    return result
Exemplo n.º 3
0
def getDepth(pair, connection=None, error_handler=None):
    """
    Retrieve the depth for the given pair. Returns a tuple (asks, bids);
    each of these is a list of (price, volume) tuples.
    """
    common.validatePair(pair)

    if connection is None:
        connection = common.BTERConnection()

    depth = common.validateResponse(connection.makeJSONRequest('/api/1/depth/%s' % pair, method='GET'),
                                    error_handler=error_handler)

    if type(depth) is not dict:
        raise TypeError("The response is not a dict.")

    asks = depth.get('asks')
    if type(asks) is not list:
        raise TypeError("The response does not contain an asks list.")

    bids = depth.get('bids')
    if type(bids) is not list:
        raise TypeError("The response does not contain a bids list.")

    return asks, bids
Exemplo n.º 4
0
    def get_price(self,coin_pair):
        connection = common.BTERConnection()
        result = connection.makeJSONRequest('/api/1/ticker/' + coin_pair, method='GET',
                                            extra_headers={}, params=None)

        return common.validateResponse(result, error_handler=None)

        pass
Exemplo n.º 5
0
    def _post(self, api_method, params=None, connection=None, error_handler=None):
        if params is None:
            params = {'nonce': datetime.now().microsecond}
        else:
            params["nonce"] = datetime.now().microsecond
        encoded_params = urllib.urlencode(params)

        # Hash the params string to produce the Sign header value
        H = hmac.new(self.secret, digestmod=hashlib.sha512)
        H.update(encoded_params)
        sign = H.hexdigest()

        if connection is None:
            connection = common.BTERConnection()

        headers = {"Key": self.key, "Sign": sign}
        result = connection.makeJSONRequest('/api/1/private/' + api_method, method='POST',
                                            extra_headers=headers, params=encoded_params)

        return common.validateResponse(result, error_handler=error_handler)