forked from softwaremill/tapir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scala Native support for sttpClient with tests
- Loading branch information
Showing
16 changed files
with
235 additions
and
24 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
2 changes: 1 addition & 1 deletion
2
client/http4s-client/src/test/scala/sttp/tapir/client/http4s/Http4ClientStreamingTests.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
60 changes: 60 additions & 0 deletions
60
...-client/src/main/scalanative/sttp/tapir/client/sttp/SttpClientInterpreterExtensions.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,60 @@ | ||
package sttp.tapir.client.sttp | ||
|
||
import sttp.client3.CurlBackend | ||
import sttp.model.Uri | ||
import sttp.tapir.{Endpoint, PublicEndpoint} | ||
|
||
trait SttpClientInterpreterExtensions { this: SttpClientInterpreter => | ||
|
||
// public | ||
|
||
/** Interprets the public 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. | ||
* | ||
* Returns a function which, when applied to the endpoint's input parameters (given as a tuple), will encode them to appropriate request | ||
* parameters: path, query, headers and body. The request is sent using a synchronous backend, and the result of decoding the response | ||
* (error or success value) is returned. If decoding the result fails, an exception is thrown. | ||
*/ | ||
def toQuickClient[I, E, O](e: PublicEndpoint[I, E, O, Any], baseUri: Option[Uri]): I => Either[E, O] = { | ||
val backend = CurlBackend() | ||
SttpClientInterpreter().toClientThrowDecodeFailures(e, baseUri, backend) | ||
} | ||
|
||
/** Interprets the public 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. | ||
* | ||
* Returns a function which, when applied to the endpoint's input parameters (given as a tuple), will encode them to appropriate request | ||
* parameters: path, query, headers and body. The request is sent using a synchronous backend, and the result (success value) is | ||
* returned. If decoding the result fails, or if the response corresponds to an error value, an exception is thrown. | ||
*/ | ||
def toQuickClientThrowErrors[I, E, O](e: PublicEndpoint[I, E, O, Any], baseUri: Option[Uri]): I => O = { | ||
val backend = CurlBackend() | ||
SttpClientInterpreter().toClientThrowErrors(e, baseUri, backend) | ||
} | ||
|
||
// secure | ||
|
||
/** Interprets the secure 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. | ||
* | ||
* Returns a function which, when applied to the endpoint's security and regular input parameters (given as tuples), will encode them to | ||
* appropriate request parameters: path, query, headers and body. The request is sent using a synchronous backend, and the result of | ||
* decoding the response (error or success value) is returned. If decoding the result fails, an exception is thrown. | ||
*/ | ||
def toQuickSecureClient[A, I, E, O](e: Endpoint[A, I, E, O, Any], baseUri: Option[Uri]): A => I => Either[E, O] = { | ||
val backend = CurlBackend() | ||
SttpClientInterpreter().toSecureClientThrowDecodeFailures(e, baseUri, backend) | ||
} | ||
|
||
/** Interprets the secure 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. | ||
* | ||
* Returns a function which, when applied to the endpoint's security and regular input parameters (given as tuples), will encode them to | ||
* appropriate request parameters: path, query, headers and body. The request is sent using a synchronous backend, and the result | ||
* (success value) is returned. If decoding the result fails, or if the response corresponds to an error value, an exception is thrown. | ||
*/ | ||
def toQuickSecureClientThrowErrors[A, I, E, O](e: Endpoint[A, I, E, O, Any], baseUri: Option[Uri]): A => I => O = { | ||
val backend = CurlBackend() | ||
SttpClientInterpreter().toSecureClientThrowErrors(e, baseUri, backend) | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
client/sttp-client/src/test/scalajvm/sttp/tapir/client/sttp/SttpClientStreamingTests.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
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
58 changes: 58 additions & 0 deletions
58
client/sttp-client/src/test/scalanative/sttp/tapir/client/sttp/SttpClientTests.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,58 @@ | ||
package sttp.tapir.client.sttp | ||
|
||
import scala.util.Try | ||
import cats.effect.IO | ||
|
||
import sttp.tapir.{DecodeResult, Endpoint} | ||
import sttp.tapir.client.tests.ClientTests | ||
import sttp.client3._ | ||
|
||
object Backend { | ||
val backend = CurlTryBackend(verbose = false) | ||
} | ||
|
||
abstract class SttpClientTests[R >: Any] extends ClientTests[R] { | ||
|
||
val backend: SttpBackend[Try, R] = Backend.backend | ||
def wsToPipe: WebSocketToPipe[R] | ||
|
||
override def send[A, I, E, O]( | ||
e: Endpoint[A, I, E, O, R], | ||
port: Port, | ||
securityArgs: A, | ||
args: I, | ||
scheme: String = "http" | ||
): IO[Either[E, O]] = { | ||
implicit val wst: WebSocketToPipe[R] = wsToPipe | ||
val response: Try[Either[E, O]] = | ||
SttpClientInterpreter() | ||
.toSecureRequestThrowDecodeFailures(e, Some(uri"$scheme://localhost:$port")) | ||
.apply(securityArgs) | ||
.apply(args) | ||
.send(backend) | ||
.map(_.body) | ||
IO.fromTry(response) | ||
} | ||
|
||
override def safeSend[A, I, E, O]( | ||
e: Endpoint[A, I, E, O, R], | ||
port: Port, | ||
securityArgs: A, | ||
args: I | ||
): IO[DecodeResult[Either[E, O]]] = { | ||
implicit val wst: WebSocketToPipe[R] = wsToPipe | ||
def response: Try[DecodeResult[Either[E, O]]] = | ||
SttpClientInterpreter() | ||
.toSecureRequest(e, Some(uri"http://localhost:$port")) | ||
.apply(securityArgs) | ||
.apply(args) | ||
.send(backend) | ||
.map(_.body) | ||
IO.fromTry(response) | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
backend.close() | ||
super.afterAll() | ||
} | ||
} |
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
1 change: 0 additions & 1 deletion
1
client/tests/src/main/scala/sttp/tapir/client/tests/ClientMultipartTests.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
1 change: 0 additions & 1 deletion
1
client/tests/src/main/scala/sttp/tapir/client/tests/ClientStreamingTests.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
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
1 change: 0 additions & 1 deletion
1
client/tests/src/main/scala/sttp/tapir/client/tests/ClientWebSocketTests.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
14 changes: 14 additions & 0 deletions
14
client/tests/src/main/scalajs/sttp/tapir/client/tests/ClientContext.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,14 @@ | ||
package sttp.tapir.client.tests | ||
|
||
import cats.effect.unsafe.IORuntime | ||
import scala.concurrent.ExecutionContext | ||
|
||
object ClientContext { | ||
// Using the default ScalaTest execution context seems to cause issues on JS. | ||
// https://github.com/scalatest/scalatest/issues/1039 | ||
val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
val ioRT: IORuntime = cats.effect.unsafe.implicits.global | ||
|
||
val platformIsScalaJS: Boolean = true | ||
val platformIsScalaNative: Boolean = false | ||
} |
14 changes: 14 additions & 0 deletions
14
client/tests/src/main/scalajvm/sttp/tapir/client/tests/ClientContext.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,14 @@ | ||
package sttp.tapir.client.tests | ||
|
||
import cats.effect.unsafe.IORuntime | ||
import scala.concurrent.ExecutionContext | ||
|
||
object ClientContext { | ||
// Using the default ScalaTest execution context seems to cause issues on JS. | ||
// https://github.com/scalatest/scalatest/issues/1039 | ||
val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
val ioRT: IORuntime = cats.effect.unsafe.implicits.global | ||
|
||
val platformIsScalaJS: Boolean = false | ||
val platformIsScalaNative: Boolean = false | ||
} |
21 changes: 21 additions & 0 deletions
21
client/tests/src/main/scalanative/sttp/tapir/client/tests/ClientContext.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,21 @@ | ||
package sttp.tapir.client.tests | ||
|
||
import cats.effect.unsafe.IORuntime | ||
import scala.concurrent.ExecutionContext | ||
|
||
object ClientContext { | ||
// This two lines should be fine to define IO Runtime and ExcecutionContext on Scala Native, but ScalaTest AsyncFunSuite fails with Timeout exception | ||
// val ioRT:IORuntime = cats.effect.unsafe.implicits.global | ||
// val executionContext: ExecutionContext = ioRT.compute | ||
|
||
// Inspired by the default ExecutionContext implementation in MUnit, MUnit works fine on Native | ||
val executionContext: ExecutionContext = new ExecutionContext { | ||
def execute(runnable: Runnable): Unit = runnable.run() | ||
def reportFailure(cause: Throwable): Unit = cause.printStackTrace() | ||
} | ||
private val globalRT:IORuntime = cats.effect.unsafe.implicits.global | ||
val ioRT:IORuntime = IORuntime.apply(executionContext,executionContext,globalRT.scheduler,globalRT.shutdown,globalRT.config) | ||
|
||
val platformIsScalaJS: Boolean = false | ||
val platformIsScalaNative: Boolean = true | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/src/main/scalanative/sttp/tapir/tests/TestUtilExtensions.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,24 @@ | ||
package sttp.tapir.tests | ||
|
||
import java.io.{File, PrintWriter} | ||
|
||
import scala.concurrent.Future | ||
import scala.io.Source | ||
|
||
trait TestUtilExtensions { | ||
def writeToFile(s: String): File = { | ||
val f = File.createTempFile("test", "tapir") | ||
new PrintWriter(f) { write(s); close() } | ||
f.deleteOnExit() | ||
f | ||
} | ||
|
||
def readFromFile(f: File): Future[String] = { | ||
val s = Source.fromFile(f) | ||
try { | ||
Future.successful(s.mkString) | ||
} finally { | ||
s.close() | ||
} | ||
} | ||
} |
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 @@ | ||
ThisBuild/version := "1.1.3-SNAPSHOT" |