Akka-http: How do I map response to object - scala

Not sure if I'm getting this whole routing DSL thing right but here's the question. I want to do a post to external service such as:
val post = pathPrefix("somePath") {
post {
//get the response mapped to my Output object
}
}
Then I want the response (which is a Json) to be mapped to an object matching the fields for example Output (assuming I have my JsonProtocol set up). How is this done?

You are using HTTP server directives to "retrieve" something "externally". This is what typically an HTTP client does.
For this sort of things, you can use akka http client api.
For example:
val response = Http().singleRequest(HttpRequest(uri = "http://akka.io"))
response onComplete {
case Success(res) =>
val entity = Unmarshal(res.entity).to[YourDomainObject]
// use entity here
case Failure(ex) => // do something here
}
However, this requires some Unmarshaller (to deserialize the received json). Take also a look at Json Support, as it helps you define marshallers easily:
case class YourDomainObject(id: String, name: String)
implicit val YourDomainObjectFormat = jsonFormat2(YourDomainObject)

I think what you are trying to ask is how to get the body i.e in JSOn format to the Case class that you have
Here is a quick example:
path("createBot" / Segment) { tag: String =>
post {
decodeRequest {
entity(as[CaseClassName]) { caseclassInstance: CaseClassName =>
val updatedAnswer = doSomeStuff(caseclassInstance)
complete {
"Done"
}
}
}
You can find more detailed example from here : https://github.com/InternityFoundation/Stackoverflowbots/blob/master/src/main/scala/in/internity/http/RestService.scala#L56
I hope it answers your question.

Related

ScalaPlay > 2.6 how to access POST requests while faking a trivial server in tests

I'm trying to setup a fake server with Play2.7 and the environment suggested by https://developer.lightbend.com/guides/play-rest-api/ just echoing json from a POST request. While I was able to make GET and POST requests returning hardwired values I can't access directly the request to return or process it. NOTE: this was doable with versions < 2.6 but now Action has become deprecated, so I'm wondering which is the correct way to deal with this in Play >= 2.6
I have read the following how to mock external WS API calls in Scala Play framework and How to unit test servers in Play 2.6 now that Action singleton is deprecated which are actually doing almost all I am trying to do, but it seems I need something different to access the Request. In previous version of Play I could do something like the following:
case POST(p"/route") => Action { request => Ok(request.body.asJson.getOrElse(JsObject.empty)) }
But it seems calling the action this way is not more possible since I received the 'infamous'
object Action in package mvc is deprecated: Inject an ActionBuilder (e.g. DefaultActionBuilder) or extend BaseController/AbstractController/InjectedController
error.
my actual working code is
object FakeServer {
def withServerForStep1[T](codeBlock: WSClient => T): T =
Server.withRouterFromComponents() { cs =>
{
case POST(p"/route") =>
cs.defaultActionBuilder {
Results.Ok(Json.arr(Json.obj("full_name" -> "octocat/Hello-World")))
}
}
} { implicit port =>
WsTestClient.withClient(codeBlock)
}
}
and the unit Spec is something like
"The step 1" should {
"Just call the fakeservice" in {
setupContext()
FakeServer.withServerForStep1 ( {
ws =>
val request = ws.url("/route")
val data = Json.obj(
"key1" -> "value1",
"key2" -> "value2"
)
val response = request.post(data).futureValue
response.status mustBe 200
response.body mustBe Json.toJson(data)
})
}
}
I would like to write the FakeServer in such a way that the Spec will succeed in checking that returned body is equal to original sent json. Currently it is obviously failing with
"[{"full_name":"octocat/Hello-World"}]" was not equal to {"key1":"value1","key2":"value2"}
I eventually found how to do it, and the correct way as often happens in Scala is... trivial.
The "trick" was just to add request => in the body of cs.defaultActionBuilder as in the next example
object FakeServer {
def withServerForStep1[T](codeBlock: WSClient => T): T =
Server.withRouterFromComponents() { cs =>
{
case POST(p"/route") =>
cs.defaultActionBuilder { request =>
val bodyAsJson = request.body.asJson.getOrElse(JsObject.empty)
Results.Ok(bodyAsJson)
}
}
} { implicit port =>
WsTestClient.withClient(codeBlock)
}
}
Then the test just needed to deal with possible extra wrapping quotes and reads as
val response = request.post(data).futureValue
response.status mustBe 200
response.body mustBe Json.toJson(data).toString()

Error when use akka http unmarshall entity as case class with default value

Error found when I send a http post request:
The request content was malformed: No usable value for gender Did
not find value which can be converted into java.lang.String
My request body:
{
"name":"test"
}
Route in my scala code:
path("test"){
(post(entity(as[People]) { req =>
val resp = queryData(req)
complete(resp.meta.getOrElse("statusCode", 200).asInstanceOf[Int] -> resp)
}))
} ~
Code for People:
case class People(name: String, gender: String = "male")
Why still get the malformed error ???
Even though you put a default value, the extraction of the Json will look for that field, and it is not present there, so it will fail.
(I am assuming you are using spray-json as it's the default one in akka-http)
In order to avoid the issue, while keeping it simple, I would recommend you to create a case class for the request to create people, which contains an Option[String] for that field, and you can then convert the PeopleCreateRequest to a People easily.
case class PeopleCreateRequest(name: String, gender: Option[String])
That will work nicely with the framework...
Alternatively, if you want to keep the design that way, you'll need to look into implementing your own JsonFormat[People] which will treat this value as optional but add a default value when missing.
Look into spray-json https://github.com/spray/spray-json#providing-jsonformats-for-other-types
But I imagine it would be something like:
implicit val peopleFormat = new RootJsonFormat[People] {
def read(json: JsValue): People = json match {
case JsArray(Seq(JsString(name), JsString(gender))) =>
People(name, gender)
case JsArray(Seq(JsString(name))) =>
People(name)
case _ => deserializationError("Missing fields")
}
def write(obj: People): JsValue = ???
}
I am normally using different JsonSupport, using circe, but hopefully this gives you direction to solve your issue

How would you implement String to json object unmarshaller for parameter of url-encoded POST?

How would you implement String to json object unmarshaller for parameter of url-encoded POST ? I'm using version 1.2.
Here is what I want. Foursquare pushes url-encoded POST to my service. My route looks like this
path("handle_4sq_push") {
formFields("checkin".as[FsqCheckin], "user".as[String], "secret".as[String]) {
(checkin, user, secret) =>
complete {
StatusCodes.OK
}
}
}
I have json parser for FsqCheckin which is defined like this
implicit val fsqCheckinFormat = jsonFormat(FsqCheckin.apply, "id", "createdAt", "timeZoneOffset", "user", "venue")
So it's all good but it works only if parameters are form-encoded. Otherwise Spray says
There was a problem with the requests Content-Type:
Field 'checkin' can only be read from 'multipart/form-data' form content
So I thought I'd write unmarshaller. I wrote this
implicit def MyJsonUnmarshaller[T: RootJsonReader] =
Unmarshaller.delegate[String, T](ContentTypes.`*`) {
value => {
val json = JsonParser(value)
json.convertTo[T]
}
}
But if I bring it to scope of my route I get following compile error
too many arguments for method formFields: (fdm: spray.routing.directives.FieldDefMagnet)fdm.Out
formFields("checkin".as[FsqCheckin], "user".as[String], "secret".as[String]) {
^
It's the same error I have if I didn't have json parser for FsqCheckin in the scope.
How can I deal with this problem ?
Shockingly I figured it out myself. Here is working version of universal unmarshaller.
implicit def String2JsonParser[T: RootJsonReader] = new FromStringDeserializer[T] {
def apply(value: String) =
try
Right(JsonParser(value).convertTo[T])
catch {
case ex: Throwable => Left(spray.httpx.unmarshalling.MalformedContent(ex.getMessage))
}
}

Return exact response/header?

From the client-side of a webapp, I hit a server-side route which is just a wrapper for a third-party API. Using dispatch, I am trying to make that server-side request return the exact header and response of the third-party API to the client-side AJAX call.
When I do this:
val req = host("third-pary.api.com, 80)
val post = req.as("user", "pass") / "route" << Map("key" -> "akey", "val" -> "aval")
Http(post > as.String)
I always see a 200 response returned to the AJAX call (kind of expectedly). I have seen an Either syntax used, but I'm really more of an Any, as it's just the exact response and header. How would this be written?
I should mention I'm using Scalatra on the server-side, so the local route is:
post("/route") {
}
EDIT:
Here is the suggested Either matching example, which I'm playing with, but the match syntax doesn't make sense - I don't care if there is an error, I just want to return it. Also, I can't seem to get the BODY returned with this method.
val asHeaders = as.Response { response =>
println("BODY: " + response.getResponseBody())
scala.collection.JavaConverters.mapAsScalaMapConverter(
response.getHeaders).asScala.toMap.mapValues(_.asScala.toList)
}
val response: Either[Throwable, Map[String, List[String]]] =
Http(post > asHeaders).either()
response match {
case Left(wrong) =>
println("Left: " + wrong.getMessage())
// return Action with header + body
case Right(good) =>
println("Right: " + good)
// return Action with header + body
}
Ideally, the solutions returns the Scalatra ActionResult(responseStatus(status, reason), body, headers).
It's actually very easy to get response headers while using Dispatch. For example with 0.9.4:
import dispatch._
import scala.collection.JavaConverters._
val headers: java.util.Map[String, java.util.List[String]] = Http(
url("http://www.google.com")
)().getHeaders
And now, for example:
scala> headers.asScala.mapValues(_.asScala).foreach {
| case (k, v) => println(k + ": " + v)
| }
X-Frame-Options: Buffer(SAMEORIGIN)
Transfer-Encoding: Buffer(chunked)
Date: Buffer(Fri, 30 Nov 2012 20:42:45 GMT)
...
If you do this often it's better to encapsulate it, like this, for example:
val asHeaders = as.Response { response =>
scala.collection.JavaConverters.mapAsScalaMapConverter(
response.getHeaders
).asScala.toMap.mapValues(_.asScala.toList)
}
Now you can write the following:
val response: Either[Throwable, Map[String, List[String]]] =
Http(url("http://www.google.com") OK asHeaders).either()
And you've got error checking, nice immutable collections, etc.
We needed the response body of failed requests to an API, so we came up with this solution:
Define your own ApiHttpError class with code and body (for the body text):
case class ApiHttpError(code: Int, body: String)
extends Exception("Unexpected response status: %d".format(code))
Define OkWithBodyHandler similar to what is used in the source of displatch:
class OkWithBodyHandler[T](f: Response => T) extends AsyncCompletionHandler[T] {
def onCompleted(response: Response) = {
if (response.getStatusCode / 100 == 2) {
f(response)
} else {
throw ApiHttpError(response.getStatusCode, response.getResponseBody)
}
}
}
Now, near your call to the code that might throw and exception (calling API), add implicit override to the ToupleBuilder (again similar to the source code) and call OkWithBody on request:
class MyApiService {
implicit class MyRequestHandlerTupleBuilder(req: Req) {
def OKWithBody[T](f: Response => T) =
(req.toRequest, new OkWithBodyHandler(f))
}
def callApi(request: Req) = {
Http(request OKWithBody as.String).either
}
}
From now on, fetching either will give you the [Throwable, String] (using as.String), and the Throwable is our ApiHttpError with code and body.
Hope it helped.

How to read the browser HTTP Header sent to play in a controller action?

Question text/background and finds
My browser sends some HTTP header (e.g. referer) to my application I'm building in the play 2.0 framework. I'm completly clueless how to read them so i can pass them on (google isn't helping).
I think I might need to do something mentioned here (http://www.playframework.org/documentation/2.0/ScalaInterceptors). Which resulted in:
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
println("headers:" + request.headers.toString)
super.onRouteRequest(request)
}
Which works outputting to the console. But I don't know how to pass them to a specific controller action. I could, for example, add an if statement and call myAction once it sees a specific 'route' (eg /client/view/123) or call super.onRouteRequest(request) otherwise. But I'd lose the functionality in my /conf/routing. What is the 'proper' way of doing this?
In my queste to answer that I found this: Http.Context.current().​request() but using that in my controller action gave me a [RuntimeException: There is no HTTP Context available from here.].
Another thing I found is this where Guillaume Bort replies to a, I think unrelated question:
I'm not sure what you are trying to do but:
case class CustomRequest(token: String, request: Request[AnyContent])
extends WrappedRequest(request)
case class CustomAction(f: CustomRequest => Result)
extends Action[AnyContent] {
lazy val parser = BodyParsers.parse.anyContent
def apply(req: Request[AnyContent]) = req match {
case r: CustomRequest => f(r)
case _ => sys.error("Invalid usage")
}
}
object Application extends Controller {
def index = CustomAction { request =>
Ok("Token: " + request.token)
}
}
With onRouteRequest:
override def onRouteRequest(req: RequestHeader) = {
super.onRouteRequest(req).map { _ match {
case a: CustomAction => Action { (request: Request[AnyContent]) =>
a(CustomRequest("XXX", request))
}
case o => o
}
}
}
But that is a bit over my head atm (and perhaps not even an answer to my question). But if this is the way to go let me know.
Question summarized
What would be the proper/nice way to read HTTP Headers sent by the browser in a contoller action? I only care/need the HTTP Header in a few routes.
Thanks for any pointers or nudges!
PS:
1) I'm new to scala and play (and development on the web via rails like frameworks), so my apology for any lingo errors (do tell).
2) New to stackoverflow as well ... but it looks awsome, hope I did everything OK for my first question here!
3) I had 5 links/finds but no reputation to allow that, so narrowed my question down to the 3 interesting webfinds , sorry.
You can read headers using the examples here.
Essentially:
package controllers
import play.api.mvc._
object Application extends Controller {
def index = Action {
request =>
// your code that reads request.headers here
Ok("Got request [" + request + "]")
}
}
You can change the headers in a response using this example.