PageRenderTime 187ms CodeModel.GetById 183ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 1ms

/library/server/rest/app/doc/rest_api_documentation.e

http://github.com/jocelyn/EiffelWebReloaded
Specman e | 255 lines | 225 code | 29 blank | 1 comment | 31 complexity | d3b1ed6bf3b488a233ac848ad37fd8f5 MD5 | raw file
  1note
  2	description: "Summary description for {REST_API_DOCUMENTATION}."
  3	author: ""
  4	date: "$Date$"
  5	revision: "$Revision$"
  6
  7class
  8	REST_API_DOCUMENTATION
  9
 10inherit
 11	REST_REQUEST_HANDLER
 12
 13create
 14	make
 15
 16feature {NONE} -- Initialization
 17
 18	make (a_path: STRING; a_handler_manager: like handler_manager)
 19		do
 20			path := a_path
 21			handler_manager := a_handler_manager
 22			description := "Technical documention for the API"
 23			initialize
 24		end
 25
 26feature {NONE} -- Access: Implementation
 27
 28	handler_manager: REST_REQUEST_HANDLER_MANAGER
 29
 30feature -- Access
 31
 32	authentication_required: BOOLEAN = False
 33
 34feature -- Execution
 35
 36	execute_application (ctx: REST_REQUEST_CONTEXT; a_format: detachable STRING; a_args: detachable STRING)
 37		local
 38			rep: like new_html_page
 39			s: STRING
 40			rq: detachable REST_REQUEST_HANDLER
 41			l_dft_format_name: detachable STRING
 42			hdl_cursor: like handler_manager.new_cursor
 43		do
 44			rep := new_html_page
 45			rep.headers.put_content_type_text_html
 46			create s.make_empty
 47
 48			if a_args /= Void and then not a_args.is_empty then
 49				rq := handler_manager.handler_by_path (a_args)
 50				if rq = Void then
 51					rq := handler_manager.smart_handler_by_path (a_args)
 52					if attached {REST_REQUEST_GROUP_HANDLER} rq as grp then
 53						rq := grp.handlers.handler_by_path (a_args)
 54					end
 55				end
 56				if
 57					rq /= Void and then
 58					attached rq.path_information (a_args) as l_info
 59				then
 60					l_dft_format_name := l_info.format
 61				end
 62			end
 63
 64
 65			if rq /= Void then
 66				rep.set_big_title ("API: Technical documentation for ["+ rq.path +"]")
 67
 68				s.append_string ("<div class=%"api%">")
 69				s.append_string ("<h2 class=%"api-name%" >")
 70				s.append_string ("<a href=%"" + url (ctx, Void, False) + "%">.. Show all features ..</a>")
 71				s.append_string ("</h2></div>%N")
 72
 73				process_request_handler_doc (rq, s, ctx, a_format, l_dft_format_name)
 74			else
 75				rep.set_big_title ("API: Technical documentation")
 76
 77				from
 78					hdl_cursor := handler_manager.new_cursor
 79				until
 80					hdl_cursor.after
 81				loop
 82					rq := hdl_cursor.item
 83					rep.add_shortcut (rq.path)
 84					s.append ("<a name=%"" + rep.last_added_shortcut + "%"/>")
 85					process_request_handler_doc (rq, s, ctx, a_format, Void)
 86					hdl_cursor.forth
 87				end
 88			end
 89			rep.set_body (s)
 90			rep.compute
 91			ctx.output.put_string (rep.string)
 92			rep.recycle
 93		end
 94
 95	process_request_handler_doc (rq: REST_REQUEST_HANDLER; s: STRING; ctx: REST_REQUEST_CONTEXT; a_format: detachable STRING; a_dft_format: detachable STRING)
 96		local
 97			l_dft_format_name: detachable STRING
 98		do
 99			if a_dft_format /= Void then
