mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 15:21:13 +02:00
Create /hypervisor and /controller namespace
This commit is contained in:
231 files changed
+1576
-1480
No files matched your search
+28
-15
@@ -26,11 +26,20 @@ import os
|
||||
|
||||
|
||||
class Query:
|
||||
"""
|
||||
Helper to make query againt the test server
|
||||
"""
|
||||
|
||||
def __init__(self, loop, host='localhost', port=8001):
|
||||
def __init__(self, loop, host='localhost', port=8001, prefix='', api_version=None):
|
||||
"""
|
||||
:param prefix: Prefix added before path (ex: /hypervisor)
|
||||
:param api_version: Version of the api
|
||||
"""
|
||||
self._loop = loop
|
||||
self._port = port
|
||||
self._host = host
|
||||
self._prefix = prefix
|
||||
self._api_version = api_version
|
||||
|
||||
def post(self, path, body={}, **kwargs):
|
||||
return self._fetch("POST", path, body, **kwargs)
|
||||
@@ -44,25 +53,24 @@ class Query:
|
||||
def delete(self, path, **kwargs):
|
||||
return self._fetch("DELETE", path, **kwargs)
|
||||
|
||||
def get_url(self, path, version):
|
||||
if version is None:
|
||||
return "http://{}:{}{}".format(self._host, self._port, path)
|
||||
return "http://{}:{}/v{}{}".format(self._host, self._port, version, path)
|
||||
def get_url(self, path):
|
||||
if self._api_version is None:
|
||||
return "http://{}:{}{}{}".format(self._host, self._port, self._prefix, path)
|
||||
return "http://{}:{}/v{}{}{}".format(self._host, self._port, self._api_version, self._prefix, path)
|
||||
|
||||
def _fetch(self, method, path, body=None, api_version=1, **kwargs):
|
||||
def _fetch(self, method, path, body=None, **kwargs):
|
||||
"""Fetch an url, parse the JSON and return response
|
||||
|
||||
Options:
|
||||
- example if True the session is included inside documentation
|
||||
- raw do not JSON encode the query
|
||||
- api_version Version of API, None if no version
|
||||
"""
|
||||
if body is not None and not kwargs.get("raw", False):
|
||||
body = json.dumps(body)
|
||||
|
||||
@asyncio.coroutine
|
||||
def go_request(future):
|
||||
response = yield from aiohttp.request(method, self.get_url(path, api_version), data=body)
|
||||
response = yield from aiohttp.request(method, self.get_url(path), data=body)
|
||||
future.set_result(response)
|
||||
future = asyncio.Future()
|
||||
asyncio.async(go_request(future))
|
||||
@@ -79,7 +87,8 @@ class Query:
|
||||
response.body = future.result()
|
||||
x_route = response.headers.get('X-Route', None)
|
||||
if x_route is not None:
|
||||
response.route = x_route.replace("/v1", "")
|
||||
response.route = x_route.replace("/v{}".format(self._api_version), "")
|
||||
response.route = response.route .replace(self._prefix, "")
|
||||
|
||||
if response.body is not None:
|
||||
if response.headers.get("CONTENT-TYPE", "") == "application/json":
|
||||
@@ -96,27 +105,27 @@ class Query:
|
||||
response.json = {}
|
||||
response.html = ""
|
||||
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
|
||||
self._dump_example(method, response.route, api_version, path, body, response)
|
||||
self._dump_example(method, response.route, path, body, response)
|
||||
return response
|
||||
|
||||
def _dump_example(self, method, route, api_version, path, body, response):
|
||||
def _dump_example(self, method, route, path, body, response):
|
||||
"""Dump the request for the documentation"""
|
||||
if path is None:
|
||||
return
|
||||
with open(self._example_file_path(method, route), 'w+') as f:
|
||||
f.write("curl -i -X {} 'http://localhost:8000/v{}{}'".format(method, api_version, path))
|
||||
f.write("curl -i -X {} 'http://localhost:8000/v{}{}'".format(method, self._api_version, path))
|
||||
if body:
|
||||
f.write(" -d '{}'".format(re.sub(r"\n", "", json.dumps(json.loads(body), sort_keys=True))))
|
||||
f.write("\n\n")
|
||||
|
||||
f.write("{} /v{}{} HTTP/1.1\n".format(method, api_version, path))
|
||||
f.write("{} /v{}{} HTTP/1.1\n".format(method, self._api_version, path))
|
||||
if body:
|
||||
f.write(json.dumps(json.loads(body), sort_keys=True, indent=4))
|
||||
f.write("\n\n\n")
|
||||
f.write("HTTP/1.1 {}\n".format(response.status))
|
||||
for header, value in sorted(response.headers.items()):
|
||||
if header == 'DATE':
|
||||
# We fix the date otherwise the example is always different
|
||||
# We fix the date otherwise the example is always different and create change in git
|
||||
value = "Thu, 08 Jan 2015 16:09:15 GMT"
|
||||
f.write("{}: {}\n".format(header, value))
|
||||
f.write("\n")
|
||||
@@ -126,4 +135,8 @@ class Query:
|
||||
|
||||
def _example_file_path(self, method, path):
|
||||
path = re.sub('[^a-z0-9]', '', path)
|
||||
return "docs/api/examples/{}_{}.txt".format(method.lower(), path)
|
||||
if len(self._prefix):
|
||||
prefix = self._prefix.replace('/', '')
|
||||
else:
|
||||
prefix = 'common'
|
||||
return "docs/api/examples/{}_{}_{}.txt".format(prefix, method.lower(), path)
|
||||
Reference in new issue
Block a user