|
1 | | - |
2 | | - |
3 | 1 | # -------------------------------------------------------------------------- |
4 | 2 | # Copyright (c) Microsoft Corporation. All rights reserved. |
5 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
6 | 4 | # Code generated by Microsoft (R) Python Code Generator. |
7 | 5 | # Changes may cause incorrect behavior and will be lost if the code is regenerated. |
8 | 6 | # -------------------------------------------------------------------------- |
9 | | -from typing import Optional, Dict, List |
| 7 | +import functools |
10 | 8 |
|
11 | 9 |
|
12 | | -class api_version_validation: |
13 | | - def __init__( |
14 | | - self, |
15 | | - *, |
16 | | - api_versions: Optional[List[str]] = None, |
17 | | - params: Optional[Dict[str, List[str]]] = None, |
18 | | - ): |
19 | | - self.api_versions = api_versions or [] |
20 | | - self.params = params or {} |
| 10 | +def api_version_validation(**kwargs): |
| 11 | + params_valid_on = kwargs.pop("params_valid_on", {}) |
| 12 | + method_valid_on = kwargs.pop("method_valid_on", {}) |
21 | 13 |
|
22 | | - def __call__(self, func): |
23 | | - api_versions = self.api_versions |
24 | | - params = self.params |
25 | | - def wrapper(self, *args, **kwargs): |
| 14 | + def decorator(func): |
| 15 | + @functools.wraps(func) |
| 16 | + def wrapper(*args, **kwargs): |
26 | 17 | func_name = func.__name__ |
27 | | - if hasattr(self, "_get_api_version"): |
28 | | - client_api_version = self._get_api_version(func_name) |
29 | | - else: |
30 | | - client_api_version = self._api_version |
31 | | - if api_versions and client_api_version not in api_versions: |
| 18 | + try: |
| 19 | + client = args[0] |
| 20 | + client_api_version = client._get_api_version(func_name) # pylint: disable=protected-access |
| 21 | + except AttributeError: |
| 22 | + client_api_version = client._api_version # pylint: disable=protected-access |
| 23 | + |
| 24 | + if method_valid_on and client_api_version not in method_valid_on: |
32 | 25 | raise ValueError( |
33 | 26 | f"'{func_name}' is not available in API version " |
34 | | - f"{client_api_version}. Pass service API version {api_versions[0]} or newer to your client." |
| 27 | + f"{client_api_version}. All valid API version are {', '.join(method_valid_on)}." |
35 | 28 | ) |
36 | | - unsupported = [ |
37 | | - parameter |
38 | | - for parameter, api_versions in params.items() |
| 29 | + |
| 30 | + unsupported = { |
| 31 | + parameter: ", ".join(api_versions) |
| 32 | + for parameter, api_versions in params_valid_on.items() |
39 | 33 | if parameter in kwargs and client_api_version not in api_versions |
40 | | - ] |
| 34 | + } |
41 | 35 | if unsupported: |
42 | 36 | raise ValueError( |
43 | 37 | "".join( |
44 | 38 | [ |
45 | 39 | f"'{param}' is not available in API version {client_api_version}. " |
46 | | - f"Use service API version {params[param][0]} or newer.\n" |
47 | | - for param, version in unsupported |
| 40 | + f"All valid API version are {versions} \n" |
| 41 | + for param, versions in unsupported.items() |
48 | 42 | ] |
49 | 43 | ) |
50 | 44 | ) |
51 | | - return func(self, *args, **kwargs) |
| 45 | + return func(*args, **kwargs) |
52 | 46 |
|
53 | 47 | return wrapper |
| 48 | + |
| 49 | + return decorator |
0 commit comments