/** * Generated by API Builder - https://www.apibuilder.io * Service version: 2.0.1 * User agent: apibuilder app.apibuilder.io/chio/provided_test_app3/2.0.1/play_2_2_client */ package io.chio.provided.test.app3.v2.models { /** * A generic error providing a message and some basic meta data. * * @param id A guid for identifying the error e.g. in log files. * @param timestamp Timestamp of the when the error occurred. * @param source An optional source on which the error occurred. * @param `type` An optional type for categorizing e.g. `validation` or `invalid_state`. * @param message The actual message of the error. */ final case class GenericError( id: _root_.java.util.UUID, timestamp: _root_.org.joda.time.DateTime, source: _root_.scala.Option[String] = None, `type`: _root_.scala.Option[String] = None, message: String ) /** * Response model indicating the health of a service. * * @param health The current health of the service. Normally this is `healthy`, otherwise an * simple error message can be shown. */ final case class Healthcheck( health: String ) } package io.chio.provided.test.app3.v2.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.chio.provided.test.app3.v2.models.json._ private[v2] implicit val jsonReadsUUID: play.api.libs.json.Reads[_root_.java.util.UUID] = __.read[String].map { str => _root_.java.util.UUID.fromString(str) } private[v2] 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[v2] 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[v2] 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[v2] 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[v2] 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 def jsonReadsProvidedTestApp3GenericError: play.api.libs.json.Reads[io.chio.provided.test.app3.v2.models.GenericError] = { for { id <- (__ \ "id").read[_root_.java.util.UUID] timestamp <- (__ \ "timestamp").read[_root_.org.joda.time.DateTime] source <- (__ \ "source").readNullable[String] `type` <- (__ \ "type").readNullable[String] message <- (__ \ "message").read[String] } yield GenericError(id, timestamp, source, `type`, message) } def jsObjectGenericError(obj: io.chio.provided.test.app3.v2.models.GenericError): play.api.libs.json.JsObject = { play.api.libs.json.Json.obj( "id" -> play.api.libs.json.JsString(obj.id.toString), "timestamp" -> play.api.libs.json.JsString(_root_.org.joda.time.format.ISODateTimeFormat.dateTime.print(obj.timestamp)), "message" -> play.api.libs.json.JsString(obj.message) ) ++ (obj.source match { case None => play.api.libs.json.Json.obj() case Some(x) => play.api.libs.json.Json.obj("source" -> play.api.libs.json.JsString(x)) }) ++ (obj.`type` match { case None => play.api.libs.json.Json.obj() case Some(x) => play.api.libs.json.Json.obj("type" -> play.api.libs.json.JsString(x)) }) } implicit def jsonWritesProvidedTestApp3GenericError: play.api.libs.json.Writes[GenericError] = { (obj: io.chio.provided.test.app3.v2.models.GenericError) => { io.chio.provided.test.app3.v2.models.json.jsObjectGenericError(obj) } } implicit def jsonReadsProvidedTestApp3Healthcheck: play.api.libs.json.Reads[io.chio.provided.test.app3.v2.models.Healthcheck] = { (__ \ "health").read[String].map { x => Healthcheck(health = x) } } def jsObjectHealthcheck(obj: io.chio.provided.test.app3.v2.models.Healthcheck): play.api.libs.json.JsObject = { play.api.libs.json.Json.obj( "health" -> play.api.libs.json.JsString(obj.health) ) } implicit def jsonWritesProvidedTestApp3Healthcheck: play.api.libs.json.Writes[Healthcheck] = { (obj: io.chio.provided.test.app3.v2.models.Healthcheck) => { io.chio.provided.test.app3.v2.models.json.jsObjectHealthcheck(obj) } } } } package io.chio.provided.test.app3.v2 { object Bindables { import play.api.mvc.{PathBindable, QueryStringBindable} // import models directly for backwards compatibility with prior versions of the generator import Core._ 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) } 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.chio.provided.test.app3.v2 { object Constants { val Namespace = "io.chio.provided.test.app3.v2" val UserAgent = "apibuilder app.apibuilder.io/chio/provided_test_app3/2.0.1/play_2_2_client" val Version = "2.0.1" val VersionMajor = 2 } class Client( val baseUrl: String, auth: scala.Option[io.chio.provided.test.app3.v2.Authorization] = None, defaultHeaders: Seq[(String, String)] = Nil ) extends interfaces.Client { import io.chio.provided.test.app3.v2.models.json._ private val logger = play.api.Logger("io.chio.provided.test.app3.v2.Client") logger.info(s"Initializing io.chio.provided.test.app3.v2.Client for url $baseUrl") def _requestHolder(path: String): play.api.libs.ws.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(""), com.ning.http.client.Realm.AuthScheme.BASIC) } } } def _logRequest(method: String, req: play.api.libs.ws.WS.WSRequestHolder): play.api.libs.ws.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.Response] = { 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" => { sys.error("PATCH method is not supported in Play Framework Version 2.2.x") } 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.Response, 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.chio.provided.test.app3.v2.errors.FailedRequest(r.status, s"Invalid json for class[" + className + "]: " + errors.mkString(" "), requestUri = Some(r.ahcResponse.getUri)) } } } } 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 } } package errors { final case class FailedRequest(responseCode: Int, message: String, requestUri: Option[_root_.java.net.URI] = None) extends _root_.java.lang.Exception(s"HTTP $responseCode: $message") } }