Skip to content

Commit 843e12c

Browse files
committed
refactor: improve logging format in APIRequest class
1 parent b5438da commit 843e12c

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

src/mistapi/__api_request.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def _log_proxy(self) -> None:
8888
def _next_apitoken(self) -> None:
8989
logger.info("apirequest:_next_apitoken:rotating API Token")
9090
logger.debug(
91-
f"apirequest:_next_apitoken:current API Token is "
92-
f"{self._apitoken[self._apitoken_index][:4]}..."
93-
f"{self._apitoken[self._apitoken_index][-4:]}"
91+
"apirequest:_next_apitoken:current API Token is %s...%s",
92+
self._apitoken[self._apitoken_index][:4],
93+
self._apitoken[self._apitoken_index][-4:],
9494
)
9595
new_index = self._apitoken_index + 1
9696
if new_index >= len(self._apitoken):
@@ -101,9 +101,9 @@ def _next_apitoken(self) -> None:
101101
{"Authorization": "Token " + self._apitoken[self._apitoken_index]}
102102
)
103103
logger.debug(
104-
f"apirequest:_next_apitoken:new API Token is "
105-
f"{self._apitoken[self._apitoken_index][:4]}..."
106-
f"{self._apitoken[self._apitoken_index][-4:]}"
104+
"apirequest:_next_apitoken:new API Token is %s...%s",
105+
self._apitoken[self._apitoken_index][:4],
106+
self._apitoken[self._apitoken_index][-4:],
107107
)
108108
else:
109109
logger.critical(" /!\\ API TOKEN CRITICAL ERROR /!\\")
@@ -184,42 +184,43 @@ def _request_with_retry(
184184
proxy_failed = False
185185
for attempt in range(self._MAX_429_RETRIES + 1):
186186
try:
187-
logger.info(f"apirequest:{method_name}:sending request to {url}")
187+
logger.info("apirequest:%s:sending request to %s", method_name, url)
188188
self._log_proxy()
189189
resp = request_fn()
190190
logger.debug(
191-
f"apirequest:{method_name}:request headers:{self._remove_auth_from_headers(resp)}"
191+
"apirequest:%s:request headers:%s", method_name, self._remove_auth_from_headers(resp)
192192
)
193193
resp.raise_for_status()
194194
break
195195
except requests.exceptions.ProxyError as e:
196-
logger.error(f"apirequest:{method_name}:Proxy Error: {e}")
196+
logger.error("apirequest:%s:Proxy Error: %s", method_name, e)
197197
proxy_failed = True
198198
break
199199
except requests.exceptions.ConnectionError as e:
200-
logger.error(f"apirequest:{method_name}:Connection Error: {e}")
200+
logger.error("apirequest:%s:Connection Error: %s", method_name, e)
201201
break
202202
except HTTPError as e:
203203
if e.response.status_code == 429 and attempt < self._MAX_429_RETRIES:
204204
logger.warning(
205-
f"apirequest:{method_name}:HTTP 429 (attempt {attempt + 1}/{self._MAX_429_RETRIES})"
205+
"apirequest:%s:HTTP 429 (attempt %s/%s)",
206+
method_name, attempt + 1, self._MAX_429_RETRIES,
206207
)
207208
try:
208209
self._next_apitoken()
209210
except RuntimeError:
210211
pass # single token — still retry with backoff
211212
self._handle_rate_limit(e.response, attempt)
212213
continue
213-
logger.error(f"apirequest:{method_name}:HTTP error: {e}")
214+
logger.error("apirequest:%s:HTTP error: %s", method_name, e)
214215
if resp:
215216
logger.error(
216-
f"apirequest:{method_name}:HTTP error description: {resp.json()}"
217+
"apirequest:%s:HTTP error description: %s", method_name, resp.json()
217218
)
218219
break
219220
except Exception as e:
220-
logger.error(f"apirequest:{method_name}:error: {e}")
221+
logger.error("apirequest:%s:error: %s", method_name, e)
221222
logger.error(
222-
f"apirequest:{method_name}:Exception occurred", exc_info=True
223+
"apirequest:%s:Exception occurred", method_name, exc_info=True
223224
)
224225
break
225226
self._count += 1
@@ -261,7 +262,7 @@ def mist_post(self, uri: str, body: dict | list | None = None) -> APIResponse:
261262
"""
262263
url = self._url(uri)
263264
headers = {"Content-Type": "application/json"}
264-
logger.debug(f"apirequest:mist_post:Request body:{body}")
265+
logger.debug("apirequest:mist_post:Request body:%s", body)
265266
if isinstance(body, str):
266267
fn = lambda: self._session.post(url, data=body, headers=headers)
267268
else:
@@ -285,7 +286,7 @@ def mist_put(self, uri: str, body: dict | None = None) -> APIResponse:
285286
"""
286287
url = self._url(uri)
287288
headers = {"Content-Type": "application/json"}
288-
logger.debug(f"apirequest:mist_put:Request body:{body}")
289+
logger.debug("apirequest:mist_put:Request body:%s", body)
289290
if isinstance(body, str):
290291
fn = lambda: self._session.put(url, data=body, headers=headers)
291292
else:
@@ -333,19 +334,19 @@ def mist_post_file(
333334
multipart_form_data = {}
334335
url = self._url(uri)
335336
logger.debug(
336-
f"apirequest:mist_post_file:initial multipart_form_data:{multipart_form_data}"
337+
"apirequest:mist_post_file:initial multipart_form_data:%s", multipart_form_data
337338
)
338339
generated_multipart_form_data: dict[str, Any] = {}
339340
for key in multipart_form_data:
340341
logger.debug(
341-
f"apirequest:mist_post_file:"
342-
f"multipart_form_data:{key} = {multipart_form_data[key]}"
342+
"apirequest:mist_post_file:multipart_form_data:%s = %s",
343+
key, multipart_form_data[key],
343344
)
344345
if multipart_form_data[key]:
345346
try:
346347
if key in ["csv", "file"]:
347348
logger.debug(
348-
f"apirequest:mist_post_file:reading file:{multipart_form_data[key]}"
349+
"apirequest:mist_post_file:reading file:%s", multipart_form_data[key]
349350
)
350351
f = open(multipart_form_data[key], "rb")
351352
generated_multipart_form_data[key] = (
@@ -360,23 +361,23 @@ def mist_post_file(
360361
)
361362
except (OSError, json.JSONDecodeError):
362363
logger.error(
363-
f"apirequest:mist_post_file:multipart_form_data:"
364-
f"Unable to parse JSON object {key} "
365-
f"with value {multipart_form_data[key]}"
364+
"apirequest:mist_post_file:multipart_form_data:"
365+
"Unable to parse JSON object %s with value %s",
366+
key, multipart_form_data[key],
366367
)
367368
logger.error(
368369
"apirequest:mist_post_file: Exception occurred",
369370
exc_info=True,
370371
)
371372
logger.debug(
372-
f"apirequest:mist_post_file:"
373-
f"final multipart_form_data:{generated_multipart_form_data}"
373+
"apirequest:mist_post_file:final multipart_form_data:%s",
374+
generated_multipart_form_data,
374375
)
375376

376377
def _do_post_file():
377378
resp = self._session.post(url, files=generated_multipart_form_data)
378379
logger.debug(
379-
f"apirequest:mist_post_file:request body:{self.remove_file_from_body(resp)}"
380+
"apirequest:mist_post_file:request body:%s", self.remove_file_from_body(resp)
380381
)
381382
return resp
382383

0 commit comments

Comments
 (0)