unwitting / bitstampy

Bitstamp API wrapper for Python
MIT License
54 stars 21 forks source link

python3 #11

Open philipperemy opened 7 years ago

philipperemy commented 7 years ago

Does this library support python3?

philipperemy commented 7 years ago

Because I'm having this error:

c = 'hello'
k = 'k'
s = 's'

pprint(api.account_balance(c, k, s))
Traceback (most recent call last):
  File "/Users/philipperemy/PycharmProjects/bitstamp/hello.py", line 19, in <module>
    pprint(api.account_balance(c, k, s))
  File "/Users/philipperemy/venv/lib/python3.6/site-packages/bitstampy/api.py", line 40, in account_balance
    calls.APIAccountBalanceCall(client_id, api_key, api_secret)
  File "/Users/philipperemy/venv/lib/python3.6/site-packages/bitstampy/calls.py", line 81, in call
    self.api_secret, msg=message, digestmod=hashlib.sha256)
  File "/Users/philipperemy/venv/lib/python3.6/hmac.py", line 144, in new
    return HMAC(key, msg, digestmod)
  File "/Users/philipperemy/venv/lib/python3.6/hmac.py", line 42, in __init__
    raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'

Can you please advice? Thanks!

philipperemy commented 7 years ago

In calls.py:

    def call(self, **params):
        nonce = self._get_nonce()
        message = nonce + self.client_id + self.api_key
        signature = hmac.new(bytes(self.api_secret.encode('utf8')),
                             msg=bytes(message.encode('utf8')),
                             digestmod=hashlib.sha256)
        signature = signature.hexdigest().upper()
        params.update({
            'key': self.api_key, 'signature': signature, 'nonce': nonce
        })
        return super(APIPrivateCall, self).call(**params)

Just for information I changed this function and it works. Not confident if it would work for python2 so I prefer to ask you before sending a PR.

Thanks

AdityaNayak commented 7 years ago

Worked fine on 3 for me so far. Perhaps it was a function specific bug.

unwitting commented 7 years ago

@philipperemy what exactly did you change? Was it a problem with sending the output to pprint? If it's small, might be easiest to submit as a change and I can have a look.

philipperemy commented 7 years ago

@unwitting I converted the arguments from str to bytes when calling hmac.new.

self.api_secret to bytes(self.api_secret.encode('utf8')) message to bytes(message.encode('utf8'))

unwitting commented 7 years ago

Seems OK :) send an MR?

KevinLefevre commented 6 years ago

Encountered the same problem on python 3.6

unwitting commented 6 years ago

Would somebody be able to try the code in https://github.com/unwitting/bitstampy/issues/11#issuecomment-322133630 locally and submit as an MR if it's good please?