Skip to content

Commit fd01fc6

Browse files
committed
adds TorClient class and _RequestManager base class
1 parent 974ffc8 commit fd01fc6

File tree

7 files changed

+71
-39
lines changed

7 files changed

+71
-39
lines changed

dydx3/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dydx3.dydx_client import Client
1+
from dydx3.dydx_client import Client, TorClient
22
from dydx3.errors import DydxError
33
from dydx3.errors import DydxApiError
44
from dydx3.errors import TransactionReverted

dydx3/dydx_client.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dydx3.constants import NETWORK_ID_MAINNET
44
from dydx3.eth_signing import SignWithWeb3
55
from dydx3.eth_signing import SignWithKey
6+
from dydx3.helpers.requests import _RequestManager
67
from dydx3.modules.api_keys import ApiKeys
78
from dydx3.modules.eth import Eth
89
from dydx3.modules.private import Private
@@ -14,12 +15,12 @@
1415
)
1516

1617

17-
class Client(object):
18+
class Client(_RequestManager):
1819

1920
def __init__(
2021
self,
2122
host,
22-
api_timeout=3000, # TODO: Actually use this.
23+
api_timeout=3000,
2324
default_ethereum_address=None,
2425
eth_private_key=None,
2526
eth_send_options=None,
@@ -73,7 +74,12 @@ def __init__(
7374

7475
# Initialize the public module. Other modules are initialized on
7576
# demand, if the necessary configuration options were provided.
77+
78+
self._set_session()
79+
7680
self._public = Public(host)
81+
self._public._session = self._session
82+
self._public.api_timeout = self.api_timeout
7783
self._private = None
7884
self._api_keys = None
7985
self._eth = None
@@ -140,6 +146,8 @@ def private(self):
140146
default_address=self.default_address,
141147
api_key_credentials=self.api_key_credentials,
142148
)
149+
self._private._session = self._session
150+
self._private.api_timeout = self.api_timeout
143151
else:
144152
raise Exception(
145153
'Private endpoints not supported ' +
@@ -161,6 +169,8 @@ def api_keys(self):
161169
network_id=self.network_id,
162170
default_address=self.default_address,
163171
)
172+
self._api_keys._session = self._session
173+
self._api_keys.api_timeout = self.api_timeout
164174
else:
165175
raise Exception(
166176
'API keys module is not supported since no Ethereum ' +
@@ -187,6 +197,8 @@ def onboarding(self):
187197
self.stark_public_key_y_coordinate
188198
),
189199
)
200+
self._onboarding._session = self._session
201+
self._onboarding.api_timeout = self.api_timeout
190202
else:
191203
raise Exception(
192204
'Onboarding is not supported since no Ethereum ' +
@@ -218,3 +230,13 @@ def eth(self):
218230
'eth_private_key nor web3_account was provided',
219231
)
220232
return self._eth
233+
234+
235+
class TorClient(Client):
236+
237+
def _set_session(self):
238+
super()._set_session()
239+
self._session.proxies = {
240+
'http': 'socks5://127.0.0.1:9050',
241+
'https': 'socks5://127.0.0.1:9050'
242+
}

dydx3/helpers/requests.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,38 @@
55
from dydx3.errors import DydxApiError
66
from dydx3.helpers.request_helpers import remove_nones
77

8-
# TODO: Use a separate session per client instance.
9-
session = requests.session()
10-
session.headers.update({
11-
'Accept': 'application/json',
12-
'Content-Type': 'application/json',
13-
'User-Agent': 'dydx/python',
14-
})
15-
16-
17-
def request(uri, method, headers=None, data_values={}):
18-
response = send_request(
19-
uri,
20-
method,
21-
headers,
22-
data=json.dumps(
23-
remove_nones(data_values)
24-
)
25-
)
26-
if not str(response.status_code).startswith('2'):
27-
raise DydxApiError(response)
28-
return response.json() if response.content else '{}'
8+
requests.get
9+
10+
class _RequestManager:
11+
12+
_session = None
13+
api_timeout = None
2914

