8000 Add annotation for endpoints to indicate minimum supported API versio… · SnarkyPapi/server-client-python@f7da0db · GitHub
[go: up one dir, main page]

Skip to content

Commit f7da0db

Browse files
t8y8Russell Hay
authored and
Russell Hay
committed
Add annotation for endpoints to indicate minimum supported API version (tableau#124)
* Add annotation for endpoints to indicate minimum supported API version `endpoint.py` gets a new decorator called `api` that takes a version parameter. This gets normalized and then will check `Server.version` before making the API call. If you are calling an endpoint that is newer than the server version you get a nice error message before it even tries a request against the server! This can be extended in the future to be more complex (eg building a registry of supported methods, etc) but for now this is a huge usability win rather than throwning a Server-returned 404! This PR only adds the decorator, actually identifying the minimum for endpoints will be done in a different PR that needs way more manual testing than this did
1 parent 8a0112e commit f7da0db

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tableauserverclient/server/endpoint/endpoint.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
from .exceptions import ServerResponseError
1+
from .exceptions import ServerResponseError, EndpointUnavailableError
2+
from functools import wraps
3+
24
import logging
35

6+
try:
7+
from distutils2.version import NormalizedVersion as Version
8+
except ImportError:
9+
from distutils.version import LooseVersion as Version
410

511
logger = logging.getLogger('tableau.endpoint')
612

@@ -69,3 +75,35 @@ def post_request(self, url, xml_request, content_type='text/xml'):
6975
content=xml_request,
7076
auth_token=self.parent_srv.auth_token,
7177
content_type=content_type)
78+
79+
80+
def api(version):
81+
'''Annotate the minimum supported version for an endpoint.
82+
83+
Checks the version on the server object and compares normalized versions.
84+
It will raise an exception if the server version is > the version specified.
85+
86+
Args:
87+
`version` minimum version that supports the endpoint. String.
88+
Raises:
89+
EndpointUnavailableError
90+
Returns:
91+
None
92+
93+
Example:
94+
>>> @api(version="2.3")
95+
>>> def get(self, req_options=None):
96+
>>> ...
97+
'''
98+
def _decorator(func):
99+
@wraps(func)
100+
def wrapper(self, *args, **kwargs):
101+
server_version = Version(self.parent_srv.version)
102+
minimum_supported = Version(version)
103+
if server_version < minimum_supported:
104+
error = "This endpoint is not available in API version {}. Requires {}".format(
105+
server_version, minimum_supported)
106+
raise EndpointUnavailableError(error)
107+
return func(self, *args, **kwargs)
108+
return wrapper
109+
return _decorator

tableauserverclient/server/endpoint/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ class MissingRequiredFieldError(Exception):
2828

2929
class ServerInfoEndpointNotFoundError(Exception):
3030
pass
31+
32+
33+
class EndpointUnavailableError(Exception):
34+
pass

0 commit comments

Comments
 (0)
0