/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-dev * User agent: apibuilder app.apibuilder.io/nicolai-moraru/joglog-api/0.0.1-dev/http4s_0_17 */ package nm.joglog.api.v0.models { /** * Time entry * * @param id Unique time id * @param distance Distance in meters * @param time Time in seconds * @param createdAt Creation date-time * @param updatedAt Last update date-time */ final case class Time( id: Int, distance: Int, time: Int, createdAt: _root_.java.time.Instant, updatedAt: _root_.java.time.Instant ) /** * Time entry * * @param distance Distance in meters * @param time Time in seconds */ final case class TimeMinimal( distance: Int, time: Int ) /** * Time patch entry */ final case class TimePatchEntry( op: .models.Operation, path: time.models.Path, value: Int ) /** * User * * @param id Unique user id * @param username Username * @param role User Role * @param createdAt Creation date-time * @param updatedAt Last update date-time */ final case class User( id: Int, username: String, role: .models.Role, createdAt: _root_.java.time.Instant, updatedAt: _root_.java.time.Instant ) /** * User * * @param username Username * @param password Password */ final case class UserMinimal( username: String, password: String ) /** * User patch entry */ final case class UserPatchEntry( op: .models.Operation, path: user.models.Path, value: nm.joglog.api.v0.models.StringOrInteger ) /** * Patch operation */ sealed trait PatchOperation extends _root_.scala.Product with _root_.scala.Serializable object PatchOperation { case object Test extends PatchOperation { override def toString = "test" } case object Replace extends PatchOperation { override def toString = "replace" } /** * 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 PatchOperation /** * 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[PatchOperation] = scala.List(Test, Replace) private val byName: Map[String, PatchOperation] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): PatchOperation = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[PatchOperation] = byName.get(value.toLowerCase) } /** * Integer for ID or string for any other */ sealed trait StringOrInteger extends _root_.scala.Product with _root_.scala.Serializable object StringOrInteger { case object String extends StringOrInteger { override def toString = "string" } case object Integer extends StringOrInteger { override def toString = "integer" } /** * 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 StringOrInteger /** * 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[StringOrInteger] = scala.List(String, Integer) private val byName: Map[String, StringOrInteger] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): StringOrInteger = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[StringOrInteger] = byName.get(value.toLowerCase) } /** * Time patch field path (id only for test) */ sealed trait TimePatchPath extends _root_.scala.Product with _root_.scala.Serializable object TimePatchPath { case object Distance extends TimePatchPath { override def toString = "distance" } case object Time extends TimePatchPath { override def toString = "time" } case object Id extends TimePatchPath { override def toString = "id" } /** * 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 TimePatchPath /** * 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[TimePatchPath] = scala.List(Distance, Time, Id) private val byName: Map[String, TimePatchPath] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): TimePatchPath = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[TimePatchPath] = byName.get(value.toLowerCase) } /** * User patch field path (id only for test) */ sealed trait UserPatchPath extends _root_.scala.Product with _root_.scala.Serializable object UserPatchPath { case object Username extends UserPatchPath { override def toString = "username" } case object Password extends UserPatchPath { override def toString = "password" } case object Role extends UserPatchPath { override def toString = "role" } case object Id extends UserPatchPath { override def toString = "id" } /** * 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 UserPatchPath /** * 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[UserPatchPath] = scala.List(Username, Password, Role, Id) private val byName: Map[String, UserPatchPath] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): UserPatchPath = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[UserPatchPath] = byName.get(value.toLowerCase) } /** * User Role */ sealed trait UserRole extends _root_.scala.Product with _root_.scala.Serializable object UserRole { /** * Regular user */ case object User extends UserRole { override def toString = "user" } /** * User manager */ case object Manager extends UserRole { override def toString = "manager" } /** * The God himself */ case object Admin extends UserRole { override def toString = "admin" } /** * 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 UserRole /** * 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[UserRole] = scala.List(User, Manager, Admin) private val byName: Map[String, UserRole] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): UserRole = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[UserRole] = byName.get(value.toLowerCase) } } package nm.joglog.api.v0.models { package object json { import io.circe.Decoder._ import io.circe.Encoder._ import scala.language.implicitConversions // See below - Make Scala 2.11 Either monadic import scala.util.Try import io.circe.{Json, JsonObject, Encoder, Decoder, DecodingFailure} import io.circe.syntax._ import nm.joglog.api.v0.models.json._ // Make Scala 2.11 Either monadic private[v0] implicit def eitherOps[A,B](e: Either[A,B]) = cats.implicits.catsSyntaxEither(e) private[v0] implicit val decodeUUID: Decoder[_root_.java.util.UUID] = Decoder.decodeString.emapTry(str => Try(_root_.java.util.UUID.fromString(str))) private[v0] implicit val encodeUUID: Encoder[_root_.java.util.UUID] = Encoder.encodeString.contramap[_root_.java.util.UUID](uuid => uuid.toString) private[v0] implicit val decodeInstant: Decoder[_root_.java.time.Instant] = Decoder.decodeString.emapTry(str => Try(_root_.java.time.OffsetDateTime.parse(str).toInstant)) private[v0] implicit val encodeInstant: Encoder[_root_.java.time.Instant] = Encoder.encodeString.contramap[_root_.java.time.Instant](_.toString) private[v0] implicit val decodeLocalDate: Decoder[_root_.java.time.LocalDate] = Decoder.decodeString.emapTry(str => Try(_root_.java.time.LocalDate.parse(str))) private[v0] implicit val encodeLocalDate: Encoder[_root_.java.time.LocalDate] = Encoder.encodeString.contramap[_root_.java.time.LocalDate](_.toString) implicit val jsonDecoderJoglogApiPatchOperation: Decoder[nm.joglog.api.v0.models.PatchOperation] = Decoder.decodeString.map(nm.joglog.api.v0.models.PatchOperation(_)) implicit val jsonEncoderJoglogApiPatchOperation: Encoder[nm.joglog.api.v0.models.PatchOperation] = Encoder.encodeString.contramap[nm.joglog.api.v0.models.PatchOperation](_.toString) implicit val jsonDecoderJoglogApiStringOrInteger: Decoder[nm.joglog.api.v0.models.StringOrInteger] = Decoder.decodeString.map(nm.joglog.api.v0.models.StringOrInteger(_)) implicit val jsonEncoderJoglogApiStringOrInteger: Encoder[nm.joglog.api.v0.models.StringOrInteger] = Encoder.encodeString.contramap[nm.joglog.api.v0.models.StringOrInteger](_.toString) implicit val jsonDecoderJoglogApiTimePatchPath: Decoder[nm.joglog.api.v0.models.TimePatchPath] = Decoder.decodeString.map(nm.joglog.api.v0.models.TimePatchPath(_)) implicit val jsonEncoderJoglogApiTimePatchPath: Encoder[nm.joglog.api.v0.models.TimePatchPath] = Encoder.encodeString.contramap[nm.joglog.api.v0.models.TimePatchPath](_.toString) implicit val jsonDecoderJoglogApiUserPatchPath: Decoder[nm.joglog.api.v0.models.UserPatchPath] = Decoder.decodeString.map(nm.joglog.api.v0.models.UserPatchPath(_)) implicit val jsonEncoderJoglogApiUserPatchPath: Encoder[nm.joglog.api.v0.models.UserPatchPath] = Encoder.encodeString.contramap[nm.joglog.api.v0.models.UserPatchPath](_.toString) implicit val jsonDecoderJoglogApiUserRole: Decoder[nm.joglog.api.v0.models.UserRole] = Decoder.decodeString.map(nm.joglog.api.v0.models.UserRole(_)) implicit val jsonEncoderJoglogApiUserRole: Encoder[nm.joglog.api.v0.models.UserRole] = Encoder.encodeString.contramap[nm.joglog.api.v0.models.UserRole](_.toString) implicit def decodeJoglogApiTime: Decoder[Time] = Decoder.instance { c => for { id <- c.downField("id").as[Int] distance <- c.downField("distance").as[Int] time <- c.downField("time").as[Int] createdAt <- c.downField("createdAt").as[_root_.java.time.Instant] updatedAt <- c.downField("updatedAt").as[_root_.java.time.Instant] } yield { Time( id = id, distance = distance, time = time, createdAt = createdAt, updatedAt = updatedAt ) } } implicit def encodeJoglogApiTime: Encoder[Time] = Encoder.instance { t => Json.fromFields(Seq( Some("id" -> t.id.asJson), Some("distance" -> t.distance.asJson), Some("time" -> t.time.asJson), Some("createdAt" -> t.createdAt.asJson), Some("updatedAt" -> t.updatedAt.asJson) ).flatten) } implicit def decodeJoglogApiTimeMinimal: Decoder[TimeMinimal] = Decoder.instance { c => for { distance <- c.downField("distance").as[Int] time <- c.downField("time").as[Int] } yield { TimeMinimal( distance = distance, time = time ) } } implicit def encodeJoglogApiTimeMinimal: Encoder[TimeMinimal] = Encoder.instance { t => Json.fromFields(Seq( Some("distance" -> t.distance.asJson), Some("time" -> t.time.asJson) ).flatten) } implicit def decodeJoglogApiTimePatchEntry: Decoder[TimePatchEntry] = Decoder.instance { c => for { op <- c.downField("op").as[.models.Operation] path <- c.downField("path").as[time.models.Path] value <- c.downField("value").as[Int] } yield { TimePatchEntry( op = op, path = path, value = value ) } } implicit def encodeJoglogApiTimePatchEntry: Encoder[TimePatchEntry] = Encoder.instance { t => Json.fromFields(Seq( Some("op" -> t.op.asJson), Some("path" -> t.path.asJson), Some("value" -> t.value.asJson) ).flatten) } implicit def decodeJoglogApiUser: Decoder[User] = Decoder.instance { c => for { id <- c.downField("id").as[Int] username <- c.downField("username").as[String] role <- c.downField("role").as[.models.Role] createdAt <- c.downField("createdAt").as[_root_.java.time.Instant] updatedAt <- c.downField("updatedAt").as[_root_.java.time.Instant] } yield { User( id = id, username = username, role = role, createdAt = createdAt, updatedAt = updatedAt ) } } implicit def encodeJoglogApiUser: Encoder[User] = Encoder.instance { t => Json.fromFields(Seq( Some("id" -> t.id.asJson), Some("username" -> t.username.asJson), Some("role" -> t.role.asJson), Some("createdAt" -> t.createdAt.asJson), Some("updatedAt" -> t.updatedAt.asJson) ).flatten) } implicit def decodeJoglogApiUserMinimal: Decoder[UserMinimal] = Decoder.instance { c => for { username <- c.downField("username").as[String] password <- c.downField("password").as[String] } yield { UserMinimal( username = username, password = password ) } } implicit def encodeJoglogApiUserMinimal: Encoder[UserMinimal] = Encoder.instance { t => Json.fromFields(Seq( Some("username" -> t.username.asJson), Some("password" -> t.password.asJson) ).flatten) } implicit def decodeJoglogApiUserPatchEntry: Decoder[UserPatchEntry] = Decoder.instance { c => for { op <- c.downField("op").as[.models.Operation] path <- c.downField("path").as[user.models.Path] value <- c.downField("value").as[nm.joglog.api.v0.models.StringOrInteger] } yield { UserPatchEntry( op = op, path = path, value = value ) } } implicit def encodeJoglogApiUserPatchEntry: Encoder[UserPatchEntry] = Encoder.instance { t => Json.fromFields(Seq( Some("op" -> t.op.asJson), Some("path" -> t.path.asJson), Some("value" -> t.value.asJson) ).flatten) } } } package nm.joglog.api.v0 { import org.http4s.client.blaze._ import io.circe.syntax._ object Constants { val BaseUrl = "http://localhost:8081" val Namespace = "nm.joglog.api.v0" val UserAgent = "apibuilder app.apibuilder.io/nicolai-moraru/joglog-api/0.0.1-dev/http4s_0_17" val Version = "0.0.1-dev" val VersionMajor = 0 } class Client( val baseUrl: org.http4s.Uri = org.http4s.Uri.unsafeFromString("http://localhost:8081"), auth: scala.Option[nm.joglog.api.v0.Authorization] = None, defaultHeaders: Seq[(String, String)] = Nil, asyncHttpClient: org.http4s.client.Client = Client.defaultAsyncHttpClient ) extends interfaces.Client { import org.http4s.Response import nm.joglog.api.v0.models.json._ def closeAsyncHttpClient(): Unit = { asyncHttpClient.shutdownNow() } def times: Times = Times def users: Users = Users object Times extends Times { override def post( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] = { val urlPath = Seq("times") val (payload, formPayload) = (Some(minimal), None) _executeRequest[.models.Minimal, nm.joglog.api.v0.models.Time]("POST", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 201 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.Time]("nm.joglog.api.v0.models.Time", r) case r if r.status.code == 400 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 201, 400, 403")) } } override def get( requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[Seq[nm.joglog.api.v0.models.Time]] = { val urlPath = Seq("times") _executeRequest[Unit, Seq[nm.joglog.api.v0.models.Time]]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[Seq[nm.joglog.api.v0.models.Time]]("Seq[nm.joglog.api.v0.models.Time]", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403")) } } override def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] = { val urlPath = Seq("times", id.toString) _executeRequest[Unit, nm.joglog.api.v0.models.Time]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.Time]("nm.joglog.api.v0.models.Time", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 404 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403, 404")) } } override def patch( entries: Seq[time.models.Entry], requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] = { val urlPath = Seq("times") val (payload, formPayload) = (Some(entries), None) _executeRequest[Seq[time.models.Entry], nm.joglog.api.v0.models.Time]("PATCH", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.Time]("nm.joglog.api.v0.models.Time", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403")) } } override def deleteById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] = { val urlPath = Seq("times", id.toString) _executeRequest[Unit, nm.joglog.api.v0.models.Time]("DELETE", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.Time]("nm.joglog.api.v0.models.Time", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 404 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403, 404")) } } } object Users extends Users { override def postAuthenticate( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[String] = { val urlPath = Seq("users", "authenticate") val (payload, formPayload) = (Some(minimal), None) _executeRequest[.models.Minimal, String]("POST", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 201 => _root_.nm.joglog.api.v0.Client.parseJson[String]("String", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 201, 403")) } } override def post( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] = { val urlPath = Seq("users") val (payload, formPayload) = (Some(minimal), None) _executeRequest[.models.Minimal, nm.joglog.api.v0.models.User]("POST", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 201 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.User]("nm.joglog.api.v0.models.User", r) case r if r.status.code == 400 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 201, 400, 403")) } } override def get( requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[Seq[nm.joglog.api.v0.models.User]] = { val urlPath = Seq("users") _executeRequest[Unit, Seq[nm.joglog.api.v0.models.User]]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[Seq[nm.joglog.api.v0.models.User]]("Seq[nm.joglog.api.v0.models.User]", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403")) } } override def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] = { val urlPath = Seq("users", id.toString) _executeRequest[Unit, nm.joglog.api.v0.models.User]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.User]("nm.joglog.api.v0.models.User", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 404 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403, 404")) } } override def patch( entries: Seq[user.models.Entry], requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] = { val urlPath = Seq("users") val (payload, formPayload) = (Some(entries), None) _executeRequest[Seq[user.models.Entry], nm.joglog.api.v0.models.User]("PATCH", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.User]("nm.joglog.api.v0.models.User", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403")) } } override def deleteById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] = { val urlPath = Seq("users", id.toString) _executeRequest[Unit, nm.joglog.api.v0.models.User]("DELETE", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.nm.joglog.api.v0.Client.parseJson[nm.joglog.api.v0.models.User]("nm.joglog.api.v0.models.User", r) case r if r.status.code == 403 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r if r.status.code == 404 => fs2.Task.fail(new nm.joglog.api.v0.errors.ValueResponse(r)) case r => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 403, 404")) } } } private lazy val defaultApiHeaders = Seq( ("User-Agent", Constants.UserAgent), ("X-Apidoc-Version", Constants.Version), ("X-Apidoc-Version-Major", Constants.VersionMajor.toString) ) def apiHeaders: Seq[(String, String)] = defaultApiHeaders def modifyRequest(request: fs2.Task[org.http4s.Request]): fs2.Task[org.http4s.Request] = request implicit def circeJsonEncoder[A](implicit encoder: io.circe.Encoder[A]) = org.http4s.circe.jsonEncoderOf[A] def _executeRequest[T, U]( method: String, path: Seq[String], queryParameters: Seq[(String, String)] = Nil, requestHeaders: Seq[(String, String)] = Nil, body: Option[T] = None, formBody : Option[org.http4s.UrlForm] = None )(handler: org.http4s.Response => fs2.Task[U] )(implicit encoder: io.circe.Encoder[T]): fs2.Task[U] = { import org.http4s.QueryParamEncoder._ val m = org.http4s.Method.fromString(method) match { case Right(m) => m case Left(e) => sys.error(e.toString) } val headers = org.http4s.Headers(( apiHeaders ++ defaultHeaders ++ requestHeaders ).groupBy(_._1).map { case (k, l) => org.http4s.Header(k, l.last._2) }.toList) val queryMap = queryParameters.groupBy(_._1).map { case (k, v) => k -> v.map(_._2) } val uri = path.foldLeft(baseUrl){ case (uri, segment) => uri / segment }.setQueryParams(queryMap) val request = org.http4s.Request(method = m, uri = uri, headers = headers) val reqAndMaybeAuth = auth.fold(request) { case Authorization.Basic(username, passwordOpt) => { val userpass = s"$username:${passwordOpt.getOrElse("")}" val token = java.util.Base64.getEncoder.encodeToString(userpass.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1)) request.putHeaders(org.http4s.Header("Authorization", s"Basic $token")) } case a => sys.error("Invalid authorization scheme[" + a.getClass + "]") } val reqAndMaybeAuthAndBody = if (formBody.nonEmpty) formBody.fold(fs2.Task.now(reqAndMaybeAuth))(reqAndMaybeAuth.withBody) else body.fold(fs2.Task.now(reqAndMaybeAuth))(reqAndMaybeAuth.withBody) asyncHttpClient.fetch(modifyRequest(reqAndMaybeAuthAndBody))(handler) } } object Client { implicit def circeJsonDecoder[A](implicit decoder: io.circe.Decoder[A]) = org.http4s.circe.jsonOf[A] private lazy val defaultAsyncHttpClient = PooledHttp1Client() def parseJson[T]( className: String, r: org.http4s.Response )(implicit decoder: io.circe.Decoder[T]): fs2.Task[T] = r.attemptAs[T].value.flatMap { case Right(value) => fs2.Task.now(value) case Left(error) => fs2.Task.fail(new nm.joglog.api.v0.errors.FailedRequest(r.status.code, s"Invalid json for class[" + className + "]", None, error)) } } 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: org.http4s.Uri def times: nm.joglog.api.v0.Times def users: nm.joglog.api.v0.Users } } trait Times { /** * Create a time entry */ def post( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] /** * List all times */ def get( requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[Seq[nm.joglog.api.v0.models.Time]] /** * Get a specific time entry * * @param id Time entry ID */ def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] /** * Patch a time entry */ def patch( entries: Seq[time.models.Entry], requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] /** * Remove a specific time entry * * @param id Time ID */ def deleteById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.Time] } trait Users { /** * Authentication */ def postAuthenticate( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[String] /** * Create a user */ def post( minimal: .models.Minimal, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] /** * List all users */ def get( requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[Seq[nm.joglog.api.v0.models.User]] /** * Get a specific user * * @param id User ID */ def getById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] /** * Patch a user */ def patch( entries: Seq[user.models.Entry], requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] /** * Remove a specific user * * @param id User ID */ def deleteById( id: Int, requestHeaders: Seq[(String, String)] = Nil ): fs2.Task[nm.joglog.api.v0.models.User] } package errors { import nm.joglog.api.v0.models.json._ final case class ValueResponse( response: org.http4s.Response, message: Option[String] = None ) extends Exception(message.getOrElse(s"${response.status.code}: ${response.body}")) { lazy val value = _root_.nm.joglog.api.v0.Client.parseJson[String]("String", response) } final case class FailedRequest(responseCode: Int, message: String, requestUri: Option[_root_.java.net.URI] = None, parent: Exception = null) extends _root_.java.lang.Exception(s"HTTP $responseCode: $message", parent) } }