Reputation: 453
I am learning to develop restful api for machine learning service. I used openapi spec to define my api specification, and I tried to use flask rest for this. However, I find this medium post is relevant to my intention, so I downloaded source code and tried to run the server locally, but server endpoint failed. I could able to run my other flask app on the same endpoint, but this project is not working even I followed its instruction. I created my open api spec
file so I want to learn and understand this medium post by run server locally. Can anyone point me out what's going on this medium post source code? any quick solution to figure out how to run server locally? thanks
for me, a simple flask app works and can run it on server endpoint.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run()
I want to run the server from this APIs with OpenAPI, because I want to use same structure for developing my api. Can anyone point me out why I can't run a server? any idea?
update
in this medium post, all artifacts generated by openapi code generator, and structured out by openapi spec file, so able to run the server from its source code will help me understand how to proceed my workflow.
Upvotes: 0
Views: 797
Reputation: 1209
The examples in the repository you mention come with docker files. You could use Docker to quickly spin up the server.
cd api_tutorial/openapi/photo_album/codegen_server
docker build . -t codegen_server
docker run codegen_server
However, this does not work.
Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/src/app/openapi_server/__main__.py", line 3, in <module>
import connexion
File "/usr/local/lib/python3.8/site-packages/connexion/__init__.py", line 3, in <module>
from .apis import AbstractAPI # NOQA
File "/usr/local/lib/python3.8/site-packages/connexion/apis/__init__.py", line 1, in <module>
from .abstract import AbstractAPI # NOQA
File "/usr/local/lib/python3.8/site-packages/connexion/apis/abstract.py", line 16, in <module>
from ..operations import OpenAPIOperation, Swagger2Operation
File "/usr/local/lib/python3.8/site-packages/connexion/operations/__init__.py", line 1, in <module>
from .abstract import AbstractOperation # noqa
File "/usr/local/lib/python3.8/site-packages/connexion/operations/abstract.py", line 11, in <module>
from ..decorators.response import ResponseValidator
File "/usr/local/lib/python3.8/site-packages/connexion/decorators/response.py", line 12, in <module>
from .validation import ResponseBodyValidator
File "/usr/local/lib/python3.8/site-packages/connexion/decorators/validation.py", line 9, in <module>
from werkzeug import FileStorage
ImportError: cannot import name 'FileStorage' from 'werkzeug' (/usr/local/lib/python3.8/site-packages/werkzeug/__init__.py)
As pointed out on this Github issue, this issue is related to connexion
and does not happen with version 2.6.0
. So, let's update requirements.txt
to use this version instead.
connexion == 2.6.0
Build and run again with docker and voilá, it should work.
docker run codegen_server
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
You could also run the server without Docker by cd-ing into the root directory and doing.
python -m openapi_server
Which in fact, is exactly what docker does, as we can see in the Dockerfile.
...
ENTRYPOINT ["python3"]
CMD ["-m", "openapi_server"]
Upvotes: 2