11from __future__ import annotations
22
33import time
4- from typing import NoReturn
4+ from typing import NoReturn , Protocol
55
66import requests
77
2626from .volumes import VolumesClient
2727
2828
29+ class PollIntervalFunction (Protocol ):
30+ def __call__ (self , retries : int ) -> float :
31+ """
32+ Return a interval in seconds to wait between each API call.
33+
34+ :param retries: Number of calls already made.
35+ """
36+
37+
2938class Client :
3039 """Base Client for accessing the Hetzner Cloud API"""
3140
@@ -39,7 +48,8 @@ def __init__(
3948 api_endpoint : str = "https://api.hetzner.cloud/v1" ,
4049 application_name : str | None = None ,
4150 application_version : str | None = None ,
42- poll_interval : int = 1 ,
51+ poll_interval : int | float | PollIntervalFunction = 1.0 ,
52+ poll_max_retries : int = 120 ,
4353 timeout : float | tuple [float , float ] | None = None ,
4454 ):
4555 """Create a new Client instance
@@ -48,7 +58,11 @@ def __init__(
4858 :param api_endpoint: Hetzner Cloud API endpoint
4959 :param application_name: Your application name
5060 :param application_version: Your application _version
51- :param poll_interval: Interval for polling information from Hetzner Cloud API in seconds
61+ :param poll_interval:
62+ Interval in seconds to use when polling actions from the API.
63+ You may pass a function to compute a custom poll interval.
64+ :param poll_max_retries:
65+ Max retries before timeout when polling actions from the API.
5266 :param timeout: Requests timeout in seconds
5367 """
5468 self .token = token
@@ -57,7 +71,12 @@ def __init__(
5771 self ._application_version = application_version
5872 self ._requests_session = requests .Session ()
5973 self ._requests_timeout = timeout
60- self ._poll_interval = poll_interval
74+
75+ if isinstance (poll_interval , (int , float )):
76+ self ._poll_interval_func = lambda _ : poll_interval # Constant poll interval
77+ else :
78+ self ._poll_interval_func = poll_interval
79+ self ._poll_max_retries = poll_max_retries
6180
6281 self .datacenters = DatacentersClient (self )
6382 """DatacentersClient Instance
0 commit comments