77
88from uuid import uuid4
99from typing import Any , Dict , List , Optional , Mapping , Union
10+
1011try :
1112 from urllib .parse import parse_qs , quote , urlparse
1213except ImportError :
3839 DEFAULT_COSMOS_ENDPOINT_SUFFIX ,
3940 DEFAULT_STORAGE_ENDPOINT_SUFFIX ,
4041)
41- from ._error import (
42- RequestTooLargeError ,
43- TableTransactionError ,
44- _decode_error ,
45- _validate_tablename_error
46- )
42+ from ._error import RequestTooLargeError , TableTransactionError , _decode_error , _validate_tablename_error
4743from ._models import LocationMode
4844from ._authentication import _configure_credential
4945from ._policies import (
5652
5753_SUPPORTED_API_VERSIONS = ["2019-02-02" , "2019-07-07" , "2020-12-06" ]
5854# cspell:disable-next-line
59- _DEV_CONN_STRING = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1" # pylint: disable=line-too-long
55+ _DEV_CONN_STRING = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1" # pylint: disable=line-too-long
6056
6157
6258def get_api_version (kwargs : Dict [str , Any ], default : str ) -> str :
6359 api_version = kwargs .pop ("api_version" , None )
6460 if api_version and api_version not in _SUPPORTED_API_VERSIONS :
6561 versions = "\n " .join (_SUPPORTED_API_VERSIONS )
66- raise ValueError (
67- f"Unsupported API version '{ api_version } '. Please select from:\n { versions } "
68- )
62+ raise ValueError (f"Unsupported API version '{ api_version } '. Please select from:\n { versions } " )
6963 return api_version or default
7064
7165
@@ -74,7 +68,7 @@ def __init__(
7468 self ,
7569 account_url : str ,
7670 credential : Optional [Union [AzureNamedKeyCredential , AzureSasCredential , TokenCredential ]] = None ,
77- ** kwargs
71+ ** kwargs ,
7872 ) -> None :
7973 try :
8074 if not account_url .lower ().startswith ("http" ):
@@ -87,9 +81,7 @@ def __init__(
8781
8882 _ , sas_token = parse_query (parsed_url .query )
8983 if not sas_token and not credential :
90- raise ValueError (
91- "You need to provide either an AzureSasCredential or AzureNamedKeyCredential"
92- )
84+ raise ValueError ("You need to provide either an AzureSasCredential or AzureNamedKeyCredential" )
9385 self ._query_str , credential = format_query_string (sas_token , credential )
9486 self ._location_mode = kwargs .get ("location_mode" , LocationMode .PRIMARY )
9587 self ._hosts = kwargs .get ("_hosts" )
@@ -115,17 +107,15 @@ def __init__(
115107 if self .scheme .lower () != "https" and hasattr (self .credential , "get_token" ):
116108 raise ValueError ("Token credential is only supported with HTTPS." )
117109 if hasattr (self .credential , "named_key" ):
118- self .account_name = self .credential .named_key .name # type: ignore
110+ self .account_name = self .credential .named_key .name # type: ignore
119111 endpoint_suffix = os .getenv ("TABLES_STORAGE_ENDPOINT_SUFFIX" , DEFAULT_STORAGE_ENDPOINT_SUFFIX )
120112 secondary_hostname = f"{ self .account_name } -secondary.table.{ endpoint_suffix } "
121113
122114 if not self ._hosts :
123115 if len (account ) > 1 :
124116 secondary_hostname = parsed_url .netloc .replace (
125117 account [0 ], account [0 ] + "-secondary"
126- ) + parsed_url .path .replace (
127- account [0 ], account [0 ] + "-secondary"
128- ).rstrip ("/" )
118+ ) + parsed_url .path .replace (account [0 ], account [0 ] + "-secondary" ).rstrip ("/" )
129119 if kwargs .get ("secondary_hostname" ):
130120 secondary_hostname = kwargs ["secondary_hostname" ]
131121 primary_hostname = (parsed_url .netloc + parsed_url .path ).rstrip ("/" )
@@ -214,12 +204,12 @@ class TablesBaseClient(AccountHostsMixin):
214204 :ivar str api_version: The service API version.
215205 """
216206
217- def __init__ ( # pylint: disable=missing-client-constructor-parameter-credential
207+ def __init__ ( # pylint: disable=missing-client-constructor-parameter-credential
218208 self ,
219209 endpoint : str ,
220210 * ,
221211 credential : Optional [Union [AzureSasCredential , AzureNamedKeyCredential , TokenCredential ]] = None ,
222- ** kwargs
212+ ** kwargs ,
223213 ) -> None :
224214 """Create TablesBaseClient from a Credential.
225215
@@ -236,13 +226,9 @@ def __init__( # pylint: disable=missing-client-constructor-parameter-credential
236226 is "2019-02-02".
237227 :paramtype api_version: str
238228 """
239- super (TablesBaseClient , self ).__init__ (endpoint , credential = credential , ** kwargs ) # type: ignore
240- self ._client = AzureTable (
241- self .url ,
242- policies = kwargs .pop ('policies' , self ._policies ),
243- ** kwargs
244- )
245- self ._client ._config .version = get_api_version (kwargs , self ._client ._config .version ) # type: ignore # pylint: disable=protected-access
229+ super (TablesBaseClient , self ).__init__ (endpoint , credential = credential , ** kwargs ) # type: ignore
230+ self ._client = AzureTable (self .url , policies = kwargs .pop ("policies" , self ._policies ), ** kwargs )
231+ self ._client ._config .version = get_api_version (kwargs , self ._client ._config .version ) # type: ignore # pylint: disable=protected-access
246232
247233 def __enter__ (self ):
248234 self ._client .__enter__ ()
@@ -284,17 +270,15 @@ def _batch_send(self, table_name: str, *reqs: HttpRequest, **kwargs) -> List[Map
284270 policies = [StorageHeadersPolicy ()]
285271
286272 changeset = HttpRequest ("POST" , None ) # type: ignore
287- changeset .set_multipart_mixed (
288- * reqs , policies = policies , boundary = f"changeset_{ uuid4 ()} " # type: ignore
289- )
273+ changeset .set_multipart_mixed (* reqs , policies = policies , boundary = f"changeset_{ uuid4 ()} " ) # type: ignore
290274 request = self ._client ._client .post ( # pylint: disable=protected-access
291275 url = f"{ self .scheme } ://{ self ._primary_hostname } /$batch" ,
292276 headers = {
293277 "x-ms-version" : self .api_version ,
294278 "DataServiceVersion" : "3.0" ,
295279 "MaxDataServiceVersion" : "3.0;NetFx" ,
296- ' Content-Type' : ' application/json' ,
297- ' Accept' : ' application/json'
280+ " Content-Type" : " application/json" ,
281+ " Accept" : " application/json" ,
298282 },
299283 )
300284 request .set_multipart_mixed (
@@ -307,9 +291,8 @@ def _batch_send(self, table_name: str, *reqs: HttpRequest, **kwargs) -> List[Map
307291 response = pipeline_response .http_response
308292 if response .status_code == 413 :
309293 raise _decode_error (
310- response ,
311- error_message = "The transaction request was too large" ,
312- error_type = RequestTooLargeError )
294+ response , error_message = "The transaction request was too large" , error_type = RequestTooLargeError
295+ )
313296 if response .status_code != 202 :
314297 decoded = _decode_error (response )
315298 _validate_tablename_error (decoded , table_name )
@@ -320,13 +303,9 @@ def _batch_send(self, table_name: str, *reqs: HttpRequest, **kwargs) -> List[Map
320303 if any (error_parts ):
321304 if error_parts [0 ].status_code == 413 :
322305 raise _decode_error (
323- response ,
324- error_message = "The transaction request was too large" ,
325- error_type = RequestTooLargeError )
326- decoded = _decode_error (
327- response = error_parts [0 ],
328- error_type = TableTransactionError
329- )
306+ response , error_message = "The transaction request was too large" , error_type = RequestTooLargeError
307+ )
308+ decoded = _decode_error (response = error_parts [0 ], error_type = TableTransactionError )
330309 _validate_tablename_error (decoded , table_name )
331310 raise decoded
332311 return [extract_batch_part_metadata (p ) for p in parts ]
@@ -346,6 +325,7 @@ class TransportWrapper(HttpTransport):
346325 :param transport: The Http Transport instance
347326 :type transport: ~azure.core.pipeline.transport.HttpTransport
348327 """
328+
349329 def __init__ (self , transport ):
350330 self ._transport = transport
351331
@@ -409,16 +389,17 @@ def parse_connection_str(conn_str, credential, keyword_args):
409389
410390def extract_batch_part_metadata (response_part ):
411391 metadata = {}
412- if ' Etag' in response_part .headers :
413- metadata [' etag' ] = response_part .headers [' Etag' ]
392+ if " Etag" in response_part .headers :
393+ metadata [" etag" ] = response_part .headers [" Etag" ]
414394 return metadata
415395
416396
417397def format_query_string (sas_token , credential ):
418398 query_str = "?"
419399 if sas_token and isinstance (credential , AzureSasCredential ):
420400 raise ValueError (
421- "You cannot use AzureSasCredential when the resource URI also contains a Shared Access Signature." )
401+ "You cannot use AzureSasCredential when the resource URI also contains a Shared Access Signature."
402+ )
422403 if sas_token and not credential :
423404 query_str += sas_token
424405 elif credential :
@@ -429,11 +410,7 @@ def format_query_string(sas_token, credential):
429410def parse_query (query_str ):
430411 sas_values = QueryStringConstants .to_list ()
431412 parsed_query = {k : v [0 ] for k , v in parse_qs (query_str ).items ()}
432- sas_params = [
433- f"{ k } ={ quote (v , safe = '' )} "
434- for k , v in parsed_query .items ()
435- if k in sas_values
436- ]
413+ sas_params = [f"{ k } ={ quote (v , safe = '' )} " for k , v in parsed_query .items () if k in sas_values ]
437414 sas_token = None
438415 if sas_params :
439416 sas_token = "&" .join (sas_params )
0 commit comments