spray.json.DefaultJsonProtocol Scala Examples
The following examples show how to use spray.json.DefaultJsonProtocol.
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: ModelService.scala From reactive-machine-learning-systems with MIT License | 6 votes |
package com.reactivemachinelearning import akka.actor.ActorSystem import akka.event.{Logging, LoggingAdapter} import akka.http.scaladsl.Http import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} //import spray.json._ import spray.json.DefaultJsonProtocol import scala.concurrent.{ExecutionContextExecutor, Future} case class Prediction(id: Long, timestamp: Long, value: Double) trait Protocols extends DefaultJsonProtocol { implicit val ipInfoFormat = jsonFormat3(Prediction.apply) } trait Service extends Protocols { implicit val system: ActorSystem implicit def executor: ExecutionContextExecutor implicit val materializer: Materializer val logger: LoggingAdapter // private def parseFeatures(features: String): Map[Long, Double] = { // features.parseJson.convertTo[Map[Long, Double]] // } def predict(features: String): Future[Prediction] = { Future(Prediction(123, 456, 0.5)) } val routes = { logRequestResult("predictive-service") { pathPrefix("ip") { (get & path(Segment)) { features => complete { predict(features).map[ToResponseMarshallable] { // case prediction: Prediction => prediction case _ => BadRequest } } } } } } } object PredictiveService extends App with Service { override implicit val system = ActorSystem() override implicit val executor = system.dispatcher override implicit val materializer = ActorMaterializer() override val logger = Logging(system, getClass) Http().bindAndHandle(routes, "0.0.0.0", 9000) }
Example 2
Source File: GraphJsonTestSupport.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.workflowmanager.storage import org.mockito.Mockito._ import org.scalatest.mockito.MockitoSugar import org.scalatest.{Matchers, WordSpec} import spray.json.{DefaultJsonProtocol, JsObject} import ai.deepsense.deeplang.DOperation import ai.deepsense.graph.Endpoint trait GraphJsonTestSupport extends WordSpec with MockitoSugar with DefaultJsonProtocol with Matchers { def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = { assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString) assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex) } def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = { edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString && edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex } def mockOperation( inArity: Int, outArity: Int, id: DOperation.Id, name: String): DOperation = { val dOperation = mock[DOperation] when(dOperation.inArity).thenReturn(inArity) when(dOperation.outArity).thenReturn(outArity) when(dOperation.id).thenReturn(id) when(dOperation.name).thenReturn(name) when(dOperation.paramValuesToJson).thenReturn(JsObject()) dOperation } }
Example 3
Source File: JsonGoogleParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{GoogleModel, Model} import spray.http._ import scala.util._ def decode(entity: HttpEntity): Deserialized[Results] = { import spray.httpx.unmarshalling._ val mediaTypeTextHtml = MediaTypes.`text/html` val mediaTypeJson = MediaTypes.`application/json` val contentTypeJson = ContentType(mediaTypeJson, HttpCharsets.`UTF-8`) // val contentTypeText = ContentType(mediaTypeTextHtml, HttpCharsets.`ISO-8859-1`) entity match { case HttpEntity.NonEmpty(`contentTypeJson`, _) => entity.as[Results] case HttpEntity.NonEmpty(ContentType(`mediaTypeTextHtml`, x), y) => HttpEntity(ContentType(mediaTypeJson, x), fix(y)).as[Results] case HttpEntity.NonEmpty(x, _) => Left(MalformedContent(s"logic error: contentType=$x")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = s.substring(3) }
Example 4
Source File: FunctionGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.composite import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.composite.FunctionTimeSeries import spray.json.{DefaultJsonProtocol, JsNumber, JsObject, JsString, JsValue, _} class FunctionGenerator(name: Option[String], val generator: Either[String, Generator[Any]], val slope: Double, val intercept: Double) extends Generator[Double](name, "function") { override def timeseries(generators: String => Generator[Any]) = { Model.generator(generators)(generator) match { // Could also be expressed as a Sum(Times(generator, Constant(slope), intercept) case g: Generator[Double] => FunctionTimeSeries[Double](g.timeseries(generators), (t,v) => Some(slope * v + intercept)) case _ => throw new ClassCastException } } override def toString = "Function(" + name + ", " + generator + ", " + slope + ", " + intercept + ")" override def equals(o: Any) = o match { case that: FunctionGenerator => (that.name == this.name && that.generator == this.generator && that.slope == this.slope && that.intercept == this.intercept) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "generator" -> either2json(generator), "slope" -> slope.toJson, "intercept" -> intercept.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object FunctionGenerator { def apply(json: JsValue): FunctionGenerator = { val fields = json.asJsObject.fields val name = json.asJsObject.fields.get("name").map { case JsString(x) => x } val generator = fields("generator") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val slope = fields("slope") match { case JsNumber(n) => n.toDouble } val intercept = fields("intercept") match { case JsNumber(n) => n.toDouble } new FunctionGenerator(name, generator, slope, intercept) } }
Example 5
Source File: GreaterThanGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.GreaterThanTimeSeries import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class GreaterThanGenerator( name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]], val strict: Option[Boolean]) extends Generator[Any](name, "greater-than") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Double] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Double] => t } new GreaterThanTimeSeries(first, second, strict match { case None => true case Some(x) => x }) } override def toString = "GreaterThan(" + name + ", " + a + ", " + b + ", " + strict + ")" override def equals(o: Any) = o match { case that: GreaterThanGenerator => that.name == this.name && that.a == this.a && that.b == this.b && that.strict == this.strict case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) if(strict.isDefined) t = t.updated("strict", strict.toJson) new JsObject(t) } } object GreaterThanGenerator extends DefaultJsonProtocol { def apply(value: JsValue): GreaterThanGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val strict = fields.get("strict").map(_.convertTo[Boolean]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new GreaterThanGenerator(name, a, b, strict) } }
Example 6
Source File: LesserThanGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.binary import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.Generator import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.binary.LesserThanTimeSeries import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, RootJsonFormat, _} class LesserThanGenerator( name: Option[String], val a: Either[String, Generator[Any]], val b: Either[String, Generator[Any]], val strict: Option[Boolean]) extends Generator[Any](name, "lesser-than") { override def timeseries(generators: (String) => Generator[Any]) = { val first = Model.generator(generators)(a).timeseries(generators) match { case t: TimeSeries[Double] => t } val second = Model.generator(generators)(b).timeseries(generators) match { case t: TimeSeries[Double] => t } new LesserThanTimeSeries(first, second, strict match { case None => true case Some(x) => x }) } override def toString = "LesserThan(" + name + ", " + a + ", " + b + ", " + strict + ")" override def equals(o: Any) = o match { case that: LesserThanGenerator => that.name == this.name && that.a == this.a && that.b == this.b && that.strict == this.strict case _ => false } override def toJson: JsValue = { val _a = a match { case Left(s) => s.toJson case Right(g) => g.toJson } val _b = b match { case Left(s) => s.toJson case Right(g) => g.toJson } var t = Map( "a" -> _a, "b" -> _b, "type" -> `type`.toJson ) if(name.isDefined) t = t.updated("name", name.toJson) if(strict.isDefined) t = t.updated("strict", strict.toJson) new JsObject(t) } } object LesserThanGenerator extends DefaultJsonProtocol { def apply(value: JsValue): LesserThanGenerator = { val fields = value.asJsObject.fields val name = fields.get("name").map(_.convertTo[String]) val strict = fields.get("strict").map(_.convertTo[Boolean]) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g)) } new LesserThanGenerator(name, a, b, strict) } }
Example 7
Source File: GaussianNoiseGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.primary import be.cetic.tsimulus.config.ARMAModel import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.primary.GaussianNoiseTimeSeries import com.github.nscala_time.time.Imports._ import org.joda.time.Duration import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue} import spray.json._ import scala.util.Random class GaussianNoiseGenerator(name: Option[String], val seed: Int, val std: Double) extends Generator[Double](name, "gaussian") { override def timeseries(generators: String => Generator[Any]) = GaussianNoiseTimeSeries(seed, std) override def toString = "GaussianNoise(" + seed + ", " + std + ")" override def equals(o: Any) = o match { case that: GaussianNoiseGenerator => that.name == this.name && that.seed == this.seed && Math.abs(that.std - this.std) < 0.0001 case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "seed" -> seed.toJson, "std" -> std.toJson ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object GaussianNoiseGenerator extends DefaultJsonProtocol with TimeToJson { def apply(json: JsValue): GaussianNoiseGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val seed = fields("seed").convertTo[Int] val std = fields("std").convertTo[Double] new GaussianNoiseGenerator(name, seed, std) } }
Example 8
Source File: MinuteGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MillisecondTimeSeries, MinuteTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MinuteGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "minute") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MinuteTimeSeries(ts) } override def toString = "MinuteGenerator()" override def equals(o: Any) = o match { case that: MinuteGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MinuteGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MinuteGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MinuteGenerator(name, Right(base)) def apply(json: JsValue): MinuteGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MinuteGenerator(name, base) } }
Example 9
Source File: MillisecondTimeGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.MillisecondTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MillisecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "ms") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MillisecondTimeSeries(ts) } override def toString = "MillisecondTimeGenerator()" override def equals(o: Any) = o match { case that: MillisecondTimeGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MillisecondTimeGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MillisecondTimeGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MillisecondTimeGenerator(name, Right(base)) def apply(json: JsValue): MillisecondTimeGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MillisecondTimeGenerator(name, base) } }
Example 10
Source File: DayOfYearGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfWeekTimeSeries, DayOfYearTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfYearGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "doy") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfYearTimeSeries(ts) } override def toString = "DayOfYearGenerator()" override def equals(o: Any) = o match { case that: DayOfYearGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfYearGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfYearGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfYearGenerator(name, Right(base)) def apply(json: JsValue): DayOfYearGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfYearGenerator(name, base) } }
Example 11
Source File: YearGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.YearTimeSeries import org.joda.time.{Duration, LocalDateTime} import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue} import spray.json._ class YearGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "year") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new YearTimeSeries(ts) } override def toString = "YearGenerator()" override def equals(o: Any) = o match { case that: YearGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object YearGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new YearGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new YearGenerator(name, Right(base)) def apply(json: JsValue): YearGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new YearGenerator(name, base) } }
Example 12
Source File: WeekGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, WeekTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class WeekGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "week") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new WeekTimeSeries(ts) } override def toString = "WeekGenerator()" override def equals(o: Any) = o match { case that: WeekGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object WeekGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new WeekGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new WeekGenerator(name, Right(base)) def apply(json: JsValue): WeekGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new WeekGenerator(name, base) } }
Example 13
Source File: DayOfMonthGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.composite.TimeShiftTimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfMonthTimeSeries, MonthTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfMonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dom") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfMonthTimeSeries(ts) } override def toString = s"DayOfMonthGenerator(${base})" override def equals(o: Any) = o match { case that: DayOfMonthGenerator => that.name == this.name && this.base == that.base case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfMonthGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfMonthGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfMonthGenerator(name, Right(base)) def apply(json: JsValue): DayOfMonthGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfMonthGenerator(name, base) } }
Example 14
Source File: DayOfWeekGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.DayOfWeekTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DayOfWeekGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dow") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DayOfWeekTimeSeries(ts) } override def toString = "DayOfWeekGenerator()" override def equals(o: Any) = o match { case that: DayOfWeekGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DayOfWeekGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new DayOfWeekGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfWeekGenerator(name, Right(base)) def apply(json: JsValue): DayOfWeekGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DayOfWeekGenerator(name, base) } }
Example 15
Source File: SecondTimeGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, SecondTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class SecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "second") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new SecondTimeSeries(ts) } override def toString = "SecondTimeGenerator()" override def equals(o: Any) = o match { case that: SecondTimeGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object SecondTimeGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new SecondTimeGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new SecondTimeGenerator(name, Right(base)) def apply(json: JsValue): SecondTimeGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new SecondTimeGenerator(name, base) } }
Example 16
Source File: DateTimeDifferenceGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.DateTimeDifferenceTimeSeries import org.joda.time.{Duration, LocalDateTime} import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class DateTimeDifferenceGenerator(name: Option[String], val a: Either[String, Generator[LocalDateTime]], val b: Either[String, Generator[LocalDateTime]]) extends Generator[Duration](name, "dt::diff") { override def timeseries(generators: String => Generator[Any]) = { val aTS = Model.generator(generators)(a).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] val bTS = Model.generator(generators)(b).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new DateTimeDifferenceTimeSeries(aTS, bTS) } override def toString = s"DateTimeDifferenceGenerator(${a}, ${b})" override def equals(o: Any) = o match { case that: DateTimeDifferenceGenerator => that.name == this.name && this.a == that.a && this.b == that.b case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "a" -> either2json(a), "b" -> either2json(b) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object DateTimeDifferenceGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], a: String, b: String) = new DateTimeDifferenceGenerator(name, Left(a), Left(b)) def apply(name: Option[String], a: String, b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Left(a), Right(b)) def apply(name: Option[String], a: Generator[LocalDateTime], b: String) = new DateTimeDifferenceGenerator(name, Right(a), Left(b)) def apply(name: Option[String], a: Generator[LocalDateTime], b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Right(a), Right(b)) def apply(json: JsValue): DateTimeDifferenceGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val a = fields("a") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } val b = fields("b") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new DateTimeDifferenceGenerator(name, a, b) } }
Example 17
Source File: HourGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.{DayOfYearTimeSeries, HourTimeSeries} import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class HourGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "hour") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new HourTimeSeries(ts) } override def toString = "HourGenerator()" override def equals(o: Any) = o match { case that: HourGenerator => that.name == this.name case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object HourGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new HourGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new HourGenerator(name, Right(base)) def apply(json: JsValue): HourGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new HourGenerator(name, base) } }
Example 18
Source File: MonthGenerator.scala From TSimulus with Apache License 2.0 | 5 votes |
package be.cetic.tsimulus.generators.dt import be.cetic.tsimulus.config.{GeneratorFormat, Model} import be.cetic.tsimulus.generators.{Generator, TimeToJson} import be.cetic.tsimulus.timeseries.TimeSeries import be.cetic.tsimulus.timeseries.dt.MonthTimeSeries import org.joda.time.LocalDateTime import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _} class MonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "month") { override def timeseries(generators: String => Generator[Any]) = { val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]] new MonthTimeSeries(ts) } override def toString = "MonthGenerator()" override def equals(o: Any) = o match { case that: MonthGenerator => (that.name == this.name) && (that.base == this.base) case _ => false } override def toJson: JsValue = { val t = Map( "type" -> `type`.toJson, "base" -> either2json(base) ) new JsObject( name.map(n => t + ("name" -> n.toJson)).getOrElse(t) ) } } object MonthGenerator extends DefaultJsonProtocol with TimeToJson { def apply(name: Option[String], base: String) = new MonthGenerator(name, Left(base)) def apply(name: Option[String], base: Generator[LocalDateTime]) = new MonthGenerator(name, Right(base)) def apply(json: JsValue): MonthGenerator = { val fields = json.asJsObject.fields val name = fields.get("name") .map(f => f match { case JsString(x) => x }) val base = fields("base") match { case JsString(s) => Left(s) case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]]) } new MonthGenerator(name, base) } }
Example 19
Source File: HttpAuctionServiceRouteSpec.scala From akka-dddd-template with Apache License 2.0 | 5 votes |
package com.boldradius.auction import akka.actor.{ActorSystem, Actor, ActorRef, Props} import com.boldradius.cqrs.AuctionCommandQueryProtocol._ import com.boldradius.cqrs._ import com.typesafe.config.ConfigFactory import org.scalatest._ import spray.http.Uri import spray.routing._ import scala.concurrent.duration._ import spray.json._ import spray.json.DefaultJsonProtocol import spray.testkit.ScalatestRouteTest import com.boldradius.util.MarshallingSupport._ object HttpAuctionServiceRouteSpec{ import spray.util.Utils val (_, akkaPort) = Utils temporaryServerHostnameAndPort() val config = ConfigFactory.parseString( s""" akka.remote.netty.tcp.port = $akkaPort akka.log-dead-letters = off akka.log-dead-letters-during-shutdown = off """) val testSystem = ActorSystem("offers-route-spec", config) } class HttpAuctionServiceRouteSpec extends FeatureSpecLike with GivenWhenThen with ScalatestRouteTest with MustMatchers with BeforeAndAfterAll with HttpAuctionServiceRoute { import HttpAuctionServiceRouteSpec._ implicit val ec = system.dispatcher override protected def createActorSystem(): ActorSystem = testSystem def actorRefFactory = testSystem val cmdActor:ActorRef = system.actorOf( Props( new Actor { def receive: Receive = { case StartAuctionCmd(id, start, end, initialPrice,prodId) => sender() ! StartedAuctionAck(id) case PlaceBidCmd(id,buyer,bidPrice)=> sender ! PlacedBidAck(id,buyer,bidPrice,1) } })) val queryActor:ActorRef = system.actorOf( Props( new Actor { def receive: Receive = { case WinningBidPriceQuery(id) => sender() ! WinningBidPriceResponse(id,1) case GetBidHistoryQuery(id) => sender() ! BidHistoryResponse(id,List(Bid(1,"buyer",1))) case GetProdIdQuery(id) => sender() ! ProdIdResponse(id,"1") } })) feature("Good Requests") { scenario("post is made to create auction") { Given("route is properly formed") When("/startAuction is called with POST") Post(Uri("/startAuction"),StartAuctionDto("123", "2015-01-20-15:53", "2015-01-20-15:53", 1, "1")) ~> route(cmdActor,queryActor) ~> check { //responseAs[Any] must be(Map("action" -> "AuctionStarted", "details" -> Map("auctionId" -> "123"))) responseAs[AuctionStartedDto] must be(AuctionStartedDto("123","AuctionStartedDto")) } Then(s"Received POST response: ${AuctionStartedDto("123","AuctionStartedDto")}") } scenario("post is made to bid") { Given("route is properly formed") When("/bid is called with POST") Post(Uri("/bid"),PlaceBidDto("123", "buyer", 1)) ~> route(cmdActor,queryActor) ~> check { responseAs[SuccessfulBidDto] must be(SuccessfulBidDto("123",1 , "1969-12-31-19:00","SuccessfulBidDto")) } Then(s"Received POST response: ${SuccessfulBidDto("123",1 , "1969-12-31-19:00","SuccessfulBidDto")}") } } }
Example 20
Source File: IdentityManager.scala From reactive-microservices with MIT License | 5 votes |
import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.stream.ActorFlowMaterializer import com.typesafe.config.ConfigFactory import scala.concurrent.blocking import scala.slick.driver.PostgresDriver.simple._ import scala.slick.lifted.{ProvenShape, Tag} import spray.json.DefaultJsonProtocol case class Identity(id: Option[Long], createdAt: Long) class Identities(tag: Tag) extends Table[Identity](tag, "identity") { def id = column[Long]("id", O.PrimaryKey, O.AutoInc) def createdAt = column[Long]("created_at", O.NotNull) override def * : ProvenShape[Identity] = (id.?, createdAt) <> ((Identity.apply _).tupled, Identity.unapply) } object IdentityManager extends App with DefaultJsonProtocol { val config = ConfigFactory.load() val interface = config.getString("http.interface") val port = config.getInt("http.port") val dbUrl = config.getString("db.url") val dbUser = config.getString("db.user") val dbPassword = config.getString("db.password") implicit val actorSystem = ActorSystem() implicit val materializer = ActorFlowMaterializer() implicit val dispatcher = actorSystem.dispatcher implicit val identityFormat = jsonFormat2(Identity.apply) val db = Database.forURL(url = dbUrl, user = dbUser, password = dbPassword, driver = "org.postgresql.Driver") val identities = TableQuery[Identities] def getAllIdentities(): List[Identity] = { blocking { db.withSession { implicit s => identities.list } } } def saveIdentity(identity: Identity): Identity = { blocking { db.withSession { implicit s => identities returning identities.map(_.id) into ((_, id) => identity.copy(id = Option(id))) += identity } } } Http().bindAndHandle(interface = interface, port = port, handler = { logRequestResult("identity-manager") { path("identities") { pathEndOrSingleSlash { post { complete { val newIdentity = Identity(id = None, createdAt = System.currentTimeMillis()) Created -> saveIdentity(newIdentity) } } ~ get { complete { getAllIdentities() } } } } } }) }
Example 21
Source File: JsonProtocols.scala From reactive-microservices with MIT License | 5 votes |
import spray.json.DefaultJsonProtocol trait JsonProtocols extends DefaultJsonProtocol { protected implicit val identityFormat = jsonFormat1(Identity) protected implicit val tokenFormat = jsonFormat4(Token) protected implicit val codeCardFormat = jsonFormat3(CodeCard) protected implicit val authEntryFormat = jsonFormat4(AuthEntry) protected implicit val internalLoginRequestFormat = jsonFormat2(InternalLoginRequest) protected implicit val internalReloginRequestFormat = jsonFormat2(InternalReloginRequest) protected implicit val registerResponseFormat = jsonFormat2(RegisterResponse) protected implicit val loginRequestFormat = jsonFormat4(LoginRequest) protected implicit val activateCodeRequestFormat = jsonFormat1(ActivateCodeRequest) protected implicit val activateCodeResponseFormat = jsonFormat2(ActivateCodeResponse) protected implicit val getCodeCardRequestFormat = jsonFormat1(GetCodeCardRequest) protected implicit val getCodeCardResponseFormat = jsonFormat2(GetCodeCardResponse) }
Example 22
Source File: SpeechSchemas.scala From mmlspark with MIT License | 5 votes |
// Copyright (C) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in project root for information. package com.microsoft.ml.spark.cognitive import com.microsoft.ml.spark.core.schema.SparkBindings import spray.json.{DefaultJsonProtocol, RootJsonFormat} case class DetailedSpeechResponse(Confidence: Double, Lexical: String, ITN: String, MaskedITN: String, Display: String) case class SpeechResponse(RecognitionStatus: String, Offset: Int, Duration: Int, Id: Option[String], DisplayText: Option[String], NBest: Option[Seq[DetailedSpeechResponse]] ) object SpeechResponse extends SparkBindings[SpeechResponse] object SpeechFormat extends DefaultJsonProtocol { implicit val DetailedSpeechResponseFormat: RootJsonFormat[DetailedSpeechResponse] = jsonFormat5(DetailedSpeechResponse.apply) implicit val SpeechResponseFormat: RootJsonFormat[SpeechResponse] = jsonFormat6(SpeechResponse.apply) }
Example 23
Source File: ArrayMapParam.scala From mmlspark with MIT License | 5 votes |
// Copyright (C) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in project root for information. package org.apache.spark.ml.param import org.apache.spark.ml.util.Identifiable import spray.json.{DefaultJsonProtocol, _} import scala.collection.immutable.Map object ArrayMapJsonProtocol extends DefaultJsonProtocol { implicit object MapJsonFormat extends JsonFormat[Map[String, Any]] { def write(m: Map[String, Any]): JsValue = { JsObject(m.mapValues { case v: Int => JsNumber(v) case v: Double => JsNumber(v) case v: String => JsString(v) case true => JsTrue case false => JsFalse case v: Map[_, _] => write(v.asInstanceOf[Map[String, Any]]) case default => serializationError(s"Unable to serialize $default") }) } def read(value: JsValue): Map[String, Any] = value.asInstanceOf[JsObject].fields.map(kvp => { val convValue = kvp._2 match { case JsNumber(n) => if (n.isValidInt) n.intValue().asInstanceOf[Any] else n.toDouble.asInstanceOf[Any] case JsString(s) => s case JsTrue => true case JsFalse => false case v: JsValue => read(v) case default => deserializationError(s"Unable to deserialize $default") } (kvp._1, convValue) }) } } override def w(value: Array[Map[String, Any]]): ParamPair[Array[Map[String, Any]]] = super.w(value) override def jsonEncode(value: Array[Map[String, Any]]): String = { val json = value.toSeq.toJson json.prettyPrint } override def jsonDecode(json: String): Array[Map[String, Any]] = { val jsonValue = json.parseJson jsonValue.convertTo[Seq[Map[String, Any]]].toArray } }
Example 24
Source File: MapArrayParam.scala From mmlspark with MIT License | 5 votes |
// Copyright (C) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in project root for information. package org.apache.spark.ml.param import spray.json.{DefaultJsonProtocol, _} import scala.collection.JavaConverters._ import scala.collection.immutable.Map import scala.collection.mutable object MapArrayJsonProtocol extends DefaultJsonProtocol { implicit object MapJsonFormat extends JsonFormat[Map[String, Seq[String]]] { def write(m: Map[String, Seq[String]]): JsValue = { JsObject(m.mapValues { case v: Seq[String] => seqFormat[String].write(v) case default => serializationError(s"Unable to serialize $default") }) } def read(value: JsValue): Map[String, Seq[String]] = value.asInstanceOf[JsObject].fields.map(kvp => { val convValue = kvp._2 match { case v: JsValue => seqFormat[String].read(v) case default => deserializationError(s"Unable to deserialize $default") } (kvp._1, convValue) }) } } def w(value: java.util.HashMap[String, java.util.List[String]]): ParamPair[Map[String, Seq[String]]] = { val mutMap = mutable.Map[String, Seq[String]]() for (key <- value.keySet().asScala) { val list = value.get(key).asScala mutMap(key) = list } w(mutMap.toMap) } override def jsonEncode(value: Map[String, Seq[String]]): String = { val convertedMap = value.map(kvp => (kvp._1, kvp._2.toArray)) val json = convertedMap.toJson json.prettyPrint } override def jsonDecode(json: String): Map[String, Seq[String]] = { val jsonValue = json.parseJson jsonValue.convertTo[Map[String, Seq[String]]] } }
Example 25
Source File: GraphJsonTestSupport.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.models.json.graph import org.mockito.Mockito._ import org.scalatest.mockito.MockitoSugar import org.scalatest.{Matchers, WordSpec} import spray.json.{DefaultJsonProtocol, JsObject} import io.deepsense.deeplang.DOperation import io.deepsense.graph.Endpoint trait GraphJsonTestSupport extends WordSpec with MockitoSugar with DefaultJsonProtocol with Matchers { def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = { assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString) assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex) } def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = { edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString && edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex } def mockOperation( inArity: Int, outArity: Int, id: DOperation.Id, name: String): DOperation = { val dOperation = mock[DOperation] when(dOperation.inArity).thenReturn(inArity) when(dOperation.outArity).thenReturn(outArity) when(dOperation.id).thenReturn(id) when(dOperation.name).thenReturn(name) dOperation } }
Example 26
Source File: EchoEnumeratumService.scala From swagger-akka-http-sample with Apache License 2.0 | 5 votes |
package com.example.akka.echoenumeratum import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.server.{Directives, Route} import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.{Content, Schema} import io.swagger.v3.oas.annotations.parameters.RequestBody import io.swagger.v3.oas.annotations.responses.ApiResponse import javax.ws.rs.core.MediaType import javax.ws.rs.{Consumes, POST, Path, Produces} import pl.iterators.kebs.json.{KebsEnumFormats, KebsSpray} import spray.json.{DefaultJsonProtocol, RootJsonFormat} @Path("/echoenumeratum") object EchoEnumeratumService extends Directives with SprayJsonSupport with DefaultJsonProtocol with KebsSpray with KebsEnumFormats { case class EchoEnumeratum(enumValue: SizeEnum) implicit val echoEnumeratumFormat: RootJsonFormat[EchoEnumeratum] = jsonFormatN[EchoEnumeratum] val route: Route = echo @POST @Consumes(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON)) @Operation(summary = "Echo Enumeratum", description = "Echo Enumeratum", requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[EchoEnumeratum])))), responses = Array( new ApiResponse(responseCode = "200", description = "Echo Enumeratum", content = Array(new Content(schema = new Schema(implementation = classOf[EchoEnumeratum])))), new ApiResponse(responseCode = "400", description = "Bad Request")) ) def echo: Route = path("echoenumeratum") { post { entity(as[EchoEnumeratum]) { request => complete(request) } } } }
Example 27
Source File: ApplicationInfo.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.navigator import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import spray.json.DefaultJsonProtocol case class ApplicationInfo( id: String, name: String, version: String ) trait ApplicationInfoJsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val applicationInfoFormat = jsonFormat3(ApplicationInfo) }
Example 28
Source File: WorkflowJsonParamsOverrider.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.workflowexecutor import spray.json.lenses.JsonLenses._ import spray.json.{DefaultJsonProtocol, JsValue} object WorkflowJsonParamsOverrider extends DefaultJsonProtocol { def overrideParams(workflow: JsValue, params: Map[String, String]): JsValue = { params.foldLeft(workflow)(overrideParam) } private def overrideParam(json: JsValue, param: (String, String)): JsValue = { val (key, value) = param val pathElems = key.split("\\.") val basePath = "workflow" / "nodes" / filter("id".is[String](_ == pathElems(0))) / "parameters" val path = pathElems.drop(1).foldLeft(basePath)((a, b) => a / b) json.update(path ! modify[String](_ => value)) } }
Example 29
Source File: ParamsSerialization.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.deeplang.doperables.serialization import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue} import io.deepsense.deeplang.catalogs.doperable.exceptions.NoParameterlessConstructorInClassException import io.deepsense.deeplang.params.Params import io.deepsense.deeplang.{ExecutionContext, TypeUtils} trait ParamsSerialization { self: Params => def saveObjectWithParams(ctx: ExecutionContext, path: String): Unit = { saveMetadata(ctx, path) saveParams(ctx, path) } def loadAndSetParams(ctx: ExecutionContext, path: String): this.type = { setParams(loadParams(ctx, path)) } protected def saveMetadata(ctx: ExecutionContext, path: String) = { val metadataFilePath = ParamsSerialization.metadataFilePath(path) val metadataJson = JsObject( ParamsSerialization.classNameKey -> JsString(this.getClass.getName) ) JsonObjectPersistence.saveJsonToFile(ctx, metadataFilePath, metadataJson) } protected def saveParams(ctx: ExecutionContext, path: String): Unit = { val paramsFilePath = ParamsSerialization.paramsFilePath(path) JsonObjectPersistence.saveJsonToFile(ctx, paramsFilePath, paramValuesToJson) } protected def loadParams(ctx: ExecutionContext, path: String): JsValue = { JsonObjectPersistence.loadJsonFromFile(ctx, ParamsSerialization.paramsFilePath(path)) } private def setParams(paramsJson: JsValue): this.type = { this.set(paramPairsFromJson(paramsJson): _*) } } object ParamsSerialization { val classNameKey = "className" val paramsFileName = "params" val metadataFileName = "metadata" def load(ctx: ExecutionContext, path: String): Loadable = { import DefaultJsonProtocol._ val metadataPath = metadataFilePath(path) val metadataJson: JsObject = JsonObjectPersistence.loadJsonFromFile(ctx, metadataPath).asJsObject val className = metadataJson.fields(classNameKey).convertTo[String] val clazz: Class[_] = Class.forName(className) val loadable = TypeUtils.createInstance(TypeUtils.constructorForClass(clazz) .getOrElse(throw new NoParameterlessConstructorInClassException(clazz.getCanonicalName)) ).asInstanceOf[Loadable] loadable.load(ctx, path) } def metadataFilePath(path: String): String = { PathsUtils.combinePaths(path, metadataFileName) } def paramsFilePath(path: String): String = { PathsUtils.combinePaths(path, paramsFileName) } }
Example 30
Source File: JsonGoogleParser.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package com.phasmid.hedge_fund.actors import akka.actor.{ ActorRef, Props } import spray.http._ import scala.util._ import com.phasmid.hedge_fund.model._ def decode(entity: HttpEntity): Deserialized[Results] = { val mediaTypeTextHtml = MediaTypes.`text/html` val mediaTypeJson = MediaTypes.`application/json` val contentTypeJson = ContentType(mediaTypeJson, HttpCharsets.`UTF-8`) // val contentTypeText = ContentType(mediaTypeTextHtml, HttpCharsets.`ISO-8859-1`) entity match { case HttpEntity.NonEmpty(`contentTypeJson`, _) => entity.as[Results] case HttpEntity.NonEmpty(ContentType(`mediaTypeTextHtml`, x), y) => HttpEntity(ContentType(mediaTypeJson, x), fix(y)).as[Results] case HttpEntity.NonEmpty(x, _) => Left(MalformedContent(s"logic error: contentType=$x")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = s.substring(3) }
Example 31
Source File: JsonGoogleOptionParser.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package com.phasmid.hedge_fund.actors import akka.actor.{ ActorRef, Props } import spray.http._ import scala.util._ import com.phasmid.hedge_fund.model._ def decode(entity: HttpEntity): Deserialized[OptionChain] = { val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) entity match { case HttpEntity.NonEmpty(`contentType`, y) => HttpEntity(contentType, fix(y)).as[OptionChain] case HttpEntity.NonEmpty(s, y) => Left(MalformedContent(s"entity content type: $s")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = """([^,{:\s]+):""".r.replaceAllIn(s, """"$1":""") }
Example 32
Source File: JsonYQLParser.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
package com.phasmid.hedge_fund.actors import akka.actor.{ ActorRef, Props } import spray.http._ import scala.util._ import com.phasmid.hedge_fund.model._ class JsonYQLParser(blackboard: ActorRef) extends BlackboardActor(blackboard) { val model: Model = new YQLModel override def receive = { case ContentMessage(entity) => { log.debug("JsonYQLParser received ContentMessage") JsonYQLParser.decode(entity) match { case Right(response) => processQuote(response.query.results.quote) case Left(message) => log.warning(message.toString()) } } case m => super.receive(m) } def processQuote(quotes: Seq[Map[String, Option[String]]]) = quotes foreach { q => processInstrument(q) } def processInstrument(quote: Map[String, Option[String]]) = model.getKey("symbol") match { case Some(s) => quote.get(s) match { case Some(Some(symbol)) => updateMarket(symbol, quote) case _ => log.warning(s"symbol $s is undefined") } case _ => log.warning("'symbol' is undefined in model") } def updateMarket(symbol: String, quote: Map[String, Option[String]]) = blackboard ! KnowledgeUpdate(model, symbol, quote flatMap { case (k, Some(v)) => Option(k -> v); case _ => None }) } object JsonYQLParser { import spray.json.DefaultJsonProtocol import spray.httpx.unmarshalling._ import spray.httpx.marshalling._ import spray.httpx.SprayJsonSupport._ import spray.json._ case class Response(query: Query) case class Query(count: Int, created: String, lang: String, diagnostics: Option[Diagnostics], results: Results) case class Diagnostics(url: Seq[Map[String, String]], publiclyCallable: String, `user-time`: String, `service-time`: String, `build-version`: String, query: DiagnosticsQuery, cache: DiagnosticsCache, javascript: DiagnosticsJavascript) case class DiagnosticsQuery(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, params: String, content: String) case class DiagnosticsCache(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, method: String, `type`: String, content: String) case class DiagnosticsJavascript(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, `instructions-used`: String, `table-name`: String) case class Results(quote: Seq[Map[String, Option[String]]]) { def get(index: Int, key: String): Option[String] = { Try { quote(index) } match { case Success(y) => y.get(key) match { case Some(x) => x; case None => None } case Failure(y) => None } } } object MyJsonProtocol extends DefaultJsonProtocol with NullOptions { implicit val diagnosticsQueryFormat = jsonFormat5(DiagnosticsQuery) implicit val diagnosticsCacheFormat = jsonFormat6(DiagnosticsCache) implicit val diagnosticsJavascriptFormat = jsonFormat5(DiagnosticsJavascript) implicit val diagnosticsFormat = jsonFormat8(Diagnostics) implicit val resultsFormat = jsonFormat1(Results) implicit val queryFormat = jsonFormat5(Query) implicit val entityFormat = jsonFormat1(Response) } import MyJsonProtocol._ def decode(entity: HttpEntity) = entity.as[Response] }
Example 33
Source File: RestPi.scala From apache-spark-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend import akka.actor.ActorSystem import akka.event.{ Logging, LoggingAdapter } import akka.http.scaladsl._ import akka.http.scaladsl.common.{ EntityStreamingSupport, JsonEntityStreamingSupport } import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.server.{ Directives, Route } import akka.stream.scaladsl.{ Flow, Source } import akka.stream.{ ActorMaterializer, Materializer } import akka.util.ByteString import com.github.dnvriend.spark.CalculatePi import org.apache.spark.SparkContext import org.apache.spark.sql.SparkSession import spray.json.DefaultJsonProtocol import scala.concurrent.{ ExecutionContext, Future } object RestPi extends App with Directives with SprayJsonSupport with DefaultJsonProtocol { implicit val system: ActorSystem = ActorSystem() implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = system.dispatcher implicit val log: LoggingAdapter = Logging(system, this.getClass) val spark = SparkSession.builder() .config("spark.sql.warehouse.dir", "file:/tmp/spark-warehouse") .config("spark.scheduler.mode", "FAIR") .config("spark.sql.crossJoin.enabled", "true") .master("local") // use as many threads as cores .appName("RestPi") // The appName parameter is a name for your application to show on the cluster UI. .getOrCreate() final case class Pi(pi: Double) implicit val piJsonFormat = jsonFormat1(Pi) val start = ByteString.empty val sep = ByteString("\n") val end = ByteString.empty implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json() .withFramingRenderer(Flow[ByteString].intersperse(start, sep, end)) .withParallelMarshalling(parallelism = 8, unordered = true) def sparkContext: SparkContext = spark.newSession().sparkContext def calculatePi(num: Long = 1000000, slices: Int = 2): Future[Double] = Future(CalculatePi(sparkContext, num, slices)).map(count => slices.toDouble * count / (num - 1)) val route: Route = pathEndOrSingleSlash { complete(calculatePi().map(Pi)) } ~ path("pi" / LongNumber / IntNumber) { (num, slices) => complete(calculatePi(num, slices).map(Pi)) } ~ path("stream" / "pi" / LongNumber) { num => complete(Source.fromFuture(calculatePi()).map(Pi) .flatMapConcat(Source.repeat).take(num)) } Http().bindAndHandle(route, "0.0.0.0", 8008) sys.addShutdownHook { spark.stop() system.terminate() } }
Example 34
Source File: ModelParser.scala From aloha with MIT License | 5 votes |
package com.eharmony.aloha.factory import com.eharmony.aloha.audit.Auditor import com.eharmony.aloha.factory.jsext.JsValueExtensions import com.eharmony.aloha.id.{ModelId, ModelIdentity} import com.eharmony.aloha.models.{Model, Submodel} import com.eharmony.aloha.reflect.RefInfo import com.eharmony.aloha.semantics.Semantics import spray.json.{DefaultJsonProtocol, JsObject, JsValue, JsonFormat, JsonReader} import spray.json.DefaultJsonProtocol.{LongJsonFormat, StringJsonFormat} sealed trait ModelParser { val modelType: String private implicit val modelIdFormat = DefaultJsonProtocol.jsonFormat2(ModelId.apply) protected final def getModelId(json: JsValue): Option[ModelIdentity] = json(ModelParser.modelIdField).collect{case o: JsObject => o.convertTo[ModelId]} } private object ModelParser { val modelIdField = "modelId" } trait ModelParsingPlugin extends ModelParser { def modelJsonReader[U, N, A, B <: U](factory: SubmodelFactory[U, A], semantics: Semantics[A], auditor: Auditor[U, N, B]) (implicit r: RefInfo[N], jf: JsonFormat[N]): Option[JsonReader[Model[A, B]]] } trait SubmodelParsingPlugin extends ModelParser { def submodelJsonReader[U, N, A, B <: U](factory: SubmodelFactory[U, A], semantics: Semantics[A], auditor: Auditor[U, N, B]) (implicit r: RefInfo[N], jf: JsonFormat[N]): Option[JsonReader[Submodel[N, A, U]]] } trait ModelSubmodelParsingPlugin extends ModelParsingPlugin with SubmodelParsingPlugin { def commonJsonReader[U, N, A, B <: U](factory: SubmodelFactory[U, A], semantics: Semantics[A], auditor: Auditor[U, N, B]) (implicit r: RefInfo[N], jf: JsonFormat[N]): Option[JsonReader[_ <: Model[A, B] with Submodel[_, A, B]]] final override def modelJsonReader[U, N, A, B <: U](factory: SubmodelFactory[U, A], semantics: Semantics[A], auditor: Auditor[U, N, B]) (implicit r: RefInfo[N], jf: JsonFormat[N]): Option[JsonReader[Model[A, B]]] = { val reader = commonJsonReader(factory, semantics, auditor) reader.map(jr => jr.asInstanceOf[JsonReader[Model[A, B]]]) } final def submodelJsonReader[U, N, A, B <: U](factory: SubmodelFactory[U, A], semantics: Semantics[A], auditor: Auditor[U, N, B]) (implicit r: RefInfo[N], jf: JsonFormat[N]): Option[JsonReader[Submodel[N, A, U]]] = { val reader = commonJsonReader(factory, semantics, auditor) reader.map(jr => jr.asInstanceOf[JsonReader[Submodel[N, A, U]]]) } }
Example 35
Source File: VwLabeledJson.scala From aloha with MIT License | 5 votes |
package com.eharmony.aloha.dataset.vw.labeled.json import com.eharmony.aloha.dataset.json.{Namespace, SparseSpec} import com.eharmony.aloha.dataset.vw.json.VwJsonLike import spray.json.DefaultJsonProtocol import scala.collection.{immutable => sci} import scala.util.Try final case class VwLabeledJson( imports: sci.Seq[String], features: sci.IndexedSeq[SparseSpec], namespaces: Option[Seq[Namespace]] = Some(Nil), normalizeFeatures: Option[Boolean] = Some(false), label: String, importance: Option[String] = Some("1"), tag: Option[String] = None) extends VwJsonLike { def validateImportance(): Boolean = { importance.nonEmpty || Try { importance.get.trim.toDouble }.map { case d if d >= 0 => true case _ => false }.getOrElse(true) } } object VwLabeledJson extends DefaultJsonProtocol { implicit val labeledVwJsonFormat = jsonFormat7(VwLabeledJson.apply) }
Example 36
Source File: VwMultilabeledJson.scala From aloha with MIT License | 5 votes |
package com.eharmony.aloha.dataset.vw.multilabel.json import com.eharmony.aloha.dataset.json.{Namespace, SparseSpec} import com.eharmony.aloha.dataset.vw.json.VwJsonLike import spray.json.{DefaultJsonProtocol, RootJsonFormat} import scala.collection.{immutable => sci} final case class VwMultilabeledJson( imports: sci.Seq[String], features: sci.IndexedSeq[SparseSpec], namespaces: Option[Seq[Namespace]] = Some(Nil), normalizeFeatures: Option[Boolean] = Some(false), positiveLabels: String) extends VwJsonLike object VwMultilabeledJson extends DefaultJsonProtocol { implicit val labeledVwJsonFormat: RootJsonFormat[VwMultilabeledJson] = jsonFormat5(VwMultilabeledJson.apply) }
Example 37
Source File: VwDownsampledMultilabeledJson.scala From aloha with MIT License | 5 votes |
package com.eharmony.aloha.dataset.vw.multilabel.json import com.eharmony.aloha.dataset.json.{Namespace, SparseSpec} import com.eharmony.aloha.dataset.vw.json.VwJsonLike import spray.json.{DefaultJsonProtocol, RootJsonFormat} import scala.collection.{immutable => sci} final case class VwDownsampledMultilabeledJson( imports: sci.Seq[String], features: sci.IndexedSeq[SparseSpec], namespaces: Option[Seq[Namespace]] = Some(Nil), normalizeFeatures: Option[Boolean] = Some(false), positiveLabels: String, numDownsampledNegLabels: Int ) extends VwJsonLike { require( 0 < numDownsampledNegLabels, s"numDownsampledNegLabels must be positive, found $numDownsampledNegLabels" ) } object VwDownsampledMultilabeledJson extends DefaultJsonProtocol { implicit val vwDownsampledMultilabeledJson: RootJsonFormat[VwDownsampledMultilabeledJson] = jsonFormat6(VwDownsampledMultilabeledJson.apply) }
Example 38
Source File: FileEntry.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.libraryservice import java.io.File import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, JsonFormat} import ai.deepsense.commons.json.EnumerationSerializer._ case class FileEntry private[libraryservice] ( name: String, kind: FileType.Value, children: Seq[FileEntry]) object FileEntry { def fromFile(file: java.io.File): FileEntry = { FileEntry(file.getName, FileType.File, Nil) } def fromDirectory(directory: java.io.File): FileEntry = { val sortedFiles = directory.listFiles.sortBy(f => (f.isFile, f.getName)) val children = sortedFiles.map(fileToFileEntry) FileEntry(directory.getName, FileType.Directory, children) } def fileToFileEntry(f: File): FileEntry = { if (f.isFile) { fromFile(f) } else { fromDirectory(f) } } } object FileType extends Enumeration { val File = Value("file") val Directory = Value("directory") implicit val fileTypeJsonFormat = jsonEnumFormat(FileType) } object FileEntryJsonProtocol extends DefaultJsonProtocol { implicit val fileEntryJsonFormat: JsonFormat[FileEntry] = lazyFormat(jsonFormat3(FileEntry.apply)) }
Example 39
Source File: Domain.scala From kafka-connect-tools with Apache License 2.0 | 5 votes |
package com.datamountaineer.connect.tools import spray.json.DefaultJsonProtocol object MyJsonProtocol extends DefaultJsonProtocol { implicit val task = jsonFormat2(Task) implicit val connectorinfo = jsonFormat3(ConnectorInfo) implicit val tasklessconnectorinfo = jsonFormat2(TasklessConnectorInfo) implicit val errormsg = jsonFormat2(ErrorMessage) implicit val connectorstatus = jsonFormat3(ConnectorStatus) implicit val taskstatus = jsonFormat4(TaskStatus) implicit val taskid = jsonFormat2(TaskId) implicit val taskinfo = jsonFormat2(TaskInfo) implicit val connectortaskstatus = jsonFormat3(ConnectorTaskStatus) implicit val connectorplugins = jsonFormat3(ConnectorPlugins) implicit val values = jsonFormat5(Values) implicit val definitions = jsonFormat9(Definition) implicit val configs = jsonFormat2(Configs) implicit val connectorpluginsvalidate = jsonFormat4(ConnectorPluginsValidate) }
Example 40
Source File: StatsEndpoint.scala From akka-kubernetes-tests with Apache License 2.0 | 5 votes |
package akka.kubernetes.soak import akka.actor.{ActorRef, ActorSystem} import akka.event.Logging import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.{Directives, Route} import spray.json.DefaultJsonProtocol import akka.pattern.ask import akka.util.Timeout import scala.concurrent.duration._ import scala.util.{Failure, Success} // collect your json format instances into a support trait: trait StatsJsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val testResultFormat = jsonFormat2(TestResult) implicit val testResultsFormat = jsonFormat7(TestResults) } class StatsEndpoint(system: ActorSystem, client: ActorRef) extends Directives with StatsJsonSupport { private implicit val askTimeout = Timeout(5.seconds) private val log = Logging(system, getClass) val route: Route = path("stats") { get { onComplete(client.ask(GetTestResults()).mapTo[TestResults]) { case Failure(t) => log.error(t, "Failed to get test results") complete(StatusCodes.InternalServerError) case Success(value) => complete(value) } } } }
Example 41
Source File: ValidationDirectivesSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.pattern.validation import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.http.scaladsl.server.directives.MethodDirectives import akka.http.scaladsl.testkit.ScalatestRouteTest import org.scalatest.{FunSpecLike, Matchers} import org.squbs.pattern.validation.ValidationDirectives.{validate => _} import spray.json.{DefaultJsonProtocol, RootJsonFormat} object MyJsonProtocol extends SprayJsonSupport with DefaultJsonProtocol { implicit val PersonFormat: RootJsonFormat[Person] = jsonFormat4(Person) } class ValidationDirectivesSpec extends FunSpecLike with Matchers with ScalatestRouteTest{ val ValidationPassed = "Validation Passed" import MyJsonProtocol._ import org.squbs.pattern.validation.SampleValidators._ val route: Route = (path("person") & MethodDirectives.post) { entity(as[Person]) { person => import ValidationDirectives._ validate(person) { complete(ValidationPassed) } } } describe("ValidationDirectives") { it(s"should return [$ValidationPassed] string with 200 for a valid content without middle name") { Post("/person", Person("John", "Smith", age = 25)) ~> route ~> check { status shouldEqual StatusCodes.OK responseAs[String] shouldEqual ValidationPassed } } it(s"should return [$ValidationPassed] string with 200 for a valid content with middle name") { Post("/person", Person("John", "Smith", Some("Mike"), age = 25)) ~> route ~> check { status shouldEqual StatusCodes.OK responseAs[String] shouldEqual ValidationPassed } } it("should reject with Last Name") { Post("/person", Person("John", "", age = 25)) ~> route ~> check { rejections shouldEqual List(ValidationRejection("Last Name")) } } it("should reject with middleName") { Post("/person", Person("John", "Smith", Some(""), age = 25)) ~> route ~> check { rejections shouldEqual List(ValidationRejection("middleName")) } } it("should reject with Last Name, middleName, age") { Post("/person", Person("John", "", Some(""), age = -1)) ~> route ~> check { rejections shouldEqual List(ValidationRejection("Last Name, middleName, age")) } } } }
Example 42
Source File: ApiData.scala From Neutrino with Apache License 2.0 | 5 votes |
package com.ebay.neutrino.api import java.util.Date import com.ebay.neutrino.config.CompletionStatus import com.ebay.neutrino.metrics.Metrics import spray.json.DefaultJsonProtocol trait ApiData { import com.ebay.neutrino.api.ApiData._ // Faked start time (should be moved to core) val apiStartTime = new Date() def generateStatus(): ApiData.Status = { import Metrics._ import nl.grons.metrics.scala.{Timer => TimerData} def toTimer(data: TimerData) = { val snapshot = data.snapshot Timer( data.count, snapshot.getMin, snapshot.getMax, snapshot.getMean, snapshot.get95thPercentile, data.oneMinuteRate, data.fiveMinuteRate, data.fifteenMinuteRate ) } Status( HostInfo( "localhost", "127.0.0.1", apiStartTime.toString ), Traffic( UpstreamBytesRead.count, UpstreamBytesWrite.count, UpstreamPacketsRead.count, UpstreamPacketsWrite.count, UpstreamOpen.count, UpstreamTotal.count ), Traffic( DownstreamBytesRead.count, DownstreamBytesWrite.count, DownstreamPacketsRead.count, DownstreamPacketsWrite.count, DownstreamOpen.count, DownstreamTotal.count ), Requests( // Sessions SessionActive.count, toTimer(Metrics.SessionDuration) ), Requests( // Requests RequestsOpen.count, toTimer(Metrics.RequestsCompleted) ), Seq( Responses("2xx", toTimer(Metrics.RequestsCompletedType(CompletionStatus.Status2xx))), Responses("4xx", toTimer(Metrics.RequestsCompletedType(CompletionStatus.Status4xx))), Responses("5xx", toTimer(Metrics.RequestsCompletedType(CompletionStatus.Status5xx))), Responses("incomplete", toTimer(Metrics.RequestsCompletedType(CompletionStatus.Incomplete))), Responses("other", toTimer(Metrics.RequestsCompletedType(CompletionStatus.Other))) ) ) } } object ApiData { // Supported metric types sealed trait MetricInt case class Metric(key: String, `type`: String, values: MetricInt) case class Counter(count: Long) extends MetricInt case class Timer(count: Long, min: Long, max: Long, mean: Double, `95th`: Double, `1min`: Double, `5min`: Double, `15min`: Double) extends MetricInt case class Meter(count: Long) extends MetricInt case class HostInfo(hostname: String, address: String, lastRestart: String) case class Traffic(bytesIn: Long, bytesOut: Long, packetsIn: Long, packetsOut: Long, currentConnections: Long, totalConnections: Long) //case class RequestStats(active: Long, total: Long, minElapsed: Long, avgElapsed: Long, lastRate: Long) case class Requests(active: Long, stats: Timer) case class Responses(responseType: String, stats: Timer) case class Status(host: HostInfo, upstreamTraffic: Traffic, downstreamTraffic: Traffic, sessions: Requests, requests: Requests, responses: Seq[Responses]) object JsonImplicits extends DefaultJsonProtocol { implicit val timerJson = jsonFormat8(Timer) implicit val hostinfoJson = jsonFormat3(HostInfo) implicit val trafficJson = jsonFormat6(Traffic) implicit val requestsJson = jsonFormat2(Requests) implicit val responsesJson = jsonFormat2(Responses) implicit val statusJson = jsonFormat6(Status) } }
Example 43
Source File: Domain.scala From fraud with Apache License 2.0 | 5 votes |
package fraud.main import org.apache.spark.mllib.linalg.Vectors import spray.json.DefaultJsonProtocol import java.util.UUID._ case class Transaction(id: String, user: String, receiver: String, amount: String, timestamp: String) object TransactionJsonProtocol extends DefaultJsonProtocol { implicit val TransactionFormat = jsonFormat5(Transaction) } object Domain { val receivers = Seq("Albert Hein", "E-Bay", "Power Company") val receiverIds = Map(receivers(0) -> 0, receivers(1) -> 1, receivers(2) -> 2) def features(t: Transaction) = Vectors.dense(receiverId(t), amountId(t)) def receiverId(t: Transaction): Int = receiverIds(t.receiver) def amountId(t: Transaction): Int = { val amount = t.amount.toDouble if (amount < 0.0) throw new IllegalArgumentException(s"Amount can not be negative, amount = $amount") if (amount < 1.00) 0 else if (amount < 100) 1 else 2 } } object RandomTransaction { val rnd = new scala.util.Random() def randomTransaction() = apply() def randomFraud() = Transaction(randomUUID.toString, randomUUID.toString, Domain.receivers(1), generateRandomAmount(0,0).toString, timestamp) def apply(): Transaction = Transaction(randomUUID.toString, randomUUID.toString, randomReceiver, randomAmount, timestamp) def randomReceiver() = Domain.receiverIds.keys.toSeq(rnd.nextInt(Domain.receiverIds.keys.size)) def randomAmount() = Seq(generateRandomAmount(0,0), generateRandomAmount(10,99), generateRandomAmount(100,999))(rnd.nextInt(3)).toString() def timestamp() = new java.util.Date().toString() private def generateRandomAmount(minBound : Int = 0, maxBound: Int): Double ={ minBound + (if(maxBound==0) 0 else rnd.nextInt(maxBound)) + (rnd.nextInt(99).toDouble /100) } def randomTransactions(size : Int) = { Array.fill[Transaction](size)(apply()) } }
Example 44
Source File: S3UpstreamWatcher.scala From shield with MIT License | 5 votes |
package shield.actors.config.upstream import java.util.concurrent.TimeUnit import akka.actor._ import com.typesafe.config.Config import shield.actors.RestartLogging import shield.actors.config.{ServiceDetails, UpstreamAggregatorMsgs} import shield.config._ import spray.http.Uri import shield.actors.config._ import shield.config.{ServiceLocation, ServiceType} import spray.json.DefaultJsonProtocol import scala.concurrent.duration._ import scala.language.postfixOps case class RawUpstreamService(serviceType: String, serviceLocation: String, weight: Option[Int]) object UpstreamServiceProtocol extends DefaultJsonProtocol { implicit val upstreamServiceHostFormat = jsonFormat3(RawUpstreamService) } class S3UpstreamWatcher(domainConfig: Config) extends Actor with ActorLogging with RestartLogging with JsonUpstreamUpdater with UpstreamWatcher { val system = akka.actor.ActorSystem("system") import system.dispatcher val config = domainConfig.getConfig("s3-upstream-watcher") val s3WatcherService = context.actorOf(Props( classOf[S3ObjectWatcher], config.getString("bucket-name"), config.getString("config-filename"))) val refreshInterval = Duration(config.getDuration("refresh-interval", TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS) var cancellable = system.scheduler.schedule( 0 seconds, refreshInterval, s3WatcherService, Refresh) override def postStop() = { cancellable.cancel() } } trait JsonUpstreamUpdater extends ActorLogging with UpstreamParser{ self: Actor => def receive: Receive = { case ChangedContents(contents) => import UpstreamServiceProtocol._ import spray.json._ try { val json = contents.parseJson log.debug(s"new parsed config ${json.compactPrint}") val upstreams : Map[ServiceLocation, ServiceDetails] = json.convertTo[List[RawUpstreamService]].map(raw => parseUpstreamEntry(raw.serviceType, raw.serviceLocation, raw.weight.getOrElse(1))).toMap context.parent ! UpstreamAggregatorMsgs.DiscoveredUpstreams(upstreams) } catch { case e: Throwable => log.warning(s"Error encountered while parsing json upstream config: $e") } } }
Example 45
Source File: GraphJsonTestSupport.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.models.json.graph import org.mockito.Mockito._ import org.scalatest.mockito.MockitoSugar import org.scalatest.{Matchers, WordSpec} import spray.json.{DefaultJsonProtocol, JsObject} import ai.deepsense.deeplang.DOperation import ai.deepsense.graph.Endpoint trait GraphJsonTestSupport extends WordSpec with MockitoSugar with DefaultJsonProtocol with Matchers { def assertEndpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Unit = { assert(edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString) assert(edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex) } def endpointMatchesJsObject(edgeEnd: Endpoint, edgeEndJs: JsObject): Boolean = { edgeEndJs.fields("nodeId").convertTo[String] == edgeEnd.nodeId.value.toString && edgeEndJs.fields("portIndex").convertTo[Int] == edgeEnd.portIndex } def mockOperation( inArity: Int, outArity: Int, id: DOperation.Id, name: String): DOperation = { val dOperation = mock[DOperation] when(dOperation.inArity).thenReturn(inArity) when(dOperation.outArity).thenReturn(outArity) when(dOperation.id).thenReturn(id) when(dOperation.name).thenReturn(name) dOperation } }
Example 46
Source File: WorkflowJsonParamsOverrider.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.workflowexecutor import spray.json.lenses.JsonLenses._ import spray.json.{DefaultJsonProtocol, JsValue} object WorkflowJsonParamsOverrider extends DefaultJsonProtocol { def overrideParams(workflow: JsValue, params: Map[String, String]): JsValue = { params.foldLeft(workflow)(overrideParam) } private def overrideParam(json: JsValue, param: (String, String)): JsValue = { val (key, value) = param val pathElems = key.split("\\.") val basePath = "workflow" / "nodes" / filter("id".is[String](_ == pathElems(0))) / "parameters" val path = pathElems.drop(1).foldLeft(basePath)((a, b) => a / b) json.update(path ! modify[String](_ => value)) } }
Example 47
Source File: ParamsSerialization.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.deeplang.doperables.serialization import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue} import ai.deepsense.deeplang.catalogs.doperable.exceptions.NoParameterlessConstructorInClassException import ai.deepsense.deeplang.params.Params import ai.deepsense.deeplang.{CatalogRecorder, ExecutionContext, TypeUtils} import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader trait ParamsSerialization { self: Params => def saveObjectWithParams(ctx: ExecutionContext, path: String): Unit = { saveMetadata(ctx, path) saveParams(ctx, path) } def loadAndSetParams(ctx: ExecutionContext, path: String): this.type = { setParams(loadParams(ctx, path), ctx.inferContext.graphReader) } protected def saveMetadata(ctx: ExecutionContext, path: String) = { val metadataFilePath = ParamsSerialization.metadataFilePath(path) val metadataJson = JsObject( ParamsSerialization.classNameKey -> JsString(this.getClass.getName) ) JsonObjectPersistence.saveJsonToFile(ctx, metadataFilePath, metadataJson) } protected def saveParams(ctx: ExecutionContext, path: String): Unit = { val paramsFilePath = ParamsSerialization.paramsFilePath(path) JsonObjectPersistence.saveJsonToFile(ctx, paramsFilePath, paramValuesToJson) } protected def loadParams(ctx: ExecutionContext, path: String): JsValue = { JsonObjectPersistence.loadJsonFromFile(ctx, ParamsSerialization.paramsFilePath(path)) } private def setParams(paramsJson: JsValue, graphReader: GraphReader): this.type = { this.set(paramPairsFromJson(paramsJson, graphReader): _*) } } object ParamsSerialization { val classNameKey = "className" val paramsFileName = "params" val metadataFileName = "metadata" def load(ctx: ExecutionContext, path: String): Loadable = { import DefaultJsonProtocol._ val metadataPath = metadataFilePath(path) val metadataJson: JsObject = JsonObjectPersistence.loadJsonFromFile(ctx, metadataPath).asJsObject val className = metadataJson.fields(classNameKey).convertTo[String] val clazz: Class[_] = Class.forName(className) val loadable = TypeUtils.createInstance(TypeUtils.constructorForClass(clazz) .getOrElse(throw new NoParameterlessConstructorInClassException(clazz.getCanonicalName)) ).asInstanceOf[Loadable] loadable.load(ctx, path) } def metadataFilePath(path: String): String = { PathsUtils.combinePaths(path, metadataFileName) } def paramsFilePath(path: String): String = { PathsUtils.combinePaths(path, paramsFileName) } }
Example 48
Source File: InnerWorkflowJsonProtocol.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.models.json.workflow import spray.httpx.SprayJsonSupport import spray.json.{DefaultJsonProtocol, JsValue} import ai.deepsense.commons.json.{DateTimeJsonProtocol, IdJsonProtocol} import ai.deepsense.deeplang.params.custom.{InnerWorkflow, PublicParam} import ai.deepsense.models.json.graph.GraphJsonProtocol.GraphReader import ai.deepsense.models.json.graph.{DKnowledgeJsonProtocol, NodeJsonProtocol, NodeStatusJsonProtocol} trait InnerWorkflowJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport with NodeJsonProtocol with NodeStatusJsonProtocol with DKnowledgeJsonProtocol with IdJsonProtocol with DateTimeJsonProtocol with GraphJsonProtocol { implicit val publicParamFormat = jsonFormat( PublicParam.apply, "nodeId", "paramName", "publicName") implicit val innerWorkflowFormat = jsonFormat( InnerWorkflow.apply, "workflow", "thirdPartyData", "publicParams") } trait WriteInnerWorkflowJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport with NodeJsonProtocol with NodeStatusJsonProtocol with DKnowledgeJsonProtocol with IdJsonProtocol with DateTimeJsonProtocol with WriteGraphJsonProtocol { implicit val publicParamFormat = jsonFormat( PublicParam.apply, "nodeId", "paramName", "publicName") implicit val innerWorkflowFormat = jsonFormat( InnerWorkflow.apply, "workflow", "thirdPartyData", "publicParams") } class InnerWorkflowJsonReader(override val graphReader: GraphReader) extends InnerWorkflowJsonProtocol { def toInner(jsValue: JsValue) = { jsValue.convertTo[InnerWorkflow] } } object InnerWorkflowJsonReader { def toInner(jsValue: JsValue, graphReader: GraphReader) = { val innerWorkflowJsonReader = new InnerWorkflowJsonReader(graphReader) innerWorkflowJsonReader.toInner(jsValue) } }
Example 49
Source File: WorkflowJsonProtocol.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.models.json.workflow import spray.httpx.SprayJsonSupport import spray.json.DefaultJsonProtocol import ai.deepsense.commons.exception.json.FailureDescriptionJsonProtocol import ai.deepsense.commons.json.{DateTimeJsonProtocol, EnumerationSerializer, IdJsonProtocol} import ai.deepsense.models.json.graph.{DKnowledgeJsonProtocol, NodeJsonProtocol, NodeStatusJsonProtocol} import ai.deepsense.models.workflows.{Workflow, WorkflowMetadata, WorkflowType} trait WorkflowJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport with NodeJsonProtocol with NodeStatusJsonProtocol with DKnowledgeJsonProtocol with ActionsJsonProtocol with IdJsonProtocol with FailureDescriptionJsonProtocol with DateTimeJsonProtocol with InferenceErrorJsonProtocol with InferenceWarningJsonProtocol with WorkflowInfoJsonProtocol with GraphJsonProtocol { implicit val workflowTypeFormat = EnumerationSerializer.jsonEnumFormat(WorkflowType) implicit val workflowMetadataFormat = jsonFormat(WorkflowMetadata, "type", "apiVersion") implicit val workflowFormat = jsonFormat(Workflow.apply, "metadata", "workflow", "thirdPartyData") }
Example 50
Source File: JsonMessageWithHeadersTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration.extractor import akka.camel.CamelMessage import akka.stream.integration.{ CamelMessageExtractor, HeadersExtractor, JsonCamelMessageExtractor, TestSpec } import spray.json.DefaultJsonProtocol import scalaz.Semigroup object JsonMessageWithHeadersTest extends DefaultJsonProtocol { case class MessageReceived(fileName: String, header: Option[String], timestamp: Long) implicit val messageReceivedJsonFormat = jsonFormat3(MessageReceived) // // JsonCamelMessage extractor can extract headers given an HeaderExtractor // and a Semigroup to merge the two MessageReceived case classes // implicit val messageReceivedHeaderExtractor = new HeadersExtractor[MessageReceived] { override def extract(in: Map[String, Any]): MessageReceived = MessageReceived("", in.get("ORDER_ID").map(_.toString), 0) } // what to do when merging the two messages? implicit val messageReceivedSemigroup = new Semigroup[MessageReceived] { override def append(f1: MessageReceived, f2: => MessageReceived): MessageReceived = f1.copy(header = f2.header) } implicit val jsonCamelMessageExtractor = JsonCamelMessageExtractor.jsonMessageExtractor[MessageReceived] } class JsonMessageWithHeadersTest extends TestSpec { import JsonMessageWithHeadersTest._ it should "extract a message with headers" in { val orderId = randomId val camelMessage = CamelMessage("""{"fileName":"test.txt", "timestamp":1}""", Map("ORDER_ID" -> orderId)) val extracted = implicitly[CamelMessageExtractor[MessageReceived]].extract(camelMessage) extracted shouldEqual MessageReceived("test.txt", Some(orderId), 1) } }
Example 51
Source File: PostcodeClient.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.webservices.postcode import akka.NotUsed import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.Materializer import akka.stream.scaladsl.Flow import com.github.dnvriend.component.webservices.generic.HttpClient import spray.json.DefaultJsonProtocol import scala.concurrent.{ ExecutionContext, Future } import scala.util.Try import scala.util.matching.Regex case class Address( street: String, houseNumber: Int, houseNumberAddition: String, postcode: String, city: String, municipality: String, province: String, rdX: Option[Int], rdY: Option[Int], latitude: Double, longitude: Double, bagNumberDesignationId: String, bagAddressableObjectId: String, addressType: String, purposes: Option[List[String]], surfaceArea: Int, houseNumberAdditions: List[String] ) trait Marshallers extends DefaultJsonProtocol { implicit val addressJsonFormat = jsonFormat17(Address) } case class GetAddressRequest(zip: String, houseNumber: String) trait PostcodeClient { def address(postcode: String, houseNumber: Int): Future[Option[Address]] def address[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetAddressRequest, T), (Option[Address], T), NotUsed] } object PostcodeClient { import spray.json._ val ZipcodeWithoutSpacePattern: Regex = """([1-9][0-9]{3})([A-Za-z]{2})""".r val ZipcodeWithSpacePattern: Regex = """([1-9][0-9]{3})[\s]([A-Za-z]{2})""".r def mapToAddress(json: String)(implicit reader: JsonReader[Address]): Option[Address] = Try(json.parseJson.convertTo[Address]).toOption def responseToString(resp: HttpResponse)(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Future[String] = HttpClient.responseToString(resp) def getAddressRequestFlow[T]: Flow[(GetAddressRequest, T), (HttpRequest, T), NotUsed] = Flow[(GetAddressRequest, T)].map { case (request, id) => (HttpClient.mkGetRequest(s"/rest/addresses/${request.zip}/${request.houseNumber}/"), id) } def mapResponseToAddressFlow[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, reader: JsonReader[Address]): Flow[(Try[HttpResponse], T), (Option[Address], T), NotUsed] = HttpClient.responseToString[T].map { case (json, id) => (mapToAddress(json), id) } def normalizeZipcode(zipcode: String): Option[String] = zipcode.toUpperCase match { case ZipcodeWithoutSpacePattern(numbers, letters) => Option(s"$numbers$letters") case ZipcodeWithSpacePattern(numbers, letters) => Option(s"$numbers$letters") case _ => None } def apply()(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter) = new PostcodeClientImpl } class PostcodeClientImpl()(implicit val system: ActorSystem, val mat: Materializer, val ec: ExecutionContext, val log: LoggingAdapter) extends PostcodeClient with Marshallers { import PostcodeClient._ private val client = HttpClient("postcode") override def address(postcode: String, houseNumber: Int): Future[Option[Address]] = normalizeZipcode(postcode) match { case Some(zip) => client.get(s"/rest/addresses/$zip/$houseNumber/") .flatMap(responseToString).map(mapToAddress) case None => Future.successful(None) } override def address[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetAddressRequest, T), (Option[Address], T), NotUsed] = getAddressRequestFlow[T] .via(client.cachedHostConnectionFlow[T]) .via(mapResponseToAddressFlow[T]) }
Example 52
Source File: LowLevelServer.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.lowlevelserver import akka.NotUsed import akka.actor.{ ActorSystem, Props } import akka.event.{ Logging, LoggingAdapter } import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.pattern.ask import akka.stream.scaladsl.{ Flow, Sink, Source } import akka.stream.{ ActorMaterializer, Materializer } import akka.util.Timeout import com.github.dnvriend.component.lowlevelserver.dto.{ Person, PersonWithId } import com.github.dnvriend.component.lowlevelserver.marshaller.Marshaller import com.github.dnvriend.component.lowlevelserver.repository.PersonRepository import spray.json.{ DefaultJsonProtocol, _ } import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, Future } class LowLevelServer(implicit val system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter, timeout: Timeout) extends DefaultJsonProtocol with Marshaller { val personDb = system.actorOf(Props[PersonRepository]) def debug(t: Any)(implicit log: LoggingAdapter = null): Unit = if (Option(log).isEmpty) println(t) else log.debug(t.toString) def http200Okay(req: HttpRequest): HttpResponse = HttpResponse(StatusCodes.OK) def http200AsyncOkay(req: HttpRequest): Future[HttpResponse] = Future(http200Okay(req)) val http200OkayFlow: Flow[HttpRequest, HttpResponse, NotUsed] = Flow[HttpRequest].map { req => HttpResponse(StatusCodes.OK) } val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] = Http().bind(interface = "localhost", port = 8080) val binding: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { conn => // conn.handleWith(http200OkayFlow) // conn.handleWithSyncHandler(http200Okay) // conn.handleWithAsyncHandler(http200AsyncOkay, 8) conn.handleWithAsyncHandler(personRequestHandler) }).run() def personRequestHandler(req: HttpRequest): Future[HttpResponse] = req match { case HttpRequest(HttpMethods.GET, Uri.Path("/api/person"), _, _, _) => for { xs <- (personDb ? "findAll").mapTo[List[PersonWithId]] entity = HttpEntity(ContentTypes.`application/json`, xs.toJson.compactPrint) } yield HttpResponse(StatusCodes.OK, entity = entity) case HttpRequest(HttpMethods.POST, Uri.Path("/api/person"), _, ent, _) => for { strictEntity <- ent.toStrict(1.second) person <- (personDb ? strictEntity.data.utf8String.parseJson.convertTo[Person]).mapTo[PersonWithId] } yield HttpResponse(StatusCodes.OK, entity = person.toJson.compactPrint) case req => req.discardEntityBytes() Future.successful(HttpResponse(StatusCodes.NotFound)) } } object LowLevelServerLauncher extends App with DefaultJsonProtocol { // setting up some machinery implicit val system: ActorSystem = ActorSystem() implicit val mat: Materializer = ActorMaterializer() implicit val ec: ExecutionContext = system.dispatcher implicit val log: LoggingAdapter = Logging(system, this.getClass) implicit val timeout: Timeout = Timeout(10.seconds) new LowLevelServer() }
Example 53
Source File: JsonSupport.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch14 import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import ch14.Commands._ import ch14.Events._ import spray.json.{DefaultJsonProtocol, RootJsonFormat} import stamina.StaminaAkkaSerializer import stamina.json._ trait JsonSupport extends SprayJsonSupport { import DefaultJsonProtocol._ type RJF[T] = RootJsonFormat[T] implicit val createArticleJF: RJF[CreateArticle] = jsonFormat2(CreateArticle) implicit val deleteArticleJF: RJF[DeleteArticle] = jsonFormat1(DeleteArticle) implicit val purchaseJF: RJF[PurchaseArticles] = jsonFormat1(PurchaseArticles) implicit val restockJF: RJF[RestockArticles] = jsonFormat1(RestockArticles) implicit val createdJF: RJF[ArticleCreated] = jsonFormat2(ArticleCreated) implicit val deletedJF: RJF[ArticleDeleted] = jsonFormat1(ArticleDeleted) implicit val pJF: RJF[ArticlesPurchased] = jsonFormat1(ArticlesPurchased) implicit val reJF: RJF[ArticlesRestocked] = jsonFormat1(ArticlesRestocked) implicit val invJF: RJF[Inventory] = jsonFormat1(Inventory) } object PersistenceSupport extends JsonSupport { val v1createdP = persister[ArticleCreated]("article-created") val v1deletedP = persister[ArticleDeleted]("article-deleted") val v1purchasedP = persister[ArticlesPurchased]("articles-purchased") val v1restockedP = persister[ArticlesRestocked]("articles-restocked") val v1inventoryP = persister[Inventory]("inventory") } import PersistenceSupport._ class EventSerializer extends StaminaAkkaSerializer(v1createdP, v1deletedP, v1purchasedP, v1restockedP, v1inventoryP)
Example 54
Source File: SchedoscopeJsonDataFormat.scala From schedoscope with Apache License 2.0 | 5 votes |
package org.schedoscope.scheduler.rest import org.schedoscope.scheduler.service._ import spray.json.{DefaultJsonProtocol, JsonFormat} object SchedoscopeJsonDataFormat extends DefaultJsonProtocol { implicit val runStatusFormat = jsonFormat5(RunStatus) implicit val actionStatusFormat = jsonFormat5(TransformationStatus) implicit val actionStatusListFormat = jsonFormat2(TransformationStatusList) implicit val viewTransformationStatusFormat: JsonFormat[ViewTransformationStatus] = lazyFormat(jsonFormat2(ViewTransformationStatus)) implicit val viewStatusFormat: JsonFormat[ViewStatus] = lazyFormat(jsonFormat15(ViewStatus)) implicit val fieldStatusFormat: JsonFormat[FieldStatus] = lazyFormat(jsonFormat3(FieldStatus)) implicit val viewStatusListFormat = jsonFormat2(ViewStatusList) implicit val queueStatusListFormat = jsonFormat2(QueueStatusList) }
Example 55
Source File: ControlInterface.scala From changestream with MIT License | 5 votes |
package changestream.actors import akka.util.Timeout import spray.httpx.SprayJsonSupport._ import spray.routing._ import akka.actor._ import ch.qos.logback.classic.Level import changestream.{ChangeStream, ChangeStreamEventDeserializer, ChangeStreamEventListener} import org.slf4j.LoggerFactory import ch.qos.logback.classic.Logger import spray.http.StatusCodes import spray.routing.HttpService import spray.json.DefaultJsonProtocol import scala.concurrent.Await import scala.concurrent.duration._ import scala.language.postfixOps class ControlInterfaceActor extends Actor with ControlInterface { def actorRefFactory = context def receive = runRoute(controlRoutes) } trait ControlInterface extends HttpService with DefaultJsonProtocol { import ControlActor._ protected val log = LoggerFactory.getLogger(getClass) // yes this is backward on purpose implicit val memoryInfoFormat = jsonFormat3(MemoryInfo) implicit val statusFormat = jsonFormat7(Status) implicit def executionContext = actorRefFactory.dispatcher implicit val timeout = Timeout(10 seconds) def controlRoutes: Route = { get { pathSingleSlash { detach() { complete(getStatus) } } ~ path("status") { detach() { complete(getStatus) } } ~ path("logs") { parameter('level) { level => setLogLevel(level) } } } } def setLogLevel(level: String) = { level.toLowerCase match { case "all" | "trace" | "debug" | "info" | "warn" | "error" | "off" => val rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).asInstanceOf[Logger] rootLogger.setLevel(Level.toLevel(level)) complete("ChangeStream logging level has been set to {}.", level) case _ => log.error("ControlActor received invalid log level {}.", level) complete(StatusCodes.BadRequest, s"Invalid log level: ${level}") } } def getStatus = { val storedPosition = Await.result(ChangeStreamEventListener.getStoredPosition, 60 seconds) Status( server = ChangeStream.serverName, clientId = ChangeStream.clientId, isConnected = ChangeStream.isConnected, binlogClientPosition = ChangeStreamEventListener.getCurrentPosition, lastStoredPosition = storedPosition.getOrElse(""), binlogClientSequenceNumber = ChangeStreamEventDeserializer.getCurrentSequenceNumber, memoryInfo = MemoryInfo( Runtime.getRuntime().totalMemory(), Runtime.getRuntime().maxMemory(), Runtime.getRuntime().freeMemory() ) ) } } object ControlActor { case class Status( server: String, clientId: Long, isConnected: Boolean, binlogClientPosition: String, lastStoredPosition: String, binlogClientSequenceNumber: Long, memoryInfo: MemoryInfo ) case class MemoryInfo(heapSize: Long, maxHeap: Long, freeHeap: Long) }
Example 56
Source File: WhiskTrigger.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.entity import java.time.Instant import spray.json.DefaultJsonProtocol import org.apache.openwhisk.core.database.DocumentFactory import spray.json._ def removeRule(rule: FullyQualifiedEntityName) = { copy(rules = rules.map(_ - rule)).revision[WhiskTrigger](docinfo.rev) } } object ReducedRule extends DefaultJsonProtocol { private implicit val fqnSerdes = FullyQualifiedEntityName.serdes implicit val serdes = jsonFormat2(ReducedRule.apply) } object WhiskTrigger extends DocumentFactory[WhiskTrigger] with WhiskEntityQueries[WhiskTrigger] with DefaultJsonProtocol { import WhiskActivation.instantSerdes override val collectionName = "triggers" private implicit val fqnSerdesAsDocId = FullyQualifiedEntityName.serdesAsDocId override implicit val serdes = jsonFormat9(WhiskTrigger.apply) override val cacheEnabled = true } object WhiskTriggerPut extends DefaultJsonProtocol { implicit val serdes = jsonFormat5(WhiskTriggerPut.apply) }
Example 57
Source File: Limits.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.entity import scala.util.Try import spray.json.JsValue import spray.json.RootJsonFormat import spray.json.deserializationError import spray.json.DefaultJsonProtocol protected[core] case class TriggerLimits protected[core] () extends Limits { override protected[entity] def toJson: JsValue = TriggerLimits.serdes.write(this) } protected[core] object ActionLimits extends ArgNormalizer[ActionLimits] with DefaultJsonProtocol { override protected[core] implicit val serdes = new RootJsonFormat[ActionLimits] { val helper = jsonFormat4(ActionLimits.apply) def read(value: JsValue) = { val obj = Try { value.asJsObject.convertTo[Map[String, JsValue]] } getOrElse deserializationError("no valid json object passed") val time = TimeLimit.serdes.read(obj.get("timeout") getOrElse deserializationError("'timeout' is missing")) val memory = MemoryLimit.serdes.read(obj.get("memory") getOrElse deserializationError("'memory' is missing")) val logs = obj.get("logs") map { LogLimit.serdes.read(_) } getOrElse LogLimit() val concurrency = obj.get("concurrency") map { ConcurrencyLimit.serdes.read(_) } getOrElse ConcurrencyLimit() ActionLimits(time, memory, logs, concurrency) } def write(a: ActionLimits) = helper.write(a) } } protected[core] object TriggerLimits extends ArgNormalizer[TriggerLimits] with DefaultJsonProtocol { override protected[core] implicit val serdes = jsonFormat0(TriggerLimits.apply _) }
Example 58
Source File: CacheKey.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.entity import spray.json.DefaultJsonProtocol class UnsupportedCacheKeyTypeException(msg: String) extends Exception(msg) case class CacheKey(mainId: String, secondaryId: Option[String]) { override def toString() = { s"CacheKey($mainId)" } } object CacheKey extends DefaultJsonProtocol { implicit val serdes = jsonFormat2(CacheKey.apply) def apply(key: Any): CacheKey = { key match { case e: EntityName => CacheKey(e.asString, None) case a: BasicAuthenticationAuthKey => CacheKey(a.uuid.asString, Some(a.key.asString)) case d: DocInfo => { val revision = if (d.rev.empty) None else Some(d.rev.asString) CacheKey(d.id.asString, revision) } case w: WhiskEntity => CacheKey(w.docid.asDocInfo) case s: String => CacheKey(s, None) case others => { throw new UnsupportedCacheKeyTypeException(s"Unable to apply the entity ${others.getClass} on CacheKey.") } } } }
Example 59
Source File: WhiskChangeEventObserverTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.cosmosdb.cache import com.azure.data.cosmos.CosmosItemProperties import common.StreamLogging import org.apache.openwhisk.core.database.CacheInvalidationMessage import org.apache.openwhisk.core.entity.CacheKey import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.{FlatSpec, Matchers} import spray.json.DefaultJsonProtocol import scala.collection.immutable.Seq @RunWith(classOf[JUnitRunner]) class WhiskChangeEventObserverTests extends FlatSpec with Matchers with StreamLogging { import WhiskChangeEventObserver.instanceId behavior of "CosmosDB extract LSN from Session token" it should "parse old session token" in { WhiskChangeEventObserver.getSessionLsn("0:12345") shouldBe 12345 } it should "parse new session token" in { WhiskChangeEventObserver.getSessionLsn("0:-1#12345") shouldBe 12345 } it should "parse new session token with multiple regional lsn" in { WhiskChangeEventObserver.getSessionLsn("0:-1#12345#Region1=1#Region2=2") shouldBe 12345 } behavior of "CosmosDB feed events" it should "generate cache events" in { val config = InvalidatorConfig(8080, None) val docs = Seq(createDoc("foo"), createDoc("bar")) val processedDocs = WhiskChangeEventObserver.processDocs(docs, config) processedDocs.map(CacheInvalidationMessage.parse(_).get) shouldBe Seq( CacheInvalidationMessage(CacheKey("foo"), instanceId), CacheInvalidationMessage(CacheKey("bar"), instanceId)) } it should "filter clusterId" in { val config = InvalidatorConfig(8080, Some("cid1")) val docs = Seq(createDoc("foo", Some("cid2")), createDoc("bar", Some("cid1")), createDoc("baz")) val processedDocs = WhiskChangeEventObserver.processDocs(docs, config) //Should not include bar as the clusterId matches processedDocs.map(CacheInvalidationMessage.parse(_).get) shouldBe Seq( CacheInvalidationMessage(CacheKey("foo"), instanceId), CacheInvalidationMessage(CacheKey("baz"), instanceId)) } private def createDoc(id: String, clusterId: Option[String] = None): CosmosItemProperties = { val cdoc = CosmosDBDoc(id, clusterId) val json = CosmosDBDoc.seredes.write(cdoc).compactPrint new CosmosItemProperties(json) } case class CosmosDBDoc(id: String, _clusterId: Option[String], _lsn: Int = 42) object CosmosDBDoc extends DefaultJsonProtocol { implicit val seredes = jsonFormat3(CosmosDBDoc.apply) } }
Example 60
Source File: JsonMessageBuilderWithoutHeadersTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration.builder import akka.camel.CamelMessage import akka.stream.integration.{ CamelMessageBuilder, JsonCamelMessageBuilder, TestSpec } import spray.json.{ DefaultJsonProtocol, _ } object JsonMessageBuilderWithoutHeadersTest extends DefaultJsonProtocol { case class MessageReceived(fileName: String, timestamp: Long) implicit val messageReceivedJsonFormat = jsonFormat2(MessageReceived) implicit val messageReceivedJsonCamelMessageBuilder = JsonCamelMessageBuilder.jsonMessageBuilder[MessageReceived] } class JsonMessageBuilderWithoutHeadersTest extends TestSpec { import JsonMessageBuilderWithoutHeadersTest._ it should "build a CamelMessage without headers" in { val msg = MessageReceived("fileName.txt", 1) val camelMessage = implicitly[CamelMessageBuilder[MessageReceived]].build(msg) camelMessage shouldEqual CamelMessage(msg.toJson.compactPrint, Map.empty[String, Any]) } }
Example 61
Source File: JsonMessageBuilderWithHeaders.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration.builder import akka.camel.CamelMessage import akka.stream.integration.{ CamelMessageBuilder, HeadersBuilder, JsonCamelMessageBuilder, TestSpec } import spray.json.DefaultJsonProtocol object JsonMessageBuilderWithHeaders extends DefaultJsonProtocol { case class MessageReceived(fileName: String, orderId: Option[String], timestamp: Long) implicit val messageReceivedJsonFormat = jsonFormat3(MessageReceived) // // JsonCamelMessageBuilder can optionally add headers to the CamelMessage // that it extracts from the case class ie. MessageReceived // implicit val messageReceivedHeadersBuilder = new HeadersBuilder[MessageReceived] { override def build(in: MessageReceived): Map[String, Any] = Map.empty[String, Any] ++ in.orderId.map("ORDER_ID" -> _) } implicit val messageReceivedJsonCamelMessageBuilder = JsonCamelMessageBuilder.jsonMessageBuilder[MessageReceived] } class JsonMessageBuilderWithHeaders extends TestSpec { import JsonMessageBuilderWithHeaders._ it should "build a CamelMessage with headers" in { val orderId = randomId val msg = MessageReceived("fileName.txt", Option(orderId), 1) val camelMessage = implicitly[CamelMessageBuilder[MessageReceived]].build(msg) camelMessage shouldEqual CamelMessage(msg.toJson.compactPrint, Map("ORDER_ID" -> orderId)) } }
Example 62
Source File: WeatherClient.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.webservices.weather import akka.NotUsed import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.stream.Materializer import akka.stream.scaladsl.Flow import com.github.dnvriend.component.webservices.generic.HttpClient import spray.json.DefaultJsonProtocol import scala.concurrent.{ ExecutionContext, Future } import scala.util.Try case class Wind(speed: Double, deg: Double) case class Main(temp: Double, temp_min: Double, temp_max: Double, pressure: Double, sea_level: Option[Double], grnd_level: Option[Double], humidity: Int) case class Cloud(all: Int) case class Weather(id: Int, main: String, description: String, icon: String) case class Sys(message: Double, country: String, sunrise: Long, sunset: Long) case class Coord(lon: Double, lat: Double) case class WeatherResult(coord: Coord, sys: Sys, weather: List[Weather], base: String, main: Main, wind: Wind, clouds: Cloud, dt: Long, id: Int, name: String, cod: Int) trait Marshallers extends DefaultJsonProtocol { implicit val windJsonFormat = jsonFormat2(Wind) implicit val mainJsonFormat = jsonFormat7(Main) implicit val cloudJsonFormat = jsonFormat1(Cloud) implicit val weatherJsonFormat = jsonFormat4(Weather) implicit val sysJsonFormat = jsonFormat4(Sys) implicit val coordJsonFormat = jsonFormat2(Coord) implicit val weatherResultJsonFormat = jsonFormat11(WeatherResult) } case class GetWeatherRequest(zip: String, country: String) trait OpenWeatherApi { def getWeather(zip: String, country: String): Future[Option[WeatherResult]] def getWeather[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetWeatherRequest, T), (Option[WeatherResult], T), NotUsed] } object OpenWeatherApi { import spray.json._ def apply()(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, log: LoggingAdapter) = new OpenWeatherApiImpl def mapResponseToWeatherResult(json: String)(implicit reader: JsonReader[WeatherResult]): Option[WeatherResult] = Try(json.parseJson.convertTo[WeatherResult]).toOption def responseToString(resp: HttpResponse)(implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Future[String] = HttpClient.responseToString(resp) def getWeatherRequestFlow[T]: Flow[(GetWeatherRequest, T), (HttpRequest, T), NotUsed] = Flow[(GetWeatherRequest, T)].map { case (request, id) => (HttpClient.mkGetRequest(s"/data/2.5/weather?zip=${request.zip},${request.country}"), id) } def mapResponseToWeatherResultFlow[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext, reader: JsonReader[WeatherResult]): Flow[(Try[HttpResponse], T), (Option[WeatherResult], T), NotUsed] = HttpClient.responseToString[T].map { case (json, id) => (mapResponseToWeatherResult(json), id) } } class OpenWeatherApiImpl()(implicit val system: ActorSystem, val ec: ExecutionContext, val mat: Materializer, val log: LoggingAdapter) extends OpenWeatherApi with Marshallers { import OpenWeatherApi._ private val client = HttpClient("weather") override def getWeather(zip: String, country: String): Future[Option[WeatherResult]] = client.get(s"/data/2.5/weather?zip=$zip,$country"). flatMap(responseToString) .map(mapResponseToWeatherResult) override def getWeather[T](implicit system: ActorSystem, mat: Materializer, ec: ExecutionContext): Flow[(GetWeatherRequest, T), (Option[WeatherResult], T), NotUsed] = getWeatherRequestFlow[T] .via(client.cachedHostConnectionFlow[T]) .via(mapResponseToWeatherResultFlow[T]) }
Example 63
Source File: DropwizardMarshallersSpec.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.dropwizard.marshalling import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.testkit.ScalatestRouteTest import fr.davit.akka.http.metrics.core.HttpMetricsRegistry.StatusGroupDimension import fr.davit.akka.http.metrics.core.scaladsl.server.HttpMetricsDirectives._ import fr.davit.akka.http.metrics.dropwizard.DropwizardRegistry import org.scalatest.BeforeAndAfterAll import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import spray.json.{DefaultJsonProtocol, JsValue} import scala.concurrent.duration._ class DropwizardMarshallersSpec extends AnyFlatSpec with Matchers with ScalatestRouteTest with BeforeAndAfterAll { private case class JsonResponse(metrics: Map[String, JsValue]) private trait Fixture extends SprayJsonSupport with DefaultJsonProtocol with DropwizardMarshallers { implicit val metricsFormat = jsonFormat1(JsonResponse) val registry = DropwizardRegistry() registry.underlying.counter("other.metric") } override def afterAll(): Unit = { cleanUp() super.afterAll() } "DropwizardMarshallers" should "expose metrics as json format" in new Fixture { // use metrics so they appear in the report val dimensions = Seq(StatusGroupDimension(StatusCodes.OK)) registry.requests.inc() registry.receivedBytes.update(10) registry.active.inc() registry.responses.inc(dimensions) registry.errors.inc() registry.duration.observe(1.second, dimensions) registry.sentBytes.update(10) Get() ~> metrics(registry) ~> check { val json = responseAs[JsonResponse] // println(json) json.metrics.keys should contain theSameElementsAs Seq( "akka.http.requests.active", "akka.http.requests", "akka.http.requests.bytes", "akka.http.responses{status=2xx}", "akka.http.responses.errors", "akka.http.responses.duration{status=2xx}", "akka.http.responses.bytes", "other.metric" ).toSet } } }
Example 64
Source File: JsonSupport.scala From darwin with Apache License 2.0 | 5 votes |
package it.agilelab.darwin.server.rest import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import org.apache.avro.Schema import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, JsonParser, PrettyPrinter, RootJsonFormat} trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val printer: PrettyPrinter.type = PrettyPrinter implicit val schemaFormat: RootJsonFormat[Schema] = new RootJsonFormat[Schema] { override def write(obj: Schema): JsValue = JsonParser(obj.toString(true)) override def read(json: JsValue): Schema = new Schema.Parser().parse(json.prettyPrint) } implicit val schemaWithIdFormat: RootJsonFormat[(Long, Schema)] = new RootJsonFormat[(Long, Schema)] { override def write(obj: (Long, Schema)): JsValue = JsObject(Map( "id" -> JsString(obj._1.toString), "schema" -> schemaFormat.write(obj._2) )) override def read(json: JsValue): (Long, Schema) = json match { case JsObject(fields) => val id = fields.get("id") match { case Some(JsString(number)) => number case _ => throw new Exception("Id field should be a long") } val schema = fields.get("schema") match { case Some(x@JsObject(_)) => x case _ => throw new Exception("schema should be an object") } (id.toLong, schemaFormat.read(schema)) case _ => throw new Exception("should be an object") } } }
Example 65
Source File: Candle.scala From scalanda with MIT License | 5 votes |
package com.msilb.scalanda.restapi.model import java.time.ZonedDateTime import com.msilb.scalanda.common.util.DateUtils._ import spray.json.DefaultJsonProtocol sealed trait Candle { def time: ZonedDateTime def volume: Int def complete: Boolean } object Candle { case class MidPointBasedCandle(time: ZonedDateTime, openMid: Double, highMid: Double, lowMid: Double, closeMid: Double, volume: Int, complete: Boolean) extends Candle case class BidAskBasedCandle(time: ZonedDateTime, openBid: Double, highBid: Double, lowBid: Double, closeBid: Double, openAsk: Double, highAsk: Double, lowAsk: Double, closeAsk: Double, volume: Int, complete: Boolean) extends Candle object CandleJsonProtocol extends DefaultJsonProtocol { implicit val midPointBasedCandleFormat = jsonFormat7(MidPointBasedCandle) implicit val bidAskBasedCandleFormat = jsonFormat11(BidAskBasedCandle) } }
Example 66
Source File: RecommenderController.scala From spark_recommender with Apache License 2.0 | 5 votes |
package es.alvsanand.spark_recommender.recommender import java.net.InetAddress import akka.actor.ActorSystem import com.mongodb.casbah.{MongoClient, MongoClientURI} import es.alvsanand.spark_recommender.model._ import es.alvsanand.spark_recommender.utils.{ESConfig, Logging, MongoConfig} import org.elasticsearch.common.settings.Settings import org.elasticsearch.common.transport.InetSocketTransportAddress import org.elasticsearch.transport.client.PreBuiltTransportClient import spray.httpx.SprayJsonSupport import spray.json.{DefaultJsonProtocol, NullOptions} import spray.routing.SimpleRoutingApp object RecommenderControllerProtocol extends DefaultJsonProtocol with NullOptions with SprayJsonSupport { implicit val productRecommendationRequestFormat = jsonFormat1(ProductRecommendationRequest) implicit val userRecommendationRequestFormat = jsonFormat1(UserRecommendationRequest) implicit val searchRecommendationRequestFormat = jsonFormat1(SearchRecommendationRequest) implicit val productHybridRecommendationRequestFormat = jsonFormat1(ProductHybridRecommendationRequest) implicit val recommendationFormat = jsonFormat2(Recommendation) implicit val hybridRecommendationFormat = jsonFormat3(HybridRecommendation) } object RecommenderController extends SimpleRoutingApp with Logging{ val ES_HOST_PORT_REGEX = "(.+):(\\d+)".r import RecommenderControllerProtocol._ implicit val system = ActorSystem("ActorSystem") def run(serverPort: Int)(implicit mongoConf: MongoConfig, esConf: ESConfig): Unit = { implicit val mongoClient = MongoClient(MongoClientURI(mongoConf.uri)) implicit val esClient = new PreBuiltTransportClient(Settings.EMPTY) esConf.transportHosts.split(";") .foreach { case ES_HOST_PORT_REGEX(host: String, port: String) => esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port.toInt)) } logger.info("Launching REST serves[port=%d]".format(serverPort)) startServer(interface = "localhost", port = serverPort) { path("recs" / "cf" / "pro") { post( entity(as[ProductRecommendationRequest]) { request => complete { RecommenderService.getCollaborativeFilteringRecommendations(request).toStream } } ) } ~ path("recs" / "cf" / "usr") { post( entity(as[UserRecommendationRequest]) { request => complete { RecommenderService.getCollaborativeFilteringRecommendations(request).toStream } } ) } ~ path("recs" / "cb" / "mrl") { post( entity(as[ProductRecommendationRequest]) { request => complete { RecommenderService.getContentBasedMoreLikeThisRecommendations(request).toStream } } ) } ~ path("recs" / "cb" / "sch") { post( entity(as[SearchRecommendationRequest]) { request => complete { RecommenderService.getContentBasedSearchRecommendations(request).toStream } } ) } ~ path("recs" / "hy" / "pro") { post( entity(as[ProductHybridRecommendationRequest]) { request => complete { RecommenderService.getHybridRecommendations(request).toStream } } ) } } } }
Example 67
Source File: HttpRequestConversionSupport.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.persistence.serializers import java.net.InetAddress import akka.http.scaladsl.model.HttpHeader.ParsingResult import akka.http.scaladsl.model.{ HttpEntity, HttpHeader, HttpMethod, HttpMethods, HttpProtocol, HttpRequest, RemoteAddress, Uri } import com.ing.wbaa.rokku.proxy.data.{ UserAssumeRole, UserRawJson } import spray.json.DefaultJsonProtocol import scala.collection.immutable trait HttpRequestConversionSupport extends DefaultJsonProtocol { case class SimplifiedRemoteAddress(host: String) { def toRemoteAddr: RemoteAddress = { val a = host.split(":") RemoteAddress(InetAddress.getByName(a(0)), Some(a(1).toInt)) } } case class SimplifiedHttpRequest(method: String, uri: String, headers: List[String], entity: String, httpProtocol: String) implicit val httpRequestF = jsonFormat5(SimplifiedHttpRequest) implicit val userRoleF = jsonFormat1(UserAssumeRole) implicit val userSTSF = jsonFormat5(UserRawJson) implicit val remoteAddressF = jsonFormat1(SimplifiedRemoteAddress) private[persistence] def convertAkkaHeadersToStrings(headers: Seq[HttpHeader]): List[String] = headers.map(h => s"${h.name()}=${h.value()}").toList private def convertStringsToAkkaHeaders(headers: List[String]): immutable.Seq[HttpHeader] = headers.map { p => val kv = p.split("=") HttpHeader.parse(kv(0), kv(1)) match { case ParsingResult.Ok(header, _) => header case ParsingResult.Error(error) => throw new Exception(s"Unable to convert to HttpHeader: ${error.summary}") } } private def httpMethodFrom(m: String): HttpMethod = m match { case "GET" => HttpMethods.GET case "HEAD" => HttpMethods.HEAD case "PUT" => HttpMethods.PUT case "POST" => HttpMethods.POST case "DELETE" => HttpMethods.DELETE } private[persistence] def toAkkaHttpRequest(s: SimplifiedHttpRequest): HttpRequest = HttpRequest( httpMethodFrom(s.method), Uri(s.uri), convertStringsToAkkaHeaders(s.headers), HttpEntity(s.entity), HttpProtocol(s.httpProtocol) ) }
Example 68
Source File: AWSMessageEvent.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.data import java.time.Instant import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.{ HttpMethod, StatusCode } import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.{ AWSRequestType, MultipartRequestType } import com.ing.wbaa.rokku.proxy.provider.aws.{ S3ObjectAction, s3MultipartUpload, s3MultipartUploadComplete } import spray.json.DefaultJsonProtocol case class Records(records: List[AWSMessageEvent]) case class UserIdentity(principalId: String) case class RequestParameters(sourceIPAddress: String) case class ResponseElements(`x-amz-request-id`: String, `x-amz-id-2`: String) case class OwnerIdentity(principalId: String) case class BucketProps(name: String, ownerIdentity: OwnerIdentity, arn: String) case class ObjectProps(key: String, size: Int, eTag: String, versionId: String, sequencer: String) case class S3(s3SchemaVersion: String, configurationId: String, bucket: BucketProps, `object`: ObjectProps) case class AWSMessageEvent( eventVersion: String, eventSource: String, awsRegion: String, eventTime: String, eventName: String, userIdentity: UserIdentity, requestParameters: RequestParameters, responseElements: ResponseElements, s3: S3 ) trait AWSMessageEventJsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val ownerIdentityFormat = jsonFormat1(OwnerIdentity) implicit val bucketFormat = jsonFormat3(BucketProps) implicit val objectPropsFormat = jsonFormat5(ObjectProps) implicit val s3Format = jsonFormat4(S3) implicit val userIdentityFormat = jsonFormat1(UserIdentity) implicit val requestParametersFormat = jsonFormat1(RequestParameters) implicit val responseElementsFormat = jsonFormat2(ResponseElements) implicit val AWSMessageEventFormat = jsonFormat9(AWSMessageEvent) implicit val recordsFormat = jsonFormat1(Records) import spray.json._ def prepareAWSMessage(s3Request: S3Request, method: HttpMethod, principalId: String, userIPs: UserIps, s3Action: S3ObjectAction, requestId: RequestId, responseStatus: StatusCode, awsRequest: AWSRequestType): Option[JsValue] = { val toMultipartRequest: AWSRequestType => Option[MultipartRequestType] = { case r: MultipartRequestType => Some(r) case _ => None } val uploadId: Option[String] = toMultipartRequest(awsRequest).map(_.uploadId) val multipartOrS3Action = toMultipartRequest(awsRequest) match { case Some(r) => if (r.completeMultipartUpload) s3MultipartUploadComplete(method.value) else s3MultipartUpload(method.value) case None => s3Action } for { bucketPath <- s3Request.s3BucketPath s3object <- s3Request.s3Object } yield Records(List(AWSMessageEvent( "2.1", "rokku:s3", "us-east-1", Instant.now().toString, multipartOrS3Action.value, UserIdentity(principalId), RequestParameters(userIPs.toString), ResponseElements(requestId.value, responseStatus.value), S3("1.0", "", BucketProps(bucketPath, OwnerIdentity(""), ""), ObjectProps(s3object, 0, "", "", uploadId.getOrElse(""))))) ).toJson } }
Example 69
Source File: KafkaMetricsSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.transport import akka.actor.ActorSystem import akka.testkit.TestKit import com.typesafe.config.ConfigFactory import hydra.core.transport.AckStrategy import hydra.kafka.producer.KafkaRecordMetadata import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig} import org.scalatest.matchers.should.Matchers import org.scalatest.funspec.AnyFunSpecLike import org.scalatest.BeforeAndAfterAll import spray.json.DefaultJsonProtocol class KafkaMetricsSpec extends TestKit(ActorSystem("hydra")) with Matchers with AnyFunSpecLike with BeforeAndAfterAll with DefaultJsonProtocol { import KafkaRecordMetadata._ implicit val config = EmbeddedKafkaConfig( kafkaPort = 8092, zooKeeperPort = 3181, customBrokerProperties = Map( "auto.create.topics.enable" -> "false", "offsets.topic.replication.factor" -> "1" ) ) override def afterAll() = { super.afterAll() EmbeddedKafka.stop() TestKit.shutdownActorSystem(system, verifySystemShutdown = true) } override def beforeAll() = { super.beforeAll() EmbeddedKafka.start() EmbeddedKafka.createCustomTopic("metrics_topic") } describe("When using the KafkaMetrics object") { it("uses the NoOpMetrics") { KafkaMetrics(ConfigFactory.empty()) shouldBe NoOpMetrics KafkaMetrics( ConfigFactory.parseString("transports.kafka.metrics.enabled=false") ) shouldBe NoOpMetrics } it("uses the PublishMetrics") { import spray.json._ val cfg = ConfigFactory.parseString(s""" | transports.kafka.metrics.topic = metrics_topic | transports.kafka.metrics.enabled=true""".stripMargin) val pm = KafkaMetrics(cfg) pm shouldBe a[PublishMetrics] val kmd = KafkaRecordMetadata(1, 1, "topic", 1, 1, AckStrategy.NoAck) pm.saveMetrics(kmd) EmbeddedKafka .consumeFirstStringMessageFrom("metrics_topic") .parseJson shouldBe kmd.toJson } } }
Example 70
Source File: IngestionErrorHandler.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.ingestors import akka.actor.Actor import com.pluralsight.hydra.avro.JsonToAvroConversionException import hydra.common.config.ConfigSupport._ import hydra.avro.registry.JsonToAvroConversionExceptionWithMetadata import hydra.common.config.ConfigSupport import hydra.core.ingest.RequestParams.HYDRA_KAFKA_TOPIC_PARAM import hydra.core.protocol.GenericIngestionError import hydra.core.transport.Transport.Deliver import hydra.kafka.producer.AvroRecord import org.apache.avro.Schema import spray.json.DefaultJsonProtocol import scala.io.Source class IngestionErrorHandler extends Actor with ConfigSupport with DefaultJsonProtocol { import spray.json._ private implicit val ec = context.dispatcher private implicit val hydraIngestionErrorInfoFormat = jsonFormat6( HydraIngestionErrorInfo ) private val errorTopic = applicationConfig .getStringOpt("ingest.error-topic") .getOrElse("_hydra_ingest_errors") private lazy val kafkaTransport = context .actorSelection( applicationConfig .getStringOpt(s"transports.kafka.path") .getOrElse(s"/user/service/kafka_transport") ) private val errorSchema = new Schema.Parser() .parse(Source.fromResource("schemas/HydraIngestError.avsc").mkString) override def receive: Receive = { case error: GenericIngestionError => kafkaTransport ! Deliver(buildPayload(error)) } private[ingestors] def buildPayload( err: GenericIngestionError ): AvroRecord = { val schema: Option[String] = err.cause match { case e: JsonToAvroConversionException => Some(e.getSchema.toString) case e: JsonToAvroConversionExceptionWithMetadata => Some(e.location) case e: Exception => None } val topic = err.request.metadataValue(HYDRA_KAFKA_TOPIC_PARAM) val errorInfo = HydraIngestionErrorInfo( err.ingestor, topic, err.cause.getMessage, err.request.metadata, schema, err.request.payload ).toJson.compactPrint AvroRecord( errorTopic, errorSchema, topic, errorInfo, err.request.ackStrategy ) } } case class HydraIngestionErrorInfo( ingestor: String, destination: Option[String], errorMessage: String, metadata: Map[String, String], schema: Option[String], payload: String )
Example 71
Source File: KafkaMetrics.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.transport import akka.actor.ActorSystem import com.typesafe.config.Config import hydra.common.config.ConfigSupport import hydra.kafka.producer.KafkaRecordMetadata import hydra.kafka.util.KafkaUtils import org.apache.kafka.clients.producer.ProducerRecord import spray.json.DefaultJsonProtocol trait KafkaMetrics { def saveMetrics(record: KafkaRecordMetadata): Unit def close(): Unit = {} } // $COVERAGE-OFF$ object NoOpMetrics extends KafkaMetrics { def saveMetrics(record: KafkaRecordMetadata): Unit = {} } // $COVERAGE-ON$ class PublishMetrics(topic: String)(implicit system: ActorSystem) extends KafkaMetrics with DefaultJsonProtocol with ConfigSupport { import spray.json._ import KafkaRecordMetadata._ private val producer = KafkaUtils .producerSettings[String, String]("string", rootConfig) .withProperty("client.id", "hydra.kafka.metrics") .createKafkaProducer() def saveMetrics(record: KafkaRecordMetadata) = { val payload = record.toJson.compactPrint producer.send(new ProducerRecord(topic, record.destination, payload)) } override def close(): Unit = { producer.close() } } object KafkaMetrics { import ConfigSupport._ def apply(config: Config)(implicit system: ActorSystem): KafkaMetrics = { val metricsEnabled = config.getBooleanOpt("transports.kafka.metrics.enabled").getOrElse(false) val metricsTopic = config .getStringOpt("transports.kafka.metrics.topic") .getOrElse("HydraKafkaError") if (metricsEnabled) new PublishMetrics(metricsTopic) else NoOpMetrics } }
Example 72
Source File: JsonSupport.scala From akka-http-slick-sample with MIT License | 5 votes |
package net.softler.data.model import java.sql.Timestamp import java.time.Instant import java.util.UUID import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import spray.json.{DefaultJsonProtocol, JsNumber, JsString, JsValue, JsonFormat, RootJsonFormat} trait BaseJsonProtocol extends DefaultJsonProtocol { implicit val timestampFormat: JsonFormat[Timestamp] = new JsonFormat[Timestamp] { override def write(obj: Timestamp): JsValue = JsNumber(obj.getTime) override def read(json: JsValue): Timestamp = json match { case JsNumber(x) => Timestamp.from(Instant.ofEpochMilli(x.toLong)) case _ => throw new IllegalArgumentException( s"Can not parse json value [$json] to a timestamp object") } } implicit val uuidJsonFormat: JsonFormat[UUID] = new JsonFormat[UUID] { override def write(x: UUID): JsValue = JsString(x.toString) override def read(value: JsValue): UUID = value match { case JsString(x) => UUID.fromString(x) case x => throw new IllegalArgumentException("Expected UUID as JsString, but got " + x.getClass) } } } trait JsonProtocol extends SprayJsonSupport with BaseJsonProtocol { implicit val userFormat: RootJsonFormat[User] = jsonFormat10(User) }
Example 73
Source File: HTTPResponseStream.scala From akka_streams_tutorial with MIT License | 5 votes |
package akkahttp import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.common.{EntityStreamingSupport, JsonEntityStreamingSupport} import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives.{complete, get, logRequestResult, path, _} import akka.http.scaladsl.server.Route import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.ThrottleMode import akka.stream.scaladsl.{Flow, Sink, Source} import com.typesafe.config.ConfigFactory import spray.json.DefaultJsonProtocol import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{Failure, Success} object HTTPResponseStream extends App with DefaultJsonProtocol with SprayJsonSupport { implicit val system = ActorSystem("HTTPResponseStream") implicit val executionContext = system.dispatcher //JSON Protocol and streaming support final case class ExamplePerson(name: String) implicit def examplePersonFormat = jsonFormat1(ExamplePerson.apply) implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json() val (address, port) = ("127.0.0.1", 8080) server(address, port) client(address, port) def client(address: String, port: Int): Unit = { val requestParallelism = ConfigFactory.load.getInt("akka.http.host-connection-pool.max-connections") val requests: Source[HttpRequest, NotUsed] = Source .fromIterator(() => Range(0, requestParallelism).map(i => HttpRequest(uri = Uri(s"http://$address:$port/download/$i"))).iterator ) // Run singleRequest and completely consume response elements def runRequestDownload(req: HttpRequest) = Http() .singleRequest(req) .flatMap { response => val unmarshalled: Future[Source[ExamplePerson, NotUsed]] = Unmarshal(response).to[Source[ExamplePerson, NotUsed]] val source: Source[ExamplePerson, Future[NotUsed]] = Source.futureSource(unmarshalled) source.via(processorFlow).runWith(printSink) } requests .mapAsync(requestParallelism)(runRequestDownload) .runWith(Sink.ignore) } val printSink = Sink.foreach[ExamplePerson] { each: ExamplePerson => println(s"Client processed element: $each") } val processorFlow: Flow[ExamplePerson, ExamplePerson, NotUsed] = Flow[ExamplePerson].map { each: ExamplePerson => { //println(s"Process: $each") each } } def server(address: String, port: Int): Unit = { def routes: Route = logRequestResult("httpecho") { path("download" / Segment) { id: String => get { println(s"Server received request with id: $id, stream response...") extractRequest { r: HttpRequest => val finishedWriting = r.discardEntityBytes().future onComplete(finishedWriting) { done => //Limit response by appending eg .take(5) val responseStream: Stream[ExamplePerson] = Stream.continually(ExamplePerson(s"request:$id")) complete(Source(responseStream).throttle(1, 1.second, 1, ThrottleMode.shaping)) } } } } } val bindingFuture = Http().bindAndHandle(routes, address, port) bindingFuture.onComplete { case Success(b) => println("Server started, listening on: " + b.localAddress) case Failure(e) => println(s"Server could not bind to: $address:$port. Exception message: ${e.getMessage}") system.terminate() } } }
Example 74
Source File: ElasticSearch.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.{ConditionRef, Token} import spray.json.{DefaultJsonProtocol, JsonFormat, RootJsonFormat} case class EBSOptions( EBSEnabled: Option[Token[Boolean]], Iops: Option[Token[Int]], VolumeType: Option[Token[VolumeType]], VolumeSize: Option[Token[Int]] ) object EBSOptions extends DefaultJsonProtocol { implicit val format = jsonFormat4(EBSOptions.apply) } case class ElasticsearchClusterConfig( DedicatedMasterCount: Option[Token[Int]], DedicatedMasterEnabled: Option[Token[Boolean]], DedicatedMasterType: Option[Token[String]], InstanceCount: Option[Token[Int]], InstanceType: Option[Token[String]], ZoneAwarenessEnabled: Option[Token[Boolean]] ) object ElasticsearchClusterConfig extends DefaultJsonProtocol { implicit val format = jsonFormat6(ElasticsearchClusterConfig.apply) } case class SnapshotOptions(AutomatedSnapshotStartHour: Option[Token[Int]]) object SnapshotOptions extends DefaultJsonProtocol { implicit val format = jsonFormat1(SnapshotOptions.apply) } case class VPCOptions( SecurityGroupIds : Seq[Token[`AWS::EC2::SecurityGroup`]] = Seq.empty[Token[`AWS::EC2::SecurityGroup`]], SubnetIds: Seq[Token[String]] ) object VPCOptions extends DefaultJsonProtocol { implicit val format : RootJsonFormat[VPCOptions] = jsonFormat2(VPCOptions.apply) } case class `AWS::Elasticsearch::Domain` ( name: String, DomainName: Token[String], AccessPolicies: Option[PolicyDocument] = None, AdvancedOptions: Option[Token[Map[String, String]]] = None, EBSOptions: Option[EBSOptions] = None, ElasticsearchClusterConfig: Option[ElasticsearchClusterConfig] = None, ElasticsearchVersion: Option[Token[String]] = None, SnapshotOptions: Option[SnapshotOptions] = None, Tags: Option[Seq[AmazonTag]] = None, VPCOptions: Option[VPCOptions] = None, override val Condition: Option[ConditionRef] = None, override val DependsOn: Option[Seq[String]] = None ) extends Resource[`AWS::Elasticsearch::Domain`]{ def when(newCondition: Option[ConditionRef] = Condition) : `AWS::Elasticsearch::Domain` = copy(Condition = newCondition) } object `AWS::Elasticsearch::Domain` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::Elasticsearch::Domain`] = jsonFormat12(`AWS::Elasticsearch::Domain`.apply) }
Example 75
Source File: JsonYQLParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{Model, YQLModel} import spray.http._ import scala.util._ class JsonYQLParser(blackboard: ActorRef) extends BlackboardActor(blackboard) { val model: Model = new YQLModel override def receive: PartialFunction[Any, Unit] = { case ContentMessage(entity) => log.debug("JsonYQLParser received ContentMessage") JsonYQLParser.decode(entity) match { case Right(response) => processQuote(response.query.results.quote) case Left(message) => log.warning(message.toString) } case m => super.receive(m) } def processQuote(quotes: Seq[Map[String, Option[String]]]): Unit = quotes foreach { q => processInstrument(q) } def processInstrument(quote: Map[String, Option[String]]): Unit = model.getKey("symbol") match { case Some(s) => quote.get(s) match { case Some(Some(symbol)) => updateMarket(symbol, quote) case _ => log.warning(s"symbol $s is undefined") } case _ => log.warning("'symbol' is undefined in model") } def updateMarket(symbol: String, quote: Map[String, Option[String]]): Unit = blackboard ! KnowledgeUpdate(model, symbol, quote flatMap { case (k, Some(v)) => Option(k -> v); case _ => None }) } object JsonYQLParser { import spray.httpx.SprayJsonSupport._ import spray.httpx.unmarshalling._ import spray.json.{DefaultJsonProtocol, _} case class Response(query: Query) case class Query(count: Int, created: String, lang: String, diagnostics: Option[Diagnostics], results: Results) case class Diagnostics(url: Seq[Map[String, String]], publiclyCallable: String, `user-time`: String, `service-time`: String, `build-version`: String, query: DiagnosticsQuery, cache: DiagnosticsCache, javascript: DiagnosticsJavascript) case class DiagnosticsQuery(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, params: String, content: String) case class DiagnosticsCache(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, method: String, `type`: String, content: String) case class DiagnosticsJavascript(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, `instructions-used`: String, `table-name`: String) case class Results(quote: Seq[Map[String, Option[String]]]) { def get(index: Int, key: String): Option[String] = { Try { quote(index) } match { case Success(y) => y.get(key) match { case Some(x) => x; case None => None } case Failure(_) => None } } } object MyJsonProtocol extends DefaultJsonProtocol with NullOptions { implicit val diagnosticsQueryFormat: RootJsonFormat[DiagnosticsQuery] = jsonFormat5(DiagnosticsQuery) implicit val diagnosticsCacheFormat: RootJsonFormat[DiagnosticsCache] = jsonFormat6(DiagnosticsCache) implicit val diagnosticsJavascriptFormat: RootJsonFormat[DiagnosticsJavascript] = jsonFormat5(DiagnosticsJavascript) implicit val diagnosticsFormat: RootJsonFormat[Diagnostics] = jsonFormat8(Diagnostics) implicit val resultsFormat: RootJsonFormat[Results] = jsonFormat1(Results) implicit val queryFormat: RootJsonFormat[Query] = jsonFormat5(Query) implicit val entityFormat: RootJsonFormat[Response] = jsonFormat1(Response) } import MyJsonProtocol._ def decode(entity: HttpEntity): Deserialized[Response] = entity.as[Response] }
Example 76
Source File: JsonGoogleParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{ActorRef, Props} import spray.http._ import scala.util._ import edu.neu.coe.csye7200.hedge_fund.model.{GoogleModel, Model} def decode(entity: HttpEntity): Deserialized[Results] = { val mediaTypeTextHtml = MediaTypes.`text/html` val mediaTypeJson = MediaTypes.`application/json` val contentTypeJson = ContentType(mediaTypeJson, HttpCharsets.`UTF-8`) // val contentTypeText = ContentType(mediaTypeTextHtml, HttpCharsets.`ISO-8859-1`) entity match { case HttpEntity.NonEmpty(`contentTypeJson`, _) => entity.as[Results] case HttpEntity.NonEmpty(ContentType(`mediaTypeTextHtml`, x), y) => HttpEntity(ContentType(mediaTypeJson, x), fix(y)).as[Results] case HttpEntity.NonEmpty(x, _) => Left(MalformedContent(s"logic error: contentType=$x")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = s.substring(3) }
Example 77
Source File: JsonGoogleOptionParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{ActorRef, Props} import spray.http._ import scala.util._ import edu.neu.coe.csye7200.hedge_fund.model.{GoogleOptionModel, Model} def decode(entity: HttpEntity): Deserialized[OptionChain] = { val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) entity match { case HttpEntity.NonEmpty(`contentType`, y) => HttpEntity(contentType, fix(y)).as[OptionChain] case HttpEntity.NonEmpty(s, y) => Left(MalformedContent(s"entity content type: $s")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = """([^,{:\s]+):""".r.replaceAllIn(s, """"$1":""") }
Example 78
Source File: JsonYQLParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.hedge_fund.actors import akka.actor.{ActorRef, Props} import spray.http._ import scala.util._ import edu.neu.coe.csye7200.hedge_fund.model.{Model, YQLModel} class JsonYQLParser(blackboard: ActorRef) extends BlackboardActor(blackboard) { val model: Model = new YQLModel override def receive = { case ContentMessage(entity) => log.debug("JsonYQLParser received ContentMessage") JsonYQLParser.decode(entity) match { case Right(response) => processQuote(response.query.results.quote) case Left(message) => log.warning(message.toString()) } case m => super.receive(m) } def processQuote(quotes: Seq[Map[String, Option[String]]]) = quotes foreach { q => processInstrument(q) } def processInstrument(quote: Map[String, Option[String]]) = model.getKey("symbol") match { case Some(s) => quote.get(s) match { case Some(Some(symbol)) => updateMarket(symbol, quote) case _ => log.warning(s"symbol $s is undefined") } case _ => log.warning("'symbol' is undefined in model") } def updateMarket(symbol: String, quote: Map[String, Option[String]]) = blackboard ! KnowledgeUpdate(model, symbol, quote flatMap { case (k, Some(v)) => Option(k -> v); case _ => None }) } object JsonYQLParser { import spray.json.DefaultJsonProtocol import spray.httpx.unmarshalling._ import spray.httpx.marshalling._ import spray.httpx.SprayJsonSupport._ import spray.json._ case class Response(query: Query) case class Query(count: Int, created: String, lang: String, diagnostics: Option[Diagnostics], results: Results) case class Diagnostics(url: Seq[Map[String, String]], publiclyCallable: String, `user-time`: String, `service-time`: String, `build-version`: String, query: DiagnosticsQuery, cache: DiagnosticsCache, javascript: DiagnosticsJavascript) case class DiagnosticsQuery(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, params: String, content: String) case class DiagnosticsCache(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, method: String, `type`: String, content: String) case class DiagnosticsJavascript(`execution-start-time`: String, `execution-stop-time`: String, `execution-time`: String, `instructions-used`: String, `table-name`: String) case class Results(quote: Seq[Map[String, Option[String]]]) { def get(index: Int, key: String): Option[String] = { Try { quote(index) } match { case Success(y) => y.get(key) match { case Some(x) => x; case None => None } case Failure(y) => None } } } object MyJsonProtocol extends DefaultJsonProtocol with NullOptions { implicit val diagnosticsQueryFormat = jsonFormat5(DiagnosticsQuery) implicit val diagnosticsCacheFormat = jsonFormat6(DiagnosticsCache) implicit val diagnosticsJavascriptFormat = jsonFormat5(DiagnosticsJavascript) implicit val diagnosticsFormat = jsonFormat8(Diagnostics) implicit val resultsFormat = jsonFormat1(Results) implicit val queryFormat = jsonFormat5(Query) implicit val entityFormat = jsonFormat1(Response) } import MyJsonProtocol._ def decode(entity: HttpEntity) = entity.as[Response] }
Example 79
Source File: S3MigrationHandlerBase.scala From flyway-awslambda with MIT License | 5 votes |
package crossroad0201.aws.flywaylambda import java.text.SimpleDateFormat import java.util.Date import com.amazonaws.services.lambda.runtime.Context import com.amazonaws.services.s3.AmazonS3 import crossroad0201.aws.flywaylambda.deploy.{FlywayDeployment, S3SourceFlywayDeployer} import crossroad0201.aws.flywaylambda.migration.{FlywayMigrator, MigrationInfo, MigrationResult} import spray.json.DefaultJsonProtocol import scala.util.Try object MigrationResultProtocol extends DefaultJsonProtocol { import spray.json._ implicit val DateFormat = new RootJsonFormat[Date] { override def write(value: Date): JsValue = if (value == null) JsNull else JsString(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(value)) override def read(json: JsValue): Date = ??? } implicit val migrationInfoFormat = jsonFormat6(MigrationInfo.apply) implicit val migrationResultFormat = jsonFormat5(MigrationResult.apply) } trait S3MigrationHandlerBase extends FlywayMigrator { type ResultJson = String type ResultStoredPath = String protected def migrate(bucketName: String, prefix: String, flywayConfFileName: String = "flyway.conf")(implicit context: Context, s3Client: AmazonS3): Try[ResultJson] = { val logger = context.getLogger def resultJson(result: MigrationResult): ResultJson = { import MigrationResultProtocol._ import spray.json._ result.toJson.prettyPrint } def storeResult(deployment: FlywayDeployment, result: MigrationResult): ResultStoredPath = { val jsonPath = s"${deployment.sourcePrefix}/migration-result.json" s3Client.putObject(deployment.sourceBucket, jsonPath, resultJson(result)) jsonPath } for { // Deploy Flyway resources. d <- new S3SourceFlywayDeployer(s3Client, bucketName, prefix, flywayConfFileName).deploy _ = { logger.log( s"""--- Flyway configuration ------------------------------------ |flyway.url = ${d.url} |flyway.user = **** |flyway.password = **** | |SQL locations = ${d.location} |SQL files = ${d.sqlFiles.mkString(", ")} |------------------------------------------------------------- """.stripMargin) } // Migrate DB. r = migrate(d) _ = { logger.log(s"${r.message}!. ${r.appliedCount} applied.") r.infos.foreach { i => logger.log(s"Version=${i.version}, Type=${i.`type`}, State=${i.state} InstalledAt=${i.installedAt} ExecutionTime=${i.execTime} Description=${i.description}") } } // Store migration result. storedPath = storeResult(d, r) _ = logger.log(s"Migration result stored to $bucketName/$storedPath.") } yield resultJson(r) } }
Example 80
Source File: Utils.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph import com.google.common.base.CaseFormat import spray.json.{DefaultJsonProtocol, _} object Utils { def camelToUpperUnderscore(str: String): String = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, str) } case class Timing(name: String, duration: Long) object TimingProtocol extends DefaultJsonProtocol { implicit val timingFormat: RootJsonFormat[Timing] = jsonFormat2(Timing) } object JsonFormats { implicit object CaseClassFormat extends JsonFormat[Product] { def write(x: Product): JsValue = { val values = x.productIterator.toList val fields = x.getClass.getDeclaredFields def getIdValue(p: Product): Option[Any] = { val values = p.productIterator.toList val fields = p.getClass.getDeclaredFields fields.zipWithIndex.find(_._1.getName == "id").map(z => values(z._2)) } val map: Map[String, Any] = values.zipWithIndex.map { case (v, i) => val key = fields(i).getName val value = v match { case v: Product if !v.isInstanceOf[Option[_]] => getIdValue(v).getOrElse("...") case Some(v: Product) => getIdValue(v).getOrElse("...") case v => v } key -> value }.toMap AnyJsonFormat.write(map) } def read(value: JsValue) = throw new UnsupportedOperationException() } implicit object AnyJsonFormat extends JsonFormat[Any] { def write(x: Any): JsValue = x match { case m: Map[_, _] => JsObject(m.asInstanceOf[Map[String, Any]].mapValues(write)) case l: List[Any] => JsArray(l.map(write).toVector) case n: Int => JsNumber(n) case n: Long => JsNumber(n) case n: Double => JsNumber(n) case s: String => JsString(s) case true => JsTrue case false => JsFalse case v: JsValue => v case null => JsNull case r => JsString(r.toString) } def read(value: JsValue) = throw new UnsupportedOperationException() } class AnyJsonWriter extends JsonWriter[Map[String, Any]] { override def write(obj: Map[String, Any]): JsValue = AnyJsonFormat.write(obj) } class SeqAnyJsonWriter[T <: Any] extends JsonWriter[Seq[Map[String, T]]] { override def write(objs: Seq[Map[String, T]]): JsValue = new JsArray(objs.map(obj => AnyJsonFormat.write(obj)).toVector) } }
Example 81
Source File: LogData.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.shared.logging import cool.graph.JsonFormats import spray.json.{DefaultJsonProtocol, _} object LogKey extends Enumeration { val RequestNew = Value("request/new") val RequestQuery = Value("request/query") val RequestComplete = Value("request/complete") val RequestMetricsFields = Value("request/metrics/fields") val RequestMetricsSql = Value("request/metrics/sql") val RequestMetricsMutactions = Value("request/metrics/mutactions") val UnhandledError = Value("error/unhandled") val HandledError = Value("error/handled") val MutactionWebhook = Value("mutaction/webhook") val AlgoliaSyncQuery = Value("mutaction/algoliaSyncQuery") val ActionHandlerWebhookComplete = Value("action_handler/webhook/complete") val IntegrityViolation = Value("integrity/violation") val RequestProxyBegin = Value("request/proxy/begin") val RequestProxyData = Value("request/proxy/data") } case class LogData( key: LogKey.Value, requestId: String, clientId: Option[String] = None, projectId: Option[String] = None, message: Option[String] = None, payload: Option[Map[String, Any]] = None ) { import LogFormats._ lazy val json: String = this.toJson(logDataFormat).compactPrint } object LogFormats extends DefaultJsonProtocol { import JsonFormats.AnyJsonFormat implicit object LogKeyJsonFormat extends RootJsonFormat[LogKey.Value] { def write(obj: LogKey.Value): JsValue = JsString(obj.toString) def read(json: JsValue): LogKey.Value = json match { case JsString(str) => LogKey.withName(str) case _ => throw new DeserializationException("Enum string expected") } } implicit val logDataFormat: RootJsonFormat[LogData] = jsonFormat(LogData, "log_key", "request_id", "client_id", "project_id", "message", "payload") }
Example 82
Source File: EmailAuthProviderManager.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.authProviders import com.github.t3hnar.bcrypt._ import cool.graph.ArgumentSchema import cool.graph.client.database.DeferredResolverProvider import cool.graph.client.mutations.Create import cool.graph.client.schema.SchemaModelObjectTypesBuilder import cool.graph.client.schema.simple.SimpleArgumentSchema import cool.graph.client.{ClientInjector, UserContext} import cool.graph.shared.errors.UserAPIErrors import cool.graph.shared.errors.UserAPIErrors.{CannotSignUpUserWithCredentialsExist, UniqueConstraintViolation} import cool.graph.shared.models.IntegrationName._ import cool.graph.shared.models.{AuthProviderMetaInformation, IntegrationName, TypeIdentifier} import cool.graph.util.coolSangria.Sangria import cool.graph.util.crypto.Crypto import sangria.schema.Context import spray.json.{DefaultJsonProtocol, RootJsonFormat} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future case class JwtEmailAuthData(email: String) object EmailAuthJsonProtocol extends DefaultJsonProtocol { implicit val authDataFormat: RootJsonFormat[JwtEmailAuthData] = jsonFormat1(JwtEmailAuthData) } class EmailAuthProviderManager()(implicit injector: ClientInjector) extends AuthProviderManager[Unit]() { val clientAuth = injector.clientAuth val emailField = ManagedField(defaultName = "email", typeIdentifier = TypeIdentifier.String, isUnique = true, isReadonly = true) val passwordField = ManagedField(defaultName = "password", typeIdentifier = TypeIdentifier.String, isReadonly = true) override val managedFields: List[ManagedField] = List(emailField, passwordField) override val signupFields: List[ManagedField] = List(emailField, passwordField) override val signinFields: List[ManagedField] = List(emailField, passwordField) override val integrationName: IntegrationName = IntegrationName.AuthProviderEmail override val name = "email" override def getmetaInformation: Option[AuthProviderMetaInformation] = None import EmailAuthJsonProtocol._ def resolveSignin(ctx: Context[UserContext, Unit], args: Map[String, Any]): Future[Option[AuthData]] = { val email = args("email").asInstanceOf[String] val password = args("password").asInstanceOf[String] ctx.ctx.dataResolver.resolveByUnique(ctx.ctx.project.getModelByName_!("User"), "email", email) flatMap { case Some(user) if password.isBcrypted(user.get[String]("password")) => clientAuth .loginUser(ctx.ctx.project, user, Some(JwtEmailAuthData(email = email))) .map(token => Some(AuthData(token = token, user = user))) case _ => throw UserAPIErrors.CannotSignInCredentialsInvalid() } } override def resolveSignup[T, A](ctx: Context[UserContext, Unit], customArgs: Map[String, Any], providerArgs: Map[String, Any], modelObjectTypesBuilder: SchemaModelObjectTypesBuilder[T], argumentSchema: ArgumentSchema, deferredResolverProvider: DeferredResolverProvider[_, UserContext]): Future[Option[AuthData]] = { val userModel = ctx.ctx.dataResolver.project.getModelByName_!("User") val createArgs = Sangria.rawArgs( raw = customArgs ++ providerArgs + (passwordField.defaultName -> Crypto .hash(providerArgs(passwordField.defaultName).asInstanceOf[String]))) val a = new Create( model = userModel, project = ctx.ctx.project, args = createArgs, dataResolver = ctx.ctx.dataResolver, argumentSchema = SimpleArgumentSchema, allowSettingManagedFields = true ).run(ctx.ctx.authenticatedRequest, ctx.ctx) .recover { case e: UniqueConstraintViolation => throw CannotSignUpUserWithCredentialsExist() } .map(user => clientAuth .loginUser(ctx.ctx.project, user, Some(JwtEmailAuthData(email = user.get[String]("email")))) .map(token => Some(AuthData(token = token, user = user)))) a.flatMap(identity) } }
Example 83
Source File: SNS.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.{ResourceRef, Token, ConditionRef} import spray.json.{JsonFormat, DefaultJsonProtocol} case class Subscription(Endpoint: Token[String], Protocol: Token[String]) object Subscription extends DefaultJsonProtocol { implicit val format: JsonFormat[Subscription] = jsonFormat2(Subscription.apply) } case class `AWS::SNS::Topic`( name: String, DisplayName: Option[Token[String]] = None, Subscription: Option[Seq[Token[Subscription]]] = None, TopicName: Option[Token[String]] = None, override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None) extends Resource[`AWS::SNS::Topic`] with HasArn { def when(newCondition: Option[ConditionRef] = Condition) = new `AWS::SNS::Topic`(name, DisplayName, Subscription, TopicName, DependsOn, newCondition) override def arn = ResourceRef(this) } object `AWS::SNS::Topic` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::SNS::Topic`] = jsonFormat6(`AWS::SNS::Topic`.apply) } case class `AWS::SNS::TopicPolicy`( name: String, PolicyDocument: PolicyDocument, Topics: Seq[ResourceRef[`AWS::SNS::Topic`]], override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None) extends Resource[`AWS::SNS::TopicPolicy`] { def when(newCondition: Option[ConditionRef] = Condition) = new `AWS::SNS::TopicPolicy`(name, PolicyDocument, Topics, DependsOn, newCondition) } object `AWS::SNS::TopicPolicy` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::SNS::TopicPolicy`] = jsonFormat5(`AWS::SNS::TopicPolicy`.apply) } trait Subscribable { def asSubscription : Token[Subscription] } case class `AWS::SNS::Subscription`( name : String, Endpoint : Option[Token[String]] = None, Protocol : Token[String], TopicArn : Token[String], override val DependsOn: Option[Seq[String]] = None, override val Condition : Option[ConditionRef] = None) extends Resource[`AWS::SNS::Subscription`] { override def when(newCondition: Option[ConditionRef]): `AWS::SNS::Subscription` = copy(Condition = newCondition) } object `AWS::SNS::Subscription` extends DefaultJsonProtocol { implicit val format : JsonFormat[`AWS::SNS::Subscription`] = jsonFormat6(`AWS::SNS::Subscription`.apply) }
Example 84
Source File: SQS.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.Token.TokenSeq import com.monsanto.arch.cloudformation.model.{ConditionRef, Token, `Fn::GetAtt`} import spray.json.{DefaultJsonProtocol, JsonFormat} case class `AWS::SQS::Queue`(name: String, QueueName: Token[String], DelaySeconds: Token[Int], MessageRetentionPeriod: Token[Int], ReceiveMessageWaitTimeSeconds: Token[Int], VisibilityTimeout: Token[Int], RedrivePolicy: Option[RedrivePolicy] = None, override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None) extends Resource[`AWS::SQS::Queue`] with HasArn with Subscribable { override def arn = `Fn::GetAtt`(Seq(name, "Arn")) def when(newCondition: Option[ConditionRef] = Condition) = copy(Condition = newCondition) override def asSubscription = Subscription( Endpoint = arn, Protocol = "sqs" ) } object `AWS::SQS::Queue` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::SQS::Queue`] = jsonFormat9(`AWS::SQS::Queue`.apply) } case class `AWS::SQS::QueuePolicy`(name: String, PolicyDocument: PolicyDocument, Queues: TokenSeq[String], override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None ) extends Resource[`AWS::SQS::QueuePolicy`] { def when(newCondition: Option[ConditionRef] = Condition) = copy(Condition = newCondition) } object `AWS::SQS::QueuePolicy` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::SQS::QueuePolicy`] = jsonFormat5(`AWS::SQS::QueuePolicy`.apply) } case class RedrivePolicy( deadLetterTargetArn: Token[String], maxReceiveCount: Token[Int] ) object RedrivePolicy extends DefaultJsonProtocol { implicit val format: JsonFormat[RedrivePolicy] = jsonFormat2(RedrivePolicy.apply) }
Example 85
Source File: ECR.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.{ConditionRef, Token} import spray.json.{DefaultJsonProtocol, JsonFormat} case class `AWS::ECR::Repository`( name: String, RepositoryName: Option[Token[String]], RepositoryPolicyText: Option[Token[PolicyDocument]] = None, override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None ) extends Resource[`AWS::ECR::Repository`] { def when(newCondition: Option[ConditionRef] = Condition) = new `AWS::ECR::Repository`(name, RepositoryName, RepositoryPolicyText, DependsOn, newCondition) } object `AWS::ECR::Repository` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::ECR::Repository`] = jsonFormat5(`AWS::ECR::Repository`.apply) }
Example 86
Source File: JsonGoogleOptionParser.scala From CSYE7200_Old with MIT License | 5 votes |
package edu.neu.coe.csye7200.actors import akka.actor.ActorRef import edu.neu.coe.csye7200.model.{GoogleOptionModel, Model} import spray.http._ import scala.util._ def decode(entity: HttpEntity): Deserialized[OptionChain] = { // import spray.httpx.unmarshalling._ val contentType = ContentType(MediaTypes.`application/json`, HttpCharsets.`UTF-8`) entity match { case HttpEntity.NonEmpty(`contentType`, y) => HttpEntity(contentType, fix(y)).as[OptionChain] case HttpEntity.NonEmpty(s, _) => Left(MalformedContent(s"entity content type: $s")) case _ => Left(MalformedContent("logic error")) } } def fix(data: HttpData): Array[Byte] = fix(data.asString).getBytes def fix(s: String): String = """([^,{:\s]+):""".r.replaceAllIn(s, """"$1":""") }
Example 87
Source File: EKS.scala From cloudformation-template-generator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.monsanto.arch.cloudformation.model.resource import com.monsanto.arch.cloudformation.model.{ConditionRef, ResourceRef, Token} import spray.json.{DefaultJsonProtocol, JsonFormat} case class `AWS::EKS::Cluster`( name: String, Name: Token[String], ResourcesVpcConfig: Token[ResourcesVpcConfig], RoleArn: Token[String], Version: Option[Token[String]] = None, override val DependsOn: Option[Seq[String]] = None, override val Condition: Option[ConditionRef] = None ) extends Resource[`AWS::EKS::Cluster`] { def when(newCondition: Option[ConditionRef] = Condition) = new `AWS::EKS::Cluster`(name, Name, ResourcesVpcConfig, RoleArn, Version, DependsOn, newCondition) } case class ResourcesVpcConfig(SecurityGroupIds : Seq[Token[ResourceRef[`AWS::EC2::SecurityGroup`]]], SubnetIds : Seq[Token[ResourceRef[`AWS::EC2::Subnet`]]]) object ResourcesVpcConfig extends DefaultJsonProtocol { implicit val format: JsonFormat[ResourcesVpcConfig] = jsonFormat2(ResourcesVpcConfig.apply) } object `AWS::EKS::Cluster` extends DefaultJsonProtocol { implicit val format: JsonFormat[`AWS::EKS::Cluster`] = jsonFormat7(`AWS::EKS::Cluster`.apply) }
Example 88
Source File: OAuth2RouteProvider.scala From slick-akka-http-oauth2 with Apache License 2.0 | 5 votes |
package rest import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives import akka.http.scaladsl.server.directives.Credentials import rest.OAuth2RouteProvider.TokenResponse import spray.json.DefaultJsonProtocol import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.{Failure, Success} import scalaoauth2.provider._ trait OAuth2RouteProvider[U] extends Directives with DefaultJsonProtocol{ import OAuth2RouteProvider.tokenResponseFormat import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ val oauth2DataHandler : DataHandler[U] val tokenEndpoint = new TokenEndpoint { override val handlers = Map( OAuthGrantType.CLIENT_CREDENTIALS -> new ClientCredentials, OAuthGrantType.PASSWORD -> new Password, OAuthGrantType.AUTHORIZATION_CODE -> new AuthorizationCode, OAuthGrantType.REFRESH_TOKEN -> new RefreshToken ) } def grantResultToTokenResponse(grantResult : GrantHandlerResult[U]) : TokenResponse = TokenResponse(grantResult.tokenType, grantResult.accessToken, grantResult.expiresIn.getOrElse(1L), grantResult.refreshToken.getOrElse("")) def oauth2Authenticator(credentials: Credentials): Future[Option[AuthInfo[U]]] = credentials match { case [email protected](token) => oauth2DataHandler.findAccessToken(token).flatMap { case Some(token) => oauth2DataHandler.findAuthInfoByAccessToken(token) case None => Future.successful(None) } case _ => Future.successful(None) } def accessTokenRoute = pathPrefix("oauth") { path("access_token") { post { formFieldMap { fields => onComplete(tokenEndpoint.handleRequest(new AuthorizationRequest(Map(), fields.map(m => m._1 -> Seq(m._2))), oauth2DataHandler)) { case Success(maybeGrantResponse) => maybeGrantResponse.fold(oauthError => complete(Unauthorized), grantResult => complete(tokenResponseFormat.write(grantResultToTokenResponse(grantResult))) ) case Failure(ex) => complete(InternalServerError, s"An error occurred: ${ex.getMessage}") } } } } } } object OAuth2RouteProvider extends DefaultJsonProtocol{ case class TokenResponse(token_type : String, access_token : String, expires_in : Long, refresh_token : String) implicit val tokenResponseFormat = jsonFormat4(TokenResponse) }
Example 89
Source File: ClusterHttpManagementProtocol.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.management.cluster import scala.collection.immutable import akka.annotation.InternalApi import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import spray.json.DefaultJsonProtocol import spray.json.RootJsonFormat final case class ClusterUnreachableMember(node: String, observedBy: immutable.Seq[String]) final case class ClusterMember(node: String, nodeUid: String, status: String, roles: Set[String]) object ClusterMember { implicit val clusterMemberOrdering: Ordering[ClusterMember] = Ordering.by(_.node) } final case class ClusterMembers( selfNode: String, members: Set[ClusterMember], unreachable: immutable.Seq[ClusterUnreachableMember], leader: Option[String], oldest: Option[String], oldestPerRole: Map[String, String]) final case class ClusterHttpManagementMessage(message: String) final case class ShardRegionInfo(shardId: String, numEntities: Int) final case class ShardDetails(regions: immutable.Seq[ShardRegionInfo]) @InternalApi private[akka] object ClusterHttpManagementOperation { def fromString(value: String): Option[ClusterHttpManagementOperation] = Vector(Down, Leave, Join).find(_.toString.equalsIgnoreCase(value)) } trait ClusterHttpManagementJsonProtocol extends SprayJsonSupport with DefaultJsonProtocol { implicit val clusterUnreachableMemberFormat: RootJsonFormat[ClusterUnreachableMember] = jsonFormat2(ClusterUnreachableMember) implicit val clusterMemberFormat: RootJsonFormat[ClusterMember] = jsonFormat4(ClusterMember.apply) implicit val clusterMembersFormat: RootJsonFormat[ClusterMembers] = jsonFormat6(ClusterMembers) implicit val clusterMemberMessageFormat: RootJsonFormat[ClusterHttpManagementMessage] = jsonFormat1(ClusterHttpManagementMessage) implicit val shardRegionInfoFormat: RootJsonFormat[ShardRegionInfo] = jsonFormat2(ShardRegionInfo) implicit val shardDetailsFormat: RootJsonFormat[ShardDetails] = jsonFormat1(ShardDetails) }
Example 90
Source File: HttpBootstrapJsonProtocol.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.management.cluster.bootstrap.contactpoint import akka.actor.{ Address, AddressFromURIString } import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import spray.json.{ DefaultJsonProtocol, JsString, JsValue, RootJsonFormat } trait HttpBootstrapJsonProtocol extends SprayJsonSupport with DefaultJsonProtocol { import HttpBootstrapJsonProtocol._ implicit object AddressFormat extends RootJsonFormat[Address] { override def read(json: JsValue): Address = json match { case JsString(s) => AddressFromURIString.parse(s) case invalid => throw new IllegalArgumentException(s"Illegal address value! Was [$invalid]") } override def write(obj: Address): JsValue = JsString(obj.toString) } implicit val SeedNodeFormat: RootJsonFormat[SeedNode] = jsonFormat1(SeedNode) implicit val ClusterMemberFormat: RootJsonFormat[ClusterMember] = jsonFormat4(ClusterMember) implicit val ClusterMembersFormat: RootJsonFormat[SeedNodes] = jsonFormat2(SeedNodes) } object HttpBootstrapJsonProtocol extends DefaultJsonProtocol { final case class SeedNode(address: Address) // we use Address since we want to know which protocol is being used (tcp, artery, artery-tcp etc) final case class ClusterMember(node: Address, nodeUid: Long, status: String, roles: Set[String]) implicit val clusterMemberOrdering: Ordering[ClusterMember] = Ordering.by(_.node) final case class SeedNodes(selfNode: Address, seedNodes: Set[ClusterMember]) }
Example 91
Source File: OrderModel.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter9 import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport import spray.json.DefaultJsonProtocol import scala.xml._ case class Item(id: Int, quantity: Int, unitPrice: Double, percentageDiscount: Option[Double]) case class Order(id: String, timestamp: Long, items: List[Item], deliveryPrice: Double, metadata: Map[String, String]) case class GrandTotal(id: String, amount: Double) trait OrderJsonSupport extends SprayJsonSupport with DefaultJsonProtocol { implicit val itemFormat = jsonFormat4(Item) implicit val orderFormat = jsonFormat5(Order) implicit val grandTotalFormat = jsonFormat2(GrandTotal) } trait OrderXmlSupport extends ScalaXmlSupport { implicit def grandTotalToXML(g: GrandTotal): NodeSeq = <grandTotal><id>{g.id}</id><amount>{g.amount}</amount></grandTotal> implicit def orderToXML(o: Order): NodeSeq = <order> <id>{o.id}</id> <timestamp>{o.timestamp}</timestamp> <deliveryPrice>{o.deliveryPrice}</deliveryPrice> <items>{o.items.map(itemToXML)}</items> <metadata>{o.metadata.map(keyValueToXML)}</metadata> </order> implicit def orderFromXML(xmlOrder: NodeSeq): Order = { val id = (xmlOrder \ "id").text val timestamp = (xmlOrder \ "timestamp").text.toLong val deliveryPrice = (xmlOrder \ "deliveryPrice").text.toDouble val items = (xmlOrder \ "item").map(itemFromXML).toList val metadata = keyValueFromXML(xmlOrder \ "metadata") Order(id, timestamp, items, deliveryPrice, metadata) } private def keyValueFromXML(xml: NodeSeq) = { xml.flatMap { case e: Elem => e.child case _ => NodeSeq.Empty }.map(x => x.label -> x.text).toMap } private def keyValueToXML(kv: (String, String)) = Elem(null, kv._1, Null, TopScope, false, Text(kv._2)) private def itemFromXML(xmlItem: NodeSeq): Item = { val id = (xmlItem \ "id").text.toInt val quantity = (xmlItem \ "quantity").text.toInt val unitPrice = (xmlItem \ "unitPrice").text.toDouble val percentageDiscount = if ((xmlItem \ "percentageDiscount").isEmpty) None else Some((xmlItem \ "percentageDiscount").text.toDouble) Item(id, quantity, unitPrice, percentageDiscount) } private def itemToXML(i: Item) = <item> <id>{i.id}</id> <quantity>{i.quantity}</quantity> <unitPrice>{i.unitPrice}</unitPrice> {if (i.percentageDiscount.isDefined) <percentageDiscount>{i.percentageDiscount.get}</percentageDiscount>} </item> }
Example 92
Source File: RestService.scala From introduction-to-akkahttp with Apache License 2.0 | 5 votes |
package com.shashank.akkahttp.project import java.util.concurrent.ConcurrentHashMap import akka.actor.ActorSystem import akka.http.scaladsl.server.Directives._ import akka.stream.ActorMaterializer import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import com.shashank.akkahttp.project.Models.{LoadRequest, ServiceJsonProtoocol} import spray.json.JsArray import scala.collection.JavaConverters._ import spray.json.{DefaultJsonProtocol, JsArray, pimpAny} import spray.json.DefaultJsonProtocol._ import org.apache.spark.sql.SparkSession import org.apache.spark.sql._ trait RestService { implicit val system: ActorSystem implicit val materializer: ActorMaterializer implicit val sparkSession: SparkSession val datasetMap = new ConcurrentHashMap[String, Dataset[Row]]() import ServiceJsonProtoocol._ val route = pathSingleSlash { get { complete { "welcome to rest service" } } } ~ path("load") { post { entity(as[LoadRequest]) { loadRequest => complete { val id = "" + System.nanoTime() val dataset = sparkSession.read.format("csv") .option("header", "true") .load(loadRequest.path) datasetMap.put(id, dataset) id } } } } ~ path("view" / """[\w[0-9]-_]+""".r) { id => get { complete { val dataset = datasetMap.get(id) dataset.take(10).map(row => row.toString()) } } } }
Example 93
Source File: CoreTransactionTestCaseProtocol.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.protocol.transaction.testprotocol import org.bitcoins.core.currency.{CurrencyUnit, Satoshis} import org.bitcoins.core.number.{Int64, UInt32} import org.bitcoins.core.protocol.script.ScriptPubKey import org.bitcoins.core.protocol.transaction.{Transaction, TransactionOutPoint} import org.bitcoins.core.script.constant.ScriptToken import org.bitcoins.core.script.flag.{ScriptFlag, ScriptFlagFactory} import org.bitcoins.core.serializers.script.ScriptParser import org.bitcoins.core.util.{BitcoinSLogger, BytesUtil} import org.bitcoins.crypto.DoubleSha256Digest import spray.json.{DefaultJsonProtocol, JsArray, JsValue, RootJsonFormat} def parseOutPointsScriptPubKeysAmount(array: JsArray): Seq[ (TransactionOutPoint, ScriptPubKey, Option[CurrencyUnit])] = { val result = array.elements.map { case array: JsArray => val prevoutHashHex = BytesUtil.flipEndianness(array.elements.head.convertTo[String]) val prevoutHash = DoubleSha256Digest(prevoutHashHex) val prevoutIndex = array.elements(1).convertTo[Long] match { case -1 => UInt32("ffffffff") case index if index >= UInt32.min.toLong && index <= UInt32.max.toLong => UInt32(index) } val amount = if (array.elements.size == 4) Some(Satoshis(array.elements(3).convertTo[Long])) else None //val prevoutIndex = UInt32(array.elements(1).convertTo[Int]) val outPoint = TransactionOutPoint(prevoutHash, prevoutIndex) val scriptTokens: Seq[ScriptToken] = ScriptParser.fromString(array.elements(2).convertTo[String]) val scriptPubKey = ScriptPubKey.fromAsm(scriptTokens) (outPoint, scriptPubKey, amount) case _: JsValue => throw new RuntimeException( "All tx outpoint/scriptpubkey info must be array elements") } result } }
Example 94
Source File: LightbendApi.scala From scabot with Apache License 2.0 | 5 votes |
package scabot package lightbend import spray.json.{RootJsonFormat, DefaultJsonProtocol} trait LightbendApi extends LightbendApiTypes with DefaultJsonProtocol with LightbendApiActions trait LightbendApiTypes { case class CLARecord(user: String, signed: Boolean, version: Option[String], currentVersion: String) } trait LightbendJsonProtocol extends LightbendApiTypes with DefaultJsonProtocol { private type RJF[x] = RootJsonFormat[x] implicit lazy val _fmtCLARecord: RJF[CLARecord] = jsonFormat4(CLARecord) } trait LightbendApiActions extends LightbendJsonProtocol with core.HttpClient { class LightbendConnection { import spray.http.{GenericHttpCredentials, Uri} import spray.httpx.SprayJsonSupport._ import spray.client.pipelining._ private implicit def connection = setupConnection("www.lightbend.com") def checkCla(user: String) = pWithStatus[CLARecord](Get(Uri("/contribute/cla/scala/check" / user))) } }
Example 95
Source File: OAuth2Provider.scala From akka-http-oauth2-provider with MIT License | 5 votes |
package scalaoauth2.provider import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives import akka.http.scaladsl.server.directives.Credentials import scalaoauth2.provider.OAuth2Provider.TokenResponse import spray.json.{JsValue, DefaultJsonProtocol} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.{Failure, Success} trait OAuth2Provider[U] extends Directives with DefaultJsonProtocol { import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ val oauth2DataHandler: DataHandler[U] val tokenEndpoint: TokenEndpoint def grantResultToTokenResponse(grantResult: GrantHandlerResult[U]): JsValue = OAuth2Provider.tokenResponseFormat.write( TokenResponse( grantResult.tokenType, grantResult.accessToken, grantResult.expiresIn.getOrElse(1L), grantResult.refreshToken.getOrElse("") ) ) def oauth2Authenticator( credentials: Credentials ): Future[Option[AuthInfo[U]]] = credentials match { case Credentials.Provided(token) => oauth2DataHandler.findAccessToken(token).flatMap { case Some(token) => oauth2DataHandler.findAuthInfoByAccessToken(token) case None => Future.successful(None) } case _ => Future.successful(None) } def accessTokenRoute = pathPrefix("oauth") { path("access_token") { post { formFieldMap { fields => onComplete( tokenEndpoint.handleRequest( new AuthorizationRequest( Map(), fields.map(m => m._1 -> Seq(m._2)) ), oauth2DataHandler ) ) { case Success(maybeGrantResponse) => maybeGrantResponse.fold( oauthError => complete(Unauthorized), grantResult => complete(grantResultToTokenResponse(grantResult)) ) case Failure(ex) => complete( InternalServerError, s"An error occurred: ${ex.getMessage}" ) } } } } } } object OAuth2Provider extends DefaultJsonProtocol { case class TokenResponse( token_type: String, access_token: String, expires_in: Long, refresh_token: String ) implicit val tokenResponseFormat = jsonFormat4(TokenResponse) }
Example 96
Source File: Main.scala From Pi-Akka-Cluster with Apache License 2.0 | 5 votes |
package com.lightbend.akka_oled import akka.NotUsed import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps import akka.actor.typed.{ActorSystem, Behavior, Terminated} import akka.cluster.ddata.LWWMapKey import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.management.scaladsl.AkkaManagement import akka.stream.Materializer import akkapi.cluster.{ClusterStatusTracker, OledClusterVisualizer, OledDriver, Settings} import spray.json.DefaultJsonProtocol object Main extends SprayJsonSupport with DefaultJsonProtocol { case class NodeStatus(status: String) implicit val transactionFormat = jsonFormat1(NodeStatus) def apply(settings: Settings): Behavior[NotUsed] = Behaviors.setup { ctx => val oledDriver = ctx.spawn(OledDriver(settings), "oled-driver") oledDriver ! OledDriver.RegisterView("Cluster State", 0) oledDriver ! OledDriver.RegisterView("Distributed Data State", 1) val clusterView = ctx.spawn(OledClusterVisualizer(0, settings, oledDriver), "oled-cluster-view") val clusterStatusTracker = ctx.spawn(ClusterStatusTracker(settings, None), "cluster-status-tracker") clusterStatusTracker ! ClusterStatusTracker.SubscribeVisualiser(clusterView) val ddataTracker = ctx.spawn( DistributedDataTracker(1, LWWMapKey[String, String]("cache"), oledDriver), "oled-ddata-view") val routes = new Routes(ddataTracker)(ctx.system) implicit val untypedSystem: akka.actor.ActorSystem = ctx.system.toClassic implicit val mat: Materializer = Materializer.createMaterializer(ctx.system.toClassic) Http()(ctx.system.toClassic).bindAndHandle(routes.route, settings.config.getString("cluster-node-configuration.external-ip"), 8080) Behaviors.receiveSignal { case (_, Terminated(_)) => Behaviors.stopped } } } object DisplayDistributedDataMain { def main(args: Array[String]): Unit = { val settings = Settings() val system = ActorSystem[NotUsed](Main(settings), "akka-oled", settings.config) // Start Akka HTTP Management extension AkkaManagement(system).start() } }
Example 97
Source File: TryRoute.scala From akka-http-test with Apache License 2.0 | 5 votes |
package com.github.dnvriend.component.simpleserver.route import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.server.{ Directives, Route } import spray.json.DefaultJsonProtocol import scala.util.Try object TryRoute extends Directives with SprayJsonSupport with DefaultJsonProtocol { def route: Route = { pathPrefix("try") { (get & path("failure")) { complete(Try((1 / 0).toString)) } ~ (get & path("success")) { complete(Try(1.toString)) } } } }