io.circe.parser.decode Scala Examples
The following examples show how to use io.circe.parser.decode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: AkkaHttpLambdaHandlerSpec.scala From scala-server-lambda with MIT License | 5 votes |
package io.github.howardjohn.lambda.akka import akka.actor.ActorSystem import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller} import akka.http.scaladsl.model.MediaTypes.`application/json` import akka.http.scaladsl.model.headers.RawHeader import akka.http.scaladsl.model.{HttpEntity, StatusCodes} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller} import akka.stream.ActorMaterializer import io.circe.parser.decode import io.circe.syntax._ import io.circe.{Decoder, Encoder} import io.github.howardjohn.lambda.LambdaHandlerBehavior import io.github.howardjohn.lambda.LambdaHandlerBehavior._ import org.scalatest.{BeforeAndAfterAll, FeatureSpec, GivenWhenThen} import scala.concurrent.Future class AkkaHttpLambdaHandlerSpec extends FeatureSpec with LambdaHandlerBehavior with GivenWhenThen with BeforeAndAfterAll { implicit val system: ActorSystem = ActorSystem("test") implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val ec = scala.concurrent.ExecutionContext.Implicits.global val route: Route = path("hello") { (get & parameter("times".as[Int] ? 1)) { times => complete { Seq .fill(times)("Hello World!") .mkString(" ") } } } ~ path("long") { get { Thread.sleep(1000) complete("") } } ~ path("post") { post { entity(as[String]) { body => complete(body) } } } ~ path("json") { post { import CirceJsonMarshalling._ import io.circe.generic.auto._ entity(as[JsonBody]) { entity => complete(LambdaHandlerBehavior.jsonReturn.asJson) } } } ~ path("exception") { get { throw RouteException() } } ~ path("error") { get { complete(StatusCodes.InternalServerError) } } ~ path("header") { get { headerValueByName(inputHeader) { header => respondWithHeaders(RawHeader(outputHeader, outputHeaderValue)) { complete(header) } } } } val handler = new AkkaHttpLambdaHandler(route) scenariosFor(behavior(handler)) override def afterAll(): Unit = { materializer.shutdown() system.terminate() } } object CirceJsonMarshalling { implicit final def unmarshaller[A: Decoder]: FromEntityUnmarshaller[A] = Unmarshaller.stringUnmarshaller .forContentTypes(`application/json`) .flatMap { ctx => mat => json => decode[A](json).fold(Future.failed, Future.successful) } implicit final def marshaller[A: Encoder]: ToEntityMarshaller[A] = Marshaller.withFixedContentType(`application/json`) { a => HttpEntity(`application/json`, a.asJson.noSpaces) } }
Example 2
Source File: AllCodec.scala From aws-lambda-scala with MIT License | 5 votes |
package io.github.mkotsur.aws.codecs import java.nio.charset.StandardCharsets.UTF_8 import io.circe.generic.auto._ import io.circe.parser.decode import io.circe.syntax._ import io.circe._ import io.github.mkotsur.aws.handler.{CanDecode, CanEncode} import cats.syntax.either.catsSyntaxEither import scala.io.Source import scala.reflect.ClassTag private[aws] trait AllCodec { implicit def canDecodeAll[T: ClassTag](implicit decoder: Decoder[T]) = CanDecode.instance[T]( implicitly[ClassTag[T]] match { case ct if ct.runtimeClass == classOf[String] => is => Right(Source.fromInputStream(is).mkString.asInstanceOf[T]) case _ => is => val string = Source.fromInputStream(is).mkString decode[T](if (string.isEmpty) "null" else string) } ) implicit def canEncodeAll[T: ClassTag](implicit encoder: Encoder[T]) = CanEncode.instance[T]( implicitly[ClassTag[T]] match { case ct if ct.runtimeClass == classOf[String] => (output, handledEither, _) => handledEither.map { s => output.write(s.asInstanceOf[String].getBytes) } case _ => (output, handledEither, _) => handledEither map { handled => val jsonString = handled.asJson.noSpaces output.write(jsonString.getBytes(UTF_8)) } } ) }
Example 3
Source File: PrintingBenchmarkSpec.scala From circe-jackson with Apache License 2.0 | 5 votes |
package io.circe.jackson.benchmark import io.circe.parser.decode import java.nio.ByteBuffer import org.scalatest.flatspec.AnyFlatSpec class PrintingBenchmarkSpec extends AnyFlatSpec { val benchmark: PrintingBenchmark = new PrintingBenchmark import benchmark._ private[this] def byteBufferToString(bb: ByteBuffer): String = { val array = new Array[Byte](bb.remaining) bb.get(array) new String(array, "UTF-8") } private[this] def decodeInts(json: String): Option[List[Int]] = decode[List[Int]](json).fold(_ => None, Some(_)) private[this] def decodeFoos(json: String): Option[Map[String, Foo]] = decode[Map[String, Foo]](json).fold(_ => None, Some(_)) it should "correctly print integers using Circe with Jackson" in { assert(decodeInts(printIntsCJString) === Some(ints)) assert(decodeInts(byteBufferToString(printIntsCJBytes)) === Some(ints)) } it should "correctly print case classes using Circe with Jackson" in { assert(decodeFoos(printFoosCJString) === Some(foos)) assert(decodeFoos(byteBufferToString(printFoosCJBytes)) === Some(foos)) } }
Example 4
Source File: JsonUnmarshallers.scala From scala-openrtb with Apache License 2.0 | 5 votes |
package com.powerspace.openrtb.akka.marshallers import akka.http.scaladsl.model.{ContentTypeRange, ContentTypes, HttpRequest} import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, FromRequestUnmarshaller, Unmarshaller} import akka.stream.Materializer import com.google.openrtb.{BidRequest, BidResponse} import io.circe.Decoder import scala.concurrent.{ExecutionContext, Future} import io.circe.parser.decode object JsonUnmarshallers { import akka.http.scaladsl.unmarshalling.PredefinedFromEntityUnmarshallers._ def bidRequestJsonUnmarshaller(implicit decoder: Decoder[BidRequest]): FromEntityUnmarshaller[BidRequest] = stringUnmarshaller .forContentTypes(ContentTypeRange(ContentTypes.`application/json`)) .map(implicit json => decode[BidRequest](json).handlingFailure) def bidResponseJsonUnmarshaller(implicit decoder: Decoder[BidResponse]): FromEntityUnmarshaller[BidResponse] = stringUnmarshaller .forContentTypes(ContentTypeRange(ContentTypes.`application/json`)) .map(implicit json => decode[BidResponse](json).handlingFailure) def bidRequestHttpJsonUnmarshaller(implicit decoder: Decoder[BidRequest]): FromRequestUnmarshaller[BidRequest] = new FromRequestUnmarshaller[BidRequest]() { override def apply( value: HttpRequest)(implicit ec: ExecutionContext, materializer: Materializer): Future[BidRequest] = Unmarshaller .stringUnmarshaller(value.entity) .map(implicit json => decode[BidRequest](json).handlingFailure) } def bidResponseHttpJsonUnmarshaller(implicit decoder: Decoder[BidResponse]): FromRequestUnmarshaller[BidResponse] = new FromRequestUnmarshaller[BidResponse]() { override def apply( value: HttpRequest)(implicit ec: ExecutionContext, materializer: Materializer): Future[BidResponse] = Unmarshaller .stringUnmarshaller(value.entity) .map(implicit json => decode[BidResponse](json).handlingFailure) } implicit class DecodingResultEnhancement[T](decodingResult: Either[_ <: Exception, T]) { def handlingFailure(implicit json: String): T = decodingResult match { case Right(decoded) => decoded case Left(error) => throw new UnsupportedOperationException(error) } } }
Example 5
Source File: UserRepository.scala From gabbler with Apache License 2.0 | 5 votes |
package de.heikoseeberger.gabbler.chat import akka.actor.{ ActorLogging, Props } import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.persistence.{ PersistentActor, RecoveryCompleted } import akka.stream.ActorMaterializer import akka.stream.alpakka.sse.scaladsl.EventSource import de.heikoseeberger.akkasse.ServerSentEvent import io.circe.parser.decode object UserRepository { private sealed trait UserEvent final case class FindUserByUsername(username: String) final case class UsernameUnknown(username: String) private final case class AddUser(id: Long, username: String, nickname: String) private final case class UserAdded(eventId: String, user: User) private final case class RemoveUser(id: Long) private final case class UserRemoved(eventId: String, user: User) final case class User(id: Long, username: String, nickname: String) final val Name = "user-repository" def apply(userEventsEndpoint: Uri): Props = Props(new UserRepository(userEventsEndpoint)) } final class UserRepository(userEventsEndpoint: Uri) extends PersistentActor with ActorLogging { import UserRepository._ import io.circe.generic.auto._ override val persistenceId = Name private implicit val mat = ActorMaterializer() private var users = Map.empty[String, User] private var lastEventId = Option.empty[String] override def receiveCommand = { case FindUserByUsername(n) => handleFindUserByUsername(n) case (eventId: String, AddUser(i, u, n)) => handleAddUser(eventId, i, u, n) case (eventId: String, RemoveUser(i)) => handleRemoveUser(eventId, i) } override def receiveRecover = { case RecoveryCompleted => userEvents(lastEventId).runForeach(self ! _) case UserAdded(eventId, user) => lastEventId = Some(eventId) users += user.username -> user log.info("Added user with username {}", user.username) case UserRemoved(eventId, user) => lastEventId = Some(eventId) users -= user.username log.info("Removed user with username {}", user.username) } private def handleFindUserByUsername(username: String) = users.get(username) match { case Some(user) => sender() ! user case None => sender() ! UsernameUnknown(username) } private def handleAddUser(eventId: String, id: Long, username: String, nickname: String) = persist(UserAdded(eventId, User(id, username, nickname)))(receiveRecover) private def handleRemoveUser(eventId: String, id: Long) = users.values.find(_.id == id) match { case Some(user) => persist(UserRemoved(eventId, user))(receiveRecover) case None => log.warning("User with id {} does not exist!", id) } private def userEvents(lastEventId: Option[String]) = EventSource(userEventsEndpoint, Http(context.system).singleRequest(_), lastEventId) .collect { case ServerSentEvent(Some(data), Some("user-added"), Some(eventId), _) => eventId -> decode[AddUser](data) case ServerSentEvent(Some(data), Some("user-removed"), Some(eventId), _) => eventId -> decode[RemoveUser](data) } .collect { case (eventId, Right(userEvent)) => eventId -> userEvent } }
Example 6
Source File: NodeClient.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.lang.v1.repl.node.http import cats.Functor import cats.implicits._ import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.lang.v1.repl.global import com.wavesplatform.lang.v1.repl.node.http.NodeClient.ResponseWrapper import com.wavesplatform.lang.v1.repl.node.http.response.model.NodeResponse import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.ExecutionContext.Implicits.{global => g} import scala.concurrent.Future private[node] case class NodeClient(baseUrl: String) { def get[F[_] : Functor : ResponseWrapper, R: Decoder](path: String): Future[F[R]] = global.requestNode(baseUrl + path) .map(r => r: F[String]) .map(_.map(decode[R]).map(_.explicitGet())) } object NodeClient { type ResponseWrapper[F[_]] = NodeResponse => F[String] implicit val optionResponse: NodeResponse => Option[String] = { case NodeResponse(404, _) => None case NodeResponse(200, body) => Some(body) case NodeResponse(_, body) => throw new RuntimeException(body) } implicit val eitherResponse: NodeResponse => Either[String, String] = { case NodeResponse(200, body) => Right(body) case NodeResponse(_, body) => Left(body) } implicit val idResponse: NodeResponse => String = { case NodeResponse(200, body) => body case NodeResponse(_, body) => throw new RuntimeException(body) } }
Example 7
Source File: EventSource.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 8
Source File: SttpCirceApi.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.circe import sttp.client._ import sttp.client.internal.Utf8 import io.circe.{Decoder, Encoder, Printer} import io.circe.parser.decode import sttp.client.{IsOption, ResponseAs, ResponseError} import sttp.model.MediaType trait SttpCirceApi { implicit def circeBodySerializer[B](implicit encoder: Encoder[B], printer: Printer = Printer.noSpaces ): BodySerializer[B] = b => StringBody(encoder(b).pretty(printer), Utf8, Some(MediaType.ApplicationJson)) def asJsonAlwaysUnsafe[B: Decoder: IsOption]: ResponseAs[B, Nothing] = asStringAlways.map(ResponseAs.deserializeOrThrow(deserializeJson)) def deserializeJson[B: Decoder: IsOption]: String => Either[io.circe.Error, B] = JsonInput.sanitize[B].andThen(decode[B]) }
Example 9
Source File: DoobieUserRepositoryInterpreter.scala From scala-pet-store with Apache License 2.0 | 5 votes |
package io.github.pauljamescleary.petstore package infrastructure.repository.doobie import cats.data.OptionT import cats.effect.Bracket import cats.implicits._ import doobie._ import doobie.implicits._ import io.circe.parser.decode import io.circe.syntax._ import domain.users.{Role, User, UserRepositoryAlgebra} import io.github.pauljamescleary.petstore.infrastructure.repository.doobie.SQLPagination._ import tsec.authentication.IdentityStore private object UserSQL { // H2 does not support JSON data type. implicit val roleMeta: Meta[Role] = Meta[String].imap(decode[Role](_).leftMap(throw _).merge)(_.asJson.toString) def insert(user: User): Update0 = sql""" INSERT INTO USERS (USER_NAME, FIRST_NAME, LAST_NAME, EMAIL, HASH, PHONE, ROLE) VALUES (${user.userName}, ${user.firstName}, ${user.lastName}, ${user.email}, ${user.hash}, ${user.phone}, ${user.role}) """.update def update(user: User, id: Long): Update0 = sql""" UPDATE USERS SET FIRST_NAME = ${user.firstName}, LAST_NAME = ${user.lastName}, EMAIL = ${user.email}, HASH = ${user.hash}, PHONE = ${user.phone}, ROLE = ${user.role} WHERE ID = $id """.update def select(userId: Long): Query0[User] = sql""" SELECT USER_NAME, FIRST_NAME, LAST_NAME, EMAIL, HASH, PHONE, ID, ROLE FROM USERS WHERE ID = $userId """.query def byUserName(userName: String): Query0[User] = sql""" SELECT USER_NAME, FIRST_NAME, LAST_NAME, EMAIL, HASH, PHONE, ID, ROLE FROM USERS WHERE USER_NAME = $userName """.query[User] def delete(userId: Long): Update0 = sql""" DELETE FROM USERS WHERE ID = $userId """.update val selectAll: Query0[User] = sql""" SELECT USER_NAME, FIRST_NAME, LAST_NAME, EMAIL, HASH, PHONE, ID, ROLE FROM USERS """.query } class DoobieUserRepositoryInterpreter[F[_]: Bracket[?[_], Throwable]](val xa: Transactor[F]) extends UserRepositoryAlgebra[F] with IdentityStore[F, Long, User] { self => import UserSQL._ def create(user: User): F[User] = insert(user).withUniqueGeneratedKeys[Long]("ID").map(id => user.copy(id = id.some)).transact(xa) def update(user: User): OptionT[F, User] = OptionT.fromOption[F](user.id).semiflatMap { id => UserSQL.update(user, id).run.transact(xa).as(user) } def get(userId: Long): OptionT[F, User] = OptionT(select(userId).option.transact(xa)) def findByUserName(userName: String): OptionT[F, User] = OptionT(byUserName(userName).option.transact(xa)) def delete(userId: Long): OptionT[F, User] = get(userId).semiflatMap(user => UserSQL.delete(userId).run.transact(xa).as(user)) def deleteByUserName(userName: String): OptionT[F, User] = findByUserName(userName).mapFilter(_.id).flatMap(delete) def list(pageSize: Int, offset: Int): F[List[User]] = paginate(pageSize, offset)(selectAll).to[List].transact(xa) } object DoobieUserRepositoryInterpreter { def apply[F[_]: Bracket[?[_], Throwable]](xa: Transactor[F]): DoobieUserRepositoryInterpreter[F] = new DoobieUserRepositoryInterpreter(xa) }
Example 10
Source File: MeetupJsonSpec.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.libs.meetup import gospeak.libs.meetup.MeetupJson._ import gospeak.libs.meetup.domain._ import gospeak.libs.scala.FileUtils import gospeak.libs.testingutils.BaseSpec import io.circe.parser.decode class MeetupJsonSpec extends BaseSpec { private val basePath = Some("libs/src/test/resources/meetup").filter(FileUtils.exists).getOrElse("src/test/resources/meetup") it("should parse access token response") { decode[MeetupToken](FileUtils.read(basePath + "/accessToken.json").get).toTry.get } it("should parse user response") { decode[MeetupUser.Alt](FileUtils.read(basePath + "/user.json").get).toTry.get } it("should parse group response") { decode[MeetupGroup](FileUtils.read(basePath + "/group.json").get).toTry.get } it("should parse event response") { decode[MeetupEvent](FileUtils.read(basePath + "/event.json").get).toTry.get decode[Seq[MeetupEvent]](FileUtils.read(basePath + "/events.json").get).toTry.get decode[MeetupEvent](FileUtils.read(basePath + "/eventCreated.json").get).toTry.get } it("should parse attendees response") { decode[Seq[MeetupAttendee]](FileUtils.read(basePath + "/attendees.json").get).toTry.get } it("should parse venues response") { decode[Seq[MeetupVenue]](FileUtils.read(basePath + "/venues.json").get).toTry.get } it("should parse locations response") { decode[Seq[MeetupLocation]](FileUtils.read(basePath + "/locations.json").get).toTry.get } it("should parse error responses") { decode[MeetupError.NotAuthorized](FileUtils.read(basePath + "/errors/not_authorized.json").get).toTry.get decode[MeetupError](FileUtils.read(basePath + "/errors/invalid_request.json").get).toTry.get decode[MeetupError.Multi](FileUtils.read(basePath + "/errors/authentication_error.json").get).toTry.get decode[MeetupError.Multi](FileUtils.read(basePath + "/errors/group_error.json").get).toTry.get decode[MeetupError.Multi](FileUtils.read(basePath + "/errors/name_error.json").get).toTry.get decode[MeetupError.Multi](FileUtils.read(basePath + "/errors/scope_error.json").get).toTry.get decode[MeetupError.Multi](FileUtils.read(basePath + "/errors/multi.json").get).toTry.get } }
Example 11
Source File: SlackJsonSpec.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.libs.slack import gospeak.libs.scala.FileUtils import gospeak.libs.slack.SlackJson._ import gospeak.libs.slack.domain.{SlackChannel, SlackMessage, SlackTokenInfo, SlackUser} import gospeak.libs.testingutils.BaseSpec import io.circe.parser.decode class SlackJsonSpec extends BaseSpec { private val basePath = Some("libs/src/test/resources/slack").filter(FileUtils.exists).getOrElse("src/test/resources/slack") describe("SlackJson") { it("should parse auth.test response") { decode[SlackTokenInfo](FileUtils.read(basePath + "/auth.test.json").get).toTry.get } it("should parse channels.create response") { decode[SlackChannel.Single](FileUtils.read(basePath + "/channels.create.json").get).toTry.get } it("should parse channels.invite response") { decode[SlackChannel.Single](FileUtils.read(basePath + "/channels.invite.json").get).toTry.get } it("should parse channels.list response") { val res = decode[SlackChannel.List](FileUtils.read(basePath + "/channels.list.json").get).toTry.get res.channels.length shouldBe 4 } it("should parse users.list response") { val res = decode[SlackUser.List](FileUtils.read(basePath + "/users.list.json").get).toTry.get res.members.length shouldBe 1 } it("should parse chat.postMessage response") { decode[SlackMessage.Posted](FileUtils.read(basePath + "/chat.postMessage.json").get).toTry.get } } }
Example 12
Source File: CloudinaryClient.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.libs.cloudinary import java.time.Instant import cats.effect.IO import gospeak.libs.cloudinary.CloudinaryJson._ import gospeak.libs.cloudinary.domain.{CloudinaryUploadRequest, CloudinaryUploadResponse} import gospeak.libs.http.HttpClient import gospeak.libs.scala.Crypto import gospeak.libs.scala.Extensions._ import gospeak.libs.scala.domain.Creds import io.circe.parser.decode class CloudinaryClient(conf: CloudinaryClient.Conf) { private val baseUrl = "https://api.cloudinary.com/v1_1" private val ignoreOnSign = Set("api_key", "file") // see https://cloudinary.com/documentation/upload_images#generating_authentication_signatures def sign(params: Map[String, String]): Either[String, String] = withCreds((_, creds) => sign(creds, params)) // see https://cloudinary.com/documentation/upload_images#uploading_with_a_direct_call_to_the_api def upload(req: CloudinaryUploadRequest): IO[Either[String, CloudinaryUploadResponse]] = { withCreds { (cloudName, creds) => val uploadUrl = s"$baseUrl/$cloudName/image/upload" val allParams = req.toMap ++ Map( "api_key" -> creds.key, "timestamp" -> Instant.now().getEpochSecond.toString) val signature = sign(creds, allParams) HttpClient.postForm(uploadUrl, allParams ++ Map("signature" -> signature)) .map(r => decode[CloudinaryUploadResponse](r.body).leftMap(_.getMessage)) }.sequence.map(_.flatMap(identity)) } private def sign(creds: Creds, queryParams: Map[String, String]): String = { val params = queryParams .filterKeys(!ignoreOnSign.contains(_)) .toList.sortBy(_._1) .map { case (key, value) => s"$key=$value" }.mkString("&") Crypto.sha1(params + creds.secret.decode) } private def withCreds[A](block: (String, Creds) => A): Either[String, A] = conf match { case CloudinaryClient.Conf(cloudName, _, Some(creds)) => Right(block(cloudName, creds)) case _: CloudinaryClient.Conf => Left("No credentials defined for cloudinary") } } object CloudinaryClient { final case class Conf(cloudName: String, uploadPreset: Option[String], creds: Option[Creds]) }
Example 13
Source File: ResultClient.scala From elastiknn with Apache License 2.0 | 5 votes |
package com.klibisz.elastiknn.benchmarks import com.amazonaws.services.s3.AmazonS3 import com.klibisz.elastiknn.api.{Mapping, NearestNeighborsQuery} import io.circe.parser.decode import io.circe.syntax._ import zio._ import codecs._ import com.amazonaws.services.s3.model.{ListObjectsV2Request, ListObjectsV2Result} import zio.blocking._ import zio.stream._ import scala.annotation.tailrec import scala.collection.JavaConverters._ object ResultClient { trait Service { def find(dataset: Dataset, mapping: Mapping, query: NearestNeighborsQuery, k: Int): IO[Throwable, Option[BenchmarkResult]] def save(result: BenchmarkResult): IO[Throwable, Unit] def all(): Stream[Throwable, BenchmarkResult] } def s3(bucket: String, keyPrefix: String): ZLayer[Has[AmazonS3] with Blocking, Nothing, ResultClient] = { def validChars(s: String): String = s.map { c => if (c.isLetter && c <= 'z' || c.isDigit || c == '.') c else '-' } def genKey(dataset: Dataset, mapping: Mapping, query: NearestNeighborsQuery, k: Int): String = { val suffix = validChars(s"res-${dataset.toString}-${mapping.toString}-${query.toString}-$k.json") if (keyPrefix.nonEmpty) s"$keyPrefix/$suffix".replace("//", "/") else suffix } ZLayer.fromServices[AmazonS3, Blocking.Service, Service] { case (client, blocking) => new Service { override def find(dataset: Dataset, mapping: Mapping, query: NearestNeighborsQuery, k: Int): IO[Throwable, Option[BenchmarkResult]] = { val key = genKey(dataset, mapping, query, k) for { ex <- blocking.effectBlocking(client.doesObjectExist(bucket, key)) res: Option[BenchmarkResult] <- if (ex) { for { body <- blocking.effectBlocking(client.getObjectAsString(bucket, key)) dec <- ZIO.fromEither(decode[BenchmarkResult](body)) } yield Some(dec) } else ZIO.effectTotal(None) } yield res } override def save(result: BenchmarkResult): IO[Throwable, Unit] = { val key = genKey(result.dataset, result.mapping, result.query, result.k) for { bucketExists <- blocking.effectBlocking(client.doesBucketExistV2(bucket)) _ <- if (bucketExists) ZIO.succeed(()) else blocking.effectBlocking(client.createBucket(bucket)) _ <- blocking.effectBlocking(client.putObject(bucket, key, result.asJson.spaces2SortKeys)) } yield () } override def all(): Stream[Throwable, BenchmarkResult] = { @tailrec def readAllKeys(req: ListObjectsV2Request, agg: Vector[String] = Vector.empty): Vector[String] = { val res = client.listObjectsV2(req) val keys = res.getObjectSummaries.asScala.toVector.map(_.getKey).filter(_.endsWith(".json")) if (res.isTruncated) readAllKeys(req.withContinuationToken(res.getNextContinuationToken), agg ++ keys) else agg ++ keys } val req = new ListObjectsV2Request().withBucketName(bucket).withPrefix(keyPrefix) Stream .fromIterableM(blocking.effectBlocking(readAllKeys(req))) .mapM(key => blocking.effectBlocking(client.getObjectAsString(bucket, key))) .mapM(body => ZIO.fromEither(decode[BenchmarkResult](body))) } } } } }
Example 14
Source File: WsServer.scala From seed with Apache License 2.0 | 5 votes |
package seed.cli.util import java.net.InetSocketAddress import java.nio.ByteBuffer import org.java_websocket.WebSocket import org.java_websocket.handshake.ClientHandshake import org.java_websocket.server.WebSocketServer import io.circe.parser.decode import seed.Log import seed.cli.WsCommand class WsServer( address: InetSocketAddress, onDisconnect: WebSocket => Unit, evalCommand: (WsServer, WebSocket, WsCommand) => Unit, log: Log ) extends WebSocketServer(address) { setReuseAddr(true) private def clientIp(conn: WebSocket): String = if (conn.getRemoteSocketAddress == null) "<unknown>" else conn.getRemoteSocketAddress.getAddress.getHostAddress override def onOpen(conn: WebSocket, handshake: ClientHandshake): Unit = log.debug(s"Client ${Ansi.italic(clientIp(conn))} connected") override def onClose( conn: WebSocket, code: Int, reason: String, remote: Boolean ): Unit = { log.debug(s"Client ${Ansi.italic(clientIp(conn))} disconnected") onDisconnect(conn) } override def onError(conn: WebSocket, ex: Exception): Unit = ex.printStackTrace() override def onStart(): Unit = { setConnectionLostTimeout(100) log.info(s"WebSocket server started on ${Ansi .italic(s"${address.getHostName}:${address.getPort}")}") } override def onMessage(conn: WebSocket, message: String): Unit = try { decode[WsCommand](message) match { case Left(e) => log.error( s"Could not process message from ${Ansi.italic(clientIp(conn))}: $e" ) conn.send(e.toString) case Right(c) => log.debug( s"${Ansi.italic(c.description)} command received from ${Ansi.italic(clientIp(conn))}" ) evalCommand(this, conn, c) } } catch { case t: Throwable => t.printStackTrace() } override def onMessage(conn: WebSocket, message: ByteBuffer): Unit = {} }
Example 15
Source File: EventSource.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.client import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.client.config.IamClientConfig import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} trait EventSource[A] { def apply[A: Decoder]( config: IamClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AuthToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AuthToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } override def apply(iri: AbsoluteIri, offset: Option[String])( implicit cred: Option[AuthToken] ): Source[A, NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => decode[A](sse.data) match { case Right(ev) => Source.single(ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 16
Source File: ProxyEncoding.scala From scala-server-lambda with MIT License | 5 votes |
package io.github.howardjohn.lambda import io.circe import io.circe.generic.auto._ import io.circe.parser.decode import io.circe.syntax._ object ProxyEncoding { case class ProxyRequest( httpMethod: String, path: String, headers: Option[Map[String, String]], body: Option[String], queryStringParameters: Option[Map[String, String]] ) case class ProxyResponse( statusCode: Int, headers: Map[String, String], body: String ) def parseRequest(rawInput: String): Either[circe.Error, ProxyRequest] = decode[ProxyRequest](rawInput) def encodeResponse(response: ProxyResponse): String = response.asJson.noSpaces def reconstructPath(request: ProxyRequest): String = { val requestString = request.queryStringParameters .map { _.map { case (k, v) => s"$k=$v" }.mkString("&") } .map { qs => if (qs.isEmpty) "" else "?" + qs } .getOrElse("") request.path + requestString } }
Example 17
Source File: BrowserActor.scala From Scala-Programming-Projects with MIT License | 5 votes |
package actors import akka.actor._ import io.circe.generic.auto._ import io.circe.parser.decode import io.fscala.shopping.shared.CartEvent object BrowserActor { def props(browserManager: ActorRef) = Props(new BrowserActor(browserManager)) } class BrowserActor(browserManager: ActorRef) extends Actor with ActorLogging { def receive = { case msg: String => log.info("Received JSON message: {}", msg) decode[CartEvent](msg) match { case Right(cartEvent) => log.info("Got {} message", cartEvent) browserManager forward cartEvent case Left(error) => log.info("Unhandled message : {}", error) } } }
Example 18
Source File: Parsers.scala From get-programming-with-scala with MIT License | 5 votes |
package org.example.movies.entities import java.time.LocalDate import io.circe.Decoder import io.circe.parser.decode import io.circe.generic.auto._ import scala.util.Try object Parsers { def parseInt(row: Map[String, String], key: String): Option[Int] = parseAs(row, key, _.toInt) def parseDouble(row: Map[String, String], key: String): Option[Double] = parseAs(row, key, _.toDouble) def parseString(row: Map[String, String], key: String): Option[String] = parseAs(row, key, identity) def parseFloat(row: Map[String, String], key: String): Option[Float] = parseAs(row, key, _.toFloat) def parseLocalDate(row: Map[String, String], key: String): Option[LocalDate] = parseAs(row, key, LocalDate.parse) def parseGenres(row: Map[String, String], key: String): Option[List[Genre]] = parseJsonAs[List[Genre]](row, key) private def parseJsonAs[T: Decoder](row: Map[String, String], key: String): Option[T] = for { rawJson <- parseString(row, key) // for some reason the CSV is using ' rather than " for JSON, // so we need to clean the data before decoding cleanJson = rawJson.replace("'", "\"") // rather than returning an error for invalid JSON, we return no value decodedValue <- decode[T](cleanJson).toOption } yield decodedValue private def parseAs[T](row: Map[String, String], key: String, f: String => T): Option[T] = row.get(key).flatMap { value => // if we cannot parse a value, we return no value Try(f(value)).toOption } }
Example 19
Source File: JSONUtils.scala From evilplot with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.cibo.evilplot import io.circe.generic.extras.Configuration import io.circe.parser.decode import io.circe.syntax._ import io.circe.{Decoder, Encoder, Error} object JSONUtils { // This should only be used for colors, drawables, and extents. private[evilplot] implicit val minifyProperties: Configuration = Configuration.default.copy( transformMemberNames = s => s.take(2).toString, transformConstructorNames = shortenedName ) // scalastyle:off private def shortenedName(s: String): String = { s match { case "EmptyDrawable" => "E" case "Line" => "L" case "Path" => "P" case "Polygon" => "p" case "Rect" => "R" case "BorderRect" => "B" case "Disc" => "D" case "Wedge" => "W" case "Translate" => "T" case "Affine" => "A" case "Scale" => "C" case "Rotate" => "O" case "Group" => "G" case "Resize" => "Re" case "Style" => "S" case "StrokeStyle" => "Y" case "StrokeWeight" => "H" case "LineDash" => "l" case "Text" => "X" case "HSLA" => "c" case "GradientFill" => "gf" case other => other } } // scalastyle:on // Wrap the Circe JSON decode method and return just the desired type, not an Either. // If parsing returns an error, throw the error rather than returning it. def decodeStr[A: Decoder](input: String): A = { val a: Either[Error, A] = decode[A](input) a match { case Left(error) => throw error case Right(result) => result } } // Encode the input object to JSON, then convert the JSON to a string and return it. def encodeObj[A: Encoder](input: A): String = { input.asJson.noSpaces } }
Example 20
Source File: EventSource.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.client import java.util.UUID import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.persistence.query.{NoOffset, Offset, Sequence, TimeBasedUUID} import akka.stream.Materializer import akka.stream.alpakka.sse.scaladsl.{EventSource => SSESource} import akka.stream.scaladsl.Source import ch.epfl.bluebrain.nexus.iam.auth.AccessToken import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import com.typesafe.scalalogging.Logger import io.circe.Decoder import io.circe.parser.decode import scala.concurrent.{ExecutionContext, Future} import scala.util.Try trait EventSource[A] { def apply[A: Decoder]( config: KgClientConfig )(implicit as: ActorSystem, mt: Materializer, ec: ExecutionContext): EventSource[A] = new EventSource[A] { private val logger = Logger[this.type] private val http = Http() private def addCredentials(request: HttpRequest)(implicit cred: Option[AccessToken]): HttpRequest = cred.map(token => request.addCredentials(OAuth2BearerToken(token.value))).getOrElse(request) private def send(request: HttpRequest)(implicit cred: Option[AccessToken]): Future[HttpResponse] = http.singleRequest(addCredentials(request)).map { resp => if (!resp.status.isSuccess()) logger.warn(s"HTTP response when performing SSE request: status = '${resp.status}'") resp } private def toOffset(id: String): Offset = Try(TimeBasedUUID(UUID.fromString(id))).orElse(Try(Sequence(id.toLong))).getOrElse(NoOffset) override def apply(iri: AbsoluteIri, offset: Option[String])(implicit cred: Option[AccessToken] ): Source[(Offset, A), NotUsed] = SSESource(iri.asAkka, send, offset, config.sseRetryDelay).flatMapConcat { sse => val offset = sse.id.map(toOffset).getOrElse(NoOffset) decode[A](sse.data) match { case Right(ev) => Source.single(offset -> ev) case Left(err) => logger.error(s"Failed to decode admin event '$sse'", err) Source.empty } } } }
Example 21
Source File: Base64Reading.scala From jsoniter-scala with MIT License | 5 votes |
package com.github.plokhotnyuk.jsoniter_scala.benchmark import java.nio.charset.StandardCharsets.UTF_8 import com.avsystem.commons.serialization.json.JsonStringInput import com.github.plokhotnyuk.jsoniter_scala.benchmark.AVSystemCodecs._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.CirceEncodersDecoders._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.DslPlatformJson._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JacksonSerDesers._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs._ import com.github.plokhotnyuk.jsoniter_scala.core.readFromArray import com.rallyhealth.weejson.v1.jackson.FromJson import com.rallyhealth.weepickle.v1.WeePickle.ToScala import io.circe.parser.decode import org.openjdk.jmh.annotations.Benchmark class Base64Reading extends Base64Benchmark { @Benchmark def avSystemGenCodec(): Array[Byte] = JsonStringInput.read[Array[Byte]](new String(jsonBytes, UTF_8), jsonBase64Options) @Benchmark def borer(): Array[Byte] = io.bullet.borer.Json.decode(jsonBytes).to[Array[Byte]].value @Benchmark def circe(): Array[Byte] = decode[Array[Byte]](new String(jsonBytes, UTF_8))(base64D5r).fold(throw _, identity) @Benchmark def dslJsonScala(): Array[Byte] = dslJsonDecode[Array[Byte]](jsonBytes) @Benchmark def jacksonScala(): Array[Byte] = jacksonMapper.readValue[Array[Byte]](jsonBytes) @Benchmark def jsoniterScala(): Array[Byte] = readFromArray[Array[Byte]](jsonBytes, tooLongStringConfig)(base64Codec) @Benchmark def weePickle(): Array[Byte] = FromJson(jsonBytes).transform(ToScala[Array[Byte]]) }
Example 22
Source File: GitHubActionsAPIReading.scala From jsoniter-scala with MIT License | 5 votes |
package com.github.plokhotnyuk.jsoniter_scala.benchmark import java.nio.charset.StandardCharsets.UTF_8 import com.avsystem.commons.serialization.json._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.AVSystemCodecs._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.BorerJsonEncodersDecoders._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.CirceEncodersDecoders._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JacksonSerDesers._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.SprayFormats._ import com.github.plokhotnyuk.jsoniter_scala.benchmark.WeePickleFromTos._ import com.github.plokhotnyuk.jsoniter_scala.core._ import com.rallyhealth.weejson.v1.jackson.FromJson import com.rallyhealth.weepickle.v1.WeePickle.ToScala import io.circe.parser.decode import org.openjdk.jmh.annotations.Benchmark import spray.json.JsonParser class GitHubActionsAPIReading extends GitHubActionsAPIBenchmark { @Benchmark def avSystemGenCodec(): GitHubActionsAPI.Response = JsonStringInput.read[GitHubActionsAPI.Response](new String(jsonBytes, UTF_8)) @Benchmark def borer(): GitHubActionsAPI.Response = io.bullet.borer.Json.decode(jsonBytes).to[GitHubActionsAPI.Response].value @Benchmark def circe(): GitHubActionsAPI.Response = decode[GitHubActionsAPI.Response](new String(jsonBytes, UTF_8)).fold(throw _, identity) @Benchmark def jacksonScala(): GitHubActionsAPI.Response = jacksonMapper.readValue[GitHubActionsAPI.Response](jsonBytes) @Benchmark def jsoniterScala(): GitHubActionsAPI.Response = readFromArray[GitHubActionsAPI.Response](jsonBytes) @Benchmark def sprayJson(): GitHubActionsAPI.Response = JsonParser(jsonBytes).convertTo[GitHubActionsAPI.Response] @Benchmark def weePickle(): GitHubActionsAPI.Response = FromJson(jsonBytes).transform(ToScala[GitHubActionsAPI.Response]) }
Example 23
Source File: Cli.scala From metarpheus with MIT License | 5 votes |
package io.buildo.metarpheus package cli import java.io.File import scala.io.Source import org.rogach.scallop._ import io.circe.parser.decode import io.circe.generic.extras._ import io.circe.generic.extras.auto._ class CommandLine(args: Array[String]) extends ScallopConf(args) { val configPath = opt[String]("config", descr = "config file path", required = false) val outputFile = opt[String]("output", descr = "output file path", required = false) val targets = trailArg[List[String]](required = true) verify() } object Cli { def main(argv: Array[String]): Unit = { val conf = new CommandLine(argv) val paths = conf.targets.get.get implicit val parseConfig: Configuration = Configuration.default.withDefaults val config = (for { fileName <- conf.configPath.get json = Source.fromFile(fileName).mkString parsed <- decode[core.Config](json).toOption } yield parsed).getOrElse(core.Config.default) val api = core.Metarpheus.run(paths, config) val serializedAPI = repr.serializeAPI(api) conf.outputFile.get match { case None => println(serializedAPI) case Some(outputFilePath) => val f = new File(outputFilePath) val p = new java.io.PrintWriter(f) try { p.println(serializedAPI) } finally { p.close() } } } private[this] def recursivelyListFiles(target: File): Array[File] = { if (target.isDirectory) { val these: Array[java.io.File] = Option(target.listFiles).getOrElse(Array()) these ++ these.filter(_.isDirectory).flatMap(recursivelyListFiles) } else { Array(target) } } }
Example 24
Source File: JSFacade.scala From metarpheus with MIT License | 5 votes |
package io.buildo.metarpheus package core import scala.scalajs.js import scala.scalajs.js.annotation._ import scala.util.control.NonFatal import io.circe._ import io.circe.syntax._ import io.circe.parser.decode import io.circe.generic.extras._ import io.circe.generic.extras.auto._ object JSFacade { trait JSConfig extends js.Object { val modelsForciblyInUse: js.UndefOr[js.Array[String]] } @JSExportTopLevel("run") def run(paths: js.Array[String], jsConfig: js.UndefOr[JSConfig]) = { implicit val circeConfiguration: Configuration = Configuration.default.withDefaults.withDiscriminator("_type") val config = jsConfig .map { jsConfig => val json = js.JSON.stringify(jsConfig) decode[Config](json) match { case Left(error) => throw js.JavaScriptException(error.toString) case Right(config) => config } } .getOrElse(Config.default) try { val result = Metarpheus.run(paths.toList, config) val printer = Printer.noSpaces.copy(dropNullKeys = true) js.JSON.parse(printer.pretty(result.asJson)) } catch { case NonFatal(e) => throw js.JavaScriptException(e.getMessage) } } }
Example 25
Source File: LinkType.scala From openlaw-core with Apache License 2.0 | 5 votes |
package org.adridadou.openlaw.parser.template.variableTypes import cats.implicits._ import io.circe.{Decoder, Encoder} import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} import io.circe.parser.decode import org.adridadou.openlaw.{OpenlawNativeValue, OpenlawString, OpenlawValue} import org.adridadou.openlaw.parser.template.formatters.Formatter import org.adridadou.openlaw.parser.template._ import org.adridadou.openlaw.parser.template.expressions.Expression import org.adridadou.openlaw.result.{Failure, FailureException, Result, Success} object LinkInfo { implicit val linkInfoEnc: Encoder[LinkInfo] = deriveEncoder implicit val linkInfoDec: Decoder[LinkInfo] = deriveDecoder } final case class LinkInfo(label: String, url: String) extends OpenlawNativeValue case object LinkType extends VariableType(name = "Link") with NoShowInFormButRender { override def cast( value: String, executionResult: TemplateExecutionResult ): Result[LinkInfo] = decode[LinkInfo](value).leftMap(FailureException(_)) override def internalFormat(value: OpenlawValue): Result[String] = VariableType.convert[OpenlawString](value) override def defaultFormatter: Formatter = new LinkFormatter override def getTypeClass: Class[LinkInfo] = classOf[LinkInfo] override def construct( constructorParams: Parameter, executionResult: TemplateExecutionResult ): Result[Option[LinkInfo]] = constructorParams match { case Parameters(seq) => val map = seq.toMap for { label <- map.get("label").traverse(getOneValueConstant) url <- map.get("url").traverse(getOneValueConstant) } yield (label, url) mapN { LinkInfo(_, _) } case _ => Failure("""Link requires parameters, not a unique value or a list""") } def thisType: VariableType = LinkType private def getOneValueConstant(value: Parameter): Result[String] = value match { case OneValueParameter(StringConstant(v)) => Success(v) case _ => Failure("""Link requires "label" argument.""") } } class LinkFormatter extends Formatter { override def format( expression: Expression, value: OpenlawValue, executionResult: TemplateExecutionResult ): Result[List[AgreementElement]] = VariableType.convert[LinkInfo](value) map { case LinkInfo(labelValue, urlValue) => List(Link(labelValue, urlValue)) } override def missingValueFormat( expression: Expression ): List[AgreementElement] = List(FreeText(Text(s"[[$expression]]"))) override def stringFormat( expression: Expression, value: OpenlawValue, executionResult: TemplateExecutionResult ): Result[String] = VariableType.convert[LinkInfo](value) map { case LinkInfo(labelValue, urlValue) => s"$labelValue[$urlValue]" } }
Example 26
Source File: SectionType.scala From openlaw-core with Apache License 2.0 | 5 votes |
package org.adridadou.openlaw.parser.template.variableTypes import cats.implicits._ import io.circe.{Decoder, Encoder} import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} import io.circe.parser.decode import org.adridadou.openlaw.{OpenlawNativeValue, OpenlawString, OpenlawValue} import org.adridadou.openlaw.parser.template.formatters.Formatter import org.adridadou.openlaw.parser.template._ import org.adridadou.openlaw.parser.template.expressions.Expression import org.adridadou.openlaw.result.{Failure, FailureException, Result, Success} final case class SectionInfo( name: Option[String], numbering: String, value: String ) extends OpenlawNativeValue case object SectionType extends VariableType(name = "Section") with NoShowInFormButRender { private implicit val enc: Encoder[SectionInfo] = deriveEncoder private implicit val dec: Decoder[SectionInfo] = deriveDecoder override def cast( value: String, executionResult: TemplateExecutionResult ): Result[SectionInfo] = decode[SectionInfo](value).leftMap(FailureException(_)) override def internalFormat(value: OpenlawValue): Result[String] = value match { case SectionInfo(_, _, value) => Success(value) case value => VariableType.convert[OpenlawString](value) } override def defaultFormatter: Formatter = new SectionFormatter override def getTypeClass: Class[SectionInfo] = classOf[SectionInfo] override def construct( constructorParams: Parameter, executionResult: TemplateExecutionResult ): Result[Option[SectionInfo]] = constructorParams match { case Parameters(seq) => val map = seq.toMap (for { numbering <- map.get("numbering") reference <- map.get("reference value") } yield for { numberingValue <- getOneValueConstant(numbering) referenceValue <- getOneValueConstant(reference) } yield SectionInfo(None, numberingValue, referenceValue)).sequence case _ => Failure("""Section requires parameters, not a unique value or a list""") } def thisType: VariableType = SectionType private def getOneValueConstant(value: Parameter): Result[String] = value match { case OneValueParameter(StringConstant(v)) => Success(v) case _ => Failure("""Section requires "numbering" argument.""") } } class SectionFormatter extends Formatter { override def format( expression: Expression, value: OpenlawValue, executionResult: TemplateExecutionResult ): Result[List[AgreementElement]] = VariableType.convert[SectionInfo](value) map { case SectionInfo(_, _, referenceValue) => List(FreeText(Text(referenceValue))) } override def missingValueFormat( expression: Expression ): List[AgreementElement] = List(FreeText(Text(s"[[$expression]]"))) override def stringFormat( expression: Expression, value: OpenlawValue, executionResult: TemplateExecutionResult ): Result[String] = VariableType.convert[SectionInfo](value) map { case SectionInfo(_, _, referenceValue) => referenceValue } override def missingValueString( expression: Expression ): String = ??? }
Example 27
Source File: JsonKeyValueStore.scala From scala-steward with Apache License 2.0 | 5 votes |
package org.scalasteward.core.persistence import better.files.File import cats.Monad import cats.implicits._ import io.chrisdavenport.log4cats.Logger import io.circe.parser.decode import io.circe.syntax._ import io.circe.{Decoder, Encoder, KeyEncoder} import org.scalasteward.core.io.{FileAlg, WorkspaceAlg} final class JsonKeyValueStore[F[_], K, V]( name: String, schemaVersion: String, maybePrefix: Option[String] = None )(implicit fileAlg: FileAlg[F], keyEncoder: KeyEncoder[K], logger: Logger[F], valueDecoder: Decoder[V], valueEncoder: Encoder[V], workspaceAlg: WorkspaceAlg[F], F: Monad[F] ) extends KeyValueStore[F, K, V] { override def get(key: K): F[Option[V]] = jsonFile(key).flatMap { file => fileAlg.readFile(file).flatMap { case Some(content) => decode[Option[V]](content) match { case Right(maybeValue) => F.pure(maybeValue) case Left(error) => logger.error(error)(s"Failed to parse or decode JSON from $file").as(Option.empty[V]) } case None => F.pure(Option.empty[V]) } } override def put(key: K, value: V): F[Unit] = write(key, Some(value)) override def modifyF(key: K)(f: Option[V] => F[Option[V]]): F[Option[V]] = get(key).flatMap(maybeValue => f(maybeValue).flatTap(write(key, _))) private def jsonFile(key: K): F[File] = { val keyPath = maybePrefix.fold("")(_ + "/") + keyEncoder(key) workspaceAlg.rootDir.map(_ / "store" / name / s"v$schemaVersion" / keyPath / s"$name.json") } private def write(key: K, value: Option[V]): F[Unit] = jsonFile(key).flatMap(fileAlg.writeFile(_, value.asJson.toString)) }
Example 28
Source File: AjaxClient.scala From canoe with MIT License | 5 votes |
package canoe.api.clients import canoe.api.{FailedMethod, ResponseDecodingError, TelegramClient} import canoe.methods.Method import canoe.models.Response import cats.effect.{Async, ContextShift} import cats.syntax.all._ import io.circe.Decoder import io.circe.parser.decode import org.scalajs.dom.console import org.scalajs.dom.ext.Ajax private[api] class AjaxClient[F[_]: Async: ContextShift](token: String) extends TelegramClient[F] { private val botApiUri: String = s"https://api.telegram.org/bot$token" def execute[Req, Res](request: Req)(implicit M: Method[Req, Res]): F[Res] = { implicit val responseDecoder: Decoder[Response[Res]] = Response.decoder[Res](M.decoder) sendJsonRequest(request, M).map(decode[Response[Res]]).flatMap { case Left(error) => handleUnknownEntity(M.name, request, error.getMessage) case Right(response) => handleTelegramResponse(M, request)(response) } } private def handleUnknownEntity[I, A](method: String, input: I, error: String): F[A] = { console.error( s"Received unknown Telegram entity during execution of '$method' method. \nInput data: $input. \n${error}" ) ResponseDecodingError(error.toString).raiseError[F, A] } private def handleTelegramResponse[A, I, C](m: Method[I, A], input: I)(response: Response[A]): F[A] = response match { case Response(true, Some(result), _, _, _) => result.pure[F] case failed => console.error(s"Received failed response from Telegram: $failed. Method name: ${m.name}, input data: $input") FailedMethod(m, input, failed).raiseError[F, A] } private def sendJsonRequest[Req, Res](request: Req, method: Method[Req, Res]): F[String] = { val url = s"$botApiUri/${method.name}" val json = method.encoder.apply(request).toString Async .fromFuture(F.delay(Ajax.post(url, json, headers = Map("Content-Type" -> "application/json")))) .map(_.responseText) } }
Example 29
Source File: PartialStatelessJWTAuth.scala From tsec with MIT License | 5 votes |
package tsec.authentication.internal import java.time.Instant import cats.data.OptionT import cats.effect.Sync import cats.syntax.all._ import io.circe.parser.decode import io.circe.syntax._ import io.circe.{Decoder, Encoder} import org.http4s._ import tsec.authentication._ import tsec.common._ import tsec.jws.mac._ import tsec.jwt.algorithms.JWTMacAlgo import tsec.jwt.{JWTClaims, JWTPrinter} import tsec.mac.jca._ import scala.concurrent.duration._ def discard(authenticator: AugmentedJWT[A, I]): F[AugmentedJWT[A, I]] = for { now <- F.delay(Instant.now) jwt <- JWTMac .build[F, A]( authenticator.jwt.body .withExpiry(now) .withJwtID(SecureRandomId.Interactive.generate), signingKey ) } yield AugmentedJWT(authenticator.id, jwt, authenticator.identity, now, authenticator.lastTouched) }
Example 30
Source File: Raw.scala From geotrellis-pointcloud with Apache License 2.0 | 5 votes |
package geotrellis.pointcloud.raster.ept import geotrellis.util.RangeReader import geotrellis.vector.Extent import cats.syntax.either._ import io.circe.generic.JsonCodec import io.circe.parser.decode import java.net.URI @JsonCodec case class Raw( bounds: Seq[Double], boundsConforming: Seq[Double], dataType: String, hierarchyType: String, points: Long, schema: Seq[Field], span: Int, srs: SRS, version: String ) { def extent: Extent = { val Seq(xmin, ymin, _, xmax, ymax, _) = bounds Extent(xmin, ymin, xmax, ymax) } } object Raw { def apply(eptSource: String): Raw = { val rr = RangeReader(new URI(eptSource).resolve("ept.json").toString) val jsonString = new String(rr.readAll) decode[Raw](jsonString).valueOr(throw _) } }
Example 31
Source File: Utils.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.vm.utils import java.io.File import akka.util.ByteString import io.circe.parser.decode import io.circe.generic.extras.Configuration import io.circe.generic.extras.auto._ import io.circe.Error import scala.io.Source object Utils { def loadContractCodeFromFile(file: File): ByteString = { val src = Source.fromFile(file) val raw = try { src.mkString } finally { src.close() } ByteString(raw.trim.grouped(2).map(Integer.parseInt(_, 16).toByte).toArray) } def loadContractAbiFromFile(file: File): Either[Error, List[ABI]] = { val src = Source.fromFile(file) val raw = try { src.mkString } finally { src.close() } implicit val config = Configuration.default.withDefaults decode[List[ABI]](raw) } }
Example 32
Source File: UserActor.scala From Scala-Programming-Projects with MIT License | 5 votes |
package actors import akka.actor._ import io.circe.generic.auto._ import io.circe.parser.decode import io.circe.syntax._ import io.fscala.shopping.shared.{CartEvent, WebsocketMessage} object UserActor { def props(browser: ActorRef, browserManager :ActorRef) = Props(new UserActor(browser, browserManager)) } class UserActor(browser: ActorRef, browserManager :ActorRef) extends Actor with ActorLogging { def receive = { case msg: String => log.info("Received JSON message: {}",msg) decode[CartEvent](msg) match { case Right(cartEvent) => log.info("Got {} message", cartEvent) browserManager forward cartEvent case Left(error) => log.info("Unhandled message : {}",error) } } }