Skip to content

Commit

Permalink
adds TorClient class and _RequestManager base class
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfMissingNo committed Sep 23, 2021
1 parent 974ffc8 commit fd01fc6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dydx3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dydx3.dydx_client import Client
from dydx3.dydx_client import Client, TorClient
from dydx3.errors import DydxError
from dydx3.errors import DydxApiError
from dydx3.errors import TransactionReverted
Expand Down
26 changes: 24 additions & 2 deletions dydx3/dydx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dydx3.constants import NETWORK_ID_MAINNET
from dydx3.eth_signing import SignWithWeb3
from dydx3.eth_signing import SignWithKey
from dydx3.helpers.requests import _RequestManager
from dydx3.modules.api_keys import ApiKeys
from dydx3.modules.eth import Eth
from dydx3.modules.private import Private
Expand All @@ -14,12 +15,12 @@
)


class Client(object):
class Client(_RequestManager):

def __init__(
self,
host,
api_timeout=3000, # TODO: Actually use this.
api_timeout=3000,
default_ethereum_address=None,
eth_private_key=None,
eth_send_options=None,
Expand Down Expand Up @@ -73,7 +74,12 @@ def __init__(

# Initialize the public module. Other modules are initialized on
# demand, if the necessary configuration options were provided.

self._set_session()

self._public = Public(host)
self._public._session = self._session
self._public.api_timeout = self.api_timeout
self._private = None
self._api_keys = None
self._eth = None
Expand Down Expand Up @@ -140,6 +146,8 @@ def private(self):
default_address=self.default_address,
api_key_credentials=self.api_key_credentials,
)
self._private._session = self._session
self._private.api_timeout = self.api_timeout
else:
raise Exception(
'Private endpoints not supported ' +
Expand All @@ -161,6 +169,8 @@ def api_keys(self):
network_id=self.network_id,
default_address=self.default_address,
)
self._api_keys._session = self._session
self._api_keys.api_timeout = self.api_timeout
else:
raise Exception(
'API keys module is not supported since no Ethereum ' +
Expand All @@ -187,6 +197,8 @@ def onboarding(self):
self.stark_public_key_y_coordinate
),
)
self._onboarding._session = self._session
self._onboarding.api_timeout = self.api_timeout
else:
raise Exception(
'Onboarding is not supported since no Ethereum ' +
Expand Down Expand Up @@ -218,3 +230,13 @@ def eth(self):
'eth_private_key nor web3_account was provided',
)
return self._eth


class TorClient(Client):

def _set_session(self):
super()._set_session()
self._session.proxies = {
'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'
}
56 changes: 33 additions & 23 deletions dydx3/helpers/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,38 @@
from dydx3.errors import DydxApiError
from dydx3.helpers.request_helpers import remove_nones

# TODO: Use a separate session per client instance.
session = requests.session()
session.headers.update({
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'dydx/python',
})


def request(uri, method, headers=None, data_values={}):
response = send_request(
uri,
method,
headers,
data=json.dumps(
remove_nones(data_values)
)
)
if not str(response.status_code).startswith('2'):
raise DydxApiError(response)
return response.json() if response.content else '{}'
requests.get

class _RequestManager:

_session = None
api_timeout = None

def _set_session(self):
self._session = requests.session()
self._session.headers.update({
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'dydx/python',
})

def request(self, uri, method, headers=None, data_values={}):
response = self.send_request(
uri,
method,
headers,
data=json.dumps(
remove_nones(data_values)
),
timeout=self.api_timeout
)
if not str(response.status_code).startswith('2'):
raise DydxApiError(response)
if response.content:
return response.json()
return '{}'

def send_request(uri, method, headers=None, **kwargs):
return getattr(session, method)(uri, headers=headers, **kwargs)
def send_request(self, uri, method, headers=None, **kwargs):
if not _RequestManager._session:
self._set_session()
return getattr(self._session, method)(uri, headers=headers, **kwargs)
6 changes: 3 additions & 3 deletions dydx3/modules/api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from dydx3.helpers.request_helpers import generate_query_path
from dydx3.helpers.request_helpers import json_stringify
from dydx3.eth_signing import SignApiKeyAction
from dydx3.helpers.requests import request
from dydx3.helpers.requests import _RequestManager


class ApiKeys(object):
class ApiKeys(_RequestManager):
"""Module for adding, querying, and deleting API keys."""

def __init__(
Expand Down Expand Up @@ -41,7 +41,7 @@ def _request(
timestamp=timestamp,
)

return request(
return self.request(
self.host + request_path,
method,
{
Expand Down
6 changes: 3 additions & 3 deletions dydx3/modules/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from dydx3.constants import OFF_CHAIN_ONBOARDING_ACTION
from dydx3.constants import OFF_CHAIN_KEY_DERIVATION_ACTION
from dydx3.eth_signing import SignOnboardingAction
from dydx3.helpers.requests import request
from dydx3.helpers.requests import _RequestManager


class Onboarding(object):
class Onboarding(_RequestManager):

def __init__(
self,
Expand Down Expand Up @@ -42,7 +42,7 @@ def _post(
)

request_path = '/'.join(['/v3', endpoint])
return request(
return self.request(
self.host + request_path,
'post',
{
Expand Down
6 changes: 3 additions & 3 deletions dydx3/modules/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
from dydx3.helpers.request_helpers import iso_to_epoch_seconds
from dydx3.helpers.request_helpers import json_stringify
from dydx3.helpers.request_helpers import remove_nones
from dydx3.helpers.requests import request
from dydx3.helpers.requests import _RequestManager
from dydx3.starkex.helpers import get_transfer_erc20_fact
from dydx3.starkex.helpers import nonce_from_client_id
from dydx3.starkex.order import SignableOrder
from dydx3.starkex.withdrawal import SignableWithdrawal
from dydx3.starkex.conditional_transfer import SignableConditionalTransfer


class Private(object):
class Private(_RequestManager):

def __init__(
self,
Expand Down Expand Up @@ -61,7 +61,7 @@ def _private_request(
'DYDX-TIMESTAMP': now_iso_string,
'DYDX-PASSPHRASE': self.api_key_credentials['passphrase'],
}
return request(
return self.request(
self.host + request_path,
method,
headers,
Expand Down
8 changes: 4 additions & 4 deletions dydx3/modules/public.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dydx3.helpers.request_helpers import generate_query_path
from dydx3.helpers.requests import request
from dydx3.helpers.requests import _RequestManager


class Public(object):
class Public(_RequestManager):

def __init__(
self,
Expand All @@ -13,13 +13,13 @@ def __init__(
# ============ Request Helpers ============

def _get(self, request_path, params={}):
return request(
return self.request(
generate_query_path(self.host + request_path, params),
'get',
)

def _put(self, endpoint, data):
return request(
return self.request(
self.host + '/v3/' + endpoint,
'put',
{},
Expand Down

0 comments on commit fd01fc6

Please sign in to comment.