Skip to content

Commit ed354f0

Browse files
Add option to allow absolute urls in @api
1 parent 2a1fef2 commit ed354f0

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

flask_apidoc/apidoc.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, folder_path=None, url_path=None, dynamic_url=True, app=None):
3232
self.url_path = '/docs'
3333

3434
self.dynamic_url = dynamic_url
35+
self.allow_absolute_url = True
3536

3637
self.app = None
3738

@@ -46,6 +47,9 @@ def init_app(self, app):
4647

4748
self.app = app
4849

50+
self.dynamic_url = self.app.config.get('APIDOC_DYNAMIC_URL', self.dynamic_url)
51+
self.allow_absolute_url = self.app.config.get('APIDOC_ALLOW_ABSOLUTE_URL', self.allow_absolute_url)
52+
4953
url = self.url_path
5054

5155
if not self.url_path.endswith('/'):
@@ -56,8 +60,8 @@ def init_app(self, app):
5660

5761
def __send_static_file(self, path=None):
5862
"""
59-
Sends apidoc files from the apidoc folder to the browser.
60-
:param path: the apidoc file
63+
Send apidoc files from the apidoc folder to the browser.
64+
:param path: the apidoc file.
6165
"""
6266

6367
if not path:
@@ -70,16 +74,19 @@ def __send_static_file(self, path=None):
7074
if self.dynamic_url and path == 'api_project.js':
7175
return self.__send_api_file(file_name)
7276

77+
if self.allow_absolute_url and path == 'main.js':
78+
return self.__send_main_file(file_name)
79+
7380
# Any other apidoc file is treated as a normal static file
7481
return self.app.send_static_file(file_name)
7582

7683
@cached
7784
def __send_api_file(self, file_name):
7885
"""
79-
Sends apidoc files from the apidoc folder to the browser.
86+
Send apidoc files from the apidoc folder to the browser.
8087
This method replaces all absolute urls in the file by
8188
the current url.
82-
:param file_name: the apidoc file
89+
:param file_name: the apidoc file.
8390
"""
8491

8592
file_name = join(self.app.static_folder, file_name)
@@ -110,6 +117,37 @@ def __send_api_file(self, file_name):
110117

111118
return response
112119

120+
@cached
121+
def __send_main_file(self, file_name):
122+
"""
123+
Send apidoc files from the apidoc folder to the browser.
124+
This method replaces all absolute urls in the file by
125+
the current url.
126+
:param file_name: the apidoc file.
127+
"""
128+
129+
file_name = join(self.app.static_folder, file_name)
130+
131+
with open(file_name, 'rt') as file:
132+
data = file.read()
133+
134+
data = data.replace(
135+
'fields.article.url = apiProject.url + fields.article.url;',
136+
'''if (fields.article.url.substr(0, 4).toLowerCase() !== \'http\')
137+
fields.article.url = apiProject.url + fields.article.url;''')
138+
139+
headers = Headers()
140+
headers['Content-Length'] = getsize(file_name)
141+
142+
response = self.app.response_class(data,
143+
mimetype=mimetypes.guess_type(file_name)[0],
144+
headers=headers,
145+
direct_passthrough=True)
146+
147+
response.last_modified = int(getmtime(file_name))
148+
149+
return response
150+
113151
@cached
114152
def __read_api_project(self):
115153
"""

0 commit comments

Comments
 (0)