@@ -38,3 +38,53 @@ def get_host_port(netloc):
38
38
port = None
39
39
40
40
return host , port
41
+
42
+
43
+ def normalize_scope (scope ):
44
+ """Normalize the scope to verify that it is a list or tuple. A string
45
+ input will split the string by commas to create a list of scopes.
46
+ A list or tuple input is used directly.
47
+
48
+ Parameters:
49
+ - scope - a string representing scopes separated by commas,
50
+ or a list/tuple of scopes.
51
+ """
52
+ if scope :
53
+ if isinstance (scope , str ):
54
+ scopes = scope .split (',' )
55
+ elif isinstance (scope , list ) or isinstance (scope , tuple ):
56
+ scopes = scope
57
+ else :
58
+ raise Exception (
59
+ "Unsupported scope value, please either provide a list of scopes, "
60
+ "or a string of scopes separated by commas."
61
+ )
62
+ return " " .join (sorted (scopes ))
63
+ else :
64
+ return None
65
+
66
+
67
+ class Retry (urllib3 .Retry ):
68
+ """
69
+ Custom class for printing a warning when a rate/request limit is reached.
70
+ """
71
+ def increment (
72
+ self ,
73
+ method : str | None = None ,
74
+ url : str | None = None ,
75
+ response : urllib3 .BaseHTTPResponse | None = None ,
76
+ error : Exception | None = None ,
77
+ _pool : urllib3 .connectionpool .ConnectionPool | None = None ,
78
+ _stacktrace : TracebackType | None = None ,
79
+ ) -> urllib3 .Retry :
80
+ if response :
81
+ retry_header = response .headers .get ("Retry-After" )
82
+ if self .is_retry (method , response .status , bool (retry_header )):
83
+ logging .warning ("Your application has reached a rate/request limit. "
84
+ f"Retry will occur after: { retry_header } " )
85
+ return super ().increment (method ,
86
+ url ,
87
+ response = response ,
88
+ error = error ,
89
+ _pool = _pool ,
90
+ _stacktrace = _stacktrace )
0 commit comments