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 | |
} |
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 insertUserInTx: IO[Int] = insertUser.transact(transactor) | |
// (2) BEGIN; INSERT; COMMIT; sendEmail(); | |
val result2: IO[Unit] = insertUserInTx.flatMap(_ => sendEmail) | |
// (3) sendEmail(); BEGIN; INSERT; COMMIT; | |
val result3: IO[Int] = sendEmail.flatMap(_ => insertUserInTx) |
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
// Re-useable effect descriptions | |
val sendEmail: IO[Unit] = IO(println("Sending email")) | |
val insertUser: ConnectionIO[Int] = | |
sql"INSERT INTO users(name, email) VALUES('Emily', '[email protected]')" | |
.update.run | |
// (1) BEGIN; INSERT; sendEmail(); COMMIT; | |
val insertThenSend: ConnectionIO[Unit] = | |
insertUser.flatMap(_ => sendEmail.to[ConnectionIO]) | |
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 sendEmail: IO[Unit] = IO(println("Sending email")) | |
val sendEmailWithConnection: ConnectionIO[Unit] = | |
sendEmail.to[ConnectionIO] | |
val sendEmailInTx: IO[Unit] = sendEmailWithConnection.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
(1) BEGIN; INSERT; sendEmail(); COMMIT; | |
(2) BEGIN; INSERT; COMMIT; sendEmail(); | |
(3) sendEmail(); BEGIN; INSERT; COMMIT; | |
(4) BEGIN; sendEmail(); INSERT; COMMIT; |