-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1089 from softwaremill/interceptors
Implements #1065: Interceptors
- Loading branch information
Showing
125 changed files
with
2,823 additions
and
3,397 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
package sttp.tapir.model | ||
|
||
import java.net.{InetSocketAddress, URI} | ||
import sttp.model.{QueryParams, RequestMetadata} | ||
import java.net.InetSocketAddress | ||
|
||
import sttp.model.Method | ||
|
||
trait ServerRequest { | ||
def method: Method | ||
trait ServerRequest extends RequestMetadata { | ||
def protocol: String | ||
def uri: URI | ||
def connectionInfo: ConnectionInfo | ||
def headers: Seq[(String, String)] | ||
def header(name: String): Option[String] | ||
def underlying: Any | ||
|
||
/** Can differ from `uri.path`, if the endpoint is deployed in a context */ | ||
def pathSegments: List[String] | ||
def queryParameters: QueryParams | ||
} | ||
|
||
case class ConnectionInfo(local: Option[InetSocketAddress], remote: Option[InetSocketAddress], secure: Option[Boolean]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package sttp.tapir.model | ||
|
||
import sttp.model.{Header, Headers, ResponseMetadata, StatusCode} | ||
|
||
import scala.collection.immutable.Seq | ||
|
||
case class ServerResponse[B](code: StatusCode, headers: Seq[Header], body: Option[B]) extends ResponseMetadata { | ||
override def statusText: String = "" | ||
override def toString: String = s"ServerResponse($code,${Headers.toStringSafe(headers)})" | ||
} |
5 changes: 0 additions & 5 deletions
5
core/src/main/scala/sttp/tapir/server/DecodeFailureContext.scala
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
core/src/main/scala/sttp/tapir/server/DecodeFailureHandling.scala
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
core/src/main/scala/sttp/tapir/server/DefaultDecodeFailureHandler.scala
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
core/src/main/scala/sttp/tapir/server/interceptor/DecodeFailureContext.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package sttp.tapir.server.interceptor | ||
|
||
import sttp.tapir.model.ServerRequest | ||
import sttp.tapir.{DecodeResult, Endpoint, EndpointInput} | ||
|
||
case class DecodeFailureContext( | ||
failingInput: EndpointInput[_], | ||
failure: DecodeResult.Failure, | ||
endpoint: Endpoint[_, _, _, _], | ||
request: ServerRequest | ||
) |
44 changes: 44 additions & 0 deletions
44
core/src/main/scala/sttp/tapir/server/interceptor/EndpointInterceptor.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package sttp.tapir.server.interceptor | ||
|
||
import sttp.monad.MonadError | ||
import sttp.tapir.model.{ServerRequest, ServerResponse} | ||
import sttp.tapir.{DecodeResult, Endpoint, EndpointInput} | ||
|
||
/** Allows intercepting the handling of a request by an endpoint, when either the endpoint's inputs have been | ||
* decoded successfully, or when decoding has failed. | ||
* @tparam B The interpreter-specific, low-level type of body. | ||
*/ | ||
trait EndpointInterceptor[F[_], B] { | ||
|
||
/** Called when the the given `request` has been successfully decoded into inputs `i`, as described by | ||
* `endpoint.input`. | ||
* | ||
* Use `next(None)` to continue processing, ultimately (after the last interceptor) calling the endpoint's server | ||
* logic, and obtaining a response. Or, provide an alternative value+output pair, which will be used as the response. | ||
* | ||
* @tparam I The type of the endpoint's inputs. | ||
* @return An effect, describing the server's response. | ||
*/ | ||
def onDecodeSuccess[I]( | ||
request: ServerRequest, | ||
endpoint: Endpoint[I, _, _, _], | ||
i: I, | ||
next: Option[ValuedEndpointOutput[_]] => F[ServerResponse[B]] | ||
)(implicit monad: MonadError[F]): F[ServerResponse[B]] = next(None) | ||
|
||
/** Called when the the given `request` hasn't been successfully decoded into inputs `i`, as described by `endpoint`, | ||
* with `failure` occurring when decoding `failingInput`. | ||
* | ||
* Use `next(None)` to continue processing, ultimately (after the last interceptor) returning `None`, and attempting | ||
* to decode the next endpoint. Or, provide an alternative value+output pair, which will be used as the response. | ||
* | ||
* @return An effect, describing the optional server response. If `None`, the next endpoint will be tried (if any). | ||
*/ | ||
def onDecodeFailure( | ||
request: ServerRequest, | ||
endpoint: Endpoint[_, _, _, _], | ||
failure: DecodeResult.Failure, | ||
failingInput: EndpointInput[_], | ||
next: Option[ValuedEndpointOutput[_]] => F[Option[ServerResponse[B]]] | ||
)(implicit monad: MonadError[F]): F[Option[ServerResponse[B]]] = next(None) | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/scala/sttp/tapir/server/interceptor/ValuedEndpointOutput.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sttp.tapir.server.interceptor | ||
|
||
import sttp.tapir.EndpointOutput | ||
|
||
case class ValuedEndpointOutput[T](output: EndpointOutput[T], value: T) |
Oops, something went wrong.