scala - Invoke 2 REST services on Playframework to compose a response -
i having problem webservice invoking on playframework. have example invoke 1 service , use response route
def test = action.async { implicit request => val baseurl = "serviceurl" ws.url(baseurl).get().map { response => val result = response.json.asopt[list[person]] ok(views.html.test(result.get)) } }
this works fine, want invoke 2 webservices, can't compose response because ws.url().get()
calls return future
, don't know how extract option
inside of it. question is:
how option
inside future
?
i tried doing this:
def test = action.async { implicit request => val baseurl = "serviceurl" val wsresult = ws.url(baseurl).get().map { response => val result = response.json.asopt[list[person]] } ok(views.html.test(wsresult.get)) }
but said wsresult
has instance of future
, not option
you can call second webservice inside map block of first webservice , change map flatmap in first service call.
val wsresult = ws.url(baseurl).get().flatmap { response => val result = response.json.asopt[list[person]] secondwebserice.map{ res=> ok(views.html.test(result.get)) } }
since question, guess, 2 web services independent, can use comprehension
val wsresult1 = ws.url(baseurl).get().map { response => val result = response.json.asopt[list[person]] } val wsresult2 = ws.url(baseurl2).get().map { response => val result = response.json.asopt[list[person]] } val result = { r1<-wsresult1 r2<-wsresult2 }yield(r1.get++r2.get) result .map {res=> ok(views.html.test(res)) }
Comments
Post a Comment