Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting options for AkkaHttpServerInterpreter as explicit parameter #1321

Merged
merged 34 commits into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
29add8d
Setting options for AkkaHttpServerInterpreter as explicit parameter
Jun 15, 2021
dba6ec5
Fixes in docs after changes in handling options
Jun 15, 2021
35baac7
Fixed compilation issue in AkkaHttpServerTest
Jun 15, 2021
3bc60b4
Setting options for Finatra based servers as explicit parameter
Jun 15, 2021
54f9e13
Setting options for Http4s based servers as explicit parameter
Jun 16, 2021
f776b00
Setting options for PlayServerInterpreter as explicit parameter
Jun 17, 2021
3e1df5e
Setting options for Vertx based servers as explicit parameter
Jun 17, 2021
2a86735
Setting options for Http4sClientInterpreter as explicit parameter
Jun 17, 2021
b59f54c
Setting options for PlayClientInterpreter as explicit parameter
Jun 17, 2021
416e181
Setting options for Sttp based clients as explicit parameter
Jun 17, 2021
bec9900
Setting options for OpenApiDocsInterpreter as explicit parameter
Jun 18, 2021
b12b3cf
Setting options for AsyncAPIInterpreter as explicit parameter
Jun 18, 2021
67fe428
Removed duplicated brakes from README
Jun 21, 2021
f489684
Removed depreacted traits
Jun 21, 2021
013da1f
Move options to SttpClientInterpreter and used self-type in Extensions
Jun 21, 2021
1a54628
Renames for Http4s based servers
Jun 21, 2021
828b60b
Moved compation objects for Vertx based servers to traits
Jun 21, 2021
520556c
Removed implicit keyword for default options
Jun 21, 2021
9eda3b1
Setting options for AwsCatsEffectServerInterpreter as explicit parameter
Jun 21, 2021
259c215
Setting options for AwsSamInterpreter as explicit parameter
Jun 21, 2021
1434366
Setting options for AwsTerraformInterpreter as explicit parameter
Jun 22, 2021
490d2af
Merge branch 'master' into explicit-options-parameters
Jun 22, 2021
1746528
Fixes after resolving merge conflicts
Jun 22, 2021
84b3c53
Change zioReadStreamCompatible to explicit
Jun 22, 2021
d4de859
Fixes after failed CI
Jun 22, 2021
5a5e6f6
Added type parameter to Http4sServerInterpreter usage
Jun 22, 2021
b53705f
Fixes after review
Jun 23, 2021
8676e8b
Change implicit ReadStremCompatibiles to explicit
Jun 23, 2021
2de3ef7
Fixes after failed CI
Jun 23, 2021
c7857c7
Adjust docs
adamw Jun 23, 2021
11675b3
Simplify example in docs
adamw Jun 23, 2021
7a43abb
Remove whitespace, formatting
adamw Jun 23, 2021
1f84695
Remove implicit options & simplify aws terraform example
adamw Jun 23, 2021
e074b99
Fix test compilation
adamw Jun 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ val booksListing: Endpoint[(BooksFromYear, Limit, AuthToken), String, List[Book]
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter
import sttp.tapir.openapi.circe.yaml._

val docs = OpenAPIDocsInterpreter.toOpenAPI(booksListing, "My Bookshop", "1.0")
val docs = OpenAPIDocsInterpreter().toOpenAPI(booksListing, "My Bookshop", "1.0")
println(docs.toYaml)


Expand All @@ -78,7 +78,7 @@ import sttp.tapir.client.sttp.SttpClientInterpreter
import sttp.client3._

val booksListingRequest: Request[DecodeResult[Either[String, List[Book]]], Any] =
SttpClientInterpreter
SttpClientInterpreter()
.toRequest(booksListing, Some(uri"http://localhost:8080"))
.apply((BooksFromYear("SF", 2016), 20, "xyz-abc-123"))
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import sttp.tapir.{DecodeResult, Endpoint}

abstract class Http4sClientInterpreter[F[_]: ContextShift: Effect] {

def http4sClientOptions: Http4sClientOptions = Http4sClientOptions.default

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri. If `baseUri` is not provided, the request will be a relative one.
*
Expand All @@ -16,9 +18,8 @@ abstract class Http4sClientInterpreter[F[_]: ContextShift: Effect] {
*/
def toRequest[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[String])(implicit
blocker: Blocker,
clientOptions: Http4sClientOptions
): I => (Request[F], Response[F] => F[DecodeResult[Either[E, O]]]) =
new EndpointToHttp4sClient(blocker, clientOptions).toHttp4sRequest[I, E, O, R, F](e, baseUri)
new EndpointToHttp4sClient(blocker, http4sClientOptions).toHttp4sRequest[I, E, O, R, F](e, baseUri)

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri. If `baseUri` is not provided, the request will be a relative one.
Expand All @@ -29,12 +30,14 @@ abstract class Http4sClientInterpreter[F[_]: ContextShift: Effect] {
* - a response parser that extracts the expected entity from the received `org.http4s.Response[F]`.
*/
def toRequestUnsafe[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[String])(implicit
blocker: Blocker,
clientOptions: Http4sClientOptions
blocker: Blocker
): I => (Request[F], Response[F] => F[Either[E, O]]) =
new EndpointToHttp4sClient(blocker, clientOptions).toHttp4sRequestUnsafe[I, E, O, R, F](e, baseUri)
new EndpointToHttp4sClient(blocker, http4sClientOptions).toHttp4sRequestUnsafe[I, E, O, R, F](e, baseUri)
}

object Http4sClientInterpreter {
def apply[F[_]: ContextShift: Effect]: Http4sClientInterpreter[F] = new Http4sClientInterpreter[F] {}
def apply[F[_]: ContextShift: Effect](clientOptions: Http4sClientOptions = Http4sClientOptions.default): Http4sClientInterpreter[F] =
new Http4sClientInterpreter[F] {
override def http4sClientOptions: Http4sClientOptions = clientOptions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import java.io.File
case class Http4sClientOptions(createFile: () => File)

object Http4sClientOptions {
implicit val default: Http4sClientOptions = Http4sClientOptions(Defaults.createTempFile)
val default: Http4sClientOptions = Http4sClientOptions(Defaults.createTempFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Http4sClientRequestTests extends AnyFunSuite with Matchers {
val testEndpoint = endpoint.get.in(query[Option[String]]("param"))

// when
val (http4sRequest, _) = Http4sClientInterpreter[IO]
val (http4sRequest, _) = Http4sClientInterpreter[IO]()
.toRequest(testEndpoint, baseUri = None)
.apply(None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ abstract class Http4sClientTests[R] extends ClientTests[R] {
implicit val blocker: Blocker = Blocker.liftExecutionContext(global)

override def send[I, E, O](e: Endpoint[I, E, O, R], port: Port, args: I, scheme: String = "http"): IO[Either[E, O]] = {
val (request, parseResponse) = Http4sClientInterpreter[IO].toRequestUnsafe(e, Some(s"http://localhost:$port")).apply(args)
val (request, parseResponse) = Http4sClientInterpreter[IO]().toRequestUnsafe(e, Some(s"http://localhost:$port")).apply(args)

sendAndParseResponse(request, parseResponse)
}

override def safeSend[I, E, O](e: Endpoint[I, E, O, R], port: Port, args: I): IO[DecodeResult[Either[E, O]]] = {
val (request, parseResponse) = Http4sClientInterpreter[IO].toRequest(e, Some(s"http://localhost:$port")).apply(args)
val (request, parseResponse) = Http4sClientInterpreter[IO]().toRequest(e, Some(s"http://localhost:$port")).apply(args)

sendAndParseResponse(request, parseResponse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import sttp.tapir.{DecodeResult, Endpoint}

trait PlayClientInterpreter {

def playClientOptions: PlayClientOptions = PlayClientOptions.default

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri.
*
Expand All @@ -15,10 +17,9 @@ trait PlayClientInterpreter {
* - a response parser to use on the `StandaloneWSResponse` obtained after executing the request.
*/
def toRequest[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: String)(implicit
clientOptions: PlayClientOptions,
ws: StandaloneWSClient
): I => (StandaloneWSRequest, StandaloneWSResponse => DecodeResult[Either[E, O]]) =
new EndpointToPlayClient(clientOptions, ws).toPlayRequest(e, baseUri)
new EndpointToPlayClient(playClientOptions, ws).toPlayRequest(e, baseUri)

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri.
Expand All @@ -32,11 +33,16 @@ trait PlayClientInterpreter {
* @throws IllegalArgumentException when response parsing fails
*/
def toRequestUnsafe[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: String)(implicit
clientOptions: PlayClientOptions,
ws: StandaloneWSClient
): I => (StandaloneWSRequest, StandaloneWSResponse => Either[E, O]) =
new EndpointToPlayClient(clientOptions, ws).toPlayRequestUnsafe(e, baseUri)
new EndpointToPlayClient(playClientOptions, ws).toPlayRequestUnsafe(e, baseUri)

}

object PlayClientInterpreter extends PlayClientInterpreter
object PlayClientInterpreter {
def apply(clientOptions: PlayClientOptions = PlayClientOptions.default): PlayClientInterpreter = {
new PlayClientInterpreter {
override def playClientOptions: PlayClientOptions = clientOptions
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import sttp.tapir.Defaults
case class PlayClientOptions(createFile: () => File)

object PlayClientOptions {
implicit val default: PlayClientOptions = PlayClientOptions(Defaults.createTempFile)
val default: PlayClientOptions = PlayClientOptions(Defaults.createTempFile)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class PlayClientTests[R] extends ClientTests[R] {

override def send[I, E, O](e: Endpoint[I, E, O, R], port: Port, args: I, scheme: String = "http"): IO[Either[E, O]] = {
def response: Future[Either[E, O]] = {
val (req, responseParser) = PlayClientInterpreter.toRequestUnsafe(e, s"http://localhost:$port").apply(args)
val (req, responseParser) = PlayClientInterpreter().toRequestUnsafe(e, s"http://localhost:$port").apply(args)
req.execute().map(responseParser)
}
IO.fromFuture(IO(response))
Expand All @@ -31,7 +31,7 @@ abstract class PlayClientTests[R] extends ClientTests[R] {
args: I
): IO[DecodeResult[Either[E, O]]] = {
def response: Future[DecodeResult[Either[E, O]]] = {
val (req, responseParser) = PlayClientInterpreter.toRequest(e, s"http://localhost:$port").apply(args)
val (req, responseParser) = PlayClientInterpreter().toRequest(e, s"http://localhost:$port").apply(args)
req.execute().map(responseParser)
}
IO.fromFuture(IO(response))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import sttp.tapir.{DecodeResult, Endpoint}

trait SttpClientInterpreter extends SttpClientInterpreterExtensions {

def sttpClientOptions: SttpClientOptions = SttpClientOptions.default

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri. If `baseUri` is not provided, the request will be a relative one.
*
Expand All @@ -14,7 +16,6 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* and the result of decoding the response (error or success value) is returned.
*/
def toClient[F[_], I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri], backend: SttpBackend[F, R])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => F[DecodeResult[Either[E, O]]] = {
val req = toRequest(e, baseUri)
Expand All @@ -30,7 +31,6 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* instead.
*/
def toClientThrowDecodeFailures[F[_], I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri], backend: SttpBackend[F, R])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => F[Either[E, O]] = {
val req = toRequestThrowDecodeFailures(e, baseUri)
Expand All @@ -46,7 +46,6 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* error value, a failed effect is returned instead.
*/
def toClientThrowErrors[F[_], I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri], backend: SttpBackend[F, R])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => F[O] = {
val req = toRequestThrowErrors(e, baseUri)
Expand All @@ -62,10 +61,9 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* success values (note that this can be the body enriched with data from headers/status code).
*/
def toRequest[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => Request[DecodeResult[Either[E, O]], R] =
new EndpointToSttpClient(clientOptions, wsToPipe).toSttpRequest(e, baseUri)
new EndpointToSttpClient(sttpClientOptions, wsToPipe).toSttpRequest(e, baseUri)

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri. If `baseUri` is not provided, the request will be a relative one.
Expand All @@ -77,10 +75,9 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* effect, when response parsing fails.
*/
def toRequestThrowDecodeFailures[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => Request[Either[E, O], R] =
i => new EndpointToSttpClient(clientOptions, wsToPipe).toSttpRequest(e, baseUri).apply(i).mapResponse(throwDecodeFailures)
i => new EndpointToSttpClient(sttpClientOptions, wsToPipe).toSttpRequest(e, baseUri).apply(i).mapResponse(throwDecodeFailures)

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
* uri. If `baseUri` is not provided, the request will be a relative one.
Expand All @@ -94,11 +91,10 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
* @throws IllegalArgumentException when response parsing fails
*/
def toRequestThrowErrors[I, E, O, R](e: Endpoint[I, E, O, R], baseUri: Option[Uri])(implicit
clientOptions: SttpClientOptions,
wsToPipe: WebSocketToPipe[R]
): I => Request[O, R] =
i =>
new EndpointToSttpClient(clientOptions, wsToPipe)
new EndpointToSttpClient(sttpClientOptions, wsToPipe)
.toSttpRequest(e, baseUri)
.apply(i)
.mapResponse(throwDecodeFailures)
Expand All @@ -119,4 +115,10 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
s"Endpoint ${endpoint.show} returned error: $e, for inputs: $i."
}

object SttpClientInterpreter extends SttpClientInterpreter
object SttpClientInterpreter {
def apply(clientOptions: SttpClientOptions = SttpClientOptions.default): SttpClientInterpreter = {
new SttpClientInterpreter {
override def sttpClientOptions: SttpClientOptions = clientOptions
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import sttp.tapir.{Defaults, TapirFile}
case class SttpClientOptions(createFile: () => TapirFile)

object SttpClientOptions {
implicit val default: SttpClientOptions = SttpClientOptions(Defaults.createTempFile)
val default: SttpClientOptions = SttpClientOptions(Defaults.createTempFile)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import scala.concurrent.Future

trait SttpClientInterpreterExtensions {

this: SttpClientInterpreter =>

/** Interprets the endpoint as a synchronous client call, using the given `baseUri` as the starting point to create
* the target uri. If `baseUri` is not provided, the request will be a relative one.
*
Expand All @@ -16,12 +18,10 @@ trait SttpClientInterpreterExtensions {
* backend, and the result of decoding the response (error or success value) is returned. If decoding the result
* fails, a failed future is returned.
*/
def toQuickClient[I, E, O](e: Endpoint[I, E, O, Any], baseUri: Option[Uri])(implicit
clientOptions: SttpClientOptions
): I => Future[Either[E, O]] = {
def toQuickClient[I, E, O](e: Endpoint[I, E, O, Any], baseUri: Option[Uri]): I => Future[Either[E, O]] = {
import scala.concurrent.ExecutionContext.Implicits.global
val backend: SttpBackend[Future, Any] = FetchBackend()
SttpClientInterpreter.toClientThrowDecodeFailures(e, baseUri, backend)
SttpClientInterpreter().toClientThrowDecodeFailures(e, baseUri, backend)
}

/** Interprets the endpoint as a client call, using the given `baseUri` as the starting point to create the target
Expand All @@ -32,11 +32,9 @@ trait SttpClientInterpreterExtensions {
* backend, and the result (success value) is returned. If decoding the result fails, or if the response corresponds
* to an error value, a failed future is returned.
*/
def toQuickClientThrowErrors[I, E, O](e: Endpoint[I, E, O, Any], baseUri: Option[Uri])(implicit
clientOptions: SttpClientOptions
): I => Future[O] = {
def toQuickClientThrowErrors[I, E, O](e: Endpoint[I, E, O, Any], baseUri: Option[Uri]): I => Future[O] = {
import scala.concurrent.ExecutionContext.Implicits.global
val backend: SttpBackend[Future, Any] = FetchBackend()
SttpClientInterpreter.toClientThrowErrors(e, baseUri, backend)
SttpClientInterpreter().toClientThrowErrors(e, baseUri, backend)
}
}
Loading