Skip to content

Commit

Permalink
dave - reintroduced DescriptionRenderer to default implementations. R…
Browse files Browse the repository at this point in the history
…epackage
  • Loading branch information
daviddenton committed May 24, 2015
1 parent 91574bd commit 9f5e8fb
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.twitter.util.Future
import io.github.daviddenton.fintrospect.FintrospectModule._
import io.github.daviddenton.fintrospect.Routing.fromBinding
import io.github.daviddenton.fintrospect.parameters.Requirement._
import io.github.daviddenton.fintrospect.renderers.TypedResponseBuilder
import io.github.daviddenton.fintrospect.util.ResponseBuilder._
import io.github.daviddenton.fintrospect.util.TypedResponseBuilder
import org.jboss.netty.handler.codec.http.HttpMethod.GET
import org.jboss.netty.handler.codec.http.{HttpMethod, HttpRequest, HttpResponse}
import scala.language.existentials
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.github.daviddenton.fintrospect.util
package io.github.daviddenton.fintrospect.renderers

import argo.jdom.JsonRootNode
import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect.Route
import io.github.daviddenton.fintrospect.DescriptionRenderer
import io.github.daviddenton.fintrospect.util.ArgoUtil._
import io.github.daviddenton.fintrospect.util.JsonResponseBuilder

import scala.language.implicitConversions

