Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Jul 26, 2023
1 parent fd55305 commit d835ffc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
4 changes: 1 addition & 3 deletions core/src/main/scala/sttp/client4/backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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]] = {
Expand All @@ -64,7 +64,7 @@ class HttpClientSyncBackend private (
Left(emptyInputStream()),
request
)
}(monad)
}
}

private def sendWebSocket[T](
Expand All @@ -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),
Expand All @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion docs/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d835ffc

Please sign in to comment.