@@ -94,6 +94,9 @@ class Api(object):
94
94
:param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
95
95
the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
96
96
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.
97
100
"""
98
101
99
102
def __init__ (
@@ -123,6 +126,7 @@ def __init__(
123
126
catch_all_404s = False ,
124
127
serve_challenge_on_401 = False ,
125
128
format_checker = None ,
129
+ url_scheme = None ,
126
130
** kwargs
127
131
):
128
132
self .version = version
@@ -178,6 +182,7 @@ def __init__(
178
182
api = self ,
179
183
path = "/" ,
180
184
)
185
+ self .url_scheme = url_scheme
181
186
if app is not None :
182
187
self .app = app
183
188
self .init_app (app )
@@ -198,7 +203,9 @@ def init_app(self, app, **kwargs):
198
203
:param str contact: A contact email for the API (used in Swagger documentation)
199
204
:param str license: The license associated to the API (used in Swagger documentation)
200
205
: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.
202
209
"""
203
210
self .app = app
204
211
self .title = kwargs .get ("title" , self .title )
@@ -209,6 +216,7 @@ def init_app(self, app, **kwargs):
209
216
self .contact_email = kwargs .get ("contact_email" , self .contact_email )
210
217
self .license = kwargs .get ("license" , self .license )
211
218
self .license_url = kwargs .get ("license_url" , self .license_url )
219
+ self .url_scheme = kwargs .get ("url_scheme" , self .url_scheme )
212
220
self ._add_specs = kwargs .get ("add_specs" , True )
213
221
214
222
# If app is a blueprint, defer the initialization
@@ -220,7 +228,6 @@ def init_app(self, app, **kwargs):
220
228
else :
221
229
self .blueprint = app
222
230
223
-
224
231
def _init_app (self , app ):
225
232
"""
226
233
Perform initialization actions with the given :class:`flask.Flask` object.
@@ -261,7 +268,6 @@ def _init_app(self, app):
261
268
DeprecationWarning
262
269
)
263
270
264
-
265
271
def __getattr__ (self , name ):
266
272
try :
267
273
return getattr (self .default_namespace , name )
@@ -515,11 +521,16 @@ def endpoint(self, name):
515
521
@property
516
522
def specs_url (self ):
517
523
"""
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).
519
527
520
528
:rtype: str
521
529
"""
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
+ )
523
534
524
535
@property
525
536
def base_url (self ):
@@ -528,7 +539,7 @@ def base_url(self):
528
539
529
540
:rtype: str
530
541
"""
531
- return url_for (self .endpoint ("root" ), _external = True )
542
+ return url_for (self .endpoint ("root" ), _scheme = self . url_scheme , _external = True )
532
543
533
544
@property
534
545
def base_path (self ):
0 commit comments