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 Logging.scala
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}")
View polynote_sttp.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View polynote.scala
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
View sttp4.scala
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._
View sttp3.scala
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 {
View sttp2.scala
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
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]] = 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()
View t10.scala
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))
View t9.scala
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
}
You can’t perform that action at this time.