Skip to content

Commit d84889b

Browse files
committed
Return doc and swagger links on /
1 parent 8eaaf8f commit d84889b

File tree

12 files changed

+114
-64
lines changed

12 files changed

+114
-64
lines changed

deepaas/api/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717

1818
from aiohttp import web
19-
import aiohttp_apispec
2019
from oslo_config import cfg
2120
from oslo_log import log as logging
2221

@@ -25,6 +24,8 @@
2524
from deepaas.api import versions
2625
from deepaas import model
2726

27+
from deepaas import aiohttp_apispec
28+
2829
LOG = logging.getLogger(__name__)
2930

3031
APP = None
@@ -53,6 +54,7 @@
5354

5455

5556
async def get_app(swagger=True, doc="/ui", prefix="",
57+
static_path="/static/swagger",
5658
enable_train=True, enable_predict=True):
5759
"""Get the main app."""
5860
global APP
@@ -122,6 +124,7 @@ async def get_app(swagger=True, doc="/ui", prefix="",
122124
url="/swagger.json",
123125
swagger_path=doc if doc else None,
124126
prefix=prefix,
127+
static_path=static_path,
125128
in_place=True,
126129
)
127130

deepaas/api/v1.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# under the License.
1616

1717
from aiohttp import web
18-
import aiohttp_apispec
18+
from deepaas import aiohttp_apispec
1919
import marshmallow
2020
from marshmallow import fields
2121
from webargs import aiohttpparser
@@ -51,37 +51,21 @@ def get_app():
5151
@aiohttp_apispec.response_schema(responses.Version(), 200)
5252
@aiohttp_apispec.response_schema(responses.Failure(), 400)
5353
async def get_version(request):
54+
# NOTE(aloga): we use the router table from this application (i.e. the
55+
# global APP in this module) to be able to build the correct url, as it can
56+
# be prefixed outside of this module (in an add_subapp() call)
57+
root = APP.router["v1"].url_for()
5458
version = {
5559
"version": "deprecated",
5660
"id": "v1",
5761
"links": [
5862
{
5963
"rel": "self",
60-
# NOTE(aloga): we use our the router table from this
61-
# application (i.e. the global APP in this module) to be able
62-
# to build the correct url, as it can be prefixed outside of
63-
# this module (in an add_subapp() call)
64-
"href": "%s" % APP.router["v1"].url_for(),
65-
},
64+
"type": "application/json",
65+
"href": "%s" % root,
66+
}
6667
]
6768
}
68-
69-
# NOTE(aloga): skip these for now, until this issue is solved:
70-
# https://github.com/maximdanilchenko/aiohttp-apispec/issues/65
71-
# doc = "%s.doc" % v.split(".")[0]
72-
# d = {"rel": "help",
73-
# "type": "text/html",
74-
# # FIXME(aloga): this -v is wrong
75-
# "href": "flask.url_for(doc)"}
76-
# versions[-1]["links"].append(d)
77-
#
78-
# specs = "%s.specs" % v.split(".")[0]
79-
# d = {"rel": "describedby",
80-
# "type": "application/json",
81-
# # FIXME(aloga): this -v is wrong
82-
# "href": "flask.url_for(specs)"}
83-
# versions[-1]["links"].append(d)
84-
8569
return web.json_response(version)
8670

8771

deepaas/api/v2/__init__.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# under the License.
1616

1717
from aiohttp import web
18-
import aiohttp_apispec
1918
from oslo_config import cfg
2019
from oslo_log import log
2120

21+
from deepaas import aiohttp_apispec
2222
from deepaas.api.v2 import debug as v2_debug
2323
from deepaas.api.v2 import models as v2_model
2424
from deepaas.api.v2 import predict as v2_predict
@@ -59,35 +59,20 @@ def get_app(enable_train=True, enable_predict=True):
5959
@aiohttp_apispec.response_schema(responses.Version(), 200)
6060
@aiohttp_apispec.response_schema(responses.Failure(), 400)
6161
async def get_version(request, wsk_args=None):
62+
# NOTE(aloga): we use the router table from this application (i.e. the
63+
# global APP in this module) to be able to build the correct url, as it can
64+
# be prefixed outside of this module (in an add_subapp() call)
65+
root = APP.router["v2"].url_for()
6266
version = {
6367
"version": "stable",
6468
"id": "v2",
6569
"links": [
6670
{
6771
"rel": "self",
68-
# NOTE(aloga): we use our the router table from this
69-
# application (i.e. the global APP in this module) to be able
70-
# to build the correct url, as it can be prefixed outside of
71-
# this module (in an add_subapp() call)
72-
"href": "%s" % APP.router["v2"].url_for(),
73-
},
72+
"type": "application/json",
73+
"href": "%s" % root,
74+
}
7475
]
7576
}
7677

77-
# NOTE(aloga): skip these for now, until this issue is solved:
78-
# https://github.com/maximdanilchenko/aiohttp-apispec/issues/65
79-
# doc = "%s.doc" % v.split(".")[0]
80-
# d = {"rel": "help",
81-
# "type": "text/html",
82-
# # FIXME(aloga): this -v is wrong
83-
# "href": "flask.url_for(doc)"}
84-
# versions[-1]["links"].append(d)
85-
#
86-
# specs = "%s.specs" % v.split(".")[0]
87-
# d = {"rel": "describedby",
88-
# "type": "application/json",
89-
# # FIXME(aloga): this -v is wrong
90-
# "href": "flask.url_for(specs)"}
91-
# versions[-1]["links"].append(d)
92-
9378
return web.json_response(version)

