Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 5cf0ffe

Browse files
authored
TIG-1956: Add openapi support (#2)
* TIG-1956: Add openapi support
1 parent 086d0c7 commit 5cf0ffe

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ $ black src tests --config setup.cfg
3737
See the black [documentation](https://github.com/psf/black#editor-integration) for details on how
3838
to configure your editor to automatically format your code.
3939

40+
##Swagger
41+
42+
The swagger documentation for this service can be found at the /swagger endpoint. If running
43+
locally, navigate to http://localhost:8080/swagger to see it.
44+
45+
If any new endpoints are added to the service or if the service is updated in such a way that any of
46+
the existing endpoints' contracts change, the swagger documentation must be updated to reflect the
47+
new state of the service before that change can be merged to master.
48+
49+
Documentation for how the swagger documentation is done can be found
50+
[here](https://flask-restplus.readthedocs.io/en/stable/swagger.html).
51+
4052
## Deploy
4153

4254
Deployment is done via helm to [kanopy](https://github.com/10gen/kanopy-docs#index). The project

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
black==19.3b0
22
gunicorn==19.9.0
33
pytest-flake8==1.0.4
4-
pytest==4.2.1
4+
pytest==5.2.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
],
3939
install_requires=[
4040
'flask==1.0.4',
41+
'flask-restplus==0.13.0',
4142
],
4243
entry_points={
4344
'console_scripts': [

src/selectedtests/app/app.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
Application to serve API of selected-tests service.
33
"""
44

5-
from flask import Flask, jsonify
5+
from flask import Flask
6+
from flask_restplus import Api
7+
8+
from selectedtests.app.controllers.health_controller import add_health_endpoints
69

710
DEFAULT_PORT = 8080
811

@@ -14,13 +17,15 @@ def create_app() -> Flask:
1417
:return: Instance of flask application.
1518
"""
1619
app = Flask(__name__)
17-
18-
@app.route("/health")
19-
def health():
20-
"""
21-
Get information about whether service is running
22-
"""
23-
return jsonify({"online": True})
20+
api = Api(
21+
app,
22+
version="1.0",
23+
title="Selected Tests Service",
24+
description="This service is used to predict which tests need to run based on code changes",
25+
doc="/swagger",
26+
)
27+
28+
add_health_endpoints(api)
2429

2530
return app
2631

src/selectedtests/app/controllers/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
The controller for the health endpoints.
3+
"""
4+
from flask import jsonify
5+
from flask_restplus import Api, Resource, fields
6+
7+
8+
def add_health_endpoints(api: Api):
9+
"""
10+
This adds to the given app instance the health endpoints of the service
11+
:param api: An instance of a Flask Restplus Api that wraps a Flask instance
12+
"""
13+
14+
ns = api.namespace("health", description="Health check endpoint")
15+
16+
health_model = ns.model("HealthCheckResponse", {"online": fields.Boolean})
17+
18+
@ns.route("")
19+
class Health(Resource):
20+
"""
21+
The class that represents the collection of endpoints that give the health of the service.
22+
"""
23+
24+
@ns.response(200, "Success", health_model)
25+
def get(self):
26+
"""
27+
Get the current status of the service
28+
"""
29+
return jsonify({"online": True})
File renamed without changes.

tests/selectedtests/test_app.py renamed to tests/selectedtests/app/controllers/test_health_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import testing
22

33

4-
def test_health(app_client: testing.FlaskClient):
4+
def test_health_endpoint(app_client: testing.FlaskClient):
55
"""
66
Test /health endpoint
77
"""

0 commit comments

Comments
 (0)