Skip to content

Commit

Permalink
Avoid requests without timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Jun 23, 2024
1 parent e15ebf6 commit cc5936f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion scripts/fetch_contract_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate_kernel_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand Down
2 changes: 2 additions & 0 deletions src/pytezos/cli/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def create_deployment(repo_slug, oauth_token, environment, ref='master'):
'environment': environment,
'required_contexts': [],
}, # bypass checking
timeout=60,
).json()


Expand All @@ -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()
2 changes: 1 addition & 1 deletion src/pytezos/contract/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/pytezos/contract/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/pytezos/contract/token_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pytezos/protocol/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand Down
56 changes: 47 additions & 9 deletions src/pytezos/rpc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit cc5936f

Please sign in to comment.