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

[storage] Queues samples updates #6732

Merged
merged 8 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions sdk/storage/azure-storage-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ from azure.storage.queue import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
queue.create_queue()
```
Create a queue asynchronously.
```python
from azure.storage.queue.aio import QueueClient

queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
await queue.create_queue()
```
### Enqueue messages
Enqueue a message in your queue.

Expand All @@ -124,6 +131,15 @@ queue = QueueClient.from_connection_string(conn_str="my_connection_string", queu
queue.enqueue_message("I'm using queues!")
queue.enqueue_message("This is my second message")
```
Enqueue messages with an async client
```python
from azure.storage.queue.aio import QueueClient

queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
await asyncio.gather(
queue.enqueue_message("I'm using queues!"),
queue.enqueue_message("This is my second message"))
```

### Receive messages
Receive messages from your queue.
Expand All @@ -136,11 +152,34 @@ response = queue.receive_messages()

for message in response:
print(message.content)
queue.delete_message(message)

# Printed messages from the front of the queue
# >>I'm using queues!
# >>This is my second message
```
Receive messages by batch.
```python
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
response = queue.receive_messages(messages_per_page=10)

for message_batch in response.by_page():
for message in message_batch:
print(message.content)
queue.delete_message(message)
```
Receive messages asynchronously:
```python
from azure.storage.queue.aio import QueueClient

queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
response = queue.receive_messages()

async for message in response:
print(message.content)
await queue.delete_message(message)

