diff --git a/scripts/fetch_contract_data.py b/scripts/fetch_contract_data.py index dc760eb4f..66700149c 100644 --- a/scripts/fetch_contract_data.py +++ b/scripts/fetch_contract_data.py @@ -26,7 +26,7 @@ def _get(url: str, params: Optional[Dict[str, Any]] = None): logger.info(f'GET {url}?{"&".join(f"{k}={v}" for k, v in (params or {}).items())}') - return requests.get(url, params=params) + return requests.get(url, params=params, timeout=60) def write_test_data(path: str, name: str, data: Dict[str, Any]) -> None: diff --git a/scripts/generate_kernel_docs.py b/scripts/generate_kernel_docs.py index ae0319de1..ba2071a83 100644 --- a/scripts/generate_kernel_docs.py +++ b/scripts/generate_kernel_docs.py @@ -24,9 +24,9 @@ def format_entry(title, concl, descr): def generate(): - meta_src = requests.get(meta_url).text + meta_src = requests.get(meta_url, timeout=10).text meta = yaml.load(meta_src, Loader=yaml.SafeLoader) - sema = requests.get(sema_url).json() + sema = requests.get(sema_url, timeout=60).json() docs = extra.copy() for section in ['instructions', 'types']: diff --git a/src/pytezos/cli/github.py b/src/pytezos/cli/github.py index 321e3a5fa..916618e44 100644 --- a/src/pytezos/cli/github.py +++ b/src/pytezos/cli/github.py @@ -15,6 +15,7 @@ def create_deployment(repo_slug, oauth_token, environment, ref='master'): 'environment': environment, 'required_contexts': [], }, # bypass checking + timeout=60, ).json() @@ -31,4 +32,5 @@ def create_deployment_status(repo_slug, oauth_token, deployment_id, state, envir 'environment': environment, 'environment_url': environment_url, }, + timeout=60, ).json() diff --git a/src/pytezos/contract/interface.py b/src/pytezos/contract/interface.py index 011670612..e57b3aea4 100644 --- a/src/pytezos/contract/interface.py +++ b/src/pytezos/contract/interface.py @@ -108,7 +108,7 @@ def from_url(url: str, context: Optional[ExecutionContext] = None) -> 'ContractI :param context: optional execution context :rtype: ContractInterface """ - res = requests.get(url) + res = requests.get(url, timeout=60) if res.status_code != 200: raise ValueError(f'cannot fetch `{url} {res.status_code}`', res.text) return ContractInterface.from_michelson(res.text, context) diff --git a/src/pytezos/contract/metadata.py b/src/pytezos/contract/metadata.py index 8a731fea4..8a34ed768 100644 --- a/src/pytezos/contract/metadata.py +++ b/src/pytezos/contract/metadata.py @@ -179,13 +179,13 @@ def from_json(cls, metadata_json: Dict[str, Any], context: Optional[ExecutionCon def from_ipfs(cls, multihash: str, context: Optional[ExecutionContext] = None) -> 'ContractMetadata': """Fetch metadata from IPFS network by multihash""" context = context or ExecutionContext() - metadata_json = requests.get(f'{context.ipfs_gateway}/{multihash}').json() + metadata_json = requests.get(f'{context.ipfs_gateway}/{multihash}', timeout=60).json() return cls.from_json(metadata_json, context) @classmethod def from_url(cls, url: str, context: Optional[ExecutionContext] = None) -> 'ContractMetadata': """Fetch metadata from HTTP(S) URL""" - metadata_json = requests.get(url).json() + metadata_json = requests.get(url, timeout=60).json() return cls.from_json(metadata_json, context) @classmethod diff --git a/src/pytezos/contract/token_metadata.py b/src/pytezos/contract/token_metadata.py index 476df44f8..5c350675d 100644 --- a/src/pytezos/contract/token_metadata.py +++ b/src/pytezos/contract/token_metadata.py @@ -130,13 +130,13 @@ def from_json( def from_ipfs(cls, multihash: str, context: Optional[ExecutionContext] = None) -> 'ContractTokenMetadata': """Fetch token metadata from IPFS network by multihash""" context = context or ExecutionContext() - token_metadata_json = requests.get(f'{context.ipfs_gateway}/{multihash}').json() + token_metadata_json = requests.get(f'{context.ipfs_gateway}/{multihash}', timeout=60).json() return cls.from_json(token_metadata_json, context) @classmethod def from_url(cls, url: str, context: Optional[ExecutionContext] = None) -> 'ContractTokenMetadata': """Fetch token metadata from HTTP(S) URL""" - token_metadata_json = requests.get(url).json() + token_metadata_json = requests.get(url, timeout=60).json() return cls.from_json(token_metadata_json, context) @classmethod diff --git a/src/pytezos/protocol/protocol.py b/src/pytezos/protocol/protocol.py index b9c4f9b86..385a44c83 100644 --- a/src/pytezos/protocol/protocol.py +++ b/src/pytezos/protocol/protocol.py @@ -54,7 +54,7 @@ def tar_to_files(path=None, raw=None) -> List[Tuple[str, str]]: def url_to_files(url) -> List[Tuple[str, str]]: - res = requests.get(url, stream=True) + res = requests.get(url, stream=True, timeout=60) raw = b'' for data in tqdm(res.iter_content()): diff --git a/src/pytezos/rpc/node.py b/src/pytezos/rpc/node.py index c211031a4..506279598 100644 --- a/src/pytezos/rpc/node.py +++ b/src/pytezos/rpc/node.py @@ -104,6 +104,7 @@ def request(self, method: str, path: str, **kwargs) -> requests.Response: method=method, url=_urljoin(self.uri[0], path), headers={'content-type': 'application/json', 'user-agent': 'PyTezos', **self.headers}, + timeout=kwargs.pop('timeout', None) or 60, **kwargs, ) if res.status_code == 401: @@ -125,20 +126,57 @@ def get( params: Optional[Dict[str, Any]] = None, timeout: Optional[int] = None, ) -> requests.Response: - return self.request('GET', path, params=params, timeout=timeout).json() - - def post(self, path: str, params: Optional[Dict[str, Any]] = None, json=None) -> Union[requests.Response, str]: - response = self.request('POST', path, params=params, json=json) + return self.request( + 'GET', + path, + params=params, + timeout=timeout, + ).json() + + def post( + self, + path: str, + params: Optional[Dict[str, Any]] = None, + json=None, + timeout: Optional[int] = None, + ) -> Union[requests.Response, str]: + response = self.request( + 'POST', + path, + params=params, + json=json, + timeout=timeout, + ) try: return response.json() except JSONDecodeError: return response.text - def delete(self, path: str, params: Optional[Dict[str, Any]] = None) -> requests.Response: - return self.request('DELETE', path, params=params).json() - - def put(self, path: str, params: Optional[Dict[str, Any]] = None) -> requests.Response: - return self.request('PUT', path, params=params).json() + def delete( + self, + path: str, + params: Optional[Dict[str, Any]] = None, + timeout: Optional[int] = None, + ) -> requests.Response: + return self.request( + 'DELETE', + path, + params=params, + timeout=timeout, + ).json() + + def put( + self, + path: str, + params: Optional[Dict[str, Any]] = None, + timeout: Optional[int] = None, + ) -> requests.Response: + return self.request( + 'PUT', + path, + params=params, + timeout=timeout, + ).json() class RpcMultiNode(RpcNode):