@@ -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 ):
0 commit comments