Skip to content

Commit 66471c5

Browse files
authored
Merge pull request #238 from DustinMoriarty/feature/specs-url-scheme
Added specs-url-scheme option for Api. contrib: DustinMoriarty reviewer: j5awry
2 parents b0ea3be + 0e8cd95 commit 66471c5

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ doc/_build/
6262
# Specifics
6363
flask_restx/static
6464
node_modules
65+
66+
# pyenv
67+
.python-version
68+
69+
# Jet Brains
70+
.idea

flask_restx/api.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class Api(object):
9494
:param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
9595
the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
9696
checkers), otherwise the default action is to not enforce any format validation.
97+
:param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use this
98+
scheme regardless of how the application is deployed. This is necessary for some deployments behind a reverse
99+
proxy.
97100
"""
98101

99102
def __init__(
@@ -123,6 +126,7 @@ def __init__(
123126
catch_all_404s=False,
124127
serve_challenge_on_401=False,
125128
format_checker=None,
129+
url_scheme=None,
126130
**kwargs
127131
):
128132
self.version = version
@@ -178,6 +182,7 @@ def __init__(
178182
api=self,
179183
path="/",
180184
)
185+
self.url_scheme = url_scheme
181186
if app is not None:
182187
self.app = app
183188
self.init_app(app)
@@ -198,7 +203,9 @@ def init_app(self, app, **kwargs):
198203
:param str contact: A contact email for the API (used in Swagger documentation)
199204
:param str license: The license associated to the API (used in Swagger documentation)
200205
:param str license_url: The license page URL (used in Swagger documentation)
201-
206+
:param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use
207+
this scheme regardless of how the application is deployed. This is necessary for some deployments behind a
208+
reverse proxy.
202209
"""
203210
self.app = app
204211
self.title = kwargs.get("title", self.title)
@@ -209,6 +216,7 @@ def init_app(self, app, **kwargs):
209216
self.contact_email = kwargs.get("contact_email", self.contact_email)
210217
self.license = kwargs.get("license", self.license)
211218
self.license_url = kwargs.get("license_url", self.license_url)
219+
self.url_scheme = kwargs.get("url_scheme", self.url_scheme)
212220
self._add_specs = kwargs.get("add_specs", True)
213221

214222
# If app is a blueprint, defer the initialization
@@ -220,7 +228,6 @@ def init_app(self, app, **kwargs):
220228
else:
221229
self.blueprint = app
222230

223-
224231
def _init_app(self, app):
225232
"""
226233
Perform initialization actions with the given :class:`flask.Flask` object.
@@ -261,7 +268,6 @@ def _init_app(self, app):
261268
DeprecationWarning
262269
)
263270

264-
265271
def __getattr__(self, name):
266272
try:
267273
return getattr(self.default_namespace, name)
@@ -515,11 +521,16 @@ def endpoint(self, name):
515521
@property
516522
def specs_url(self):
517523
"""
518-
The Swagger specifications relative url (ie. `swagger.json`)
524+
The Swagger specifications relative url (ie. `swagger.json`). If
525+
the spec_url_scheme attribute is set, then the full url is provided instead
526+
(e.g. http://localhost/swaggger.json).
519527
520528
:rtype: str
521529
"""
522-
return url_for(self.endpoint("specs"))
530+
external = None if self.url_scheme is None else True
531+
return url_for(
532+
self.endpoint("specs"), _scheme=self.url_scheme, _external=external
533+
)
523534

524535
@property
525536
def base_url(self):
@@ -528,7 +539,7 @@ def base_url(self):
528539
529540
:rtype: str
530541
"""
531-
return url_for(self.endpoint("root"), _external=True)
542+
return url_for(self.endpoint("root"), _scheme=self.url_scheme, _external=True)
532543

533544
@property
534545
def base_path(self):

tests/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,13 @@ class TestResource(restx.Resource):
342342
assert decorator1.called is True
343343
assert decorator2.called is True
344344
assert decorator3.called is True
345+
346+
def test_specs_url(self, app):
347+
api = restx.Api(app)
348+
specs_url = api.specs_url
349+
assert specs_url == "/swagger.json"
350+
351+
def test_url_scheme(self, app):
352+
api = restx.Api(app, url_scheme="https")
353+
assert api.specs_url == "https://localhost/swagger.json"
354+
assert api.base_url == "https://localhost/"

0 commit comments

Comments
 (0)