Skip to content

Commit

Permalink
Make RequestInterceptor more like old ContextWrapper (ghostdogpr#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr authored and Fluxx committed Jan 13, 2022
1 parent 86a5625 commit 1c6d306
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
6 changes: 2 additions & 4 deletions examples/src/main/scala/example/akkahttp/AuthExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ object AuthExampleApp extends App {
type Auth = Has[FiberRef[Option[AuthToken]]]

object AuthInterceptor extends RequestInterceptor[Auth] {
override def apply[R <: Auth](
request: ServerRequest
): ZIO[R, StatusCode, Unit] =
override def apply[R <: Auth, A](request: ServerRequest)(effect: ZIO[R, StatusCode, A]): ZIO[R, StatusCode, A] =
request.headers.collectFirst {
case header if header.is("token") => header.value
} match {
case Some(token) => ZIO.accessM[Auth](_.get.set(Some(AuthToken(token))))
case Some(token) => ZIO.accessM[Auth](_.get.set(Some(AuthToken(token)))) *> effect
case _ => ZIO.fail(StatusCode.Forbidden)
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/scala/example/play/AuthExampleApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ object AuthExampleApp extends App {
implicit val executionContext: ExecutionContextExecutor = system.dispatcher

object AuthWrapper extends RequestInterceptor[Auth] {
def apply[R1 <: Auth](request: ServerRequest): ZIO[R1, StatusCode, Unit] =
override def apply[R <: Auth, A](request: ServerRequest)(effect: ZIO[R, StatusCode, A]): ZIO[R, StatusCode, A] =
request.header("token") match {
case Some(token) => ZIO.accessM[Auth](_.get.set(Some(AuthToken(token))))
case Some(token) => ZIO.accessM[Auth](_.get.set(Some(AuthToken(token)))) *> effect
case None => ZIO.fail(StatusCode.Forbidden)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import zio.ZIO
* query execution or injecting context into ZIO environment.
*/
trait RequestInterceptor[-R] { self =>
def apply[R1 <: R](request: ServerRequest): ZIO[R1, StatusCode, Unit]
def apply[R1 <: R, A](request: ServerRequest)(e: ZIO[R1, StatusCode, A]): ZIO[R1, StatusCode, A]

def |+|[R1 <: R](that: RequestInterceptor[R1]): RequestInterceptor[R1] = new RequestInterceptor[R1] {
override def apply[R2 <: R1](request: ServerRequest): ZIO[R2, StatusCode, Unit] =
that.apply[R2](request) *> self.apply[R2](request)
override def apply[R2 <: R1, A](request: ServerRequest)(e: ZIO[R2, StatusCode, A]): ZIO[R2, StatusCode, A] =
that.apply[R2, A](request)(self.apply[R2, A](request)(e))
}
}

object RequestInterceptor {
def empty: RequestInterceptor[Any] = new RequestInterceptor[Any] {
override def apply[R](request: ServerRequest): ZIO[R, StatusCode, Unit] = ZIO.unit
override def apply[R, A](request: ServerRequest)(e: ZIO[R, StatusCode, A]): ZIO[R, StatusCode, A] = e
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ object TapirAdapter {
def logic(request: (GraphQLRequest, ServerRequest)): RIO[R, Either[StatusCode, GraphQLResponse[E]]] = {
val (graphQLRequest, serverRequest) = request

(requestInterceptor(serverRequest) *>
requestInterceptor(serverRequest)(
interpreter
.executeRequest(
graphQLRequest,
skipValidation = skipValidation,
enableIntrospection = enableIntrospection,
queryExecution
)).either
)
).either
}

makeHttpEndpoints.map(_.serverLogic(logic))
Expand Down Expand Up @@ -147,7 +148,6 @@ object TapirAdapter {

val io =
for {
_ <- requestInterceptor(serverRequest)
rawOperations <- ZIO.fromOption(partsMap.get("operations")) orElseFail StatusCode.BadRequest
request <- requestCodec.rawDecode(new String(rawOperations.body, "utf-8")) match {
case _: DecodeResult.Failure => ZIO.fail(StatusCode.BadRequest)
Expand Down Expand Up @@ -191,7 +191,7 @@ object TapirAdapter {
.provideSomeLayer[R with Random](uploadQuery.fileHandle.toLayerMany)
} yield response

io.either
requestInterceptor(serverRequest)(io).either
}

makeHttpUploadEndpoint.serverLogic(logic)
Expand Down Expand Up @@ -288,7 +288,7 @@ object TapirAdapter {
} yield pipe

makeWebSocketEndpoint.serverLogic[RIO[R, *]](serverRequest =>
requestInterceptor(serverRequest).foldM(statusCode => ZIO.left(statusCode), _ => io)
requestInterceptor(serverRequest)(io).catchAll(ZIO.left(_))
)
}

Expand Down

0 comments on commit 1c6d306

Please sign in to comment.