Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for optional timeout #21

Merged
merged 2 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ Or:
3. For custom api node:
```python
import pyvsystems as pv
custom_wrapper = pv.create_api_wrapper('http://<full node ip>:9922', api_key='')
# you can set the request timeout
custom_wrapper = pv.create_api_wrapper('http://<full node ip>:9922', api_key='', timeout='')
ts_chain = pv.testnet_chain(custom_wrapper)
```

4. For completely custom chain:
```python
import pyvsystems as pv
custom_wrapper = pv.create_api_wrapper('http://<full node ip>:9922', api_key='')
custom_wrapper = pv.create_api_wrapper('http://<full node ip>:9922', api_key='', timeout=''))
t_chain = pv.Chain(chain_name='testnet', chain_id='T', address_version=5, api_wrapper=custom_wrapper)
custom_wrapper2 = pv.create_api_wrapper('http://<full node ip>:9922', api_key='')
custom_wrapper2 = pv.create_api_wrapper('http://<full node ip>:9922', api_key='', timeout=''))
m_chain = pv.Chain(chain_name='mainnet', chain_id='M', address_version=5, api_wrapper=custom_wrapper2)
custom_wrapper3 = pv.create_api_wrapper('http://<full node ip>:9922', api_key='')
custom_wrapper3 = pv.create_api_wrapper('http://<full node ip>:9922', api_key='', timeout=''))
c_chain = pv.Chain(chain_name='mychain', chain_id='C', address_version=1, api_wrapper=custom_wrapper3)
```

Expand Down
4 changes: 2 additions & 2 deletions pyvsystems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def is_offline():
from .setting import *


def create_api_wrapper(node_host=DEFAULT_NODE, api_key=DEFAULT_API_KEY):
return Wrapper(node_host, api_key)
def create_api_wrapper(node_host=DEFAULT_NODE, api_key=DEFAULT_API_KEY, timeout=''):
return Wrapper(node_host, api_key, timeout)


from .chain import *
Expand Down
13 changes: 10 additions & 3 deletions pyvsystems/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@


class Wrapper(object):
def __init__(self, node_host, api_key=''):
def __init__(self, node_host, api_key='', timeout=''):
jwzheng96 marked this conversation as resolved.
Show resolved Hide resolved
self.node_host = node_host
self.api_key = api_key
self.timeout = timeout

def request(self, api, post_data=''):
headers = {}
Expand All @@ -17,9 +18,15 @@ def request(self, api, post_data=''):
try:
if post_data:
headers['Content-Type'] = 'application/json'
return requests.post(url, data=post_data, headers=headers).json()
if self.timeout:
return requests.post(url, data=post_data, headers=headers, timeout=self.timeout).json()
else:
return requests.post(url, data=post_data, headers=headers).json()
else:
return requests.get(url, headers=headers).json()
if self.timeout:
return requests.get(url, headers=headers, timeout=self.timeout).json()
else:
return requests.get(url, headers=headers).json()
except RequestException as ex:
msg = 'Failed to get response: {}'.format(ex)
raise NetworkException(msg)