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

Missing enter, exit and close on publisher clients #16321

Merged
merged 1 commit into from
Jan 25, 2021
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
15 changes: 15 additions & 0 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,18 @@ def send_events(self, events, **kwargs):
self._client.publish_custom_event_events(self._endpoint, cast(List, serialized_events), **kwargs)
else:
raise ValueError("Event schema is not correct.")

def close(self):
# type: () -> None
"""Close the :class:`~azure.eventgrid.EventGridPublisherClient` session.
"""
return self._client.close()

def __enter__(self):
# type: () -> EventGridPublisherClient
self._client.__enter__() # pylint:disable=no-member
return self

def __exit__(self, *args):
# type: (*Any) -> None
self._client.__exit__(*args) # pylint:disable=no-member
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,15 @@ async def send_events(
)
else:
raise ValueError("Event schema is not correct.")

async def __aenter__(self) -> "EventGridPublisherClient":
await self._client.__aenter__()
return self

async def __aexit__(self, *args: "Any") -> None:
await self._client.__aexit__(*args)

async def close(self) -> None:
"""Close the :class:`~azure.eventgrid.aio.EventGridPublisherClient` session.
"""
await self._client.__aexit__()
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,17 @@ async def test_send_custom_schema_event_as_list(self, resource_group, eventgrid_
}
)
await client.send_events([custom_event1, custom_event2])

@CachedResourceGroupPreparer(name_prefix='eventgridtest')
@CachedEventGridTopicPreparer(name_prefix='cloudeventgridtest')
@pytest.mark.asyncio
async def test_send_and_close_async_session(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
client = EventGridPublisherClient(eventgrid_topic_endpoint, akc_credential)
async with client: # this throws if client can't close
cloud_event = CloudEvent(
source = "http://samplesource.dev",
data = "cloudevent",
type="Sample.Cloud.Event"
)
await client.send_events(cloud_event)