Skip to content

Commit

Permalink
Merge pull request #94 from s4w3d0ff/dev
Browse files Browse the repository at this point in the history
create PoloniexError exception
  • Loading branch information
s4w3d0ff authored Apr 12, 2017
2 parents 9c0627c + bf5a666 commit fc51a1a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 50 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ Inspired by [this](http://pastebin.com/8fBVpjaj) wrapper written by 'oipminer'
### Features:
- The first 2 args in the `poloniex.Poloniex` object (`Key` and `Secret`) are _optional_ when used for _public api commands_.
- Api commands have been 'mapped' into methods within the `Poloniex` class for your convenience.
- Raises `ValueError` if the command supplied does not exist __or__ if the api keys are not defined and attempting to access a private api command.
- Raises `poloniex.PoloniexError` if the command supplied does not exist, if the api keys are not defined and attempting to access a private api command, or if Poloniex.com returns an api error.
- The `poloniex.Poloniex(timeout=1)` attribute/arg adjusts the number of seconds to wait for a response from poloniex, else `requests.exceptions.Timeout` is raised.
- If a `requests` exception is raised (including `Timeout`s), signaling that the api call did not go through, the wrapper will attempt to try the call again. The wait pattern between retrys are as follows (in seconds): (0, 2, 5, 30). Once the retry delay list is exausted and the call still throws an error, the list of captured exceptions is raised.
- A call restrictor (`coach`) is enabled by default, limiting the api wrapper to 6 calls per second. If you wish, you can deactivate the coach using `Poloniex(coach=False)` or use an 'external' coach.
- By default, json floats are parsed as strings (ints are ints), you can define `Poloniex(jsonNums=float)` to have _all numbers_ (floats _and_ ints) parsed as floats (or import and use `decimal.Decimal`).
- `poloniex.coach`, `poloniex.retry`, and `poloniex` have self named loggers.

## Install:
Python 2:
Python 2:
```
pip install git+https://github.com/s4w3d0ff/python-poloniex.git
```

Python 3:
Python 3:
```
pip3 install git+https://github.com/s4w3d0ff/python-poloniex.git
```

## Uninstall:
Python 2:
Python 2:
```
pip uninstall poloniex
```

Python 3:
Python 3:
```
pip3 uninstall poloniex
```
Expand Down Expand Up @@ -74,7 +74,7 @@ myCoach = Coach()

public = Poloniex(coach=myCoach)
private = Poloniex(Key, Secret, coach=myCoach)
# now make calls using both 'private' and 'public' and myCoach will handle both
# now make calls using both 'private' and 'public' and myCoach will handle both
```

**Examples of WAMP applications using the websocket push API can be found [here](https://github.com/s4w3d0ff/python-poloniex/tree/master/examples).**
100 changes: 56 additions & 44 deletions poloniex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
'closeMarginPosition']


class PoloniexError(Exception):
pass


class Poloniex(object):
"""The Poloniex Object!"""

Expand Down Expand Up @@ -150,49 +154,52 @@ def __call__(self, command, args={}):
if command in PRIVATE_COMMANDS:
# check for keys
if not self.Key or not self.Secret:
raise ValueError("A Key and Secret needed!")
raise PoloniexError("A Key and Secret needed!")
# set nonce
args['nonce'] = self.nonce

try:
# encode arguments for url
postData = _urlencode(args)
# sign postData with our Secret
sign = _new(
self.Secret.encode('utf-8'),
postData.encode('utf-8'),
_sha512)
# post request
ret = _post(
'https://poloniex.com/tradingApi',
data=args,
headers={
'Sign': sign.hexdigest(),
'Key': self.Key
},
timeout=self.timeout)
self.logger.debug(ret.url)
except Exception as e:
raise e
# return decoded json
# encode arguments for url
postData = _urlencode(args)
# sign postData with our Secret
sign = _new(
self.Secret.encode('utf-8'),
postData.encode('utf-8'),
_sha512)
# post request
ret = _post(
'https://poloniex.com/tradingApi',
data=args,
headers={'Sign': sign.hexdigest(), 'Key': self.Key},
timeout=self.timeout)
# decode json
if not self.jsonNums:
return _loads(ret.text, parse_float=str)
return _loads(ret.text, parse_float=self.jsonNums, parse_int=self.jsonNums)
jsonout = _loads(ret.text, parse_float=str)
else:
jsonout = _loads(ret.text,
parse_float=self.jsonNums,
parse_int=self.jsonNums)
# check if poloniex returned an error
if 'error' in jsonout:
raise PoloniexError(jsonout['error'])
return jsonout

# public?
elif command in PUBLIC_COMMANDS:
try:
ret = _get(
'https://poloniex.com/public?' + _urlencode(args),
timeout=self.timeout)
self.logger.debug(ret.url)
except Exception as e:
raise e
ret = _get(
'https://poloniex.com/public?' + _urlencode(args),
timeout=self.timeout)
# decode json
if not self.jsonNums:
return _loads(ret.text, parse_float=str)
return _loads(ret.text, parse_float=self.jsonNums, parse_int=self.jsonNums)
jsonout = _loads(ret.text, parse_float=str)
else:
jsonout = _loads(ret.text,
parse_float=self.jsonNums,
parse_int=self.jsonNums)
# check if poloniex returned an error
if 'error' in jsonout:
raise PoloniexError(jsonout['error'])
return jsonout
else:
raise ValueError("Invalid Command!")
raise PoloniexError("Invalid Command!: %s" % command)

# --PUBLIC COMMANDS-------------------------------------------------------
def returnTicker(self):
Expand Down Expand Up @@ -241,6 +248,7 @@ def returnChartData(self, pair, period=False, start=False, end=False):
'end': str(end)
})

@retry(delays=retryDelays, exception=RequestException)
def marketTradeHist(self, pair, start=False, end=False):
"""
Returns public trade history for <pair>
Expand All @@ -254,16 +262,20 @@ def marketTradeHist(self, pair, start=False, end=False):
args['start'] = start
if end:
args['end'] = end
try:
ret = _get(
'https://poloniex.com/public?' + _urlencode(args),
timeout=self.timeout)
self.logger.debug(ret.url)
except Exception as e:
raise e
ret = _get(
'https://poloniex.com/public?' + _urlencode(args),
timeout=self.timeout)
# decode json
if not self.jsonNums:
return _loads(ret.text, parse_float=str)
return _loads(ret.text, parse_float=self.jsonNums, parse_int=self.jsonNums)
jsonout = _loads(ret.text, parse_float=str)
else:
jsonout = _loads(ret.text,
parse_float=self.jsonNums,
parse_int=self.jsonNums)
# check if poloniex returned an error
if 'error' in jsonout:
raise PoloniexError(jsonout['error'])
return jsonout

# --PRIVATE COMMANDS------------------------------------------------------
def generateNewAddress(self, coin):
Expand Down

0 comments on commit fc51a1a

Please sign in to comment.