igx
igx

Reputation: 4231

akka-http complete response with different types

How can I complete akka-http response with different types based on future response? I am trying to do something like

implicit val textFormat = jsonFormat1(Text.apply)
implicit val userFormat = jsonFormat2(User.apply)
 path(Segment) { id =>
              get {
                complete {
                  (fooActor ? GetUser(id)).map {
                    case n: NotFound => Text("Not Found")
                    case u: User => u
                  }
                }

              }
            }

but I am getting

type mismatch , expected: ToResponseMarshable , actual Futue[Product with Serializable ]

Upvotes: 0

Views: 800

Answers (1)

You could use the onComplete directive:

get {
  path(Segment) { id =>
    onComplete(fooActor ? GetUser(id)) {
      case Success(actorResponse) => actorResponse match {
        case _ : NotFound => complete(StatusCodes.NotFound,"Not Found")
        case u : User => complete(StatusCodes.OK, u)
      }
      case Failure(ex) => complete(StatusCodes.InternalServerError, "bad actor")
    }
  }
}

Upvotes: 3

Related Questions