Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask: Go from Swagger YAML to Google App Engine?

I have used the Swagger Editor to create a REST API and I have requested the server code download for Python Flask. I'm trying to deploy this out to Google Cloud Platform (I think that's the latest name? Or is it still GAE?) but I need to fill in some gaps.

I know the Swagger code works because I have deployed it locally without any issues. However, it uses the connexion library instead of Flask outright.

I'm mostly lost on how I can incorporate an app.yaml file for GCP and the right entrypoints within the generated code. In addition, I know that the generated code declares it's own app server which I don't think you need to do for GCP. Here's my current app.yaml

application: some-app-name
version: 1
runtime: python27
api_version: 1
threadsafe: yes
entrypoint: python app.py

libraries:
- name: connexion
  version: "latest"

And here's my app.py

import connexion

if __name__ == '__main__':
    app = connexion.App(__name__, specification_dir='./swagger/')
    app.add_api('swagger.yaml', arguments={'title': 'this is my API'})
    app.run(port=8080)

The primary error I'm getting now is

google.appengine.api.yaml_errors.EventError: the library "connexion" is not supported

I have a feeling that's because of the way I am declaring an app server in my app.py - it probably shouldn't be needed. How would I modify this file to still use my Swagger code but run on GCP?

like image 493
Unknown Coder Avatar asked Oct 15 '16 15:10

Unknown Coder


1 Answers

You seem to have some inconsistencies in your file, it's unclear if you intended it to be a standard environment app.yaml file or a flexible environment one. I can't tell as I'm unfamiliar with swagger and flask.

If it's supposed to be a standard environment one then:

  • the entrypoint: is not a supported config keyword
  • the connexion library is not one of the runtime-provided third-party libraries, so you can't request it (i.e. listing it in the libraries section). You need to install it (vendor it in).
    • it's missing the handlers section

Probably a good idea to go through Getting Started with Flask on App Engine Standard Environment

If, however, your goal was a flexible environment app.yaml file then:

  • you need the env: flex and runtime: python config in it(vm: true and runtime: python27 in the original answer are now deprecated)
  • installing/specifying dependencies is done differently, not via the libraries section.
like image 190
Dan Cornilescu Avatar answered Nov 04 '22 05:11

Dan Cornilescu