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

Added Vision NPB API version support #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions RestApi/Python/ksvisionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
# - Added method disableCtePortGroup
# - Added method enableCtePortGroup
# - Added method modifyCtePortGroup
# February 15, 2021
# - Added API versioning support
#
# COPYRIGHT 2019-2021 Keysight Technologies.
#
Expand Down Expand Up @@ -201,7 +203,7 @@ class UnknownError(KeysightNpbExceptions):

class VisionWebApi(object):

def __init__(self, host, username, password, port=8000, debug=False, logFile=None, timeout=30, retries=2):
def __init__(self, host, username, password, port=8000, version=None, debug=False, logFile=None, timeout=30, retries=2):
#urllib3.disable_warnings()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
self.host = host
Expand All @@ -212,6 +214,7 @@ def __init__(self, host, username, password, port=8000, debug=False, logFile=Non
self.auth_b64 = ''
self.password_headers = ''
self.token_headers = ''
self.version = version
self.connection = ''
self.logFile = logFile
self.__request_timeout = timeout
Expand Down Expand Up @@ -266,7 +269,10 @@ def _sendRequest(self, httpMethod, url, args=None, decode=True):
self._log (" args={:s}\n".format(str(args)))

args = json.dumps(args)
response = self.connection.urlopen(httpMethod, url, body=args, headers=self.token_headers)
request_headers = self.token_headers
if self.version:
request_headers.update({'Version': self.version})
Comment on lines +273 to +274
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This very interesting, I didn't know we could select the API version. Very cool!

When using the curl command, 'version' has to be all lower case, otherwise it is ignored. Does 'Version' work in Python?
% curl -s -k -u admin:admin 'https://192.168.29.143:9000/api/ports/P3-32?version=9.0.0'
Unknown Web API version: 9.0.0
% curl -s -k -u admin:admin 'https://192.168.29.143:9000/api/ports/P3-32?Version=9.0.0'
{
"afm_pipeline_direction": "INGRESS",
"burst_buffer_settings": null,
"cdr_bypass_enabled": false,

Isn't it better to have these changes in the init function? That way we only add the version header only once.

If we make the library to support the following syntax:
% curl -s -k -u admin:admin 'https://192.168.29.143:9000/api/ports/P3-32?version=4.4.0'
{
"afm_pipeline_direction": "INGRESS",
"burst_buffer_settings": null,

or
% curl -s -k -u admin:admin 'https://192.168.29.143:9000/api/ports/P3-32?{properties=id&version=4.4.0}'
{"id": 246}
then it will make sense to implement the changes in the _sendRequest function. But then we will have to modify every method so that we could pass the version, or define a setter for the version variable, i.e., @version.setter.

response = self.connection.urlopen(httpMethod, url, body=args, headers=request_headers)

if self.__debug:
self._log ("Response:\n")
Expand Down