Skip to content

Commit

Permalink
Added retry decorator; Bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
SadParad1se committed May 14, 2024
1 parent 2bca310 commit 4733930
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
57 changes: 29 additions & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "snek-sploit"
version = "0.5.0"
version = "0.6.0"
description = "Python RPC client for Metasploit Framework"
authors = [
"Jiří Rája <[email protected]>"
Expand Down
2 changes: 2 additions & 0 deletions snek_sploit/lib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Union, List, Dict

from snek_sploit.util import constants, exceptions
from snek_sploit.util.retry import retry


ResponseDict = Dict[Union[int, str, bytes], Union[int, str, bytes, list, dict, bool]]
Expand Down Expand Up @@ -63,6 +64,7 @@ def _create_arguments(self, call_arguments: list, use_token: bool) -> list:

return arguments

@retry(tries=3, on_errors=(requests.RequestException,))
def call(
self, endpoint: str, arguments: list = None, use_token: bool = True, timeout: Union[float, tuple] = None
) -> RPCResponse:
Expand Down
27 changes: 27 additions & 0 deletions snek_sploit/util/retry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from functools import wraps, partial


def retry(func=None, *, tries: int = 1, on_errors: tuple = None):
"""
Retry a function if an error occurs.
:param func: Original function
:param tries: Number of times to retry
:param on_errors: On what errors to retry
:return: Wrapper
"""
if func is None:
return partial(retry, tries=tries, on_errors=on_errors)

if not on_errors:
on_errors = (Exception,)

@wraps(func)
def wrapper(*args, **kwargs):
for i in range(tries + 1):
try:
return func(*args, **kwargs)
except on_errors as ex:
if i == tries:
raise ex

return wrapper

0 comments on commit 4733930

Please sign in to comment.