Skip to content

Commit 6d020e7

Browse files
committed
Rebased with master and updated everything to be CBPRO instead of GDAX. Updated to check errors in send_message instead of _get
1 parent e0467b1 commit 6d020e7

File tree

9 files changed

+119
-433
lines changed

9 files changed

+119
-433
lines changed

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ time.sleep(10)
354354
order_book.close()
355355
```
356356

357-
<<<<<<< HEAD
358357
### Testing
359358
Unit tests are under development using the pytest framework. Contributions are
360359
welcome!
@@ -365,37 +364,32 @@ directory run:
365364
python -m pytest
366365
```
367366

368-
## Change Log
369-
*1.1.2* **Current PyPI release**
370-
- Refactor project for Coinbase Pro
371-
- Major overhaul on how pagination is handled
372-
373-
*1.0*
374-
=======
375367
### Error Handling
376368
Error handling has been added in version 2.0. Currently, the only HTTP error codes that
377369
are handled are the ones documented on the GDAX API error section: [here](https://docs.gdax.com/?python#errors)
378370

379-
- HTTP STATUS CODE 400: Raises InvalidGdaxRequest
380-
- HTTP STATUS CODE 401: Raises UnauthorizedGdaxRequest
381-
- HTTP STATUS CODE 403: Raises ForbiddenGdaxRequest
382-
- HTTP STATUS CODE 404: Raises NotFoundGdaxRequest
383-
- HTTP STATUS CODE 429: Raises GdaxRateLimitRequest
384-
- HTTP STATUS CODE 4XX: Raises UnknownGdaxClientRequest
385-
- HTTP STATUS CODE 5XX: Raises InternalErrorGdaxRequest
371+
- HTTP STATUS CODE 400: Raises InvalidCbproRequest
372+
- HTTP STATUS CODE 401: Raises UnauthorizedCbproRequest
373+
- HTTP STATUS CODE 403: Raises ForbiddenCbproRequest
374+
- HTTP STATUS CODE 404: Raises NotFoundCbproRequest
375+
- HTTP STATUS CODE 429: Raises CbproRateLimitRequest
376+
- HTTP STATUS CODE 4XX: Raises UnknownCbproClientRequest
377+
- HTTP STATUS CODE 5XX: Raises InternalErrorCbproRequest
386378

387379
All HTTP requests from both the public client and authenticated client run through `_determine_response`, which
388380
either returns the JSON body or raises the appropriate error.
389381

390-
Please consider creating new Exception classes and mapping as Gdax error states are discovered.
391-
382+
Please consider creating new Exception classes and mapping as Cbpro error states are discovered.
392383

393384
## Change Log
394385
*2.0*
395386
- Added error handling to all HTTP requests [Error Handling Docs](#error-handling)
396387

397-
*1.0* **Current PyPI release**
398-
>>>>>>> Added README info, added self to contributors, bumped version
388+
*1.1.2* **Current PyPI release**
389+
- Refactor project for Coinbase Pro
390+
- Major overhaul on how pagination is handled
391+
392+
*1.0*
399393
- The first release that is not backwards compatible
400394
- Refactored to follow PEP 8 Standards
401395
- Improved Documentation

cbpro/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from cbpro.websocket_client import WebsocketClient
44
from cbpro.order_book import OrderBook
55
from cbpro.cbpro_auth import CBProAuth
6+
from cbpro.exceptions import InvalidCbproRequest, UnauthorizedCbproRequest, ForbiddenCbproRequest, NotFoundCbproRequest, CbproRateLimitRequest, UnknownCbproClientRequest, InternalErrorCbproRequest

cbpro/exceptions.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class CbproException(Exception):
2+
"""
3+
Base Coinbase Pro Exception
4+
Raised when Bad Response returned from Coinbase Pro
5+
See: https://docs.pro.coinbase.com/?r=1#errors
6+
"""
7+
8+
def __init__(self, message, code):
9+
"""
10+
:param message: Message from Coinbase Pro response
11+
:type message: str
12+
:param code: HTTP Code
13+
:type code: int
14+
"""
15+
self._message = message
16+
self._code = code
17+
18+
@property
19+
def message(self):
20+
return self._message
21+
22+
@message.setter
23+
def message(self, message):
24+
self._message = message
25+
26+
@property
27+
def code(self):
28+
return self._code
29+
30+
@message.setter
31+
def code(self, code):
32+
self._code = code
33+
34+
35+
class InvalidCbproRequest(CbproException):
36+
"""
37+
Raised on 400 response from Coinbase Pro
38+
"""
39+
pass
40+
41+
42+
class UnauthorizedCbproRequest(CbproException):
43+
"""
44+
Raised on 401 response from Coinbase Pro
45+
"""
46+
pass
47+
48+
49+
class ForbiddenCbproRequest(CbproException):
50+
"""
51+
Raised on 403 response from Coinbase Pro
52+
"""
53+
pass
54+
55+
56+
class NotFoundCbproRequest(CbproException):
57+
"""
58+
Raised on 404 response from Coinbase Pro
59+
"""
60+
pass
61+
62+
63+
class CbproRateLimitRequest(CbproException):
64+
"""
65+
Raised on 429 response from Coinbase Pro
66+
"""
67+
pass
68+
69+
70+
class UnknownCbproClientRequest(CbproException):
71+
"""
72+
Raised on 4XX responses not tracked
73+
"""
74+
pass
75+
76+
77+
class InternalErrorCbproRequest(CbproException):
78+
"""
79+
Raised on 500 response from Coinbase Pro
80+
"""
81+
pass

cbpro/public_client.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from http import HTTPStatus
88
import requests
9-
from gdax import exceptions
9+
from cbpro import exceptions
1010

1111

1212
class PublicClient(object):
@@ -46,45 +46,39 @@ def _is_http_server_error(self, code):
4646

4747
def _determine_response(self, response):
4848
"""
49-
Determines if GDAX response is success or error
49+
Determines if CBPRO response is success or error
5050
If success, returns response json
51-
If error, raises appropriate GdaxException
51+
If error, raises appropriate CbproException
5252
"""
5353
if self._is_http_success(response.status_code):
5454
return response.json()
5555
elif self._is_http_client_error(response.status_code):
5656
body = response.json()
5757
message = body.get('message')
5858
if response.status_code == HTTPStatus.BAD_REQUEST:
59-
raise exceptions.InvalidGdaxRequest(message,
59+
raise exceptions.InvalidCbproRequest(message,
6060
HTTPStatus.BAD_REQUEST)
6161
elif response.status_code == HTTPStatus.UNAUTHORIZED:
62-
raise exceptions.UnauthorizedGdaxRequest(message,
62+
raise exceptions.UnauthorizedCbproRequest(message,
6363
HTTPStatus.UNAUTHORIZED)
6464
elif response.status_code == HTTPStatus.FORBIDDEN:
65-
raise exceptions.ForbiddenGdaxRequest(message,
65+
raise exceptions.ForbiddenCbproRequest(message,
6666
HTTPStatus.FORBIDDEN)
6767
elif response.status_code == HTTPStatus.NOT_FOUND:
68-
raise exceptions.NotFoundGdaxRequest(message,
68+
raise exceptions.NotFoundCbproRequest(message,
6969
HTTPStatus.NOT_FOUND)
7070
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
71-
raise exceptions.GdaxRateLimitRequest(message,
71+
raise exceptions.CbproRateLimitRequest(message,
7272
HTTPStatus.TOO_MANY_REQUESTS)
7373
else: # Other 4XX response not yet mapped
74-
raise exceptions.UnknownGdaxClientRequest(message,
74+
raise exceptions.UnknownCbproClientRequest(message,
7575
response.status_code)
7676

7777
elif self._is_http_server_error(response.status_code):
7878
body = response.json()
79-
raise exceptions.InternalErrorGdaxRequest(body.get('message'),
79+
raise exceptions.InternalErrorCbproRequest(body.get('message'),
8080
HTTPStatus.INTERNAL_SERVER_ERROR)
8181

82-
def _get(self, path, params=None):
83-
"""Perform get request"""
84-
85-
r = requests.get(self.url + path, params=params, timeout=self.timeout)
86-
return self._determine_response(r)
87-
8882
def get_products(self):
8983
"""Get a list of available currency pairs for trading.
9084
@@ -323,7 +317,7 @@ def _send_message(self, method, endpoint, params=None, data=None):
323317
url = self.url + endpoint
324318
r = self.session.request(method, url, params=params, data=data,
325319
auth=self.auth, timeout=30)
326-
return r.json()
320+
return self._determine_response(r)
327321

328322
def _send_paginated_message(self, endpoint, params=None):
329323
""" Send API message that results in a paginated response.
@@ -353,7 +347,7 @@ def _send_paginated_message(self, endpoint, params=None):
353347
url = self.url + endpoint
354348
while True:
355349
r = self.session.get(url, params=params, auth=self.auth, timeout=30)
356-
results = r.json()
350+
results = self._determine_response(r)
357351
for result in results:
358352
yield result
359353
# If there are no more pages, we're done. Otherwise update `after`

0 commit comments

Comments
 (0)