From c8b92f5a8851a2a171aff00f04073f2c7eabc0aa Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 27 Mar 2024 12:38:26 -0700 Subject: [PATCH] [EG] dont hardcode api_version on request (#34965) * dont hardcode api_version on request * pylint fixes * revert * api version * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py * Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py --- .../azure/eventgrid/_legacy/_helpers.py | 4 ++-- .../eventgrid/_legacy/_publisher_client.py | 20 ++++++++++++++----- .../_legacy/aio/_publisher_client_async.py | 12 ++++++++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_helpers.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_helpers.py index fbc84fe522b1..0e7f08461e26 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_helpers.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_helpers.py @@ -172,7 +172,7 @@ def _from_cncf_events(event): # pylint: disable=inconsistent-return-statements raise ValueError(msg) from err -def _build_request(endpoint, content_type, events, *, channel_name=None): +def _build_request(endpoint, content_type, events, *, channel_name=None, api_version=constants.DEFAULT_API_VERSION): serialize = Serializer() header_parameters: Dict[str, Any] = {} header_parameters['Content-Type'] = serialize.header("content_type", content_type, 'str') @@ -181,7 +181,7 @@ def _build_request(endpoint, content_type, events, *, channel_name=None): header_parameters["aeg-channel-name"] = channel_name query_parameters: Dict[str, Any] = {} - query_parameters['api-version'] = serialize.query("api_version", "2018-01-01", 'str') + query_parameters['api-version'] = serialize.query("api_version", api_version, 'str') body = serialize.body(events, "[object]") if body is None: diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_publisher_client.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_publisher_client.py index 695ad7e9c42e..409644f893dc 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_publisher_client.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/_publisher_client.py @@ -44,6 +44,7 @@ EventGridPublisherClient as EventGridPublisherClientImpl, ) from ._policies import CloudEventDistributedTracingPolicy +from ._constants import DEFAULT_API_VERSION from ._version import VERSION if TYPE_CHECKING: @@ -81,6 +82,9 @@ class EventGridPublisherClient( implements SAS key authentication or SAS token authentication or a TokenCredential. :type credential: ~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.AzureSasCredential or ~azure.core.credentials.TokenCredential + :keyword api_version: Api Version. Will default to the most recent Api Version. Note that overriding this + default value may result in unsupported behavior. + :paramtype api_version: str :rtype: None .. admonition:: Example: @@ -100,12 +104,19 @@ class EventGridPublisherClient( :caption: Creating the EventGridPublisherClient with an endpoint and AzureSasCredential. """ - def __init__(self, endpoint, credential, **kwargs): - # type: (str, Union[AzureKeyCredential, AzureSasCredential, TokenCredential], Any) -> None + def __init__( + self, + endpoint: str, + credential: Union["AzureKeyCredential", "AzureSasCredential", "TokenCredential"], + *, + api_version: Optional[str] = None, + **kwargs: Any + ) -> None: self._endpoint = endpoint self._client = EventGridPublisherClientImpl( policies=EventGridPublisherClient._policies(credential, **kwargs), **kwargs ) + self._api_version = api_version if api_version is not None else DEFAULT_API_VERSION @staticmethod def _policies(credential, **kwargs): @@ -217,9 +228,8 @@ def send( _eventgrid_data_typecheck(event) response = self._client.send_request( # pylint: disable=protected-access _build_request( - self._endpoint, content_type, events, channel_name=channel_name - ), - **kwargs + self._endpoint,content_type, events, channel_name=channel_name, api_version=self._api_version), + **kwargs ) error_map = { 401: ClientAuthenticationError, diff --git a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/aio/_publisher_client_async.py b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/aio/_publisher_client_async.py index b57d9dee8c0b..692451987fd7 100644 --- a/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/aio/_publisher_client_async.py +++ b/sdk/eventgrid/azure-eventgrid/azure/eventgrid/_legacy/aio/_publisher_client_async.py @@ -42,6 +42,7 @@ _get_authentication_policy, _from_cncf_events, ) +from .._constants import DEFAULT_API_VERSION from .._generated.aio import EventGridPublisherClient as EventGridPublisherClientAsync from .._version import VERSION @@ -73,6 +74,9 @@ class EventGridPublisherClient: # pylint: disable=client-accepts-api-version-ke SAS key authentication or SAS token authentication or an AsyncTokenCredential. :type credential: ~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.AzureSasCredential or ~azure.core.credentials_async.AsyncTokenCredential + :keyword api_version: Api Version. Will default to the most recent Api Version. Note that overriding this + default value may result in unsupported behavior. + :paramtype api_version: str :rtype: None .. admonition:: Example: @@ -98,12 +102,15 @@ def __init__( credential: Union[ "AsyncTokenCredential", AzureKeyCredential, AzureSasCredential ], + *, + api_version: Optional[str] = None, **kwargs: Any ) -> None: self._client = EventGridPublisherClientAsync( policies=EventGridPublisherClient._policies(credential, **kwargs), **kwargs ) self._endpoint = endpoint + self._api_version = api_version if api_version is not None else DEFAULT_API_VERSION @staticmethod def _policies( @@ -220,9 +227,8 @@ async def send( for event in events: _eventgrid_data_typecheck(event) response = await self._client.send_request( # pylint: disable=protected-access - _build_request( - self._endpoint, content_type, events, channel_name=channel_name - ), + _build_request(self._endpoint, content_type, events, + channel_name=channel_name, api_version=self._api_version), **kwargs ) error_map = {