1
- from typing import Any , Optional
1
+ from __future__ import annotations
2
2
3
- from requests import get
3
+ from typing import Any , Optional , Union
4
+
5
+ from requests import Session
6
+ from requests .adapters import HTTPAdapter , Retry
4
7
from requests .auth import AuthBase
5
8
from requests .models import PreparedRequest , Response
6
9
@@ -21,22 +24,76 @@ def __call__(self, request: PreparedRequest) -> PreparedRequest:
21
24
class Client :
22
25
"""ACRCloud client to fetch metadata.
23
26
24
- Args:
25
- bearer_token: The bearer token for ACRCloud.
27
+ >>> bearer_token = "bearer-token"
28
+ >>> config = Client.Config(retries= 5, backoff_factor= 0.1)
29
+ >>> client = Client(bearer_token, config=config)
30
+
31
+ :param str bearer_token
32
+ The bearer token for ACRCloud.
26
33
"""
27
34
28
- def __init__ (self , bearer_token : str , base_url = "https://eu-api-v2.acrcloud.com" ):
29
- self .base_url = base_url
30
- self .auth = _Auth (bearer_token = bearer_token )
35
+ class Config :
36
+ """Configuration for acrclient.
37
+
38
+ :param int retries
39
+ Total number of retries to allow.
40
+
41
+ :param float backoff_factor
42
+ A backoff factor to apply between attempts after the second try
43
+ (most errors are resolved immediately by a second try without a
44
+ delay). urllib3 will sleep for::
45
+ {backoff factor} * (2 ** ({number of total retries} - 1))
46
+ seconds. If the backoff_factor is 0.1, then :func:`Retry.sleep` will sleep
47
+ for [0.0s, 0.2s, 0.4s, ...] between retries. It will never be longer
48
+ than `backoff_max`.
49
+ By default, backoff is set to 0.1.
50
+ """
51
+
52
+ def __init__ (
53
+ self ,
54
+ retries : Union [bool | int | None ] = 5 ,
55
+ backoff_factor : float = 0.1 ,
56
+ ):
57
+ self ._retries : Union [bool | int | None ] = retries
58
+ self ._backoff_factor : float = backoff_factor
59
+
60
+ @property
61
+ def retries (self ):
62
+ return self ._retries
63
+
64
+ @property
65
+ def backoff_factor (self ):
66
+ return self ._backoff_factor
67
+
68
+ def __init__ (
69
+ self ,
70
+ bearer_token : str ,
71
+ base_url : str = "https://eu-api-v2.acrcloud.com" ,
72
+ config : Optional [Config ] = None ,
73
+ ):
74
+ self .base_url : str = base_url
75
+
76
+ self ._config : Optional [Client .Config ] = config or Client .Config ()
77
+ self ._auth : _Auth = _Auth (bearer_token = bearer_token )
78
+ self ._session = Session ()
79
+ self ._session .mount (
80
+ "https://" ,
81
+ HTTPAdapter (
82
+ max_retries = Retry (
83
+ total = self ._config .retries ,
84
+ backoff_factor = self ._config .backoff_factor ,
85
+ )
86
+ ),
87
+ )
31
88
32
89
def get (self , path : str , params : Any = None , ** kwargs ) -> Response :
33
90
"""Fetch JSON data from ACRCloud API with set Access Key param."""
34
91
url = f"{ self .base_url } { path } "
35
92
if not kwargs .get ("timeout" ):
36
- kwargs ["timeout" ] = 10
93
+ kwargs ["timeout" ] = 60
37
94
38
95
# pylint: disable-next=missing-timeout
39
- response = get (url = url , auth = self .auth , params = params , ** kwargs )
96
+ response = self . _session . get (url = url , auth = self ._auth , params = params , ** kwargs )
40
97
response .raise_for_status ()
41
98
return response
42
99
0 commit comments