Skip to content

Commit 35f880c

Browse files
Consistent returns (Azure#13245)
* fixes sync create_entity to return just the metadata * fixes the create_table method to return just metadata * fixes the create_table method to return just metadata * fixed update_entity too * changed upsert_entity too, now to change the async methods * fixed async create and update entity * fixed async create and update entity * updated async methods to return metadata only, updated tests accordingly and reran tests * tox fixes * addressing izzys comments, fixing typehint formats, checking for a correct response instead that it is not None * fixed comments * making the same change for async code * addressing annas comments, trimming metadata returns, removed if_not_match possibility * had an extra space in there causing pylint failure * changed how _trim_service_metadata works
1 parent d25ce0e commit 35f880c

File tree

125 files changed

+3678
-3007
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+3678
-3007
lines changed

sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,12 @@ def _return_headers_and_deserialized(response, deserialized, response_headers):
206206

207207
def _return_context_and_deserialized(response, deserialized, response_headers): # pylint: disable=unused-argument
208208
return response.http_response.location_mode, deserialized, response_headers
209+
210+
211+
def _trim_service_metadata(metadata):
212+
# type: (dict[str,str] -> None)
213+
return {
214+
"date": metadata.pop("date", None),
215+
"etag": metadata.pop("etag", None),
216+
"version": metadata.pop("version", None)
217+
}

sdk/tables/azure-data-tables/azure/data/tables/_table_client.py

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from azure.core.exceptions import HttpResponseError, ResourceNotFoundError
1818
from azure.core.tracing.decorator import distributed_trace
1919

20-
from ._deserialize import _convert_to_entity
20+
from ._deserialize import _convert_to_entity, _trim_service_metadata
2121
from ._entity import TableEntity
2222
from ._generated import AzureTable
2323
from ._generated.models import AccessPolicy, SignedIdentifier, TableProperties, QueryOptions
@@ -28,7 +28,7 @@
2828
from ._deserialize import _return_headers_and_deserialized
2929
from ._error import _process_table_error
3030
from ._version import VERSION
31-
from ._models import TableEntityPropertiesPaged, UpdateMode, TableItem
31+
from ._models import TableEntityPropertiesPaged, UpdateMode
3232

3333

3434
class TableClient(TableClientBase):
@@ -126,7 +126,7 @@ def get_table_access_policy(
126126
self,
127127
**kwargs # type: Any
128128
):
129-
# type: (...) -> dict[str,AccessPolicy]
129+
# type: (...) -> Dict[str,AccessPolicy]
130130
"""Retrieves details about any stored access policies specified on the table that may be
131131
used with Shared Access Signatures.
132132
@@ -148,7 +148,7 @@ def get_table_access_policy(
148148
@distributed_trace
149149
def set_table_access_policy(
150150
self,
151-
signed_identifiers, # type: dict[str,AccessPolicy]
151+
signed_identifiers, # type: Dict[str,AccessPolicy]
152152
**kwargs):
153153
# type: (...) -> None
154154
"""Sets stored access policies for the table that may be used with Shared Access Signatures.
@@ -180,17 +180,19 @@ def create_table(
180180
self,
181181
**kwargs # type: Any
182182
):
183-
# type: (...) -> TableItem
183+
# type: (...) -> Dict[str,str]
184184
"""Creates a new table under the current account.
185185
186-
:return: TableItem created
187-
:rtype: TableItem
186+
:return: Dictionary of operation metadata returned from service
187+
:rtype: dict[str,str]
188188
:raises: ~azure.core.exceptions.HttpResponseError
189189
"""
190190
table_properties = TableProperties(table_name=self.table_name, **kwargs)
191191
try:
192-
table = self._client.table.create(table_properties)
193-
return TableItem(table=table)
192+
metadata, _ = self._client.table.create(
193+
table_properties,
194+
cls=kwargs.pop('cls', _return_headers_and_deserialized))
195+
return _trim_service_metadata(metadata)
194196
except HttpResponseError as error:
195197
_process_table_error(error)
196198

@@ -231,59 +233,57 @@ def delete_entity(
231233
:raises: ~azure.core.exceptions.HttpResponseError
232234
"""
233235

234-
if_match, if_not_match = _get_match_headers(kwargs=dict(kwargs, etag=kwargs.pop('etag', None),
236+
if_match, _ = _get_match_headers(kwargs=dict(kwargs, etag=kwargs.pop('etag', None),
235237
match_condition=kwargs.pop('match_condition', None)),
236238
etag_param='etag', match_param='match_condition')
237239
try:
238240
self._client.table.delete_entity(
239241
table=self.table_name,
240242
partition_key=partition_key,
241243
row_key=row_key,
242-
if_match=if_match or if_not_match or '*',
244+
if_match=if_match or '*',
243245
**kwargs)
244246
except HttpResponseError as error:
245247
_process_table_error(error)
246248

247249
@distributed_trace
248250
def create_entity(
249251
self,
250-
entity, # type: Union[TableEntity, dict[str,str]]
252+
entity, # type: Union[TableEntity, Dict[str,str]]
251253
**kwargs # type: Any
252254
):
253-
# type: (...) -> TableEntity
255+
# type: (...) -> Dict[str,str]
254256
"""Insert entity in a table.
255257
256258
:param entity: The properties for the table entity.
257259
:type entity: Union[TableEntity, dict[str,str]]
258-
:return: TableEntity mapping str to azure.data.tables.EntityProperty
259-
:rtype: ~azure.data.tables.TableEntity
260+
:return: Dictionary mapping operation metadata returned from the service
261+
:rtype: dict[str,str]
260262
:raises: ~azure.core.exceptions.HttpResponseError
261263
"""
262264

263265
if "PartitionKey" in entity and "RowKey" in entity:
264266
entity = _add_entity_properties(entity)
265-
# TODO: Remove - and run test to see what happens with the service
266267
else:
267268
raise ValueError('PartitionKey and RowKey were not provided in entity')
268269
try:
269-
inserted_entity = self._client.table.insert_entity(
270+
metadata, _ = self._client.table.insert_entity(
270271
table=self.table_name,
271272
table_entity_properties=entity,
272-
**kwargs
273-
)
274-
properties = _convert_to_entity(inserted_entity)
275-
return properties
273+
cls=kwargs.pop('cls', _return_headers_and_deserialized),
274+
**kwargs)
275+
return _trim_service_metadata(metadata)
276276
except ResourceNotFoundError as error:
277277
_process_table_error(error)
278278

279279
@distributed_trace
280280
def update_entity( # pylint:disable=R1710
281281
self,
282-
entity, # type: Union[TableEntity, dict[str,str]]
282+
entity, # type: Union[TableEntity, Dict[str,str]]
283283
mode=UpdateMode.MERGE, # type: UpdateMode
284284
**kwargs # type: Any
285285
):
286-
# type: (...) -> None
286+
# type: (...) -> Dict[str,str]
287287
"""Update entity in a table.
288288
289289
:param entity: The properties for the table entity.
@@ -294,33 +294,41 @@ def update_entity( # pylint:disable=R1710
294294
:keyword str row_key: The row key of the entity.
295295
:keyword str etag: Etag of the entity
296296
:keyword ~azure.core.MatchConditions match_condition: MatchCondition
297-
:return: None
298-
:rtype: None
297+
:return: Dictionary mapping operation metadata returned from the service
298+
:rtype: dict[str,str]
299299
:raises: ~azure.core.exceptions.HttpResponseError
300300
"""
301301

302-
if_match, if_not_match = _get_match_headers(kwargs=dict(kwargs, etag=kwargs.pop('etag', None),
302+
if_match, _ = _get_match_headers(kwargs=dict(kwargs, etag=kwargs.pop('etag', None),
303303
match_condition=kwargs.pop('match_condition', None)),
304304
etag_param='etag', match_param='match_condition')
305305

306306
partition_key = entity['PartitionKey']
307307
row_key = entity['RowKey']
308308
entity = _add_entity_properties(entity)
309309
try:
310+
metadata = None
310311
if mode is UpdateMode.REPLACE:
311-
self._client.table.update_entity(
312+
metadata, _ = self._client.table.update_entity(
312313
table=self.table_name,
313314
partition_key=partition_key,
314315
row_key=row_key,
315316
table_entity_properties=entity,
316-
if_match=if_match or if_not_match or "*",
317+
if_match=if_match or "*",
318+
cls=kwargs.pop('cls', _return_headers_and_deserialized),
317319
**kwargs)
318320
elif mode is UpdateMode.MERGE:
319-
self._client.table.merge_entity(table=self.table_name, partition_key=partition_key,
320-
row_key=row_key, if_match=if_match or if_not_match or "*",
321-
table_entity_properties=entity, **kwargs)
321+
metadata, _ = self._client.table.merge_entity(
322+
table=self.table_name,
323+
partition_key=partition_key,
324+
row_key=row_key,
325+
if_match=if_match or "*",
326+
table_entity_properties=entity,
327+
cls=kwargs.pop('cls', _return_headers_and_deserialized),
328+
**kwargs)
322329
else:
323330
raise ValueError('Mode type is not supported')
331+
return _trim_service_metadata(metadata)
324332
except HttpResponseError as error:
325333
_process_table_error(error)
326334

@@ -395,15 +403,15 @@ def get_entity(
395403
row_key, # type: str
396404
**kwargs # type: Any
397405
):
398-
# type: (...) -> TableEntity
406+
# type: (...) -> Dict[str,str]
399407
"""Queries entities in a table.
400408
401409
:param partition_key: The partition key of the entity.
402410
:type partition_key: str
403411
:param row_key: The row key of the entity.
404412
:type row_key: str
405-
:return: Entity mapping str to azure.data.tables.EntityProperty
406-
:rtype: ~azure.data.tables.TableEntity
413+
:return: Dictionary mapping operation metadata returned from the service
414+
:rtype: dict[str,str]
407415
:raises: ~azure.core.exceptions.HttpResponseError
408416
"""
409417
try:
@@ -420,19 +428,19 @@ def get_entity(
420428
@distributed_trace
421429
def upsert_entity( # pylint:disable=R1710
422430
self,
423-
entity, # type: Union[TableEntity, dict[str,str]]
431+
entity, # type: Union[TableEntity, Dict[str,str]]
424432
mode=UpdateMode.MERGE, # type: UpdateMode
425433
**kwargs # type: Any
426434
):
427-
# type: (...) -> None
435+
# type: (...) -> Dict[str,str]
428436
"""Update/Merge or Insert entity into table.
429437
430438
:param entity: The properties for the table entity.
431439
:type entity: Union[TableEntity, dict[str,str]]
432440
:param mode: Merge or Replace and Insert on fail
433441
:type mode: ~azure.data.tables.UpdateMode
434-
:return: None
435-
:rtype: None
442+
:return: Dictionary mapping operation metadata returned from the service
443+
:rtype: dict[str,str]
436444
:raises: ~azure.core.exceptions.HttpResponseError
437445
"""
438446

@@ -441,25 +449,30 @@ def upsert_entity( # pylint:disable=R1710
441449
entity = _add_entity_properties(entity)
442450

443451
try:
452+
metadata = None
444453
if mode is UpdateMode.MERGE:
445-
self._client.table.merge_entity(
454+
metadata, _ = self._client.table.merge_entity(
446455
table=self.table_name,
447456
partition_key=partition_key,
448457
row_key=row_key,
449458
table_entity_properties=entity,
459+
cls=kwargs.pop('cls', _return_headers_and_deserialized),
450460
**kwargs
451461
)
452462
elif mode is UpdateMode.REPLACE:
453-
self._client.table.update_entity(
463+
metadata, _ = self._client.table.update_entity(
454464
table=self.table_name,
455465
partition_key=partition_key,
456466
row_key=row_key,
457467
table_entity_properties=entity,
468+
cls=kwargs.pop('cls', _return_headers_and_deserialized),
458469
**kwargs)
459470
else:
460-
raise ValueError('Mode type is not supported')
471+
raise ValueError("""Update mode {} is not supported.
472+
For a list of supported modes see the UpdateMode enum""".format(mode))
473+
return _trim_service_metadata(metadata)
461474
except ResourceNotFoundError:
462-
self.create_entity(
475+
return self.create_entity(
463476
partition_key=partition_key,
464477
row_key=row_key,
465478
table_entity_properties=entity,

0 commit comments

Comments
 (0)