Last active
February 1, 2016 09:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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