Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 515 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swagger with Flask-Restplus, API and multiple Blueprints

#1
I'm building a very complex microservice using Flask and Flask-Restplus.<br> It will have many endpoints, thus I'm organizing each endpoint into a separate Blueprint.<br><p>

- Currently, I'm struggling with the usage of Flask-Restplus and API **using multiple Blueprints** in combination with swagger
- I want to be able to get all the endpoints of my blueprints into the built-in swagger of API, but this doesn't seem to work.
- I can access my endpoints via postman, but the swagger-UI doesn't show anything. :(


The following example code and directory structure should give you a hint towards my idea:

.
├── endpoints
│   ├── endpointa.py
│   ├── endpointb.py
│   ├── endpointc.py
│   └── __init__.py
├── __init__.py
└── run.py

My main __init__.py looks like this:

from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api


# create app and api
app = Flask(__name__)
api_prefix = '/api/v1/'

# register Blueprints
from endpoints.endpointa import endpointa_api
app.register_blueprint(endpointa_api, url_prefix=api_prefix)

from endpoints.endpointb import endpointb_api
app.register_blueprint(endpointb_api, url_prefix=api_prefix)

from endpoints.endpointc import endpointc_api
app.register_blueprint(endpointc_api, url_prefix=api_prefix)


api = Api(app,
version='1',
title='Test Service REST-API',
description='A REST-API for the Test Service, implemented in python')


if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=5060)

endpointa.py with the corresponding Blueprint:

from os import environ
import json, ast, syslog
import requests
import gc
from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api

endpointa_api = Blueprint('endpointa_api', __name__)

@endpointa_api.route('testa', methods=['GET'])
def testa():
...


@endpointa_api.route('testa/<string:testa_id>', methods=['GET', 'POST'])
def testa_id():
...


**Again: <p> I can access my endpoints via postman, but the swagger-UI doesn't show anything:**

[![enter image description here][1]][1]


Normally I would add endpoints to API using something like

api.add_resource(TestClass, api_prefix + 'test')

but this doesn't seem to be possible with multiple Blueprints.

<P>**Can anyone show me how to add/register these Blueprints (endpointa_api, endpointb_api and endpointc_api) with Api ?**</p>

[1]:
Reply

#2
There are 2 possible solutions using Flask-Restplus:

- Use Flask-RestPlus namespaces
- Turn your blueprints into Flask-RestPlus `Api`s

You can read about both in the documentation:

[To see links please register here]


**Namespaces**

> Flask-RESTPlus provides a way to use almost the same pattern as Flask’s blueprint. The main idea is to split your app into reusable namespaces.

from flask_restplus import Api

from .namespace1 import api as ns1
from .namespace2 import api as ns2
# ...
from .namespaceX import api as nsX

api = Api(
title='My Title',
version='1.0',
description='A description',
# All API metadatas
)

api.add_namespace(ns1)
api.add_namespace(ns2)
# ...
api.add_namespace(nsX)

**Blueprint Apis**

> Here’s an example of how to link an Api up to a Blueprint.

from flask import Blueprint
from flask_restplus import Api

blueprint = Blueprint('api', __name__)
api = Api(blueprint)
# ...

> Using a blueprint will allow you to mount your API on any url prefix
> and/or subdomain in you application:

from flask import Flask
from apis import blueprint as api

app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api/1')
app.run(debug=True)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through