Minimal Python module for using the GNS3 API for both v2 and v3 #2389
ghost
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently I use both GNS3 v2.2 and v3.0(beta). When using the API I would like to use the same python module supporting both versions. So I created a tiny one, that just fulfills my basic needs.
It get initialized by
api = gns3api_mini.GNS3Api(gns3_server_url, user, password)
. It creates a connection to the server and automatically detect the API version (available inapi.version
).The main method is
api.request(method, path, args=None, timeout=60)
. It automatically converts the Python data to JSON and vice versa.In addition, the module contains the get, put, post, and delete functions, which are wrappers around api.request and call the API with the appropriate method.
If the path is a list or a tuple, the components are concatenated with "/" as the separator. If the path doesn't start with a "/" the version ("/v2/" or "/v3/") is prefixed. Of course, this prefixing makes only sense for API methods, that are available both in v2 and v3 and use similar parameters.
The API module even supports image I/O. For uploading the
args
parameter must be either a byte string containing the image or a file type object. For downloadingapi.request
returns a file type object with the image data.Here the API module gns3api_mini.py.txt, remove the txt extension after downloading.
And here a test program:
Beta Was this translation helpful? Give feedback.
All reactions