How to pipeline transform to optional type by status code

88 views
Skip to first unread message

Joshua Fernandes

unread,
Feb 15, 2015, 5:33:46 PM2/15/15
to spray...@googlegroups.com

Hello all,

Im using spray client and what I'd like to do after a get request is to take the response code and return an optional type; however I keep running into a brick wall, and I am not sure what Im doing incorrectly. Could some please point me in the right direction

This is the code I have so far :

        val pipeline: HttpRequest => Future[HttpResponse] = (
           addHeader(HttpHeaders.`Content-Type`.apply(ContentTypes.`application/json`))
           ~> sendReceive ~> transformerOption[EditorError]
        )

My transformer looks like this:

       def transformerOption[CustomError]: Future[HttpResponse] => Future[Option[CustomError]] =
                                                                                    (futRes: Future[HttpResponse]) => futRes.map { res =>
         if (res.status == StatusCodes.NotFound) {
           Some(CustomError(res.status.defaultMessage, "some string here"))
         } else if (res.status == StatusCodes.NotFound) {
           Some(CustomError(res.status.defaultMessage, "some other string here"))
         } else {
           None
         }
       }


I've defined my type and the case codec like so:

        case class CustomError(name: String, details: String)

        implicit def CustomErrorCodec: CodecJson[CustomError] =
         casecodec2(CustomError.apply, CustomError.unapply)("name", "details")


I'd appreciate any help here with this. Thanks for taking the time to look.

Cheers



Mathias Doenitz

unread,
Feb 17, 2015, 7:21:02 AM2/17/15
to spray...@googlegroups.com
Joshua,

> I keep running into a brick wall, and I am not sure what Im doing incorrectly.


what error are you getting?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/5d8e4105-70ee-4c5c-a24c-4737a6145deb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Joshua Fernandes

unread,
Feb 17, 2015, 8:42:51 PM2/17/15
to spray...@googlegroups.com
Hi Mathias, Was getting type errors.

Have since rewritten it and worked it through with a few monads and it's playing nice again.

Cheers
Reply all
Reply to author
Forward
0 new messages