I am new to api development using openapi sepc, so I followed this blog where I just used server artifact to understand the code. I used /openapi/photo_album/codegen_server
for my simple work. To do so, I downloaded the file and tried to run server on default server end point. I installed all required dependencies and hit python -m openapi_server
, but browser endpoint always keep bugging me with following error message:
{ "detail": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
"status": 404, "title": "Not Found", "type": "about:blank" }
my attempt
here is the sourcecode that I tried:
I also tried the following:
import connexion
from openapi_server import encoder
def main():
app = connexion.App(__name__, specification_dir='./openapi/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('openapi.yaml', arguments={'title': 'Sample OpenAPI Specification'})
app.run(host='127.0.0.1',port=5000, debug=True)
if __name__ == '__main__':
main()
I tried to close firewall and tried to access server endpoint, still is not working. I am using windows-x64 for my system, this is first time I am trying to work with api development. I don't know what's going on and how to fix my problem. even I tried route print -4
and ping 127.0.0.1
on cmd and tried to use local ipv4 address but still not working. My intension is to make request to default server endpoint and do basic api testing. Can anyone provide possible solution how to fix this bug? any quick solution? thanks
update:
I tried any flask rest api sample project on my windows machine, none of them works, server endpoint is always failing. Is that because of my local machine setting? this error is killing me, how to fix this? any quick solution? thanks
I figured out this problem by updating connexion==2.6.0
in requirements
. Also using different value for host
, port
would run the server on default endpoint. This source code has some issue since code is generated by openapi code generator, there is no guarantee that sample api server run. my recent observation for api development, is to use flask-restplus which come with nice UI api view on server endpoint.
This is test api by using flask-restplus, hope whoever new to flask-restplus API development find it useful.
from flask import Flask
from flask_restplus import Api, fields, Resource, marshal
app = Flask(__name__)
api = Api()
api.init_app(app)
metadata_model = api.model("metadata", {
'file': fields.String()
})
user_model = api.model('UserModel', {
"user_id": fields.Integer(required=True, description=''),
"user_name": fields.String(required=True, description=''),
"user_role": fields.String(required=False, description='')
})
response_model = api.model("Result", {
'metadata': fields.List(fields.Nested(metadata_model)),
'result': fields.Raw()
})
@api.route("/test")
class ApiView(Resource):
@api.marshal_with(response_model)
def get(self):
data = {'metadata': {},
'result': self.get_user()}
return data
def get_user(self):
# Access database and get data
user_data = [{'user_id': 1, 'user_name': 'John', 'user_role': 'editor'},
{'user_id': 2, 'user_name': 'Sue', 'user_role': 'curator'}]
# The kwarg envelope does the trick
return marshal(user_data, user_model, envelope='data')
app.run(host='127.0.0.1', port='8080', debug=True)