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

[AS-216] swagger using flask-restx #21

Merged
merged 11 commits into from
Feb 20, 2020
Merged

[AS-216] swagger using flask-restx #21

merged 11 commits into from
Feb 20, 2020

Conversation

helgridly
Copy link
Contributor

@helgridly helgridly commented Feb 13, 2020

This uses flask-restx to integrate Swagger. If you want to see it in action, DM me on Slack and I'll send you the link.

The auth is a bit funky. For our Docker-y services, the Apache proxy handles the oauth dance, so we don't have to think about it. Handling OAuth properly would require endpoints in import-service to do the dance ourselves, which isn't necessary in practice as this will hide behind Orchestration and not need to do the dance. Swagger v3 supports auth-in-header but restx is still only on v2. Thus we have to do a mildly gross thing and tell Swagger that we're authing using an API key, and expect the user to type the word Bearer before their token in the Authorize box. Given this Swagger will only be seen by internal developers this seemed like the right time/effort compromise.

@davidangb
Copy link
Collaborator

I like this approach better than #23 , at least until its constraints cause us problems when writing code. I think this approach makes the code more self-documenting, as well as generating swagger documentation. I do recognize it's a lot of work to lean into restx.

@MatthewBemis
Copy link
Member

I think I like this one more? The alternative feels very tedious, verbose, and error-prone. This one feels like it's more intrusive but I suppose that's a reasonable cost for the benefits :)

@helgridly helgridly changed the title [EXPERIMENTAL] swagger using flask-restx [AS-216] swagger using flask-restx Feb 19, 2020
@helgridly helgridly marked this pull request as ready for review February 19, 2020 16:39


def create_app() -> flask.Flask:
app = flask.Flask(__name__)
app.response_class = json_response.JsonResponse
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is handled magically for us by restx now



schema_validator = jsonschema.Draft7Validator(NEW_IMPORT_SCHEMA)
Copy link
Contributor Author

@helgridly helgridly Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validation of incoming json is now handled by restx

"in": "header",
"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."
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird hack as described in the description

assert len(dbres) == 1
assert dbres[0].id == str(resp.get_data(as_text=True))
assert dbres[0].id == id
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tidied a bunch of tests up here

@@ -0,0 +1,59 @@
import flask.testing
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to rebase and add swagger for the new health check endpoint, so I wrote a test for it too.

app/health.py Outdated

class HealthResponse:
def __init__(self, db_health: bool, rawls_health: bool, sam_health: bool):
self.ok = all([db_health, rawls_health, sam])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be self.ok = all([db_health, rawls_health, sam_health]), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

@helgridly helgridly merged commit 77a4d85 into develop Feb 20, 2020
@helgridly helgridly deleted the swagger_restx branch February 20, 2020 15:53
Copy link
Collaborator

@davidangb davidangb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retroactive 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants