From d32701eda98cc26db6bd4e009e6f7cb7001fd150 Mon Sep 17 00:00:00 2001 From: plee Date: Wed, 18 Mar 2020 15:36:35 -0400 Subject: [PATCH] #7 Add Error Code for Stream Endpoint Implement the same error code from normal endpoint to the stream endpoint --- src/main/scala/Main.scala | 8 ++-- src/main/scala/RouteGenerator.scala | 58 ++++++++++++++--------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index 2e05a2c..94df6a1 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -50,22 +50,22 @@ object Main extends App { //test hello GET endpoint val result: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/hello?name=Frodo").send().body -// println("Got result: " + result) + // println("Got result: " + result) assert(result == "{\"code\":\"200\",\"msg\":\"hello : name : Frodo\"}") //test bye POST endpoint val result2: String = basicRequest.response(asStringAlways).post(uri"http://localhost:8080/bye?name=Frodo").send().body -// println("Got result: " + result2) + // println("Got result: " + result2) assert(result2 == "{\"code\":\"200\",\"msg\":\"bye : name : Frodo\"}") //test streaming GET endpoint val result3: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/stream?text=Frodo").send().body -// println("Got result: " + result3) + // println("Got result: " + result3) assert(result3 == "{\n \"code\" : \"200\",\n \"msg\" : \"stream : text : Frodo\"\n}") //test Not Found val result4: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/stream2?text=Frodo").send().statusText -// println("Got result: " + result4) + // println("Got result: " + result4) assert(result4 == "Not Found") //running server diff --git a/src/main/scala/RouteGenerator.scala b/src/main/scala/RouteGenerator.scala index 7ade7ff..7c845c0 100644 --- a/src/main/scala/RouteGenerator.scala +++ b/src/main/scala/RouteGenerator.scala @@ -17,26 +17,26 @@ object RouteGenerator { case class Unauthorized(realm: String) extends ErrorInfo case class Unknown(code: Int, msg: String) extends ErrorInfo case object NoContent extends ErrorInfo -// + // case class Messages(code: String, msg: String) val msgCodec = jsonBody[Messages] class Endpoints(paramName: String, endpointName: String, method: String) { val baseEndpoint = endpoint.errorOut( - oneOf( - statusMapping(StatusCode.NotFound, jsonBody[NotFound].description("not found")), - statusMapping(StatusCode.Unauthorized, jsonBody[Unauthorized].description("unauthorized")), - statusMapping(StatusCode.NoContent, emptyOutput.map(_ => NoContent)(_ => ())), - statusDefaultMapping(jsonBody[Unknown].description("unknown")) - ) + oneOf( + statusMapping(StatusCode.NotFound, jsonBody[NotFound].description("not found")), + statusMapping(StatusCode.Unauthorized, jsonBody[Unauthorized].description("unauthorized")), + statusMapping(StatusCode.NoContent, emptyOutput.map(_ => NoContent)(_ => ())), + statusDefaultMapping(jsonBody[Unknown].description("unknown")) + ) ) val aEndpoint: Endpoint[String, ErrorInfo with Product with Serializable, Messages, Nothing] = { - method match { - case "get" => baseEndpoint.get.in(endpointName).in(query[String](paramName)).out(jsonBody[Messages]) - case "post" => baseEndpoint.post.in(endpointName).in(query[String](paramName)).out(jsonBody[Messages]) - } + method match { + case "get" => baseEndpoint.get.in(endpointName).in(query[String](paramName)).out(jsonBody[Messages]) + case "post" => baseEndpoint.post.in(endpointName).in(query[String](paramName)).out(jsonBody[Messages]) + } } @@ -45,28 +45,28 @@ object RouteGenerator { class StreamEndpoints(paramName: String, endpointName: String, method: String) { - val baseEndpoint = endpoint.errorOut( - oneOf( - statusMapping(StatusCode.NotFound, jsonBody[NotFound].description("not found")), - statusMapping(StatusCode.Unauthorized, jsonBody[Unauthorized].description("unauthorized")), - statusMapping(StatusCode.NoContent, emptyOutput.map(_ => NoContent)(_ => ())), - statusDefaultMapping(jsonBody[Unknown].description("unknown")) + val baseEndpoint = endpoint.errorOut( + oneOf( + statusMapping(StatusCode.NotFound, jsonBody[NotFound].description("not found")), + statusMapping(StatusCode.Unauthorized, jsonBody[Unauthorized].description("unauthorized")), + statusMapping(StatusCode.NoContent, emptyOutput.map(_ => NoContent)(_ => ())), + statusDefaultMapping(jsonBody[Unknown].description("unknown")) + ) ) - ) - val streamingEndpoint: Endpoint[String, ErrorInfo with Product with Serializable, Source[ByteString, Any], Source[ByteString, Any]] = { - method match { - case "get" => baseEndpoint.get.in(endpointName).in(query[String](paramName)).out(streamBody[Source[ByteString, Any]](schemaFor[Messages], CodecFormat.TextPlain())) - case "post" => baseEndpoint.post.in(endpointName).in(query[String](paramName)).out(streamBody[Source[ByteString, Any]](schemaFor[Messages], CodecFormat.TextPlain())) + val streamingEndpoint: Endpoint[String, ErrorInfo with Product with Serializable, Source[ByteString, Any], Source[ByteString, Any]] = { + method match { + case "get" => baseEndpoint.get.in(endpointName).in(query[String](paramName)).out(streamBody[Source[ByteString, Any]](schemaFor[Messages], CodecFormat.TextPlain())) + case "post" => baseEndpoint.post.in(endpointName).in(query[String](paramName)).out(streamBody[Source[ByteString, Any]](schemaFor[Messages], CodecFormat.TextPlain())) + } } - } - def createStream(in: String) = { - val stringMsg = Messages("200", s"$endpointName : $paramName : $in").asJson.toString() - val streamMsg = Source.repeat(stringMsg).take(1).map(s => ByteString(s)) - Future.successful(Right(streamMsg)) - } - val streamingRoute: Route = streamingEndpoint.toRoute(createStream _) + def createStream(in: String) = { + val stringMsg = Messages("200", s"$endpointName : $paramName : $in").asJson.toString() + val streamMsg = Source.repeat(stringMsg).take(1).map(s => ByteString(s)) + Future.successful(Right(streamMsg)) + } + val streamingRoute: Route = streamingEndpoint.toRoute(createStream _) } }