Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit f2fed5f

Browse files
author
Hussein Elgridly
committed
rest of swagger
1 parent 0604c22 commit f2fed5f

File tree

4 files changed

+36
-44
lines changed

4 files changed

+36
-44
lines changed

app.yaml.ctmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ env_variables:
99
RAWLS_URL: "{env.rawls_url}"
1010
SAM_URL: "{env.sam_url}"
1111

12+
SWAGGER_OAUTH_CLIENT_ID: "{env.oauth_id}"
13+
1214
IMPORT_SVC_SA_EMAIL: "{env.import_svc_sa_email}"
1315
BATCH_UPSERT_BUCKET: "import-service-batchupsert-{env}"
1416

app/new_import.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
import flask
2-
import jsonschema
3-
import logging
42

53
from app import translate
6-
from app.util import exceptions
74
from app.db import db, model
85
from app.external import sam, pubsub
96
from app.auth import user_auth
107

11-
NEW_IMPORT_SCHEMA = {
12-
"$schema": "http://json-schema.org/draft-07/schema#",
13-
"type": "object",
14-
"properties": {
15-
"path": {
16-
"type": "string"
17-
},
18-
"filetype": {
19-
"type": "string",
20-
"enum": list(translate.FILETYPE_TRANSLATORS.keys())
21-
}
22-
},
23-
"required": ["path", "filetype"]
24-
}
25-
26-
27-
schema_validator = jsonschema.Draft7Validator(NEW_IMPORT_SCHEMA)
28-
298

309
def handle(request: flask.Request, ws_ns: str, ws_name: str) -> model.ImportStatusResponse:
3110
access_token = user_auth.extract_auth_token(request)
@@ -37,12 +16,6 @@ def handle(request: flask.Request, ws_ns: str, ws_name: str) -> model.ImportStat
3716
# make sure the user is allowed to import to this workspace
3817
workspace_uuid = user_auth.workspace_uuid_with_auth(ws_ns, ws_name, access_token, "write")
3918

40-
try: # now validate that the input is correctly shaped
41-
schema_validator.validate(request_json)
42-
except jsonschema.ValidationError as ve:
43-
logging.info("Got malformed JSON.")
44-
raise exceptions.BadJsonException(ve.message)
45-
4619
import_url = request_json["path"]
4720

4821
# and validate the input's path

app/server/routes.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import flask
2-
from flask_restx import Api, Resource
2+
from flask_restx import Api, Resource, fields
33
import json
44
import humps
55
from typing import Dict, Callable, Any
@@ -10,37 +10,58 @@
1010
from app.server.requestutils import httpify_excs, pubsubify_excs
1111

1212
routes = flask.Blueprint('import-service', __name__, '/')
13+
14+
authorizations = {
15+
'Bearer': {
16+
"type": "apiKey",
17+
"name": "Authorization",
18+
"in": "header",
19+
"description": "Use your GCP auth token, i.e. `gcloud auth print-access-token`. Required scopes are [openid, email, profile]. Write `Bearer <yourtoken>` in the box."
20+
}
21+
}
22+
1323
api = Api(routes, version='1.0', title='Import Service',
14-
description='import service')
24+
description='import service',
25+
authorizations=authorizations,
26+
security=[{"Bearer": "[]"}])
1527

1628
ns = api.namespace('/', description='import handling')
1729

1830

31+
new_import_model = ns.model("NewImport",
32+
{"path": fields.String(required=True),
33+
"filetype": fields.String(enum=list(translate.FILETYPE_TRANSLATORS.keys()), required=True)})
1934
import_status_response_model = ns.model("ImportStatusResponse", model.ImportStatusResponse.get_model())
2035

2136

22-
@ns.route('/<ws_ns>/<ws_name>/imports/<import_id>')
37+
@ns.route('/<workspace_project>/<workspace_name>/imports/<import_id>')
38+
@ns.param('workspace_project', 'Workspace project')
39+
@ns.param('workspace_name', 'Workspace name')
40+
@ns.param('import_id', 'Import id')
2341
class SpecificImport(Resource):
2442
@httpify_excs
2543
@ns.marshal_with(import_status_response_model)
26-
def get(self, ws_ns, ws_name, import_id):
44+
def get(self, workspace_project, workspace_name, import_id):
2745
"""Return status for this import."""
28-
return status.handle_get_import_status(flask.request, ws_ns, ws_name, import_id)
46+
return status.handle_get_import_status(flask.request, workspace_project, workspace_name, import_id)
2947

3048

31-
@ns.route('/<ws_ns>/<ws_name>/imports')
49+
@ns.route('/<workspace_project>/<workspace_name>/imports')
50+
@ns.param('workspace_project', 'Workspace project')
51+
@ns.param('workspace_name', 'Workspace name')
3252
class Imports(Resource):
3353
@httpify_excs
34-
@ns.marshal_with(import_status_response_model, 201)
35-
def post(self, ws_ns, ws_name):
54+
@ns.expect(new_import_model, validate=True)
55+
@ns.marshal_with(import_status_response_model, code=201)
56+
def post(self, workspace_project, workspace_name):
3657
"""Accept an import request."""
37-
return new_import.handle(flask.request, ws_ns, ws_name), 201
58+
return new_import.handle(flask.request, workspace_project, workspace_name), 201
3859

3960
@httpify_excs
40-
@ns.marshal_with(import_status_response_model, 200)
41-
def get(self, ws_ns, ws_name):
61+
@ns.marshal_with(import_status_response_model, code=200, as_list=True)
62+
def get(self, workspace_project, workspace_name):
4263
"""Return all imports in the workspace."""
43-
return status.handle_list_import_status(flask.request, ws_ns, ws_name)
64+
return status.handle_list_import_status(flask.request, workspace_project, workspace_name)
4465

4566

4667
# Dispatcher for pubsub messages.
@@ -55,7 +76,7 @@ def get(self, ws_ns, ws_name):
5576
@ns.route('/_ah/push-handlers/receive_messages', doc=False)
5677
class PubSub(Resource):
5778
@pubsubify_excs
58-
@ns.marshal_with(import_status_response_model, 200)
79+
@ns.marshal_with(import_status_response_model, code=200)
5980
def post(self) -> flask.Response:
6081
app.auth.service_auth.verify_pubsub_jwt(flask.request)
6182

app/tests/test_new_import.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from app.db.model import *
1010

1111

12-
def test_schema_valid():
13-
jsonschema.Draft7Validator.check_schema(new_import.NEW_IMPORT_SCHEMA)
14-
15-
1612
good_json = {"path": f"https://{translate.VALID_NETLOCS[0]}/some/path", "filetype": "pfb"}
1713
good_headers = {"Authorization": "Bearer ya29.blahblah", "Accept": "application/json"}
1814

0 commit comments

Comments
 (0)