diff --git a/core/src/main/scala/sttp/client4/backend.scala b/core/src/main/scala/sttp/client4/backend.scala index b61e54b278..77ca89010c 100644 --- a/core/src/main/scala/sttp/client4/backend.scala +++ b/core/src/main/scala/sttp/client4/backend.scala @@ -53,9 +53,7 @@ trait SyncBackend extends Backend[Identity] { } /** A [[GenericBackend]] which is synchronous (side effects are run directly), and supports web sockets. */ -trait WebSocketSyncBackend extends SyncBackend with WebSocketBackend[Identity] { - override def monad: MonadError[Identity] = IdMonad -} +trait WebSocketSyncBackend extends SyncBackend with WebSocketBackend[Identity] /** A [[GenericBackend]] which supports streams of type `S` and uses `F` to represent side-effects. */ trait StreamBackend[F[_], +S] extends Backend[F] with GenericBackend[F, S] diff --git a/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientBackend.scala b/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientBackend.scala index 077f173a08..d9ce966472 100644 --- a/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientBackend.scala +++ b/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientBackend.scala @@ -152,12 +152,12 @@ abstract class HttpClientBackend[F[_], S <: Streams[S], P, B]( wsBuilder } - private[client4] def filterIllegalWsHeaders[T](request: GenericRequest[T, R]): GenericRequest[T, R] = + private def filterIllegalWsHeaders[T](request: GenericRequest[T, R]): GenericRequest[T, R] = request.withHeaders(request.headers.filter(h => !wsIllegalHeaders.contains(h.name.toLowerCase))) // these headers can't be sent using HttpClient; the SecWebSocketProtocol is supported through a builder method, // the resit is ignored - private[client4] lazy val wsIllegalHeaders: Set[String] = { + private lazy val wsIllegalHeaders: Set[String] = { import HeaderNames._ Set(SecWebSocketAccept, SecWebSocketExtensions, SecWebSocketKey, SecWebSocketVersion, SecWebSocketProtocol).map( _.toLowerCase diff --git a/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientSyncBackend.scala b/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientSyncBackend.scala index 3467212b73..4ec80b2168 100644 --- a/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientSyncBackend.scala +++ b/core/src/main/scalajvm/sttp/client4/httpclient/HttpClientSyncBackend.scala @@ -16,7 +16,7 @@ import sttp.client4.internal.httpclient.{ } import sttp.client4.internal.ws.{SimpleQueue, SyncQueue, WebSocketEvent} import sttp.client4.monad.IdMonad -import sttp.client4.testing.{WebSocketBackendStub, WebSocketSyncBackendStub} +import sttp.client4.testing.WebSocketSyncBackendStub import sttp.client4.{wrappers, BackendOptions, GenericRequest, Identity, Response, WebSocketSyncBackend} import sttp.model.StatusCode import sttp.monad.MonadError @@ -31,7 +31,6 @@ import java.util.concurrent.{ArrayBlockingQueue, CompletionException} import java.util.concurrent.atomic.AtomicBoolean import java.util.zip.{GZIPInputStream, InflaterInputStream} import scala.concurrent.{blocking, Await, ExecutionContext, Future} -import scala.util.Try class HttpClientSyncBackend private ( client: HttpClient, @@ -46,6 +45,7 @@ class HttpClientSyncBackend private ( with WebSocketSyncBackend { private implicit val ec: ExecutionContext = ExecutionContext.global + private implicit def _monad: MonadError[Identity] = monad override val streams: NoStreams = NoStreams override protected def sendRegular[T](request: GenericRequest[T, R]): Identity[Response[T]] = { @@ -64,7 +64,7 @@ class HttpClientSyncBackend private ( Left(emptyInputStream()), request ) - }(monad) + } } private def sendWebSocket[T]( @@ -75,9 +75,9 @@ class HttpClientSyncBackend private ( val isOpen: AtomicBoolean = new AtomicBoolean(false) val responseCell = new ArrayBlockingQueue[Either[Throwable, Future[Response[T]]]](1) - def fillCellError(t: Throwable): Unit = responseCell.add(Left(t)) + def fillCellError(t: Throwable): Unit = responseCell.add(Left(t)): Unit - def fillCell(wr: Future[Response[T]]): Unit = responseCell.add(Right(wr)) + def fillCell(wr: Future[Response[T]]): Unit = responseCell.add(Right(wr)): Unit val listener = new DelegatingWebSocketListener( new AddToQueueListener(queue, isOpen), @@ -88,10 +88,7 @@ class HttpClientSyncBackend private ( isOpen, sequencer, monad, - cf => - monad.suspend { - Try(cf.get()).fold(monad.error, _ => monad.unit(())) - } + _.get(): Unit ) val baseResponse = Response((), StatusCode.SwitchingProtocols, "", Nil, Nil, request.onlyMetadata) val body = Future(blocking(bodyFromHttpClient(Right(webSocket), request.response, baseResponse))) diff --git a/core/src/main/scalajvm/sttp/client4/internal/httpclient/IdSequencer.scala b/core/src/main/scalajvm/sttp/client4/internal/httpclient/IdSequencer.scala index 35542f7bec..3420f39b89 100644 --- a/core/src/main/scalajvm/sttp/client4/internal/httpclient/IdSequencer.scala +++ b/core/src/main/scalajvm/sttp/client4/internal/httpclient/IdSequencer.scala @@ -9,9 +9,7 @@ private[client4] class IdSequencer extends Sequencer[Identity] { private val semaphore = new Semaphore(1) def apply[T](t: => T): T = { - blocking { - semaphore.acquire() - } + blocking(semaphore.acquire()) try t finally semaphore.release() } diff --git a/docs/json.md b/docs/json.md index e1d974cb7c..0a100503b0 100644 --- a/docs/json.md +++ b/docs/json.md @@ -163,7 +163,7 @@ import sttp.client4._ import sttp.client4.ziojson._ import zio.json._ -val backend: WebSocketSyncBackend = DefaultSyncBackend() +val backend: SyncBackend = DefaultSyncBackend() implicit val payloadJsonEncoder: JsonEncoder[RequestPayload] = DeriveJsonEncoder.gen[RequestPayload] implicit val myResponseJsonDecoder: JsonDecoder[ResponsePayload] = DeriveJsonDecoder.gen[ResponsePayload]