class ArgoJsonResponseBuilder(descBuilder: (Path, Seq[Route]) => JsonRootNode) extends TypedResponseBuilder[JsonRootNode](
class ArgoJsonResponseBuilder(descriptionRenderer: DescriptionRenderer[JsonRootNode]) extends TypedResponseBuilder[JsonRootNode](
() => new JsonResponseBuilder,
descBuilder,
descriptionRenderer,
badParameters => {
val messages = badParameters.map(p => obj(
"name" -> string(p.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package io.github.daviddenton.fintrospect.renderers
import argo.jdom.JsonNodeFactories.string
import argo.jdom.JsonRootNode
import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect.Route
import io.github.daviddenton.fintrospect.util.ArgoJsonResponseBuilder
import io.github.daviddenton.fintrospect.util.ArgoUtil._
import io.github.daviddenton.fintrospect.{DescriptionRenderer, Route}

/**
* Ultra-basic Renderer implementation that only supports the route paths and the main descriptions of each.
*/
object SimpleJson {

class SimpleJson private() extends DescriptionRenderer[JsonRootNode] {
private def render(basePath: Path, route: Route): Field = {
route.method + ":" + route.describeFor(basePath) -> string(route.describedRoute.summary)
}

private def render(basePath: Path, routes: Seq[Route]): JsonRootNode = obj("resources" -> obj(routes.map(r => render(basePath, r))))
def apply(basePath: Path, routes: Seq[Route]): JsonRootNode = obj("resources" -> obj(routes.map(r => render(basePath, r))))
}

def apply(): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(render)

/**
* Ultra-basic Renderer implementation that only supports the route paths and the main descriptions of each.
*/
object SimpleJson {
def apply(): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(new SimpleJson())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import argo.jdom.{JsonNode, JsonRootNode}
import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect._
import io.github.daviddenton.fintrospect.parameters.{Parameter, Requirement}
import io.github.daviddenton.fintrospect.util.ArgoJsonResponseBuilder
import io.github.daviddenton.fintrospect.util.ArgoUtil._

import scala.collection.JavaConversions._

/**
* Renderer that provides basic Swagger v1.1 support. No support for bodies or schemas.
*/
object Swagger1dot1Json {
class Swagger1dot1Json private() extends DescriptionRenderer[JsonRootNode] {

private def render(requirementAndParameter: (Requirement, Parameter[_])): JsonNode = obj(
"name" -> string(requirementAndParameter._2.name),
Expand All @@ -34,13 +30,18 @@ object Swagger1dot1Json {
.map(resp => obj("code" -> number(resp.status.getCode), "reason" -> string(resp.description))).toSeq)
)

private def render(basePath: Path, routes: Seq[Route]): JsonRootNode = {
def apply(basePath: Path, routes: Seq[Route]): JsonRootNode = {
val api = routes
.groupBy(_.describeFor(basePath))
.map { case (path, routesForPath) => obj("path" -> string(path), "operations" -> array(routesForPath.map(render(_)._2)))}

obj("swaggerVersion" -> string("1.1"), "resourcePath" -> string("/"), "apis" -> array(asJavaIterable(api)))
}
}

def apply(): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(render)
/**
* Renderer that provides basic Swagger v1.1 support. No support for bodies or schemas.
*/
object Swagger1dot1Json {
def apply(): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(new Swagger1dot1Json())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import argo.jdom.{JsonNode, JsonRootNode}
import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect._
import io.github.daviddenton.fintrospect.parameters.{Body, Parameter, Requirement}
import io.github.daviddenton.fintrospect.util.ArgoJsonResponseBuilder
import io.github.daviddenton.fintrospect.util.ArgoUtil._

/**
* Renderer that provides Swagger v2.0 support
*/
object Swagger2dot0Json {
class Swagger2dot0Json private(apiInfo: ApiInfo) extends DescriptionRenderer[JsonRootNode] {

private val schemaGenerator = new JsonToJsonSchema()

Expand Down Expand Up @@ -69,25 +65,30 @@ object Swagger2dot0Json {
obj("title" -> string(apiInfo.title), "version" -> string(apiInfo.version), "description" -> string(apiInfo.description.getOrElse("")))
}

private def rendererFor(apiInfo: ApiInfo): (Path, Seq[Route]) => JsonRootNode = {
(basePath: Path, routes: Seq[Route]) =>
val pathsAndDefinitions = routes
.groupBy(_.describeFor(basePath))
.foldLeft(FieldsAndDefinitions()) {
case (memo, (path, routesForThisPath)) =>
val routeFieldsAndDefinitions = routesForThisPath.foldLeft(FieldsAndDefinitions()) {
case (memoFields, route) => memoFields.add(render(basePath, route))
}
memo.add(path -> obj(routeFieldsAndDefinitions.fields), routeFieldsAndDefinitions.definitions)
}
obj(
"swagger" -> string("2.0"),
"info" -> render(apiInfo),
"basePath" -> string("/"),
"paths" -> obj(pathsAndDefinitions.fields),
"definitions" -> obj(pathsAndDefinitions.definitions)
)
def apply(basePath: Path, routes: Seq[Route]): JsonRootNode = {
val pathsAndDefinitions = routes
.groupBy(_.describeFor(basePath))
.foldLeft(FieldsAndDefinitions()) {
case (memo, (path, routesForThisPath)) =>
val routeFieldsAndDefinitions = routesForThisPath.foldLeft(FieldsAndDefinitions()) {
case (memoFields, route) => memoFields.add(render(basePath, route))
}
memo.add(path -> obj(routeFieldsAndDefinitions.fields), routeFieldsAndDefinitions.definitions)
}
obj(
"swagger" -> string("2.0"),
"info" -> render(apiInfo),
"basePath" -> string("/"),
"paths" -> obj(pathsAndDefinitions.fields),
"definitions" -> obj(pathsAndDefinitions.definitions)
)
}
}

def apply(apiInfo: ApiInfo): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(rendererFor(apiInfo))
/**
* Renderer that provides Swagger v2.0 support
*/
object Swagger2dot0Json {
def apply(apiInfo: ApiInfo): ArgoJsonResponseBuilder = new ArgoJsonResponseBuilder(new Swagger2dot0Json(apiInfo))
}

Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package io.github.daviddenton.fintrospect.util
package io.github.daviddenton.fintrospect.renderers

import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect.Route
import io.github.daviddenton.fintrospect.parameters.RequestParameter
import io.github.daviddenton.fintrospect.util.ResponseBuilder
import io.github.daviddenton.fintrospect.{DescriptionRenderer, Route}
import org.jboss.netty.handler.codec.http.HttpResponse
import org.jboss.netty.handler.codec.http.HttpResponseStatus._

import scala.language.implicitConversions

class TypedResponseBuilder[T](newBuilder: () => ResponseBuilder[T],
descBuilder: (Path, Seq[Route]) => T,
descriptionRenderer: DescriptionRenderer[T] ,
bpToError: List[RequestParameter[_]] => T) {

def apply(): ResponseBuilder[T] = newBuilder()

def BadRequest(badParameters: List[RequestParameter[_]]): HttpResponse = apply().withCode(BAD_REQUEST).withContent(bpToError(badParameters)).build

def Description(basePath: Path, routes: Seq[Route]) = apply().withCode(OK).withContent(descBuilder(basePath, routes)).build
def Description(basePath: Path, routes: Seq[Route]) = apply().withCode(OK).withContent(descriptionRenderer(basePath, routes)).build
}


3 changes: 1 addition & 2 deletions src/test/scala/examples/LibraryApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import com.twitter.finagle.Http
import com.twitter.finagle.http.filter.Cors
import com.twitter.finagle.http.path.Root
import io.github.daviddenton.fintrospect._
import io.github.daviddenton.fintrospect.renderers.{SimpleJson, Swagger2dot0Json}
import io.github.daviddenton.fintrospect.util.TypedResponseBuilder
import io.github.daviddenton.fintrospect.renderers.{TypedResponseBuilder, SimpleJson, Swagger2dot0Json}

/**
* This example shows the intended method for implementing a simple app using Fintrospect routes and modules, using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.github.daviddenton.fintrospect._
import io.github.daviddenton.fintrospect.parameters.Path._
import io.github.daviddenton.fintrospect.parameters._
import io.github.daviddenton.fintrospect.util.ArgoUtil.{number, obj, parse}
import io.github.daviddenton.fintrospect.util.{ArgoJsonResponseBuilder, ArgoUtil}
import io.github.daviddenton.fintrospect.util.ArgoUtil
import org.jboss.netty.handler.codec.http.HttpMethod._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.scalatest.{FunSpec, ShouldMatchers}
Expand Down

0 comments on commit 9f5e8fb

Please sign in to comment.