/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

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