Is it possible to use a the stream materialized value to dictate the HTTP response status code?

48 views
Skip to first unread message

Tim Harper

unread,
Nov 14, 2015, 2:54:04 PM11/14/15
to Akka User List
Hi,

TLDR: I'd like to use the materialized value of a stream to decide the HttpResponse code.

Presently, I use a ToEntityMarshaller which marshals to:
HttpEntity.Chunked(_, Source[Chunk, Future[Boolean]])

The Materialized Future[Boolean] indicates whether the stream was completely empty which is knowable as soon as an instance of the Source is run.

Since streams can be transformed multiple ways, and run multiple times, I'd prefer to have the state which indicates whether the stream is empty come from a materialized stream.

So, what I'd like to know, is if it's possible to use the stream materialized value to inform the response headers / code in a ToResponseMarshallable. As far as I can tell, from reading the source code of akka-http, it doesn't seem possible (a layer later in the process materializes the Source[Chunk, Any] AFTER an HttpResponse (containing the code, headers, and entity) is fully generated.

If this is possible, can someone point me to an example?

By the way, the stream integration for Akka HTTP is really, really amazing! Great work on it, Akka devs! I'm planning to blog about this and do a little walk-through tutorial on how to content-negotiated streaming of either JSON or protocol buffers.

Thanks!

Tim

Tim Harper

unread,
Nov 15, 2015, 12:28:37 PM11/15/15
to akka...@googlegroups.com
So sorry about the double post on this one :(. I had an early send and thought if I deleted it in time maybe it wouldn't send. 

Sent from my iPhone
--
>>>>>>>>>> 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 a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/HSz9Ld-J-oc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Tim Harper

unread,
Nov 16, 2015, 12:15:00 AM11/16/15
to Akka User List
I read enough source code to be able to answer my own question:

The stream still isn't materialized at this point, where the StatusCode and headers are already known.


Reasoning about it further, allowing the materialized type of the HttpEntity.Chunked stream to inform the status code would problematic, considering that some status codes (like, 204 - No Content) don't allow for response entities.

So, I'll plan around this.

Akka is simply great, great, great. Thanks for doing such thoughtful design and caring about correctness.

Roland Kuhn

unread,
Nov 16, 2015, 7:34:41 AM11/16/15
to akka...@googlegroups.com
Hi Tim,

Great that you figured it out, I tried to understand your question between a train and a plane and couldn't get to the bottom of it. Glad that you like our approach :-)

Regards,

Roland 

Sent from my iPhone
--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages