1111import json
1212import logging
1313import os
14- import socket
1514import ssl
1615import tempfile
1716import time
@@ -108,11 +107,11 @@ def __init__(
108107 if user_agent is not None :
109108 self ._user_agent = user_agent
110109 else :
111- self ._user_agent = "TD-Client-Python/%s" % ( version .__version__ )
110+ self ._user_agent = f "TD-Client-Python/{ version .__version__ } "
112111
113112 if endpoint is not None :
114113 if not urlparse .urlparse (endpoint ).scheme :
115- endpoint = "https://{}" . format ( endpoint )
114+ endpoint = f "https://{ endpoint } "
116115 self ._endpoint = endpoint
117116 elif os .getenv ("TD_API_SERVER" ):
118117 self ._endpoint = os .getenv ("TD_API_SERVER" )
@@ -154,7 +153,7 @@ def _init_http(
154153 if http_proxy .startswith ("http://" ):
155154 return self ._init_http_proxy (http_proxy , ** kwargs )
156155 else :
157- return self ._init_http_proxy ("http://%s" % ( http_proxy ,) , ** kwargs )
156+ return self ._init_http_proxy (f "http://{ http_proxy } " , ** kwargs )
158157
159158 def _init_http_proxy (self , http_proxy : str , ** kwargs : Any ) -> urllib3 .ProxyManager :
160159 pool_options = dict (kwargs )
@@ -164,7 +163,7 @@ def _init_http_proxy(self, http_proxy: str, **kwargs: Any) -> urllib3.ProxyManag
164163 if "@" in netloc :
165164 auth , netloc = netloc .split ("@" , 2 )
166165 pool_options ["proxy_headers" ] = urllib3 .make_headers (proxy_basic_auth = auth )
167- return urllib3 .ProxyManager ("%s ://%s" % ( scheme , netloc ) , ** pool_options )
166+ return urllib3 .ProxyManager (f" { scheme } ://{ netloc } " , ** pool_options )
168167
169168 def get (
170169 self ,
@@ -214,12 +213,12 @@ def get(
214213 self ._max_cumul_retry_delay ,
215214 )
216215 except (
216+ OSError ,
217217 urllib3 .exceptions .TimeoutStateError ,
218218 urllib3 .exceptions .TimeoutError ,
219219 urllib3 .exceptions .PoolError ,
220220 http .client .IncompleteRead ,
221221 TimeoutError ,
222- socket .error ,
223222 ):
224223 pass
225224
@@ -235,12 +234,7 @@ def get(
235234 retry_delay *= 2
236235 else :
237236 raise APIError (
238- "Retrying stopped after %d seconds. (cumulative: %d/%d)"
239- % (
240- self ._max_cumul_retry_delay ,
241- cumul_retry_delay ,
242- self ._max_cumul_retry_delay ,
243- )
237+ f"Retrying stopped after { self ._max_cumul_retry_delay } seconds. (cumulative: { cumul_retry_delay } /{ self ._max_cumul_retry_delay } )"
244238 )
245239
246240 log .debug (
@@ -314,13 +308,15 @@ def post(
314308 self ._max_cumul_retry_delay ,
315309 )
316310 except (
311+ OSError ,
317312 urllib3 .exceptions .TimeoutStateError ,
318313 urllib3 .exceptions .TimeoutError ,
319314 urllib3 .exceptions .PoolError ,
320- socket .error ,
321315 ):
322316 if not self ._retry_post_requests :
323- raise APIError ("Retrying stopped by retry_post_requests == False" )
317+ raise APIError (
318+ "Retrying stopped by retry_post_requests == False"
319+ ) from None
324320
325321 if cumul_retry_delay <= self ._max_cumul_retry_delay :
326322 log .warning (
@@ -334,12 +330,7 @@ def post(
334330 retry_delay *= 2
335331 else :
336332 raise APIError (
337- "Retrying stopped after %d seconds. (cumulative: %d/%d)"
338- % (
339- self ._max_cumul_retry_delay ,
340- cumul_retry_delay ,
341- self ._max_cumul_retry_delay ,
342- )
333+ f"Retrying stopped after { self ._max_cumul_retry_delay } seconds. (cumulative: { cumul_retry_delay } /{ self ._max_cumul_retry_delay } )"
343334 )
344335
345336 log .debug (
@@ -408,12 +399,12 @@ def put(
408399 else :
409400 raise APIError ("Error %d: %s" , response .status , response .data )
410401 except (
402+ OSError ,
411403 urllib3 .exceptions .TimeoutStateError ,
412404 urllib3 .exceptions .TimeoutError ,
413405 urllib3 .exceptions .PoolError ,
414- socket .error ,
415406 ):
416- raise APIError ("Error: %s" % ( repr (response )))
407+ raise APIError (f "Error: { repr (response )} " ) from None
417408
418409 log .debug (
419410 "REST PUT response:\n headers: %s\n status: %d\n body: <omitted>" ,
@@ -470,10 +461,10 @@ def delete(
470461 self ._max_cumul_retry_delay ,
471462 )
472463 except (
464+ OSError ,
473465 urllib3 .exceptions .TimeoutStateError ,
474466 urllib3 .exceptions .TimeoutError ,
475467 urllib3 .exceptions .PoolError ,
476- socket .error ,
477468 ):
478469 pass
479470
@@ -489,12 +480,7 @@ def delete(
489480 retry_delay *= 2
490481 else :
491482 raise APIError (
492- "Retrying stopped after %d seconds. (cumulative: %d/%d)"
493- % (
494- self ._max_cumul_retry_delay ,
495- cumul_retry_delay ,
496- self ._max_cumul_retry_delay ,
497- )
483+ f"Retrying stopped after { self ._max_cumul_retry_delay } seconds. (cumulative: { cumul_retry_delay } /{ self ._max_cumul_retry_delay } )"
498484 )
499485
500486 log .debug (
@@ -536,7 +522,7 @@ def build_request(
536522 # use default headers first
537523 _headers = dict (self ._headers )
538524 # add default headers
539- _headers ["authorization" ] = "TD1 %s" % ( self ._apikey ,)
525+ _headers ["authorization" ] = f "TD1 { self ._apikey } "
540526 _headers ["date" ] = email .utils .formatdate (time .time ())
541527 _headers ["user-agent" ] = self ._user_agent
542528 # override given headers
@@ -571,28 +557,26 @@ def raise_error(
571557 status_code = res .status
572558 s = body if isinstance (body , str ) else body .decode ("utf-8" )
573559 if status_code == 404 :
574- raise errors .NotFoundError ("%s: %s" % ( msg , s ) )
560+ raise errors .NotFoundError (f" { msg } : { s } " )
575561 elif status_code == 409 :
576- raise errors .AlreadyExistsError ("%s: %s" % ( msg , s ) )
562+ raise errors .AlreadyExistsError (f" { msg } : { s } " )
577563 elif status_code == 401 :
578- raise errors .AuthError ("%s: %s" % ( msg , s ) )
564+ raise errors .AuthError (f" { msg } : { s } " )
579565 elif status_code == 403 :
580- raise errors .ForbiddenError ("%s: %s" % ( msg , s ) )
566+ raise errors .ForbiddenError (f" { msg } : { s } " )
581567 else :
582- raise errors .APIError ("%d: %s: %s" % ( status_code , msg , s ) )
568+ raise errors .APIError (f" { status_code } : { msg } : { s } " )
583569
584570 def checked_json (self , body : bytes , required : list [str ]) -> dict [str , Any ]:
585571 js = None
586572 try :
587573 js = json .loads (body .decode ("utf-8" ))
588574 except ValueError as error :
589- raise APIError ("Unexpected API response: %s: %s" % ( error , repr (body )))
575+ raise APIError (f "Unexpected API response: { error } : { repr (body )} " ) from error
590576 js = dict (js )
591577 if 0 < [k in js for k in required ].count (False ):
592578 missing = [k for k in required if k not in js ]
593- raise APIError (
594- "Unexpected API response: %s: %s" % (repr (missing ), repr (body ))
595- )
579+ raise APIError (f"Unexpected API response: { repr (missing )} : { repr (body )} " )
596580 return js
597581
598582 def close (self ) -> None :
@@ -619,11 +603,11 @@ def _read_file(self, file_like, fmt, **kwargs):
619603 compressed = fmt .endswith (".gz" )
620604 if compressed :
621605 fmt = fmt [0 : len (fmt ) - len (".gz" )]
622- reader_name = "_read_%s_file" % ( fmt ,)
606+ reader_name = f "_read_{ fmt } _file"
623607 if hasattr (self , reader_name ):
624608 reader = getattr (self , reader_name )
625609 else :
626- raise TypeError ("unknown format: %s" % ( fmt ,) )
610+ raise TypeError (f "unknown format: { fmt } " )
627611 if hasattr (file_like , "read" ):
628612 if compressed :
629613 file_like = gzip .GzipFile (fileobj = file_like )
0 commit comments