1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-13 20:08:55 +00:00

API for listing current projects

Fix #274
This commit is contained in:
Julien Duponchelle 2015-07-24 10:09:16 +02:00
parent cbe593f4a4
commit d9f1a7c7d3
4 changed files with 47 additions and 1 deletions

View File

@ -21,7 +21,7 @@ import json
import os
from ...web.route import Route
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA, PROJECT_LIST_SCHEMA
from ...modules.project_manager import ProjectManager
from ...modules import MODULES
from ...utils.asyncio import wait_run_in_executor
@ -35,6 +35,21 @@ class ProjectHandler:
# How many clients has subcribe to notifications
_notifications_listening = 0
@classmethod
@Route.get(
r"/projects",
description="List projects opened on the server",
status_codes={
200: "Project list",
}
# output=PROJECT_LIST_SCHEMA)
)
def list_projects(request, response):
pm = ProjectManager.instance()
response.set_status(200)
response.json(list(pm.projects))
@classmethod
@Route.post(
r"/projects",

View File

@ -104,6 +104,13 @@ PROJECT_OBJECT_SCHEMA = {
"required": ["location", "project_id", "temporary"]
}
PROJECT_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "List of projects",
"type": "array",
"items": PROJECT_OBJECT_SCHEMA
}
PROJECT_FILE_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "List files in the project",

View File

@ -87,6 +87,13 @@ class Response(aiohttp.web.Response):
self.content_type = "application/json"
if hasattr(answer, '__json__'):
answer = answer.__json__()
elif isinstance(answer, list):
newanswer = []
for elem in answer:
if hasattr(elem, '__json__'):
elem = elem.__json__()
newanswer.append(elem)
answer = newanswer
if self._output_schema is not None:
try:
jsonschema.validate(answer, self._output_schema)

View File

@ -82,6 +82,23 @@ def test_show_project_invalid_uuid(server):
assert response.status == 404
def test_list_projects(server):
ProjectManager.instance()._projects = {}
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
response = server.post("/projects", query)
assert response.status == 201
query = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0b"}
response = server.post("/projects", query)
assert response.status == 201
response = server.get("/projects", example=True)
assert response.status == 200
print(response.json)
assert len(response.json) == 2
assert response.json[0]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b" or response.json[1]["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0b"
def test_update_temporary_project(server):
query = {"name": "test", "temporary": True}
response = server.post("/projects", query)