diff --git a/src/main/scala/code/snippet/ApiExplorer.scala b/src/main/scala/code/snippet/ApiExplorer.scala index 009a1963..cf9f270b 100644 --- a/src/main/scala/code/snippet/ApiExplorer.scala +++ b/src/main/scala/code/snippet/ApiExplorer.scala @@ -4,11 +4,11 @@ import _root_.net.liftweb._ import code.lib.ObpJson.ResourceDoc import code.lib.{ObpPut, ObpDelete, ObpPost, ObpGet} //import code.snippet.CallUrlForm._ -import net.liftweb.http.S +import net.liftweb.http.{SHtml, S} import net.liftweb.json.{Extraction, JsonParser, JsonAST} import net.liftweb.json.JsonAST.{JField, JObject, JValue} -import _root_.scala.xml.{NodeSeq, Text} +import scala.xml.{XML, NodeSeq, Text} import net.liftweb._ @@ -19,10 +19,9 @@ import net.liftweb.json._ import common._ import net.liftweb.util.Helpers._ -import net.liftweb.http.SHtml.{text,ajaxSubmit} +import net.liftweb.http.SHtml.{text,ajaxSubmit, textarea} import net.liftweb.http.js.JsCmd import net.liftweb.http.js.JsCmds.{Run, SetHtml} -import xml.Text import net.liftweb.json.Serialization.writePretty @@ -35,13 +34,9 @@ Present a list of OBP resource URLs */ class ApiExplorer extends Loggable { - // This can convert Markdown to HTML - val transformer = new ActuariusTransformer() - - // Note: this may cause 500 Error if the input contains invalid HTML within the markdown - // The node sequence (HTML) is returned to the template rather than a string - def markdownToNodeSeq(input : String) : NodeSeq = - scala.xml.XML.loadString("
" + transformer(input) + "
") + def stringToNodeSeq(html : String) : NodeSeq = { + scala.xml.XML.loadString("
" + html + "
") + } def showResources = { @@ -54,20 +49,16 @@ class ApiExplorer extends Loggable { logger.info (s"API version requested is: $apiVersionRequested") // Get a list of resource docs from the API server - // This will throw exception if resource_docs key is not populated + // This will throw an exception if resource_docs key is not populated // Convert the json representation to ResourceDoc (pretty much a one to one mapping) + // The overview contains html. Just need to convert it to a NodeSeq so the template will render it as such val resources = for { r <- getResourceDocsJson.map(_.resource_docs).get - } yield ResourceDoc(id = r.id, verb = r.request_verb, url = r.request_url, description = r.description, overview = markdownToNodeSeq(r.overview), request_body = r.request_body) + } yield ResourceDoc(id = r.id, verb = r.request_verb, url = r.request_url, description = r.description, overview = stringToNodeSeq(r.overview), request_body = r.request_body) - // Render the resources into a (nested) table. - // Notes on escaping strings: - // To have a $ in the resulting string use two: $$ - // Can't escape " with \" or use triple quoted string in the string interpolation so use the replace hack - // Controls when we display the request body. @@ -93,11 +84,11 @@ class ApiExplorer extends Loggable { def process(): JsCmd = { logger.info(s"requestUrl is $requestUrl") logger.info(s"resourceId is $resourceId") + logger.info(s"requestBody is $requestBody") // Create json object from input string val jsonObject = JsonParser.parse(requestBody).asInstanceOf[JObject] - // Call the url with optional body and put the response into the appropriate result div // the id of the element we want to populate and format. val target = "result_" + resourceId @@ -107,7 +98,7 @@ class ApiExplorer extends Loggable { logger.info(s"command is $jsCommand") - // Return the commands + // Return the commands to call the url with optional body and put the response into the appropriate result div SetHtml(target, Text(getResponse(apiVersion, requestUrl, requestVerb, jsonObject))) & Run (jsCommand) } @@ -151,10 +142,14 @@ class ApiExplorer extends Loggable { // In case we use Extraction.decompose implicit val formats = net.liftweb.json.DefaultFormats + // Below we render the resources into a (nested) table. + // Notes on escaping strings: + // To have a $ in the resulting string use two: $$ + // Can't escape " with \" or use triple quoted string in the string interpolation so may need to use the replace hack - // Note! This is the return of the function. + // Note: This is the return of the function. // All the replacements you want to do *must be chained here together at the end of the function*. - // Also, you can't use replaceWith (the alias for #>) to chain + // Also, you can't use "replaceWith" (the alias for #>) to chain // See the following for some examples. // http://blog.knoldus.com/2013/03/08/lift-web-basics-of-using-snippets-for-a-beginner/ @@ -183,12 +178,12 @@ class ApiExplorer extends Loggable { ////// // The form field (on the left) is bound to the variable (urlToCall) // (However, updating the var here does not seem to update the form field value) - // TODO use this approach. // We provide a default value (i.url) and bind the user input to requestUrl. requestURL is available in the function process // text creates a text box and we can capture its input in requestUrl "@request_url_input" #> text(i.url, s => requestUrl = s, "maxlength" -> "255", "size" -> "100", "id" -> s"request_url_input_${i.id}") & // Extraction.decompose creates json representation of JObject. "@request_body_input" #> text(pretty(render(i.request_body)), s => requestBody = s, "maxlength" -> "255", "size" -> "100", "type" -> "text") & + // TODO get this working. requestBody is not populated with textarea value "@request_body_input" #> textarea(pretty(render(i.request_body)), s => requestBody = s, "cols" -> "90", "rows" -> "5") & // We're not using the id at the moment "@request_verb_input" #> text(i.verb, s => requestVerb = s, "type" -> "hidden", "id" -> s"request_verb_input_${i.id}") & "@resource_id_input" #> text(i.id.toString, s => resourceId = s, "type" -> "hidden", "id" -> s"resource_id_input_${i.id}") & diff --git a/src/main/webapp/api-explorer.html b/src/main/webapp/api-explorer.html index 8e23304a..dea48615 100644 --- a/src/main/webapp/api-explorer.html +++ b/src/main/webapp/api-explorer.html @@ -13,15 +13,17 @@ - + - - - + - -
Description
Overview
+ Description +
Overview
+
+ @@ -30,10 +32,15 @@
+ + +
+ + @@ -42,8 +49,6 @@