This file contains hidden or 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
| override def openWebsocket[T, WS_RESULT]( | |
| request: Request[T, S], | |
| handler: WS_HANDLER[WS_RESULT]): F[WebSocketResponse[WS_RESULT]] = { | |
| responseMonad.map(responseMonad.handleError(delegate.openWebsocket(request, handler)) { | |
| case e: Exception => | |
| logger.error(s"Exception when opening websocket: $request", e) | |
| responseMonad.error(e) | |
| }) { response => | |
| logger.debug(s"Websocket open: $request, with response headers: ${response.headers}") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
| case class LetterAndCount(letter: String, count: Int) | |
| // result: Response[GitHubResult] is what we've read from the API | |
| val firstLetterToName = result.items.groupBy(_.name.toUpperCase.charAt(0)) | |
| val pairs = firstLetterToName.toList.map { case (letter, projects) => | |
| LetterAndCount(letter.toString, projects.size) | |
| }.sortBy(_.letter) | |
| pairs |
This file contains hidden or 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
| package sttp.client.asynchttpclient.monix | |
| import monix.eval.Task | |
| import sttp.client._ | |
| import sttp.client.ws.{WebSocket, WebSocketResponse} | |
| import monix.execution.Scheduler.Implicits.global | |
| import sttp.model.ws.WebSocketFrame | |
| import scala.concurrent.duration._ |
This file contains hidden or 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
| import cats.effect.{ContextShift, IO, Timer} | |
| import org.asynchttpclient.ws.{WebSocket, WebSocketListener} | |
| import sttp.client._ | |
| import sttp.client.asynchttpclient.WebSocketHandler | |
| import sttp.client.ws.WebSocketResponse | |
| import scala.concurrent.ExecutionContext.global | |
| import scala.concurrent.duration._ | |
| object CatsWebsocketExample extends App { |
This file contains hidden or 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
| package sttp.client.akkahttp | |
| import akka.Done | |
| import akka.actor.{ActorSystem, Cancellable} | |
| import akka.http.scaladsl.model.ws.{Message, TextMessage} | |
| import akka.stream.ActorMaterializer | |
| import akka.stream.scaladsl.{Flow, Keep, Sink, Source} | |
| import akka.util.ByteString | |
| import sttp.client._ | |
| import sttp.client.ws.WebSocketResponse |
This file contains hidden or 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
| trait SttpBackend[F[_], -S, -WS_HANDLER[_]] { | |
| def send[T](request: Request[T, S]): F[Response[T]] | |
| def openWebsocket[T, WS_RESULT]( | |
| request: Request[T, S], | |
| handler: WS_HANDLER[WS_RESULT]): F[WebSocketResponse[WS_RESULT]] | |
| def close(): F[Unit] | |
| def responseMonad: MonadError[F] | |
| } |
This file contains hidden or 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
| val countPersons: ConnectionIO[Int] = | |
| sql"SELECT COUNT(*) FROM persons".query[Int].unique | |
| val callFromCount: ConnectionIO[IO[Unit]] = countPersons.map { count => | |
| if (count == 0) IO(println("No users!")) else IO(println(s"Found $count users")) | |
| } | |
| val showResults: IO[Unit] = callFromCount.transact(transactor).flatten | |
| showResults.unsafeRunSync() |
This file contains hidden or 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
| val insertEventQuery: ConnectionIO[Int] = | |
| sql"INSERT INTO events(msg) VALUES('made a http call')".update.run | |
| val result: IO[Int] = queryFromHttpCall | |
| .map(countQuery => insertEventQuery >> countQuery) | |
| .flatMap(_.transact(transactor)) |
This file contains hidden or 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
| val httpCall: IO[String] = IO("Zoe") | |
| val queryFromHttpCall: IO[ConnectionIO[Int]] = | |
| httpCall.map { name => | |
| sql"SELECT COUNT(*) FROM persons WHERE name = $name".query[Int].unique | |
| } |