Sent from my iPhone

> On 24 Dec 2014, at 09:34, Endre Varga <[email protected]> wrote:
> 
> Hi Greg,
> 
> 
> On Tue, Dec 23, 2014 at 10:57 PM, tigerfoot <[email protected]> wrote:
>>> Thanks for the clarification... the new API is very different.  I need to 
>>> let this whole flows thing sink in a bit, but I kinda see where its going.
> 
> Take a look at the new docs: 
> http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/scala.html
>  
>>>  Granted this is just one use case but I am a little concerned about all 
>>> the machinery needed to perform something pretty simple, a GET request in 
>>> this case.
> 
> What do you mean all the machinery? It is basically a one-liner if you 
> rewrite it:
> 
> val future = Source.single(req).via(Http().outgoingConnection(host, 
> port).flow).runWith(Sink.head)

We will definitely have a convenience API for this rather common use case, 
though. Without the boilerplate code it would shrink down to 

val f = Http().request(req, host, port)

which looks much nicer ;-)

Regards,

Roland 

> 
> Which translates to "Take the single request req through an Http outgoing 
> connection, then take the head of the output, which is the first (and in this 
> case only) response"
> 
> Once you get the basics and start looking at more complex integration 
> patterns not just the simplest req-resp cycle you will see that the new API 
> is usually simpler due to composability and handles backpressure 
> automatically.
>  
>>>  Not sure if I'm doing this right but I end up awaiting twice to get the 
>>> content I need out of the response--like the code below.  It does work...  
>>> Just appears quite a bit more complex than the Spray version. 
> 
> You need to await the second time because everything is streaming. The 
> ".data" stream you accessed can be GBytes in size! The trick is that the 
> stream never loads the whole data just the current part, propagating 
> backpressure.
> 
> If you want to avoid calling Await twice, rewrite your code like this:
> 
> val dataFuture = Source.single(req).via(Http().outgoingConnection(host, 
> port).flow).mapAsync(_.entity.toStrict).map(_.data.utf8String).runWith(Sink.head)
> 
> The above uses the "mapAsync" to flatten the Future[Entity.Strict] returned 
> by toStrict to a stream of Entity.Strict
> 
> -Endre
>  
>> 
>>   def httpGet( uri:String )(implicit s:ActorSystem) = {  // returns 
>> (status_code, entity_as_string)
>> 
>>     implicit val materializer = FlowMaterializer()
>> 
>>     var r:HttpResponse = null
>> 
>>     val req = HttpRequest(HttpMethods.GET, Uri(uri))
>> 
>>     val host:String = req.uri.authority.host.toString
>> 
>>     val port:Int = req.uri.effectivePort
>> 
>>     val httpClient = Http().outgoingConnection(host,port).flow
>> 
>>     val consumer = Sink.foreach[HttpResponse] { resp ⇒ r = resp }
>> 
>>     val finishFuture = Source.single(req).via(httpClient).runWith(consumer)
>> 
>>     Await.result(finishFuture, Duration("3 seconds"))
>> 
>>     // unpack result
>> 
>>     (r.status.intValue,
>> 
>>       Await.result(r.entity.toStrict(FiniteDuration(3,"seconds")), 
>> Duration("3 seconds") ).data.utf8String)
>> 
>>   } 
>> 
>> -- 
>> >>>>>>>>>> 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 http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> >>>>>>>>>> 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 http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.

-- 
>>>>>>>>>>      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 http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to