Skip to content

Instantly share code, notes, and snippets.

Adam Warski adamw

Block or report user

Report or block adamw

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View sttp4.scala
package sttp.client.asynchttpclient.monix
import monix.eval.Task
import sttp.client._
import{WebSocket, WebSocketResponse}
import scala.concurrent.duration._
View sttp3.scala
import cats.effect.{ContextShift, IO, Timer}
import{WebSocket, WebSocketListener}
import sttp.client._
import sttp.client.asynchttpclient.WebSocketHandler
import scala.concurrent.duration._
object CatsWebsocketExample extends App {
View sttp2.scala
package sttp.client.akkahttp
import akka.Done
import{ActorSystem, Cancellable}
import{Message, TextMessage}
import{Flow, Keep, Sink, Source}
import akka.util.ByteString
import sttp.client._
View sttp1.scala
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]
View t11.scala
val countPersons: ConnectionIO[Int] =
sql"SELECT COUNT(*) FROM persons".query[Int].unique
val callFromCount: ConnectionIO[IO[Unit]] = { count =>
if (count == 0) IO(println("No users!")) else IO(println(s"Found $count users"))
val showResults: IO[Unit] = callFromCount.transact(transactor).flatten
View t10.scala
val insertEventQuery: ConnectionIO[Int] =
sql"INSERT INTO events(msg) VALUES('made a http call')"
val result: IO[Int] = queryFromHttpCall
.map(countQuery => insertEventQuery >> countQuery)
View t9.scala
val httpCall: IO[String] = IO("Zoe")
val queryFromHttpCall: IO[ConnectionIO[Int]] = { name =>
sql"SELECT COUNT(*) FROM persons WHERE name = $name".query[Int].unique
View t8.scala
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)
View t7.scala
// Re-useable effect descriptions
val sendEmail: IO[Unit] = IO(println("Sending email"))
val insertUser: ConnectionIO[Int] =
sql"INSERT INTO users(name, email) VALUES('Emily', '')"
// (1) BEGIN; INSERT; sendEmail(); COMMIT;
val insertThenSend: ConnectionIO[Unit] =
insertUser.flatMap(_ =>[ConnectionIO])
View t6.scala
val sendEmail: IO[Unit] = IO(println("Sending email"))
val sendEmailWithConnection: ConnectionIO[Unit] =[ConnectionIO]
val sendEmailInTx: IO[Unit] = sendEmailWithConnection.transact(transactor)
You can’t perform that action at this time.