deepaas/api/v2/debug.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
import warnings
2121

2222
from aiohttp import web
23-
import aiohttp_apispec
2423
from oslo_config import cfg
2524
from oslo_log import log
2625
import six
2726

27+
from deepaas import aiohttp_apispec
28+
2829
CONF = cfg.CONF
2930

3031
app = web.Application()

deepaas/api/v2/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import urllib.parse
1818

1919
from aiohttp import web
20-
import aiohttp_apispec
2120

21+
from deepaas import aiohttp_apispec
2222
from deepaas.api.v2 import responses
2323
from deepaas import model
2424

deepaas/api/v2/predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
# under the License.
1616

1717
from aiohttp import web
18-
import aiohttp_apispec
1918
import marshmallow
2019
from webargs import aiohttpparser
2120
import webargs.core
2221

22+
from deepaas import aiohttp_apispec
2323
from deepaas.api.v2 import responses
2424
from deepaas.api.v2 import utils
2525
from deepaas import model

deepaas/api/v2/responses.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
class Location(marshmallow.Schema):
2323
rel = fields.Str(required=True)
2424
href = fields.Url(required=True)
25+
type = fields.Str(required=True)
2526

2627

2728
class Version(marshmallow.Schema):
2829
version = fields.Str(required="True")
2930
id = fields.Str(required="True")
30-
link = fields.Nested(Location)
31+
links = fields.Nested(Location)
3132
type = fields.Str()
3233

3334

deepaas/api/v2/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import uuid
2020

2121
from aiohttp import web
22-
import aiohttp_apispec
2322
from oslo_log import log
2423
from webargs import aiohttpparser
2524
import webargs.core
2625

26+
from deepaas import aiohttp_apispec
2727
from deepaas.api.v2 import responses
2828
from deepaas.api.v2 import utils
2929
from deepaas import model

deepaas/api/versions.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import json
1818

1919
from aiohttp import web
20-
import aiohttp_apispec
2120

21+
from deepaas import aiohttp_apispec
2222
from deepaas.api.v2 import responses
2323

2424
app = web.Application()
@@ -45,7 +45,30 @@ async def get(self):
4545
resp = await info(self.request)
4646
versions.append(json.loads(resp.body))
4747

48-
return web.json_response({"versions": versions})
48+
response = {
49+
"versions": versions,
50+
"links": []
51+
}
52+
# But here we use the global app coming in the request
53+
doc = self.request.app.router.named_resources().get("swagger.docs")
54+
if doc:
55+
doc = {
56+
"rel": "help",
57+
"type": "text/html",
58+
"href": "%s" % doc.url_for()
59+
}
60+
response["links"].append(doc)
61+
62+
spec = self.request.app.router.named_resources().get("swagger.spec")
63+
if spec:
64+
spec = {
65+
"rel": "describedby",
66+
"type": "application/json",
67+
"href": "%s" % spec.url_for(),
68+
}
69+
response["links"].append(spec)
70+
71+
return web.json_response(response)
4972

5073

5174
def register_version(version, func):

deepaas/tests/fake_responses.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
'id': 'v2',
4545
'links': [
4646
{
47-
'href': '/v2/',
48-
'rel': 'self'
49-
}
47+
"rel": "self",
48+
"type": "application/json",
49+
"href": "/v2/"
50+
},
5051
],
5152
}
5253

@@ -55,22 +56,63 @@
5556
'id': 'v1',
5657
'links': [
5758
{
58-
'href': '/v1/',
59-
'rel': 'self'
60-
}
59+
"rel": "self",
60+
"type": "application/json",
61+
"href": "/v1/"
62+
},
6163
],
6264
}
6365

6466
all_versions = {
6567
'versions': [
6668
v1_version,
6769
v2_version,
70+
],
71+
'links': [
72+
{
73+
"rel": "help",
74+
"type": "text/html",
75+
"href": "/ui"
76+
},
77+
{
78+
"rel": "describedby",
79+
"type": "application/json",
80+
"href": "/swagger.json"
81+
},
6882
]
6983
}
7084

7185
versions = {
7286
'versions': [
7387
v2_version,
88+
],
89+
'links': [
90+
{
91+
"rel": "help",
92+
"type": "text/html",
93+
"href": "/ui"
94+
},
95+
{
96+
"rel": "describedby",
97+
"type": "application/json",
98+
"href": "/swagger.json"
99+
},
100+
]
101+
}
102+
103+
empty_versions = {
104+
'versions': [],
105+
'links': [
106+
{
107+
"rel": "help",
108+
"type": "text/html",
109+
"href": "/ui"
110+
},
111+
{
112+
"rel": "describedby",
113+
"type": "application/json",
114+
"href": "/swagger.json"
115+
},
74116
]
75117
}
76118

0 commit comments

Comments
 (0)