1
1
from __future__ import annotations
2
2
3
3
import time
4
- from typing import NoReturn
4
+ from typing import NoReturn , Protocol
5
5
6
6
import requests
7
7
26
26
from .volumes import VolumesClient
27
27
28
28
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
+
29
38
class Client :
30
39
"""Base Client for accessing the Hetzner Cloud API"""
31
40
@@ -39,7 +48,8 @@ def __init__(
39
48
api_endpoint : str = "https://api.hetzner.cloud/v1" ,
40
49
application_name : str | None = None ,
41
50
application_version : str | None = None ,
42
- poll_interval : int = 1 ,
51
+ poll_interval : int | float | PollIntervalFunction = 1.0 ,
52
+ poll_max_retries : int = 120 ,
43
53
timeout : float | tuple [float , float ] | None = None ,
44
54
):
45
55
"""Create a new Client instance
@@ -48,7 +58,11 @@ def __init__(
48
58
:param api_endpoint: Hetzner Cloud API endpoint
49
59
:param application_name: Your application name
50
60
: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.
52
66
:param timeout: Requests timeout in seconds
53
67
"""
54
68
self .token = token
@@ -57,7 +71,12 @@ def __init__(
57
71
self ._application_version = application_version
58
72
self ._requests_session = requests .Session ()
59
73
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
61
80
62
81
self .datacenters = DatacentersClient (self )
63
82
"""DatacentersClient Instance
0 commit comments