Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Add timeout attribute for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Benzbeeb committed Mar 9, 2021
1 parent 71d1a13 commit a14e85c
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions helpers/pyband/pyband/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@


class Client(object):
def __init__(self, rpc_url: str) -> None:
def __init__(self, rpc_url: str, timeout: Optional[int] = None) -> None:
self.rpc_url = rpc_url
self.timeout = timeout

def _get(self, path, **kwargs):
r = requests.get(self.rpc_url + path, **kwargs)
r = requests.get(self.rpc_url + path, timeout=self.timeout, **kwargs)
r.raise_for_status()
return r.json()

def _post(self, path, **kwargs):
r = requests.post(self.rpc_url + path, **kwargs)
r = requests.post(self.rpc_url + path, timeout=self.timeout, **kwargs)
r.raise_for_status()
return r.json()

Expand Down Expand Up @@ -120,7 +121,9 @@ def get_request_by_id(self, id: int) -> RequestInfo:
config=DACITE_CONFIG,
)

def get_latest_request(self, oid: int, calldata: bytes, min_count: int, ask_count: int) -> RequestInfo:
def get_latest_request(
self, oid: int, calldata: bytes, min_count: int, ask_count: int
) -> RequestInfo:
return from_dict(
data_class=RequestInfo,
data=self._get_result(
Expand Down Expand Up @@ -163,7 +166,9 @@ def get_request_id_by_tx_hash(self, tx_hash: HexBytes) -> List[int]:
raise EmptyRequestMsgError("There is no request message in this tx")
return request_ids

def get_reference_data(self, pairs: List[str], min_count: int, ask_count: int) -> List[ReferencePrice]:
def get_reference_data(
self, pairs: List[str], min_count: int, ask_count: int
) -> List[ReferencePrice]:
symbols = set([symbol for pair in pairs for symbol in pair.split("/") if symbol != "USD"])
data = self._post(
"/oracle/request_prices",
Expand Down Expand Up @@ -192,8 +197,14 @@ def get_reference_data(self, pairs: List[str], min_count: int, ask_count: int) -
results.append(
ReferencePrice(
pair,
rate=(int(symbol_dict[base_symbol]["px"]) * int(symbol_dict[quote_symbol]["multiplier"]))
/ (int(symbol_dict[quote_symbol]["px"]) * int(symbol_dict[base_symbol]["multiplier"])),
rate=(
int(symbol_dict[base_symbol]["px"])
* int(symbol_dict[quote_symbol]["multiplier"])
)
/ (
int(symbol_dict[quote_symbol]["px"])
* int(symbol_dict[base_symbol]["multiplier"])
),
updated_at=ReferencePriceUpdated(
int(symbol_dict[base_symbol]["resolve_time"]),
int(symbol_dict[quote_symbol]["resolve_time"]),
Expand All @@ -208,4 +219,7 @@ def get_reference_data(self, pairs: List[str], min_count: int, ask_count: int) -

def get_request_evm_proof_by_request_id(self, request_id: int) -> EVMProof:
data = self._get_result("/oracle/proof/{}".format(request_id))
return EVMProof(json_proof=data["jsonProof"], evm_proof_bytes=HexBytes(bytes.fromhex(data["evmProofBytes"])))
return EVMProof(
json_proof=data["jsonProof"],
evm_proof_bytes=HexBytes(bytes.fromhex(data["evmProofBytes"])),
)

0 comments on commit a14e85c

Please sign in to comment.