100				if rq.supported_format_names.has (a_dft_format) then
101					l_dft_format_name := a_dft_format
102				end
103			end
104
105			s.append_string ("<div class=%"api%">")
106			s.append_string ("<h2 class=%"api-name%" ><a href=%""+ url (ctx, rq.path, False) +"%">"+ rq.path +"</a></h2>")
107			s.append_string ("<div class=%"inner%">")
108			if rq.hidden (ctx) then
109				s.append_string ("<div class=%"api-description%">This feature is hidden</div>%N")
110			else
111				if attached rq.description as desc then
112					s.append_string ("<div class=%"api-description%">" + desc + "</div>")
113				end
114				if attached {REST_REQUEST_GROUP_HANDLER} rq as grp then
115					s.append_string ("<div class=%"api-format%">Handler: <strong>")
116					if attached grp.handlers.new_cursor as l_handlers_cursor then
117						from
118
119						until
120							l_handlers_cursor.after
121						loop
122							s.append_string (" ")
123							s.append_string ("<a class=%"api-handler%" href=%"")
124							s.append_string (url (ctx, l_handlers_cursor.item.path, False))
125							s.append_string ("%">"+ l_handlers_cursor.item.path +"</a>")
126							l_handlers_cursor.forth
127						end
128					end
129					s.append_string ("</strong></div>")
130				end
131				if attached rq.supported_format_names as l_formats and then not l_formats.is_empty then
132					s.append_string ("<div class=%"api-format%">Supported formats: <strong>")
133					if attached l_formats.new_cursor as l_formats_cursor then
134						from
135
136						until
137							l_formats_cursor.after
138						loop
139							s.append_string (" ")
140							s.append_string ("<a class=%"api-name api-format")
141							if l_formats_cursor.item ~ l_dft_format_name then
142								s.append_string (" selected")
143							end
144							s.append_string ("%" href=%"" + url (ctx, rq.path, False) + "." + l_formats_cursor.item + "%">"+ l_formats_cursor.item +"</a>")
145							l_formats_cursor.forth
146						end
147					end
148					s.append_string ("</strong></div>")
149				end
150				if attached rq.supported_request_method_names as l_methods and then not l_methods.is_empty then
151					s.append_string ("<div class=%"api-method%">Supported request methods: <strong>")
152					if attached l_methods.new_cursor as l_methods_cursor then
153						from
154
155						until
156							l_methods_cursor.after
157						loop
158							s.append_string (" ")
159							s.append_string (l_methods_cursor.item)
160							l_methods_cursor.forth
161						end
162					end
163					s.append_string ("</strong></div>")
164				end
165				s.append_string ("<div class=%"api-auth%">Authentication required: <strong>" + rq.authentication_required.out + "</strong></div>")
166				if attached rq.uri_parameters as l_uri_params and then not l_uri_params.is_empty then
167					s.append_string ("<div class=%"api-uri-template%">URI Template: ")
168					s.append_string (rq.path)
169					if attached l_uri_params.new_cursor as l_uri_params_cursor then
170						from
171
172						until
173							l_uri_params_cursor.after
174						loop
175							if attached l_uri_params_cursor.item as l_uri_param then
176								s.append_string ("/<strong>" + l_uri_param.name + "</strong>")
177								s.append_string ("/<em>{" + l_uri_param.name + "}</em>")
178							end
179							l_uri_params_cursor.forth
180						end
181					end
182					s.append_string ("</div>%N")
183				end
184				if attached rq.parameters as l_params and then not l_params.is_empty then
185					s.append_string ("<div class=%"api-params%">Parameters: ")
186
187						--| show form only if we have a default format
188					if l_dft_format_name = Void then
189						s.append_string ("<span class=%"note%">to test the parameter(s), please first select a supported format.</span>%N")
190					else
191						if rq.method_post_supported then
192							s.append_string ("<form id=%""+ rq.path +"%" method=%"POST%" action=%"" + ctx.script_url (rq.path) + "." + l_dft_format_name + "%">%N")
193						else
194							s.append_string ("<form id=%""+ rq.path +"%" method=%"GET%" action=%"" + ctx.script_url (rq.path) + "." + l_dft_format_name + "%">%N")
195						end
196					end
197					s.append_string ("<ul>")
198					if attached l_params.new_cursor as l_params_cursor then
199						from
200
201						until
202							l_params_cursor.after
203						loop
204							if attached l_params_cursor.item as l_param then
205								s.append_string ("<li><strong>" + l_param.name + "</strong>")
206								if l_param.optional then
207									s.append_string (" <em>(Optional)</em>")
208								end
209								if attached l_param.description as l_param_desc then
210									s.append_string (": <em>" + l_param_desc + "</em>")
211								end
212								if l_dft_format_name /= Void then
213									s.append (" <input name=%"" + l_param.name + "%" type=%"text%" />")
214								end
215								s.append_string ("</li>")
216							end
217							l_params_cursor.forth
218						end
219					end
220
221					if l_dft_format_name /= Void then
222						s.append_string ("<input type=%"submit%" value=%"Test "+ rq.path + "." + l_dft_format_name + "%"/>")
223						s.append_string ("</form>")
224					end
225					s.append_string ("</ul></div>")
226				else
227					if l_dft_format_name /= Void then
228						s.append_string ("<a class=%"api-name%" href=%"" + ctx.script_url (rq.path + "." + l_dft_format_name) + "%">Test "+ rq.path  + "." + l_dft_format_name + "</a>")
229					else
230						s.append_string ("<a class=%"api-name%" href=%"" + ctx.script_url (rq.path) + "%">Test "+ rq.path +"</a>")
231					end
232				end
233				s.append_string ("</div>%N")
234			end
235			s.append_string ("</div>%N") -- inner
236		end
237
238feature -- Access
239
240	new_html_page: REST_API_DOCUMENTATION_HTML_PAGE
241		do
242			create Result.make (path)
243		end
244
245note
246	copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
247	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
248	source: "[
249			Eiffel Software
250			5949 Hollister Ave., Goleta, CA 93117 USA
251			Telephone 805-685-1006, Fax 805-685-6869
252			Website http://www.eiffel.com
253			Customer support http://support.eiffel.com
254		]"
255end