Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indicate Base URL in Swagger UI #286

Closed
alexisrolland opened this issue May 17, 2017 · 4 comments
Closed

Indicate Base URL in Swagger UI #286

alexisrolland opened this issue May 17, 2017 · 4 comments

Comments

@alexisrolland
Copy link

Hello,
I've posted this question on Stackoverflow but did not manage to get an answer. For some reason I am not able to define the base URL in the Swagger UI documentation. It show "BASE URL: /" as described here:
http://stackoverflow.com/questions/43632686/how-to-indicate-base-url-in-flask-restplus-documentation

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app,
    title='Data Quality Framework API',
    version='v0.1',
    doc='/data_quality_framework/api/documentation',
    contact='me@xxx.com',
    base_url='/test')

Is it a bug or am I doing something wrong?
Thank you

Alexis

@noirbizarre
Copy link
Owner

noirbizarre commented May 30, 2017

Hi !
I just responded on stackoverflow 😉
I copy-paste the response here too:

By default, when using this pattern (ie. with app as constructor argument), the API is on the root endpoint (ie. /) and the swagger documentation is on the API root (ie. still /).

You have multiple possibilities:

Use a blueprint to change the API root

If you want to keep the documentation on the API root but change the API root, use a blueprint to register you API where you want.

from flask import Flask, Blueprint
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/test')
api = Api(blueprint)
app.register_blueprint(blueprint)

assert url_for('api.doc') == '/test/'

Only change the documentation location

If you want to keep the API root at the same place but only move the documentation location, Flask-restplus allows you to specify it with the doc parameter.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, doc='/test/')

assert url_for('doc') == '/test/'

You can also combine both. See http://flask-restplus.readthedocs.io/en/stable/swagger.html#customization for more details on advanced documentation customization.

@alexisrolland
Copy link
Author

Hello and thank you for your time and answer.
I have tested the exact code above with the Blueprint and encountered the following error:
NameError: name 'url_for' is not defined

So I have changed it as below:

from flask import Flask, Blueprint, url_for
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/data_quality_framework/api')
api = Api(blueprint)
app.register_blueprint(blueprint)
assert url_for('api.doc') == '/documentation/'

I am facing the following error:

File "/opt/scripts/data_quality/api/api.py", line 23, in <module>
    assert url_for('api.doc') == '/test/'
  File "/opt/cloudera/parcels/Anaconda3-4.2.0-python-3.5/envs/ed/lib/python3.5/site-packages/flask/helpers.py", line 269, in url_for
    raise RuntimeError('Attempted to generate a URL without the '
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

I'm not very familiar with the concept of application context but I'll investigate. In the meantime if you have any clue that would be of great help.

Thank you
Alexis

@alexisrolland
Copy link
Author

alexisrolland commented May 30, 2017

Ok I've solved my issue by removing the call to url_for and proceeded as follow:

from flask import Flask, Blueprint
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/data_quality_framework/api')
api = Api(blueprint,
	title='Data Quality Framework API',
	version='v0.1',
	description='RESTful API built for the Data Quality Framework in order to perform CRUD operations on the Enterprise Data Warehouse AUDIT database.',
	doc='/documentation',
	contact='me@xxx.com')
app.register_blueprint(blueprint)

It seems to be working.
I think it would be worth to update the documentation because I was not able to figure this out by going through it. In particular because the "base_url" parameter I mentioned in my first post is a bit confusing (or maybe I misunderstand something).

Thanks a lot for the help!

@flyingduck92
Copy link

flyingduck92 commented Jul 6, 2020

I did it like this and it works

from flask import Blueprint
from flask_restplus import Api

blueprint = Blueprint('api', __name__, url_prefix='/v1')
api = Api(blueprint,
            title="My API",
            version='v0.1',
            description='Description
        )
app.register_blueprint(blueprint)

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

No branches or pull requests

3 participants