Skip to content

Instantly share code, notes, and snippets.

@maasg
Last active February 1, 2016 09:40
Show Gist options
  • Select an option

  • Save maasg/8788541 to your computer and use it in GitHub Desktop.

Select an option

Save maasg/8788541 to your computer and use it in GitHub Desktop.
Example of supporting HTTP 404 - Not found status on spray.io http-client unmarshalling pipeline
/** Adds support for Http/404 - Not Found to a processing pipeline.
* A 404/Not found is converted to None while other HTTP statuses will get converted as usual
* (HTTP/200 - will result in a marshalled object, errors will throw an exception, failing the future.
*/
def convertOption[T](implicit unmarshaller:FromResponseUnmarshaller[T]): Future[HttpResponse] => Future[Option[T]] =
(futRes: Future[HttpResponse]) => futRes.map{res =>
if (res.status == StatusCodes.NotFound) None
else Some(unmarshal[T](unmarshaller)(res))
}
// Usage example
val optionPipeline = sendReceive ~> convertOption[MyType]
val result = optionPipeline { Get(url)} // result: Future[Option[MyType]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment