@@ -48,12 +48,16 @@ def __init__(
4848 mc_b925d32c : Optional [str ] = None ,
4949 mc_sso_csrf_cookie : Optional [str ] = None ,
5050 print_bodies : bool = False ,
51- timeout : Optional [int ] = None
51+ timeout : Optional [int ] = None ,
52+ headers : Optional [Dict ] = None ,
5253 ):
5354 while base_url .endswith ("/" ):
5455 base_url = base_url [:- 1 ]
5556 self .base_url = base_url
5657
58+ # Custom headers
59+ self .headers = headers .copy () if headers else {}
60+
5761 if proxies is None :
5862 proxies = {}
5963 self .proxies = proxies
@@ -90,6 +94,13 @@ def __init__(
9094 raise_on_status = False ,
9195 )
9296
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+
93104 # This is for internal dynatrace usage
94105 self .mc_jsession_id = mc_jsession_id
95106 self .mc_b925d32c = mc_b925d32c
@@ -105,33 +116,31 @@ def make_request(
105116 body = params
106117 params = query_params
107118
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 )
113125
114126 cookies = None
115127 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 } " })
117129 cookies = {"JSESSIONID" : self .mc_jsession_id , "ssoCSRFCookie" : self .mc_sso_csrf_cookie , "b925d32c" : self .mc_b925d32c }
118130
119- s = requests .Session ()
120- s .mount ("https://" , HTTPAdapter (max_retries = self .retries ))
121-
122131 self .log .debug (f"Making { method } request to '{ url } ' with params { params } and body: { body } " )
123132 if self .print_bodies :
124133 print (method , url )
125134 if body :
126135 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 )
128137 self .log .debug (f"Received response '{ r } '" )
129138
130139 while r .status_code == 429 and self .too_many_requests_strategy == TOO_MANY_REQUESTS_WAIT :
131140 sleep_amount = int (r .headers .get ("retry-after" , 5 ))
132141 self .log .warning (f"Sleeping for { sleep_amount } s because we have received an HTTP 429" )
133142 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 )
135144
136145 if r .status_code >= 400 :
137146 raise Exception (f"Error making request to { url } : { r } . Response: { r .text } " )
0 commit comments