/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.4-dev * User agent: apibuilder app.apibuilder.io/michal/playground-api/0.0.4-dev/play_2_3_client */ 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, messageCode: String, messageLengthy: 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 for 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 for 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 err: 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 err: 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] messageCode <- (__ \ "message_code").read[String] messageLengthy <- (__ \ "message_lengthy").read[String] details <- (__ \ "details").readNullable[String] } yield Error(code, message, messageCode, messageLengthy, 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), "message_code" -> play.api.libs.json.JsString(obj.messageCode), "message_lengthy" -> play.api.libs.json.JsString(obj.messageLengthy) ) ++ (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) } } } } package io.github.mkows.playground.api.v0 { object Constants { val BaseUrl = "https://mkows.github.io" val Namespace = "io.github.mkows.playground.api.v0" val UserAgent = "apibuilder app.apibuilder.io/michal/playground-api/0.0.4-dev/play_2_3_client" val Version = "0.0.4-dev" val VersionMajor = 0 } class Client( val baseUrl: String = "https://mkows.github.io", auth: scala.Option[io.github.mkows.playground.api.v0.Authorization] = None, defaultHeaders: Seq[(String, String)] = Nil ) extends interfaces.Client { import io.github.mkows.playground.api.v0.models.json._ private val logger = play.api.Logger("io.github.mkows.playground.api.v0.Client") logger.info(s"Initializing io.github.mkows.playground.api.v0.Client for url $baseUrl") def healthchecks: Healthchecks = Healthchecks object Healthchecks extends Healthchecks { override def get( requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.github.mkows.playground.api.v0.models.Healthcheck] = { _executeRequest("GET", s"/_internal_/healthcheck", requestHeaders = requestHeaders).map { case r if r.status == 200 => _root_.io.github.mkows.playground.api.v0.Client.parseJson("io.github.mkows.playground.api.v0.models.Healthcheck", r, _.validate[io.github.mkows.playground.api.v0.models.Healthcheck]) case r => throw io.github.mkows.playground.api.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 200") } } override def postTestFormParamsJson( param1: String, requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Unit] = { val payload = play.api.libs.json.Json.obj( "param1" -> play.api.libs.json.Json.toJson(param1) ) _executeRequest("POST", s"/_internal_/healthcheck/test_form_params_json", body = Some(payload), requestHeaders = requestHeaders).map { case r if r.status == 204 => () case r => throw io.github.mkows.playground.api.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 204") } } override def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.github.mkows.playground.api.v0.models.Healthcheck] = { _executeRequest("GET", s"/_internal_/healthcheck/${id}", requestHeaders = requestHeaders).map { case r if r.status == 200 => _root_.io.github.mkows.playground.api.v0.Client.parseJson("io.github.mkows.playground.api.v0.models.Healthcheck", r, _.validate[io.github.mkows.playground.api.v0.models.Healthcheck]) case r if r.status == 400 => throw io.github.mkows.playground.api.v0.errors.ErrorResponse(r) case r if r.status == 401 => throw io.github.mkows.playground.api.v0.errors.UnitResponse(r.status) case r if r.status == 403 => throw io.github.mkows.playground.api.v0.errors.ValueResponse(r) case r if r.status == 404 => throw io.github.mkows.playground.api.v0.errors.UnitResponse(r.status) case r => throw io.github.mkows.playground.api.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 200, 400, 401, 403, 404") } } } def _requestHolder(path: String): play.api.libs.ws.WSRequestHolder = { import play.api.Play.current val holder = play.api.libs.ws.WS.url(baseUrl + path).withHeaders( "User-Agent" -> Constants.UserAgent, "X-Apidoc-Version" -> Constants.Version, "X-Apidoc-Version-Major" -> Constants.VersionMajor.toString ).withHeaders(defaultHeaders: _*) auth.fold(holder) { case Authorization.Basic(username, password) => { holder.withAuth(username, password.getOrElse(""), play.api.libs.ws.WSAuthScheme.BASIC) } } } def _logRequest(method: String, req: play.api.libs.ws.WSRequestHolder): play.api.libs.ws.WSRequestHolder = { val queryComponents = for { (name, values) <- req.queryString value <- values } yield s"$name=$value" val url = s"${req.url}${queryComponents.mkString("?", "&", "")}" auth.fold(logger.info(s"curl -X $method '$url'")) { _ => logger.info(s"curl -X $method -u '[REDACTED]:' '$url'") } req } def _executeRequest( method: String, path: String, queryParameters: Seq[(String, String)] = Nil, requestHeaders: Seq[(String, String)] = Nil, body: Option[play.api.libs.json.JsValue] = None ): scala.concurrent.Future[play.api.libs.ws.WSResponse] = { method.toUpperCase match { case "GET" => { _logRequest("GET", _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)).get() } case "POST" => { _logRequest("POST", _requestHolder(path).withHeaders(_withJsonContentType(requestHeaders): _*).withQueryString(queryParameters: _*)).post(body.getOrElse(play.api.libs.json.Json.obj())) } case "PUT" => { _logRequest("PUT", _requestHolder(path).withHeaders(_withJsonContentType(requestHeaders): _*).withQueryString(queryParameters: _*)).put(body.getOrElse(play.api.libs.json.Json.obj())) } case "PATCH" => { _logRequest("PATCH", _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)).patch(body.getOrElse(play.api.libs.json.Json.obj())) } case "DELETE" => { _logRequest("DELETE", _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)).delete() } case "HEAD" => { _logRequest("HEAD", _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)).head() } case "OPTIONS" => { _logRequest("OPTIONS", _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)).options() } case _ => { _logRequest(method, _requestHolder(path).withHeaders(requestHeaders: _*).withQueryString(queryParameters: _*)) sys.error("Unsupported method[%s]".format(method)) } } } /** * Adds a Content-Type: application/json header unless the specified requestHeaders * already contain a Content-Type header */ def _withJsonContentType(headers: Seq[(String, String)]): Seq[(String, String)] = { headers.find { _._1.toUpperCase == "CONTENT-TYPE" } match { case None => headers ++ Seq("Content-Type" -> "application/json; charset=UTF-8") case Some(_) => headers } } } object Client { def parseJson[T]( className: String, r: play.api.libs.ws.WSResponse, f: (play.api.libs.json.JsValue => play.api.libs.json.JsResult[T]) ): T = { f(play.api.libs.json.Json.parse(r.body)) match { case play.api.libs.json.JsSuccess(x, _) => x case play.api.libs.json.JsError(errors) => { throw io.github.mkows.playground.api.v0.errors.FailedRequest(r.status, s"Invalid json for class[" + className + "]: " + errors.mkString(" ")) } } } } sealed trait Authorization extends _root_.scala.Product with _root_.scala.Serializable object Authorization { final case class Basic(username: String, password: Option[String] = None) extends Authorization } package interfaces { trait Client { def baseUrl: String def healthchecks: io.github.mkows.playground.api.v0.Healthchecks } } trait Healthchecks { def get( requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.github.mkows.playground.api.v0.models.Healthcheck] def postTestFormParamsJson( param1: String, requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Unit] def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil )(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.github.mkows.playground.api.v0.models.Healthcheck] } package errors { import io.github.mkows.playground.api.v0.models.json._ final case class ErrorResponse( response: play.api.libs.ws.WSResponse, message: Option[String] = None ) extends Exception(message.getOrElse(s"${response.status}: ${response.body}")) { lazy val error = _root_.io.github.mkows.playground.api.v0.Client.parseJson("io.github.mkows.playground.api.v0.models.Error", response, _.validate[io.github.mkows.playground.api.v0.models.Error]) } final case class UnitResponse(status: Int) extends Exception(s"HTTP $status") final case class ValueResponse( response: play.api.libs.ws.WSResponse, message: Option[String] = None ) extends Exception(message.getOrElse(s"${response.status}: ${response.body}")) { lazy val value = _root_.io.github.mkows.playground.api.v0.Client.parseJson("String", response, _.validate[String]) } final case class FailedRequest(responseCode: Int, message: String, requestUri: Option[_root_.java.net.URI] = None) extends _root_.java.lang.Exception(s"HTTP $responseCode: $message") } }