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

Can't we define a retry method in the instance method? #464

Open
bbhxwl opened this issue May 29, 2024 · 0 comments
Open

Can't we define a retry method in the instance method? #464

bbhxwl opened this issue May 29, 2024 · 0 comments

Comments

@bbhxwl
Copy link

bbhxwl commented May 29, 2024

Can't we define a retry method in the instance method?
Because I want to call login again in the instance method, I cannot use static methods.

image

api = gov.api.api("test","test")
api.get_all_list(1,10)
import requests
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception, retry_if_result



class api:
    def __init__(self, username, password):
        self.session = requests.session()
        self.session.headers = {"User-Agent": ""}
        self.username = username
        self.password = password
        self.token = ""
        self.headers = {"Authorization": f"Bearer {self.token}"}

    def is_retryable_exception(self, exception):
        if isinstance(exception, requests.exceptions.HTTPError):
            if exception.response.status_code == 401:
                self.login()
        return True

    def is_retryable_result(self, result):
        print("is_retryable_result")
        if result is None:
            return True
        if result.get("code", None) == 0:
            return False
        return True

    @retry(stop=stop_after_attempt(3),
           wait=wait_fixed(2),
           retry=(retry_if_exception(is_retryable_exception) | retry_if_result(is_retryable_result)))
    def login(self):
        return ""

    @retry(stop=stop_after_attempt(3),
           wait=wait_fixed(2),
           retry=(retry_if_exception(is_retryable_exception) | retry_if_result(is_retryable_result)))
    def get_all_list(self, index, pagesize):

        return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant