Skip to content

Commit 3cdde0b

Browse files
authored
[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
1 parent 978b029 commit 3cdde0b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _from_cncf_events(event): # pylint: disable=inconsistent-return-statements
166166
raise ValueError(msg) from err
167167

168168

169-
def _build_request(endpoint, content_type, events, *, channel_name=None):
169+
def _build_request(endpoint, content_type, events, *, channel_name=None, api_version=constants.DEFAULT_API_VERSION):
170170
serialize = Serializer()
171171
header_parameters: Dict[str, Any] = {}
172172
header_parameters['Content-Type'] = serialize.header("content_type", content_type, 'str')
@@ -175,7 +175,7 @@ def _build_request(endpoint, content_type, events, *, channel_name=None):
175175
header_parameters['aeg-channel-name'] = channel_name
176176

177177
query_parameters: Dict[str, Any] = {}
178-
query_parameters['api-version'] = serialize.query("api_version", "2018-01-01", 'str')
178+
query_parameters['api-version'] = serialize.query("api_version", api_version, 'str')
179179

180180
body = serialize.body(events, '[object]')
181181
if body is None:

sdk/eventgrid/azure-eventgrid/azure/eventgrid/_publisher_client.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
EventGridPublisherClient as EventGridPublisherClientImpl,
4545
)
4646
from ._policies import CloudEventDistributedTracingPolicy
47+
from ._constants import DEFAULT_API_VERSION
4748
from ._version import VERSION
4849

4950
if TYPE_CHECKING:
@@ -79,6 +80,9 @@ class EventGridPublisherClient(object): # pylint: disable=client-accepts-api-ver
7980
implements SAS key authentication or SAS token authentication or a TokenCredential.
8081
:type credential: ~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.AzureSasCredential or
8182
~azure.core.credentials.TokenCredential
83+
:keyword api_version: Api Version. Will default to the most recent Api Version. Note that overriding this
84+
default value may result in unsupported behavior.
85+
:paramtype api_version: str
8286
:rtype: None
8387
8488
.. admonition:: Example:
@@ -98,12 +102,19 @@ class EventGridPublisherClient(object): # pylint: disable=client-accepts-api-ver
98102
:caption: Creating the EventGridPublisherClient with an endpoint and AzureSasCredential.
99103
"""
100104

101-
def __init__(self, endpoint, credential, **kwargs):
102-
# type: (str, Union[AzureKeyCredential, AzureSasCredential, TokenCredential], Any) -> None
105+
def __init__(
106+
self,
107+
endpoint: str,
108+
credential: Union["AzureKeyCredential", "AzureSasCredential", "TokenCredential"],
109+
*,
110+
api_version: Optional[str] = None,
111+
**kwargs: Any
112+
) -> None:
103113
self._endpoint = endpoint
104114
self._client = EventGridPublisherClientImpl(
105115
policies=EventGridPublisherClient._policies(credential, **kwargs), **kwargs
106116
)
117+
self._api_version = api_version if api_version is not None else DEFAULT_API_VERSION
107118

108119
@staticmethod
109120
def _policies(credential, **kwargs):
@@ -221,7 +232,9 @@ def send(
221232
for event in events:
222233
_eventgrid_data_typecheck(event)
223234
response = self._client.send_request( # pylint: disable=protected-access
224-
_build_request(self._endpoint, content_type, events, channel_name=channel_name), **kwargs
235+
_build_request(
236+
self._endpoint,content_type, events, channel_name=channel_name, api_version=self._api_version),
237+
**kwargs
225238
)
226239
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
227240
if response.status_code != 200:

sdk/eventgrid/azure-eventgrid/azure/eventgrid/aio/_publisher_client_async.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
_get_authentication_policy,
4343
_from_cncf_events,
4444
)
45+
from .._constants import DEFAULT_API_VERSION
4546
from .._generated.aio import EventGridPublisherClient as EventGridPublisherClientAsync
4647
from .._version import VERSION
4748

@@ -73,6 +74,9 @@ class EventGridPublisherClient: # pylint: disable=client-accepts-api-version-key
7374
SAS key authentication or SAS token authentication or an AsyncTokenCredential.
7475
:type credential: ~azure.core.credentials.AzureKeyCredential or ~azure.core.credentials.AzureSasCredential or
7576
~azure.core.credentials_async.AsyncTokenCredential
77+
:keyword api_version: Api Version. Will default to the most recent Api Version. Note that overriding this
78+
default value may result in unsupported behavior.
79+
:paramtype api_version: str
7680
:rtype: None
7781
7882
.. admonition:: Example:
@@ -98,12 +102,15 @@ def __init__(
98102
credential: Union[
99103
"AsyncTokenCredential", AzureKeyCredential, AzureSasCredential
100104
],
105+
*,
106+
api_version: Optional[str] = None,
101107
**kwargs: Any
102108
) -> None:
103109
self._client = EventGridPublisherClientAsync(
104110
policies=EventGridPublisherClient._policies(credential, **kwargs), **kwargs
105111
)
106112
self._endpoint = endpoint
113+
self._api_version = api_version if api_version is not None else DEFAULT_API_VERSION
107114

108115
@staticmethod
109116
def _policies(
@@ -221,7 +228,9 @@ async def send(self, events: SendType, *, channel_name: Optional[str] = None, **
221228
for event in events:
222229
_eventgrid_data_typecheck(event)
223230
response = await self._client.send_request( # pylint: disable=protected-access
224-
_build_request(self._endpoint, content_type, events, channel_name=channel_name), **kwargs
231+
_build_request(self._endpoint, content_type, events,
232+
channel_name=channel_name, api_version=self._api_version),
233+
**kwargs
225234
)
226235
error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
227236
if response.status_code != 200:

0 commit comments

Comments
 (0)