In 0.9.3, possible to compose OK with as.stream.Lines?

120 views
Skip to first unread message

Brian Maso

unread,
Oct 31, 2012, 10:09:26 AM10/31/12
to dispatc...@googlegroups.com
I've successfully made the transition from 0.8.8 asynch Futures to 0.9.3 Promises. Not that hard in the end (except for one issue, which I'll post about in a separate topic).

One feature I couldn't figure out ever in 0.8.8 seems to continue being a tough nut to crack (at least for me) in 0.9.3: how apply a HeaderVerb to a streaming handler? Specifically, I'd like to do something like the following:

val promise = someUrl / path OK as.stream.Lines({ line => ... })

But these things just don't compose, not as far as I can tell. That is, the OK verb just won't accept the as.stream.Lines. Basically the problem I have is that I need to "know" when I'm getting a 4xx or 5xx response vs. a 2xx response, and react accordingly. Is there a way to do such a thing? Or maybe its easier than I think, and somehow I can get the status code from the promise?

Brian Maso

Brian Maso

unread,
Oct 31, 2012, 1:10:34 PM10/31/12
to dispatc...@googlegroups.com
Answering my own question here...

You can't compose the OK verb with any AsyncHandler like I'm trying to do (as far as I can tell), but you can get the job done by defining your own AsynchHandler implementation -- basically a replacement for the AsynchHandler returned by as.stream.Lines.apply().

See the source for dispatch.as.stream.Lines.apply(), which basically just returns an AsynchHandler that: (1) ignores the response status code; and (2) always produces a Unit response. Its a pretty simple thing to write your own version that actually does pay attention to the response status code, and throws an appropriate exception (such as dispatch.StatusCode) for non-2xx responses.

Brian Maso

Nathan Hamblen

unread,
Nov 4, 2012, 1:07:27 AM11/4/12
to dispatc...@googlegroups.com
On 10/31/2012 01:10 PM, Brian Maso wrote:
Answering my own question here...

You can't compose the OK verb with any AsyncHandler like I'm trying to do (as far as I can tell), but you can get the job done by defining your own AsynchHandler implementation -- basically a replacement for the AsynchHandler returned by as.stream.Lines.apply().

See the source for dispatch.as.stream.Lines.apply(), which basically just returns an AsynchHandler that: (1) ignores the response status code; and (2) always produces a Unit response. Its a pretty simple thing to write your own version that actually does pay attention to the response status code, and throws an appropriate exception (such as dispatch.StatusCode) for non-2xx responses.

If you've got some ideas for incorporating that functionality into the API, have at it. The design is mostly guesswork, since I'm not working on a stream consumer myself right now.

Nathan
Reply all
Reply to author
Forward
0 new messages