@@ -48,12 +48,16 @@ def __init__(
48
48
mc_b925d32c : Optional [str ] = None ,
49
49
mc_sso_csrf_cookie : Optional [str ] = None ,
50
50
print_bodies : bool = False ,
51
- timeout : Optional [int ] = None
51
+ timeout : Optional [int ] = None ,
52
+ headers : Optional [Dict ] = None ,
52
53
):
53
54
while base_url .endswith ("/" ):
54
55
base_url = base_url [:- 1 ]
55
56
self .base_url = base_url
56
57
58
+ # Custom headers
59
+ self .headers = headers .copy () if headers else {}
60
+
57
61
if proxies is None :
58
62
proxies = {}
59
63
self .proxies = proxies
@@ -90,6 +94,13 @@ def __init__(
90
94
raise_on_status = False ,
91
95
)
92
96
97
+ # Persistent session
98
+ self .session = requests .Session ()
99
+
100
+ # Mount the adapter once during initialization
101
+ self .session .mount ("https://" , HTTPAdapter (max_retries = self .retries ))
102
+ self .session .mount ("http://" , HTTPAdapter (max_retries = self .retries ))
103
+
93
104
# This is for internal dynatrace usage
94
105
self .mc_jsession_id = mc_jsession_id
95
106
self .mc_b925d32c = mc_b925d32c
@@ -105,33 +116,31 @@ def make_request(
105
116
body = params
106
117
params = query_params
107
118
108
- if headers is None :
109
- headers = {}
110
- if files is None and "content-type" not in [key .lower () for key in headers .keys ()]:
111
- headers .update ({"content-type" : "application/json" })
112
- headers .update (self .auth_header )
119
+ request_headers = self .headers .copy ()
120
+ if headers :
121
+ request_headers .update (headers )
122
+ if files is None and "content-type" not in [key .lower () for key in request_headers .keys ()]:
123
+ request_headers .update ({"content-type" : "application/json" })
124
+ request_headers .update (self .auth_header )
113
125
114
126
cookies = None
115
127
if self .mc_b925d32c and self .mc_sso_csrf_cookie and self .mc_jsession_id :
116
- headers .update ({"Cookie" : f"JSESSIONID={ self .mc_jsession_id } ; ssoCSRFCookie={ self .mc_sso_csrf_cookie } ; b925d32c={ self .mc_b925d32c } " })
128
+ request_headers .update ({"Cookie" : f"JSESSIONID={ self .mc_jsession_id } ; ssoCSRFCookie={ self .mc_sso_csrf_cookie } ; b925d32c={ self .mc_b925d32c } " })
117
129
cookies = {"JSESSIONID" : self .mc_jsession_id , "ssoCSRFCookie" : self .mc_sso_csrf_cookie , "b925d32c" : self .mc_b925d32c }
118
130
119
- s = requests .Session ()
120
- s .mount ("https://" , HTTPAdapter (max_retries = self .retries ))
121
-
122
131
self .log .debug (f"Making { method } request to '{ url } ' with params { params } and body: { body } " )
123
132
if self .print_bodies :
124
133
print (method , url )
125
134
if body :
126
135
print (json .dumps (body , indent = 2 ))
127
- r = s . request (method , url , headers = headers , params = params , json = body , verify = False , proxies = self .proxies , data = data , cookies = cookies , files = files , timeout = self .timeout )
136
+ r = self . session . request (method , url , headers = request_headers , params = params , json = body , verify = False , proxies = self .proxies , data = data , cookies = cookies , files = files , timeout = self .timeout )
128
137
self .log .debug (f"Received response '{ r } '" )
129
138
130
139
while r .status_code == 429 and self .too_many_requests_strategy == TOO_MANY_REQUESTS_WAIT :
131
140
sleep_amount = int (r .headers .get ("retry-after" , 5 ))
132
141
self .log .warning (f"Sleeping for { sleep_amount } s because we have received an HTTP 429" )
133
142
time .sleep (sleep_amount )
134
- r = requests . request (method , url , headers = headers , params = params , json = body , verify = False , proxies = self .proxies , timeout = self .timeout )
143
+ r = self . session . request (method , url , headers = request_headers , params = params , json = body , verify = False , proxies = self .proxies , timeout = self .timeout )
135
144
136
145
if r .status_code >= 400 :
137
146
raise Exception (f"Error making request to { url } : { r } . Response: { r .text } " )
0 commit comments