15+
def _set_session(self):
16+
self._session = requests.session()
17+
self._session.headers.update({
18+
'Accept': 'application/json',
19+
'Content-Type': 'application/json',
20+
'User-Agent': 'dydx/python',
21+
})
22+
23+
def request(self, uri, method, headers=None, data_values={}):
24+
response = self.send_request(
25+
uri,
26+
method,
27+
headers,
28+
data=json.dumps(
29+
remove_nones(data_values)
30+
),
31+
timeout=self.api_timeout
32+
)
33+
if not str(response.status_code).startswith('2'):
34+
raise DydxApiError(response)
35+
if response.content:
36+
return response.json()
37+
return '{}'
3038

31-
def send_request(uri, method, headers=None, **kwargs):
32-
return getattr(session, method)(uri, headers=headers, **kwargs)
39+
def send_request(self, uri, method, headers=None, **kwargs):
40+
if not _RequestManager._session:
41+
self._set_session()
42+
return getattr(self._session, method)(uri, headers=headers, **kwargs)

dydx3/modules/api_keys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from dydx3.helpers.request_helpers import generate_query_path
33
from dydx3.helpers.request_helpers import json_stringify
44
from dydx3.eth_signing import SignApiKeyAction
5-
from dydx3.helpers.requests import request
5+
from dydx3.helpers.requests import _RequestManager
66

77

8-
class ApiKeys(object):
8+
class ApiKeys(_RequestManager):
99
"""Module for adding, querying, and deleting API keys."""
1010

1111
def __init__(
@@ -41,7 +41,7 @@ def _request(
4141
timestamp=timestamp,
4242
)
4343

44-
return request(
44+
return self.request(
4545
self.host + request_path,
4646
method,
4747
{

dydx3/modules/onboarding.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from dydx3.constants import OFF_CHAIN_ONBOARDING_ACTION
66
from dydx3.constants import OFF_CHAIN_KEY_DERIVATION_ACTION
77
from dydx3.eth_signing import SignOnboardingAction
8-
from dydx3.helpers.requests import request
8+
from dydx3.helpers.requests import _RequestManager
99

1010

11-
class Onboarding(object):
11+
class Onboarding(_RequestManager):
1212

1313
def __init__(
1414
self,
@@ -42,7 +42,7 @@ def _post(
4242
)
4343

4444
request_path = '/'.join(['/v3', endpoint])
45-
return request(
45+
return self.request(
4646
self.host + request_path,
4747
'post',
4848
{

dydx3/modules/private.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
from dydx3.helpers.request_helpers import iso_to_epoch_seconds
1616
from dydx3.helpers.request_helpers import json_stringify
1717
from dydx3.helpers.request_helpers import remove_nones
18-
from dydx3.helpers.requests import request
18+
from dydx3.helpers.requests import _RequestManager
1919
from dydx3.starkex.helpers import get_transfer_erc20_fact
2020
from dydx3.starkex.helpers import nonce_from_client_id
2121
from dydx3.starkex.order import SignableOrder
2222
from dydx3.starkex.withdrawal import SignableWithdrawal
2323
from dydx3.starkex.conditional_transfer import SignableConditionalTransfer
2424

2525

26-
class Private(object):
26+
class Private(_RequestManager):
2727

2828
def __init__(
2929
self,
@@ -61,7 +61,7 @@ def _private_request(
6161
'DYDX-TIMESTAMP': now_iso_string,
6262
'DYDX-PASSPHRASE': self.api_key_credentials['passphrase'],
6363
}
64-
return request(
64+
return self.request(
6565
self.host + request_path,
6666
method,
6767
headers,

dydx3/modules/public.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from dydx3.helpers.request_helpers import generate_query_path
2-
from dydx3.helpers.requests import request
2+
from dydx3.helpers.requests import _RequestManager
33

44

5-
class Public(object):
5+
class Public(_RequestManager):
66

77
def __init__(
88
self,
@@ -13,13 +13,13 @@ def __init__(
1313
# ============ Request Helpers ============
1414

1515
def _get(self, request_path, params={}):
16-
return request(
16+
return self.request(
1717
generate_query_path(self.host + request_path, params),
1818
'get',
1919
)
2020

2121
def _put(self, endpoint, data):
22-
return request(
22+
return self.request(
2323
self.host + '/v3/' + endpoint,
2424
'put',
2525
{},

0 commit comments

Comments
 (0)