Skip to content

Commit

Permalink
#7 Add Error Code for Stream Endpoint
Browse files Browse the repository at this point in the history
Implement the same error code from normal endpoint to the stream endpoint
  • Loading branch information
plee committed Mar 18, 2020
1 parent 79d4088 commit d32701e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 29 additions & 29 deletions src/main/scala/RouteGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

}

Expand All @@ -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 _)

}
}

0 comments on commit d32701e

Please sign in to comment.