Skip to content

Commit

Permalink
[Tables] Make credential parameter keyword-only (#19117)
Browse files Browse the repository at this point in the history
* Make credential keyword-only

* Updated tests
  • Loading branch information
annatisch authored Jun 5, 2021
1 parent 0e79a8c commit 996c680
Show file tree
Hide file tree
Showing 29 changed files with 142 additions and 140 deletions.
1 change: 1 addition & 0 deletions sdk/tables/azure-data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Storage service configuration models have now been prefixed with `Table`, including
`TableAccessPolicy`, `TableMetrics`, `TableRetentionPolicy`, `TableCorsRule`
* All parameters for `TableServiceClient.set_service_properties` are now keyword-only.
* The `credential` parameter for all Clients is now keyword-only.

**Fixes**
* Fixed support for Cosmos emulator endpoint, via URL/credential or connection string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------

from typing import Dict, Optional, Any, List, Mapping, Union
from typing import Dict, Optional, Any, List, Mapping
from uuid import uuid4
try:
from urllib.parse import parse_qs, quote, urlparse
Expand Down Expand Up @@ -200,13 +200,13 @@ def api_version(self):

class TablesBaseClient(AccountHostsMixin):

def __init__(
def __init__( # pylint: disable=missing-client-constructor-parameter-credential
self,
endpoint, # type: str
credential=None, # type: Union[AzureNamedKeyCredential, AzureSasCredential]
**kwargs # type: Any
):
# type: (...) -> None
credential = kwargs.pop('credential', None)
super(TablesBaseClient, self).__init__(endpoint, credential=credential, **kwargs)
self._client = AzureTable(
self.url,
Expand Down
30 changes: 15 additions & 15 deletions sdk/tables/azure-data-tables/azure/data/tables/_table_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,22 @@ class TableClient(TablesBaseClient):
:ivar str url: The full URL to the Tables account.
"""

def __init__(
def __init__( # pylint: disable=missing-client-constructor-parameter-credential
self,
endpoint, # type: str
table_name, # type: str
credential=None, # type: Union[AzureNamedKeyCredential, AzureSasCredential]
**kwargs # type: Any
):
# type: (...) -> None
"""Create TableClient from a Credential.
:param str endpoint: A URL to an Azure Tables account.
:param str table_name: The table name.
:param credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token, or the connection string already has shared
access key values. The value can be a SAS token string or an account shared access
key.
:type credential:
account URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
:returns: None
Expand All @@ -85,7 +83,7 @@ def __init__(
raise ValueError("Please specify a table name.")
_validate_table_name(table_name)
self.table_name = table_name
super(TableClient, self).__init__(endpoint, credential=credential, **kwargs)
super(TableClient, self).__init__(endpoint, **kwargs)

def _format_url(self, hostname):
"""Format the endpoint URL according to the current location
Expand Down Expand Up @@ -123,16 +121,18 @@ def from_connection_string(
return cls(endpoint, table_name=table_name, credential=credential, **kwargs)

@classmethod
def from_table_url(cls, table_url, credential=None, **kwargs):
# type: (str, Optional[Any], Any) -> TableClient
def from_table_url(cls, table_url, **kwargs):
# type: (str, Any) -> TableClient
"""A client to interact with a specific Table.
:param str table_url: The full URI to the table, including SAS token if used.
:param credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token. The value can be a SAS token string, an account
shared access key.
:type credential: str
account URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
:returns: A table client.
:rtype: :class:`~azure.data.tables.TableClient`
"""
Expand Down Expand Up @@ -163,7 +163,7 @@ def from_table_url(cls, table_url, credential=None, **kwargs):
raise ValueError(
"Invalid URL. Please provide a URL with a valid table name"
)
return cls(endpoint, table_name=table_name, credential=credential, **kwargs)
return cls(endpoint, table_name=table_name, **kwargs)

@distributed_trace
def get_table_access_policy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class TableServiceClient(TablesBaseClient):
The URL to the table service endpoint. Any other entities included
in the URL path (e.g. table) will be discarded. This URL can be optionally
authenticated with a SAS token.
:param credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:type credential:
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
:keyword str api_version:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@

class AsyncTablesBaseClient(AccountHostsMixin):

def __init__(
def __init__( # pylint: disable=missing-client-constructor-parameter-credential
self,
endpoint: str,
*,
credential: Optional[Union[AzureSasCredential, AzureNamedKeyCredential]] = None,
**kwargs: Any
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ class TableClient(AsyncTablesBaseClient):
:ivar str url: The full URL to the Tables account.
"""

def __init__(
def __init__( # pylint: disable=missing-client-constructor-parameter-credential
self,
endpoint: str,
table_name: str,
*,
credential: Optional[Union[AzureSasCredential, AzureNamedKeyCredential]] = None,
**kwargs
) -> None:
"""Create TableClient from a Credential.
:param str endpoint: A URL to an Azure Tables account.
:param str table_name: The table name.
:param credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token, or the connection string already has shared
access key values. The value can be a SAS token string or an account shared access
key.
:type credential:
account URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
Expand Down Expand Up @@ -113,17 +113,16 @@ def from_connection_string(
def from_table_url(
cls,
table_url: str,
credential: Optional[Union[AzureSasCredential, AzureNamedKeyCredential]] = None,
**kwargs
) -> 'TableClient':
"""A client to interact with a specific Table.
:param str table_url: The full URI to the table, including SAS token if used.
:param credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token. The value can be a SAS token string, an account
shared access key.
:type credential:
table URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
:returns: A table client.
Expand Down Expand Up @@ -156,7 +155,7 @@ def from_table_url(
raise ValueError(
"Invalid URL. Please provide a URL with a valid table name"
)
return cls(endpoint, table_name=table_name, credential=credential, **kwargs)
return cls(endpoint, table_name=table_name, **kwargs)

@distributed_trace_async
async def get_table_access_policy(self, **kwargs) -> Mapping[str, TableAccessPolicy]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ class TableServiceClient(AsyncTablesBaseClient):
The URL to the table service endpoint. Any other entities included
in the URL path (e.g. table) will be discarded. This URL can be optionally
authenticated with a SAS token.
:param str credential:
:keyword credential:
The credentials with which to authenticate. This is optional if the
account URL already has a SAS token. The value can be a SAS token string, an account
shared access key.
account URL already has a SAS token. The value can be one of AzureNamedKeyCredential
or AzureSasCredential from azure-core.
:paramtype credential:
:class:`~azure.core.credentials.AzureNamedKeyCredential` or
:class:`~azure.core.credentials.AzureSasCredential`
:keyword str api_version:
The Storage API version to use for requests. Default value is '2019-02-02'.
Setting to an older version may result in reduced feature compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def delete_entity(self):
from azure.core.credentials import AzureNamedKeyCredential

credential = AzureNamedKeyCredential(self.account_name, self.access_key)
table_client = TableClient(endpoint=self.endpoint, credential=credential, table_name=self.table_name)
table_client = TableClient(endpoint=self.endpoint, table_name=self.table_name, credential=credential)

# [START delete_entity]
async with table_client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def delete_entity(self):
from azure.core.credentials import AzureNamedKeyCredential

credential = AzureNamedKeyCredential(self.account_name, self.access_key)
with TableClient(endpoint=self.endpoint, credential=credential, table_name=self.table_name) as table_client:
with TableClient(endpoint=self.endpoint, table_name=self.table_name, credential=credential) as table_client:

# Create entity to delete (to showcase etag)
try:
Expand Down
4 changes: 2 additions & 2 deletions sdk/tables/azure-data-tables/tests/_shared/asynctestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def _create_table(self, ts, prefix=TEST_TABLE_PREFIX, table_list=None):
return table

async def _delete_all_tables(self, account_name, key):
client = TableServiceClient(self.account_url(account_name, "cosmos"), key)
client = TableServiceClient(self.account_url(account_name, "cosmos"), credential=key)
async for table in client.list_tables():
await client.delete_table(table.name)

Expand Down Expand Up @@ -118,7 +118,7 @@ async def _insert_random_entity(self, pk=None, rk=None):

async def _set_up(self, account_name, account_key, url="table"):
account_url = self.account_url(account_name, url)
self.ts = TableServiceClient(account_url, account_key)
self.ts = TableServiceClient(account_url, credential=account_key)
self.table_name = self.get_resource_name("uttable")
self.table = self.ts.get_table_client(self.table_name)
if self.is_live:
Expand Down
Loading

0 comments on commit 996c680

Please sign in to comment.