```

## Troubleshooting
Storage Queue clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md).
Expand All @@ -154,25 +193,25 @@ Get started with our [Queue samples](https://github.com/Azure/azure-sdk-for-pyth

Several Storage Queues Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Storage Queues:

* [`test_queue_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world.py) - Examples found in this article:
* [`test_queue_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world_async.py)) - Examples found in this article:
* Client creation
* Create a queue
* Enqueue messages
* Receive messages

* [`test_queue_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication.py) - Examples for authenticating and creating the client:
* [`test_queue_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication_async.py)) - Examples for authenticating and creating the client:
* From a connection string
* From a shared access key
* From a shared access signature token
* From active directory

* [`test_queue_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service.py) - Examples for interacting with the queue service:
* [`test_queue_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service_async.py)) - Examples for interacting with the queue service:
* Get and set service properties
* List queues in a storage account
* Create and delete a queue from the service
* Get the QueueClient

* [`test_queue_samples_message.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message.py) - Examples for working with queues and messages:
* [`test_queue_samples_message.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message_async.py)) - Examples for working with queues and messages:
* Set an access policy
* Get and set queue metadata
* Enqueue and receive messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,19 @@ class QueueClient(AsyncStorageAccountHostsMixin, QueueClientBase):
shared access key, or an instance of a TokenCredentials class from azure.identity.

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START create_queue_client]
:end-before: [END create_queue_client]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_create_queue_client]
:end-before: [END async_create_queue_client]
:language: python
:dedent: 12
:caption: Create the queue client with url and credential.

.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_create_queue_client_from_connection_string]
:end-before: [END async_create_queue_client_from_connection_string]
:language: python
:dedent: 8
:caption: Create the queue client with a connection string.
"""

def __init__(
Expand Down Expand Up @@ -127,9 +134,9 @@ async def create_queue(self, metadata=None, timeout=None, **kwargs): # type: ig
~azure.storage.queue._generated.models._models.StorageErrorException

Example:
.. literalinclude:: ../tests/test_queue_samples_hello_world.py
:start-after: [START create_queue]
:end-before: [END create_queue]
.. literalinclude:: ../tests/test_queue_samples_hello_world_async.py
:start-after: [START async_create_queue]
:end-before: [END async_create_queue]
:language: python
:dedent: 8
:caption: Create a queue.
Expand Down Expand Up @@ -161,9 +168,9 @@ async def delete_queue(self, timeout=None, **kwargs): # type: ignore
:rtype: None

Example:
.. literalinclude:: ../tests/test_queue_samples_hello_world.py
:start-after: [START delete_queue]
:end-before: [END delete_queue]
.. literalinclude:: ../tests/test_queue_samples_hello_world_async.py
:start-after: [START async_delete_queue]
:end-before: [END async_delete_queue]
:language: python
:dedent: 12
:caption: Delete a queue.
Expand All @@ -186,9 +193,9 @@ async def get_queue_properties(self, timeout=None, **kwargs): # type: ignore
:rtype: ~azure.storage.queue.models.QueueProperties

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START get_queue_properties]
:end-before: [END get_queue_properties]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_get_queue_properties]
:end-before: [END async_get_queue_properties]
:language: python
:dedent: 12
:caption: Get the properties on the queue.
Expand Down Expand Up @@ -217,9 +224,9 @@ async def set_queue_metadata(self, metadata=None, timeout=None, **kwargs): # ty
The server timeout, expressed in seconds.

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START set_queue_metadata]
:end-before: [END set_queue_metadata]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_set_queue_metadata]
:end-before: [END async_set_queue_metadata]
:language: python
:dedent: 12
:caption: Set metadata on the queue.
Expand Down Expand Up @@ -278,9 +285,9 @@ async def set_queue_access_policy(self, signed_identifiers=None, timeout=None, *
The server timeout, expressed in seconds.

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START set_access_policy]
:end-before: [END set_access_policy]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_set_access_policy]
:end-before: [END async_set_access_policy]
:language: python
:dedent: 12
:caption: Set an access policy on the queue.
Expand Down Expand Up @@ -350,9 +357,9 @@ async def enqueue_message( # type: ignore
:rtype: ~azure.storage.queue.models.QueueMessage

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START enqueue_messages]
:end-before: [END enqueue_messages]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_enqueue_messages]
:end-before: [END async_enqueue_messages]
:language: python
:dedent: 12
:caption: Enqueue messages.
Expand Down Expand Up @@ -414,9 +421,9 @@ def receive_messages(self, messages_per_page=None, visibility_timeout=None, time
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.queue.models.Message]

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START receive_messages]
:end-before: [END receive_messages]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_receive_messages]
:end-before: [END async_receive_messages]
:language: python
:dedent: 12
:caption: Receive messages from the queue.
Expand Down Expand Up @@ -484,9 +491,9 @@ async def update_message(
:rtype: ~azure.storage.queue.models.QueueMessage

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START update_message]
:end-before: [END update_message]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_update_message]
:end-before: [END async_update_message]
:language: python
:dedent: 12
:caption: Update a message.
Expand Down Expand Up @@ -567,9 +574,9 @@ async def peek_messages(self, max_messages=None, timeout=None, **kwargs): # typ
:rtype: list(:class:`~azure.storage.queue.models.QueueMessage`)

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START peek_message]
:end-before: [END peek_message]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_peek_message]
:end-before: [END async_peek_message]
:language: python
:dedent: 12
:caption: Peek messages.
Expand Down Expand Up @@ -599,9 +606,9 @@ async def clear_messages(self, timeout=None, **kwargs): # type: ignore
The server timeout, expressed in seconds.

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START clear_messages]
:end-before: [END clear_messages]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_clear_messages]
:end-before: [END async_clear_messages]
:language: python
:dedent: 12
:caption: Clears all messages.
Expand Down Expand Up @@ -635,9 +642,9 @@ async def delete_message(self, message, pop_receipt=None, timeout=None, **kwargs
The server timeout, expressed in seconds.

Example:
.. literalinclude:: ../tests/test_queue_samples_message.py
:start-after: [START delete_message]
:end-before: [END delete_message]
.. literalinclude:: ../tests/test_queue_samples_message_async.py
:start-after: [START async_delete_message]
:end-before: [END async_delete_message]
:language: python
:dedent: 12
:caption: Delete a message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ class QueueServiceClient(AsyncStorageAccountHostsMixin, QueueServiceClientBase):
shared access key, or an instance of a TokenCredentials class from azure.identity.

Example:
.. literalinclude:: ../tests/test_queue_samples_authentication.py
:start-after: [START create_queue_service_client]
:end-before: [END create_queue_service_client]
.. literalinclude:: ../tests/test_queue_samples_authentication_async.py
:start-after: [START async_create_queue_service_client]
:end-before: [END async_create_queue_service_client]
:language: python
:dedent: 8
:caption: Creating the QueueServiceClient with an account url and credential.

.. literalinclude:: ../tests/test_queue_samples_authentication_async.py
:start-after: [START async_create_queue_service_client_token]
:end-before: [END async_create_queue_service_client_token]
:language: python
:dedent: 8
:caption: Creating the QueueServiceClient with Azure Identity credentials.
"""

def __init__(
Expand Down Expand Up @@ -142,9 +149,9 @@ async def get_service_properties(self, timeout=None, **kwargs): # type: ignore
:rtype: ~azure.storage.queue._generated.models._models.StorageServiceProperties

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START get_queue_service_properties]
:end-before: [END get_queue_service_properties]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_get_queue_service_properties]
:end-before: [END async_get_queue_service_properties]
:language: python
:dedent: 8
:caption: Getting queue service properties.
Expand Down Expand Up @@ -191,9 +198,9 @@ async def set_service_properties( # type: ignore
:rtype: None

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START set_queue_service_properties]
:end-before: [END set_queue_service_properties]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_set_queue_service_properties]
:end-before: [END async_set_queue_service_properties]
:language: python
:dedent: 8
:caption: Setting queue service properties.
Expand Down Expand Up @@ -238,9 +245,9 @@ def list_queues(
:rtype: ~azure.core.paging.AsyncItemPaged[~azure.core.queue.models.QueueProperties]

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START qsc_list_queues]
:end-before: [END qsc_list_queues]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_qsc_list_queues]
:end-before: [END async_qsc_list_queues]
:language: python
:dedent: 12
:caption: List queues in the service.
Expand Down Expand Up @@ -277,12 +284,12 @@ async def create_queue( # type: ignore
:type metadata: dict(str, str)
:param int timeout:
The timeout parameter is expressed in seconds.
:rtype: ~azure.storage.queue.queue_client.QueueClient
:rtype: ~azure.storage.queue.aio.queue_client_async.QueueClient

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START qsc_create_queue]
:end-before: [END qsc_create_queue]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_qsc_create_queue]
:end-before: [END async_qsc_create_queue]
:language: python
:dedent: 8
:caption: Create a queue in the service.
Expand Down Expand Up @@ -318,9 +325,9 @@ async def delete_queue( # type: ignore
:rtype: None

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START qsc_delete_queue]
:end-before: [END qsc_delete_queue]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_qsc_delete_queue]
:end-before: [END async_qsc_delete_queue]
:language: python
:dedent: 12
:caption: Delete a queue in the service.
Expand All @@ -339,12 +346,12 @@ def get_queue_client(self, queue, **kwargs):
or an instance of QueueProperties.
:type queue: str or ~azure.storage.queue.models.QueueProperties
:returns: A :class:`~azure.core.queue.queue_client.QueueClient` object.
:rtype: ~azure.core.queue.queue_client.QueueClient
:rtype: ~azure.storage.queue.aio.queue_client_async.QueueClient

Example:
.. literalinclude:: ../tests/test_queue_samples_service.py
:start-after: [START get_queue_client]
:end-before: [END get_queue_client]
.. literalinclude:: ../tests/test_queue_samples_service_async.py
:start-after: [START async_get_queue_client]
:end-before: [END async_get_queue_client]
:language: python
:dedent: 8
:caption: Get the queue client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class QueueServiceClient(StorageAccountHostsMixin):
:language: python
:dedent: 8
:caption: Creating the QueueServiceClient with an account url and credential.

.. literalinclude:: ../tests/test_queue_samples_authentication.py
:start-after: [START create_queue_service_client_token]
:end-before: [END create_queue_service_client_token]
:language: python
:dedent: 8
:caption: Creating the QueueServiceClient with Azure Identity credentials.
"""

def __init__(
Expand Down
Loading