Streaming and proxying with Play part 2
In a last post, we’ve seen some streaming and proxying examples with Play Framework. Let’s see how we can simplify and improve the last example with an implicit class.
Update : As Martin said in the comments, since Play 2.3, WS provides a getStream
method returning what we’re trying to achieve, i.e. a Future[(WSResponseHeaders, Enumerator[Array[Byte]])]
.
But you can still read the following post for pedagogical purpose :)
Define a new function
First, let’s create a new case class, just for readability purpose.
Then we create our new function.
This is quite similar as the code seen in the previous example, except that all the tricky parts are in a separated function.
We can call this function to get a stream enumerator (and the response headers) from the WS response.
This is a little easier to read but we can do better!
Define an implicit class
Let’s hide all the complexity in an implicit class.
RichWSRequestHolder
will implicitly add a new getAndStream
method on the WSRequestHolder
class.
Don’t worry, everything is resolved at compile time so it’s safe.
Now you can import the RichWSRequestHolder
class anywhere, and you will be able to call a Web Service like this to get a WSStream
:
Really easier isn’t it?
N.B : Of course you can do the same with other HTTP methods (POST, PUT, etc.).