On 30 March 2017 at 14:28:34, Mo Batista ([email protected]) wrote:

I'm new to Akka-Http please bear with me.

Hello Mo and welcome to Akka :-


What I'm trying to do is

1. Connect to a Restful service
2. Unmarshall the json payload into the following case class structure

It's as simple as these docs really:
http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/unmarshalling.html#using-unmarshallers

So you just need:

    implicit val colorBlobsFormat = jsonFormat1(ColorBlobsResponse) //
this already provided all the needed marshallers

    // implicit materializer in Scope AFAIR too

    import system.dispatcher

Unmarshal(entity).to[ColorBlob]


In theory this should be simple, but it turns out that anything on this
topic is either outdated or way over my head, as in this article
http://malaw.ski/2016/04/10/hakk-the-planet-implementing-akka-http-marshallers/

That's about writing custom unmarshallers which is rather advanced - you
don't need to do that.

http://doc.akka.io/docs/akka-http/10.0.1/scala/http/common/unmarshalling.html
doesn't explain how to actually write an unmarshaller




This is where I got so far:

Issuing the request

val request = HttpRequest(method = HttpMethods.GET, uri =
s"https://colorblobs.service.net/ws/colorblobs/en?productCodes=904655";)

val future = Http().singleRequest(request)
val result: ColorBlobsResponse =
Await.result(future.flatMap(Unmarshal(_).to[ColorBlobsResponse]),
Duration.Inf)




Small note here, I would strongly recommend never using Duration.Inf, even
in sample code as you might get used to it and in general it simply is a
very bad idea to block infinitely.
Put `import scala.concurrent.duration._` and `2.seconds` instead for
example.

Providing the  unmarshaller. This is also where I got stuck

You don't need to do any of the manual Unmarshaller building, it's all
ready once you did the jsonFormat call :)


Now that I have ResponseEntity what would be the next step?

No need to go so "manual" :-) Check the docs above.


Maybe there is a blog article explaining what  jsonFormat1,
DefaultJsonProtocol and FromResponseUnmarshaller actually do?

Again, the docs:

*http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/json-support.html#json-support
<http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/json-support.html#json-support>
*

http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/marshalling.html#http-marshalling-scala


http://doc.akka.io/docs/akka-http/10.0.5/scala/http/common/unmarshalling.html#http-unmarshalling-scala





 I'm using akka-http 10.0.5 btw.

Thanks for providing that info, people often forget to and it helps a lot
to know that you're on the latest :)


Hope this helps,

-- 
Konrad `ktoso` Malawski
Akka <http://akka.io> @ Lightbend <http://lightbend.com>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to