/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-dev * User agent: apibuilder app.apibuilder.io/michal/playground-api/0.0.1-dev/play_2_x_json */ package io.github.mkows.playground.api.v0.models { sealed trait ItemState extends _root_.scala.Product with _root_.scala.Serializable { def itemStateType: ItemStateType } /** * Defines the valid type values for the type ItemState */ sealed trait ItemStateType extends _root_.scala.Product with _root_.scala.Serializable object ItemStateType { case object ItemStateProgress extends ItemStateType { override def toString = "item_state_progress" } case object ItemStateFailedCause extends ItemStateType { override def toString = "item_state_failed_cause" } final case class UNDEFINED(override val toString: String) extends ItemStateType val all: scala.List[ItemStateType] = scala.List(ItemStateProgress, ItemStateFailedCause) private val byName: Map[String, ItemStateType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ItemStateType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ItemStateType] = byName.get(value.toLowerCase) } final case class Error( code: Int, message: String, details: _root_.scala.Option[String] = None ) final case class Healthcheck( status: String, createdAt: _root_.org.joda.time.DateTime ) /** * Provides future compatibility in clients - in the future, when a type is added * to the union ItemState, it will need to be handled in the client code. This * implementation will deserialize these future types as an instance of this class. * * @param description Information about the type that we received that is undefined in this version of * the client. */ final case class ItemStateUndefinedType( description: String ) extends ItemState { override val itemStateType: ItemStateType = ItemStateType.UNDEFINED(description) } sealed trait ItemStateFailedCause extends ItemState object ItemStateFailedCause { case object ErrorCause1 extends ItemStateFailedCause { override def toString = "ErrorCause1" override val itemStateType: ItemStateType = ItemStateType.ItemStateFailedCause } case object ErrorCause2 extends ItemStateFailedCause { override def toString = "ErrorCause2" override val itemStateType: ItemStateType = ItemStateType.ItemStateFailedCause } case object ErrorCause3 extends ItemStateFailedCause { override def toString = "ErrorCause3" override val itemStateType: ItemStateType = ItemStateType.ItemStateFailedCause } /** * UNDEFINED captures values that are sent either in error or * that were added by the server after this library was * generated. We want to make it easy and obvious to users of * this library to handle this case gracefully. * * We use all CAPS for the variable name to avoid collisions * with the camel cased values above. */ final case class UNDEFINED(override val toString: String) extends ItemStateFailedCause { override val itemStateType: ItemStateType = ItemStateType.ItemStateFailedCause } /** * all returns a list of all the valid, known values. We use * lower case to avoid collisions with the camel cased values * above. */ val all: scala.List[ItemStateFailedCause] = scala.List(ErrorCause1, ErrorCause2, ErrorCause3) private val byName: Map[String, ItemStateFailedCause] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ItemStateFailedCause = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ItemStateFailedCause] = byName.get(value.toLowerCase) } sealed trait ItemStateProgress extends ItemState object ItemStateProgress { case object Init extends ItemStateProgress { override def toString = "Init" override val itemStateType: ItemStateType = ItemStateType.ItemStateProgress } case object Pending extends ItemStateProgress { override def toString = "Pending" override val itemStateType: ItemStateType = ItemStateType.ItemStateProgress } case object Completed extends ItemStateProgress { override def toString = "Completed" override val itemStateType: ItemStateType = ItemStateType.ItemStateProgress } /** * UNDEFINED captures values that are sent either in error or * that were added by the server after this library was * generated. We want to make it easy and obvious to users of * this library to handle this case gracefully. * * We use all CAPS for the variable name to avoid collisions * with the camel cased values above. */ final case class UNDEFINED(override val toString: String) extends ItemStateProgress { override val itemStateType: ItemStateType = ItemStateType.ItemStateProgress } /** * all returns a list of all the valid, known values. We use * lower case to avoid collisions with the camel cased values * above. */ val all: scala.List[ItemStateProgress] = scala.List(Init, Pending, Completed) private val byName: Map[String, ItemStateProgress] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ItemStateProgress = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ItemStateProgress] = byName.get(value.toLowerCase) } } package io.github.mkows.playground.api.v0.models { package object json { import play.api.libs.json.__ import play.api.libs.json.JsString import play.api.libs.json.Writes import play.api.libs.functional.syntax._ import io.github.mkows.playground.api.v0.models.json._ private[v0] implicit val jsonReadsUUID: play.api.libs.json.Reads[_root_.java.util.UUID] = __.read[String].map { str => _root_.java.util.UUID.fromString(str) } private[v0] implicit val jsonWritesUUID: play.api.libs.json.Writes[_root_.java.util.UUID] = (x: _root_.java.util.UUID) => play.api.libs.json.JsString(x.toString) private[v0] implicit val jsonReadsJodaDateTime: play.api.libs.json.Reads[_root_.org.joda.time.DateTime] = __.read[String].map { str => _root_.org.joda.time.format.ISODateTimeFormat.dateTimeParser.parseDateTime(str) } private[v0] implicit val jsonWritesJodaDateTime: play.api.libs.json.Writes[_root_.org.joda.time.DateTime] = (x: _root_.org.joda.time.DateTime) => { play.api.libs.json.JsString(_root_.org.joda.time.format.ISODateTimeFormat.dateTime.print(x)) } private[v0] implicit val jsonReadsJodaLocalDate: play.api.libs.json.Reads[_root_.org.joda.time.LocalDate] = __.read[String].map { str => _root_.org.joda.time.format.ISODateTimeFormat.dateTimeParser.parseLocalDate(str) } private[v0] implicit val jsonWritesJodaLocalDate: play.api.libs.json.Writes[_root_.org.joda.time.LocalDate] = (x: _root_.org.joda.time.LocalDate) => { play.api.libs.json.JsString(_root_.org.joda.time.format.ISODateTimeFormat.date.print(x)) } implicit val jsonReadsPlaygroundApiItemStateFailedCause: play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = new play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] { def reads(js: play.api.libs.json.JsValue): play.api.libs.json.JsResult[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = { js match { case v: play.api.libs.json.JsString => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateFailedCause(v.value)) case _ => { (js \ "value").validate[String] match { case play.api.libs.json.JsSuccess(v, _) => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateFailedCause(v)) case _: play.api.libs.json.JsError => (js \ "item_state_failed_cause").validate[String] match { case play.api.libs.json.JsSuccess(v, _) => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateFailedCause(v)) case err: play.api.libs.json.JsError => err } } } } } } def jsonWritesPlaygroundApiItemStateFailedCause(obj: io.github.mkows.playground.api.v0.models.ItemStateFailedCause) = { play.api.libs.json.JsString(obj.toString) } def jsObjectItemStateFailedCause(obj: io.github.mkows.playground.api.v0.models.ItemStateFailedCause) = { play.api.libs.json.Json.obj("type" -> "item_state_failed_cause", "value" -> play.api.libs.json.JsString(obj.toString)) } implicit def jsonWritesPlaygroundApiItemStateFailedCause: play.api.libs.json.Writes[ItemStateFailedCause] = { (obj: io.github.mkows.playground.api.v0.models.ItemStateFailedCause) => { io.github.mkows.playground.api.v0.models.json.jsObjectItemStateFailedCause(obj) } } implicit val jsonReadsPlaygroundApiItemStateProgress: play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.ItemStateProgress] = new play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.ItemStateProgress] { def reads(js: play.api.libs.json.JsValue): play.api.libs.json.JsResult[io.github.mkows.playground.api.v0.models.ItemStateProgress] = { js match { case v: play.api.libs.json.JsString => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateProgress(v.value)) case _ => { (js \ "value").validate[String] match { case play.api.libs.json.JsSuccess(v, _) => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateProgress(v)) case _: play.api.libs.json.JsError => (js \ "item_state_progress").validate[String] match { case play.api.libs.json.JsSuccess(v, _) => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateProgress(v)) case err: play.api.libs.json.JsError => err } } } } } } def jsonWritesPlaygroundApiItemStateProgress(obj: io.github.mkows.playground.api.v0.models.ItemStateProgress) = { play.api.libs.json.JsString(obj.toString) } def jsObjectItemStateProgress(obj: io.github.mkows.playground.api.v0.models.ItemStateProgress) = { play.api.libs.json.Json.obj("type" -> "item_state_progress", "value" -> play.api.libs.json.JsString(obj.toString)) } implicit def jsonWritesPlaygroundApiItemStateProgress: play.api.libs.json.Writes[ItemStateProgress] = { (obj: io.github.mkows.playground.api.v0.models.ItemStateProgress) => { io.github.mkows.playground.api.v0.models.json.jsObjectItemStateProgress(obj) } } implicit def jsonReadsPlaygroundApiError: play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.Error] = { for { code <- (__ \ "code").read[Int] message <- (__ \ "message").read[String] details <- (__ \ "details").readNullable[String] } yield Error(code, message, details) } def jsObjectError(obj: io.github.mkows.playground.api.v0.models.Error): play.api.libs.json.JsObject = { play.api.libs.json.Json.obj( "code" -> play.api.libs.json.JsNumber(obj.code), "message" -> play.api.libs.json.JsString(obj.message) ) ++ (obj.details match { case None => play.api.libs.json.Json.obj() case Some(x) => play.api.libs.json.Json.obj("details" -> play.api.libs.json.JsString(x)) }) } implicit def jsonWritesPlaygroundApiError: play.api.libs.json.Writes[Error] = { (obj: io.github.mkows.playground.api.v0.models.Error) => { io.github.mkows.playground.api.v0.models.json.jsObjectError(obj) } } implicit def jsonReadsPlaygroundApiHealthcheck: play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.Healthcheck] = { for { status <- (__ \ "status").read[String] createdAt <- (__ \ "created_at").read[_root_.org.joda.time.DateTime] } yield Healthcheck(status, createdAt) } def jsObjectHealthcheck(obj: io.github.mkows.playground.api.v0.models.Healthcheck): play.api.libs.json.JsObject = { play.api.libs.json.Json.obj( "status" -> play.api.libs.json.JsString(obj.status), "created_at" -> play.api.libs.json.JsString(_root_.org.joda.time.format.ISODateTimeFormat.dateTime.print(obj.createdAt)) ) } implicit def jsonWritesPlaygroundApiHealthcheck: play.api.libs.json.Writes[Healthcheck] = { (obj: io.github.mkows.playground.api.v0.models.Healthcheck) => { io.github.mkows.playground.api.v0.models.json.jsObjectHealthcheck(obj) } } implicit def jsonReadsPlaygroundApiItemState: play.api.libs.json.Reads[io.github.mkows.playground.api.v0.models.ItemState] = (js: play.api.libs.json.JsValue) => { def readDiscriminator(discriminator: String) = { discriminator match { case "item_state_progress" => io.github.mkows.playground.api.v0.models.json.jsonReadsPlaygroundApiItemStateProgress.reads(js) case "item_state_failed_cause" => io.github.mkows.playground.api.v0.models.json.jsonReadsPlaygroundApiItemStateFailedCause.reads(js) case other => play.api.libs.json.JsSuccess(io.github.mkows.playground.api.v0.models.ItemStateUndefinedType(other)) } } (js \ "type").validate[String] match { case e: play.api.libs.json.JsError => e case s: play.api.libs.json.JsSuccess[String] => readDiscriminator(s.value) } } def jsObjectItemState(obj: io.github.mkows.playground.api.v0.models.ItemState): play.api.libs.json.JsObject = { obj match { case x: io.github.mkows.playground.api.v0.models.ItemStateProgress => io.github.mkows.playground.api.v0.models.json.jsObjectItemStateProgress(x) case x: io.github.mkows.playground.api.v0.models.ItemStateFailedCause => io.github.mkows.playground.api.v0.models.json.jsObjectItemStateFailedCause(x) case other => { sys.error(s"The type[${other.getClass.getName}] has no JSON writer") } } } implicit def jsonWritesPlaygroundApiItemState: play.api.libs.json.Writes[ItemState] = { (obj: io.github.mkows.playground.api.v0.models.ItemState) => { io.github.mkows.playground.api.v0.models.json.jsObjectItemState(obj) } } } } package io.github.mkows.playground.api.v0 { object Bindables { import play.api.mvc.{PathBindable, QueryStringBindable} // import models directly for backwards compatibility with prior versions of the generator import Core._ import Models._ object Core { implicit def pathBindableDateTimeIso8601(implicit stringBinder: QueryStringBindable[String]): PathBindable[_root_.org.joda.time.DateTime] = ApibuilderPathBindable(ApibuilderTypes.dateTimeIso8601) implicit def queryStringBindableDateTimeIso8601(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[_root_.org.joda.time.DateTime] = ApibuilderQueryStringBindable(ApibuilderTypes.dateTimeIso8601) implicit def pathBindableDateIso8601(implicit stringBinder: QueryStringBindable[String]): PathBindable[_root_.org.joda.time.LocalDate] = ApibuilderPathBindable(ApibuilderTypes.dateIso8601) implicit def queryStringBindableDateIso8601(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[_root_.org.joda.time.LocalDate] = ApibuilderQueryStringBindable(ApibuilderTypes.dateIso8601) } object Models { import io.github.mkows.playground.api.v0.models._ val itemStateFailedCauseConverter: ApibuilderTypeConverter[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = new ApibuilderTypeConverter[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] { override def convert(value: String): io.github.mkows.playground.api.v0.models.ItemStateFailedCause = io.github.mkows.playground.api.v0.models.ItemStateFailedCause(value) override def convert(value: io.github.mkows.playground.api.v0.models.ItemStateFailedCause): String = value.toString override def example: io.github.mkows.playground.api.v0.models.ItemStateFailedCause = io.github.mkows.playground.api.v0.models.ItemStateFailedCause.ErrorCause1 override def validValues: Seq[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = io.github.mkows.playground.api.v0.models.ItemStateFailedCause.all } implicit def pathBindableItemStateFailedCause(implicit stringBinder: QueryStringBindable[String]): PathBindable[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = ApibuilderPathBindable(itemStateFailedCauseConverter) implicit def queryStringBindableItemStateFailedCause(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[io.github.mkows.playground.api.v0.models.ItemStateFailedCause] = ApibuilderQueryStringBindable(itemStateFailedCauseConverter) val itemStateProgressConverter: ApibuilderTypeConverter[io.github.mkows.playground.api.v0.models.ItemStateProgress] = new ApibuilderTypeConverter[io.github.mkows.playground.api.v0.models.ItemStateProgress] { override def convert(value: String): io.github.mkows.playground.api.v0.models.ItemStateProgress = io.github.mkows.playground.api.v0.models.ItemStateProgress(value) override def convert(value: io.github.mkows.playground.api.v0.models.ItemStateProgress): String = value.toString override def example: io.github.mkows.playground.api.v0.models.ItemStateProgress = io.github.mkows.playground.api.v0.models.ItemStateProgress.Init override def validValues: Seq[io.github.mkows.playground.api.v0.models.ItemStateProgress] = io.github.mkows.playground.api.v0.models.ItemStateProgress.all } implicit def pathBindableItemStateProgress(implicit stringBinder: QueryStringBindable[String]): PathBindable[io.github.mkows.playground.api.v0.models.ItemStateProgress] = ApibuilderPathBindable(itemStateProgressConverter) implicit def queryStringBindableItemStateProgress(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[io.github.mkows.playground.api.v0.models.ItemStateProgress] = ApibuilderQueryStringBindable(itemStateProgressConverter) } trait ApibuilderTypeConverter[T] { def convert(value: String): T def convert(value: T): String def example: T def validValues: Seq[T] = Nil def errorMessage(key: String, value: String, ex: java.lang.Exception): String = { val base = s"Invalid value '$value' for parameter '$key'. " validValues.toList match { case Nil => base + "Ex: " + convert(example) case values => base + ". Valid values are: " + values.mkString("'", "', '", "'") } } } object ApibuilderTypes { val dateTimeIso8601: ApibuilderTypeConverter[_root_.org.joda.time.DateTime] = new ApibuilderTypeConverter[_root_.org.joda.time.DateTime] { override def convert(value: String): _root_.org.joda.time.DateTime = _root_.org.joda.time.format.ISODateTimeFormat.dateTimeParser.parseDateTime(value) override def convert(value: _root_.org.joda.time.DateTime): String = _root_.org.joda.time.format.ISODateTimeFormat.dateTime.print(value) override def example: _root_.org.joda.time.DateTime = _root_.org.joda.time.DateTime.now } val dateIso8601: ApibuilderTypeConverter[_root_.org.joda.time.LocalDate] = new ApibuilderTypeConverter[_root_.org.joda.time.LocalDate] { override def convert(value: String): _root_.org.joda.time.LocalDate = _root_.org.joda.time.format.ISODateTimeFormat.dateTimeParser.parseLocalDate(value) override def convert(value: _root_.org.joda.time.LocalDate): String = _root_.org.joda.time.format.ISODateTimeFormat.date.print(value) override def example: _root_.org.joda.time.LocalDate = _root_.org.joda.time.LocalDate.now } } final case class ApibuilderQueryStringBindable[T]( converters: ApibuilderTypeConverter[T] ) extends QueryStringBindable[T] { override def bind(key: String, params: Map[String, Seq[String]]): _root_.scala.Option[_root_.scala.Either[String, T]] = { params.getOrElse(key, Nil).headOption.map { v => try { Right( converters.convert(v) ) } catch { case ex: java.lang.Exception => Left( converters.errorMessage(key, v, ex) ) } } } override def unbind(key: String, value: T): String = { s"$key=${converters.convert(value)}" } } final case class ApibuilderPathBindable[T]( converters: ApibuilderTypeConverter[T] ) extends PathBindable[T] { override def bind(key: String, value: String): _root_.scala.Either[String, T] = { try { Right( converters.convert(value) ) } catch { case ex: java.lang.Exception => Left( converters.errorMessage(key, value, ex) ) } } override def unbind(key: String, value: T): String = { converters.convert(value) } } } }