Resource Doc layout

This commit is contained in:
Simon Redfern 2015-10-03 11:19:38 +02:00
parent f91d88e1f0
commit eb47785873
2 changed files with 29 additions and 29 deletions

View File

@ -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("<div>" + transformer(input) + "</div>")
def stringToNodeSeq(html : String) : NodeSeq = {
scala.xml.XML.loadString("<div>" + html + "</div>")
}
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}") &

View File

@ -13,15 +13,17 @@
<table>
<!-- Each URL we document appears in a row, inside a table -->
<!-- Multiple instances of this row are generated in ApiExplorer.scalaala -->
<!-- Multiple instances of this row are generated in ApiExplorer.scala -->
<!-- Note: id's are added so we can manipulate individual elements -->
<tr class="resource">
<!-- Some documentation -->
<td><a class="resource_description" href="#replace-me">Description</a></td>
<td><div name="overview" class="markdown"><span name="overview_text">Overview</span></div></td>
<td>
<a class="resource_description" href="#replace-me">Description</a>
<div name="overview" class="markdown"><span name="overview_text">Overview</span></div>
</td>
<form class="lift:form.ajax">
<td>
<form class="lift:form.ajax" id="form1">
<!-- Attributes for these fields are replaced -->
<input type="hidden" name="resource_id_input">
<input type="hidden" name="request_verb_input" placeholder="GET, POST, PUT etc.">
@ -30,10 +32,15 @@
<div name="request_body" style="display: block;">
<input name="request_body_input" placeholder="JSON body to POST or PUT">
<!-- TODO get this working <textarea name="request_body_input" form="form1">Enter text here...</textarea> -->
</div>
<!-- This submit button is also modified by ApiExplorer.scala -->
<input class="call_button" type="submit">
</form>
<!-- API call result goes here It is highlighted by javascript called from the submit button -->
@ -42,8 +49,6 @@
</code>
</pre>
</td>
</tr>
</table>
</div>