Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update async #12978

Merged
merged 8 commits into from
Aug 14, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
Any,
)

try:
from urllib.parse import urlparse, unquote
except ImportError:
from urlparse import urlparse # type: ignore
from urllib2 import unquote # type: ignore

from azure.core.async_paging import AsyncItemPaged
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
from azure.core.tracing.decorator import distributed_trace
Expand Down Expand Up @@ -67,6 +73,68 @@ def __init__(
self._client._config.version = kwargs.get('api_version', VERSION) # pylint: disable = W0212
self._loop = loop

@classmethod
async def from_connection_string(
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
cls, conn_str, # type: str
table_name, # type: str
**kwargs # type: Any
):
# type: (...) -> TableClient
"""Create TableClient from a Connection String.

:param conn_str:
A connection string to an Azure Storage or Cosmos account.
:type conn_str: str
:param table_name: The table name.
:type table_name: str
:returns: A table client.
:rtype: ~azure.data.tables.TableClient
"""
account_url, secondary, credential = parse_connection_str(
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
conn_str=conn_str, credential=None, service='table')
if 'secondary_hostname' not in kwargs:
kwargs['secondary_hostname'] = secondary
return cls(account_url, table_name=table_name, credential=credential, **kwargs) # type: ignore

@classmethod
async def from_table_url(cls, table_url, credential=None, **kwargs):
# type: (str, Optional[Any], Any) -> TableClient
"""A client to interact with a specific Table.

:param table_url: The full URI to the table, including SAS token if used.
:type table_url: str
:param 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
:returns: A table client.
:rtype: ~azure.data.tables.TableClient
"""
try:
if not table_url.lower().startswith('http'):
table_url = "https://" + table_url
except AttributeError:
raise ValueError("Table URL must be a string.")
parsed_url = urlparse(table_url.rstrip('/'))

if not parsed_url.netloc:
raise ValueError("Invalid URL: {}".format(table_url))

table_path = parsed_url.path.lstrip('/').split('/')
account_path = ""
if len(table_path) > 1:
account_path = "/" + "/".join(table_path[:-1])
account_url = "{}://{}{}?{}".format(
parsed_url.scheme,
parsed_url.netloc.rstrip('/'),
account_path,
parsed_url.query)
table_name = unquote(table_path[-1])
if not table_name:
raise ValueError("Invalid URL. Please provide a URL with a valid table name")
return cls(account_url, table_name=table_name, credential=credential, **kwargs)

@distributed_trace_async
async def get_table_access_policy(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ def __init__(
self._client._config.version = kwargs.get('api_version', VERSION) # pylint: disable=protected-access
self._loop = loop

@classmethod
async def from_connection_string(
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
cls, conn_str, # type: any
**kwargs # type: Any
): # type: (...) -> TableServiceClient
"""Create TableServiceClient from a Connection String.

:param conn_str:
A connection string to an Azure Storage or Cosmos account.
:type conn_str: str
:returns: A Table service client.
:rtype: ~azure.data.tables.TableServiceClient
"""
account_url, secondary, credential = parse_connection_str(
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
conn_str=conn_str, credential=None, service='table')
if 'secondary_hostname' not in kwargs:
kwargs['secondary_hostname'] = secondary
return cls(account_url, credential=credential, **kwargs)

@distributed_trace_async
async def get_service_stats(self, **kwargs):
# type: (...) -> dict[str,object]
Expand Down Expand Up @@ -175,11 +194,8 @@ async def create_table(
:rtype: ~azure.data.tables.TableClient or None
:raises: ~azure.core.exceptions.HttpResponseError
"""
_validate_table_name(table_name)

table_properties = TableProperties(table_name=table_name, **kwargs)
await self._client.table.create(table_properties=table_properties, **kwargs)
table = self.get_table_client(table=table_name)
table = self.get_table_client(table_name=table_name)
await table.create_table(**kwargs)
return table

@distributed_trace_async
Expand All @@ -196,9 +212,8 @@ async def delete_table(
:return: None
:rtype: ~None
"""
_validate_table_name(table_name)

await self._client.table.delete(table=table_name, **kwargs)
table = self.get_table_client(table_name=table_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're losing checks to validate the table name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation is done here in the base client when it's attempted to instantiate, it would throw the same error.

await table.delete_table(**kwargs)

@distributed_trace
def list_tables(
Expand Down Expand Up @@ -231,8 +246,7 @@ def list_tables(

@distributed_trace
def query_tables(
self,
filter, # pylint: disable=W0622
self, filter, # type: str pylint: disable=W0622
kristapratico marked this conversation as resolved.
Show resolved Hide resolved
**kwargs # type: Any
):
# type: (...) -> AsyncItemPaged[Table]
Expand Down Expand Up @@ -262,8 +276,11 @@ def query_tables(
page_iterator_class=TablePropertiesPaged
)

def get_table_client(self, table, **kwargs):
# type: (Union[TableProperties, str], Optional[Any]) -> TableClient
def get_table_client(
self, table_name, # type: Union[TableProperties, str]
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
**kwargs # type: Optional[Any]
):
# type: (...) -> TableClient
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
"""Get a client to interact with the specified table.

The table need not already exist.
Expand All @@ -276,10 +293,6 @@ def get_table_client(self, table, **kwargs):
:rtype: ~azure.data.tables.TableClient

"""
try:
table_name = table.name
except AttributeError:
table_name = table

_pipeline = AsyncPipeline(
transport=AsyncTransportWrapper(self._pipeline._transport), # pylint: disable = protected-access
Expand Down
2 changes: 1 addition & 1 deletion sdk/tables/azure-data-tables/tests/test_table_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ async def test_set_table_acl_with_signed_identifiers(self, resource_group, locat
pytest.skip("Cosmos endpoint does not support this")
ts = TableServiceClient(url, storage_account_key)
table = await self._create_table(ts)
client = ts.get_table_client(table=table.table_name)
client = ts.get_table_client(table_name=table.table_name)

# Act
identifiers = dict()
Expand Down