Skip to content

Commit

Permalink
add request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojay committed Dec 15, 2023
1 parent f5d856a commit cd3b647
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions everpay/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from .client import Client

class Account(Client):
def __init__(self, everpay_server_url, signer):
super().__init__(everpay_server_url)
def __init__(self, everpay_server_url, signer, timeout=2):
super().__init__(everpay_server_url, timeout)
self.address = signer.address
self.signer = signer

Expand Down Expand Up @@ -40,7 +40,7 @@ def send_tx(self, action, token_symbol_or_tag, to, amount, data, is_dry_run):
if is_dry_run:
return t

return t, t.post(self.api_server).content
return t, t.post(self.api_server, timeout=self.timeout).content

def transfer(self, token_symbol_or_tag, to, amount, data='', is_dry_run=False):
return self.send_tx('transfer', token_symbol_or_tag, to, amount, data, is_dry_run)
Expand Down
10 changes: 6 additions & 4 deletions everpay/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .token import get_token_list

class Client:
def __init__(self, everpay_server_url):
def __init__(self, everpay_server_url, timeout=2):
if everpay_server_url.endswith('/'):
everpay_server_url = everpay_server_url[:-1]
self.api_server = everpay_server_url
Expand All @@ -13,6 +13,8 @@ def __init__(self, everpay_server_url):
self.eth_chain_id = self.info['ethChainID']
self.fee_recipient = self.info['feeRecipient']
self.symbol_to_tokens, self.tag_to_tokens = get_token_list(self.info)

self.timeout = timeout

def get_info(self):
return self.info
Expand Down Expand Up @@ -61,7 +63,7 @@ def get_balance(self, account, token_symbol_or_tag=''):
else:
path = '/balances/%s' % account
url = get_url(self.api_server, path)
return requests.get(url).json()
return requests.get(url, timeout=self.timeout).json()

def get_txs(self, address='', start_cursor=0, order_by='desc', limit=10, token_tag='', action='', without_action=''):
params = {}
Expand All @@ -83,9 +85,9 @@ def get_txs(self, address='', start_cursor=0, order_by='desc', limit=10, token_t
path = '/txs/%s' % address

url = get_url(self.api_server, path)
return requests.get(url, params=params).json()
return requests.get(url, params=params, timeout=self.timeout).json()

def get_tx(self, hash):
path = '/tx/%s' % hash
url = get_url(self.api_server, path)
return requests.get(url).json()
return requests.get(url, timeout=self.timeout).json()
4 changes: 2 additions & 2 deletions everpay/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def to_dict(self):
'sig': self.sig
}

def post(self, api_host):
def post(self, api_host, timeout=2):
url = api_host + '/tx'
if not self.sig:
raise Exception(self.tx_id, 'no signature.')
#print('post_data:', self.to_dict())
return requests.post(url, json=self.to_dict())
return requests.post(url, json=self.to_dict(), timeout=timeout)

0 comments on commit cd3b647

Please sign in to comment.