/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1 * User agent: apibuilder app.apibuilder.io/nccloud/reporting-api/0.0.1/http4s_0_15 */ package de.nccloud.reporting.api.v0.models { sealed trait ReportingEvent extends _root_.scala.Product with _root_.scala.Serializable { def reportingEventType: ReportingEventType } /** * Defines the valid type values for the type ReportingEvent */ sealed trait ReportingEventType extends _root_.scala.Product with _root_.scala.Serializable object ReportingEventType { case object UpdateEvent extends ReportingEventType { override def toString = "update_event" } case object DeleteEvent extends ReportingEventType { override def toString = "delete_event" } final case class UNDEFINED(override val toString: String) extends ReportingEventType val all: scala.List[ReportingEventType] = scala.List(UpdateEvent, DeleteEvent) private val byName: Map[String, ReportingEventType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ReportingEventType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ReportingEventType] = byName.get(value.toLowerCase) } /** * Deletes a reporting entity defined by its `tracking_id`. * * @param trackingId A unique id, which identifies the association of multiple events. This can i.e. * be a user guid, for events related to a user. It is important, that this id is * unique and immutable. * @param audit Audit information about this event. */ final case class DeleteEvent( trackingId: String, audit: de.nccloud.common.v0.models.Audit ) extends ReportingEvent { override val reportingEventType: ReportingEventType = ReportingEventType.DeleteEvent } /** * Single field of a topic schema. A field can be used for projecting, filtering * and sorting in reports. * * @param `type` The (scalar) type of the field. Fields can only be primitive types. * @param isArray Setting this to `true` means the field is an array of given type. * @param fields Nested fields for type `object`. When defining fields but not having set the * type to `object`, the fields will be ignored. When setting the type to `object`, * the fields need to be defined. * @param label A human-readable label for the field. * @param description A human-readable description of the field. */ final case class Field( `type`: de.nccloud.reporting.api.v0.models.ScalarType, isArray: Boolean, fields: _root_.scala.Option[Map[String, de.nccloud.reporting.api.v0.models.Field]] = None, label: _root_.scala.Option[String] = None, description: _root_.scala.Option[String] = None ) /** * Defines a condition for values. * * @param expression An SQL-like expression. * @param variables A map of values, which can be used in the expression. Variables can be access by * prepending them with `$`. */ final case class Filter( expression: _root_.scala.Option[String] = None, variables: _root_.scala.Option[Map[String, _root_.io.circe.Json]] = None ) /** * A single field of a projection schema. * * @param label A human-readable label for the field. * @param description A human-readable description for the field. * @param source The source of the field. Either `source` or `fields` must be set. * @param fields Defines nested fields for this projection. Either `source` or `fields` must be * set. */ final case class ProjectionField( label: _root_.scala.Option[String] = None, description: _root_.scala.Option[String] = None, source: _root_.scala.Option[de.nccloud.reporting.api.v0.models.ProjectionFieldSource] = None, fields: _root_.scala.Option[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]] = None ) /** * Represents the source for a single projection field from a topic's schema. * * @param topic Name of the originating topic. * @param path A slimmed down variant of [JSON * Path](https://gregsdennis.github.io/Manatee.Json/usage/path.html) to point to a * specific field in a topic's schema. * @param filter A conditional filter expression. The expression can contain variables, which * will be passed in when evaluating the projection. */ final case class ProjectionFieldSource( topic: String, path: String, filter: String ) /** * Form for querying and projecting data. * * @param view The name of a defined `view` used for projecting. If the view defines variables, * they must be provided as well. Either `view` or `projection` must be set. * @param projection Projected fields to return. Either `view` or `projection` must be set. * @param variables Variables to pass to the projection or the view. * @param sort The defined properties refer to the fields of the projection. */ final case class QueryForm( view: _root_.scala.Option[String] = None, projection: _root_.scala.Option[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]] = None, variables: Map[String, _root_.io.circe.Json], sort: _root_.scala.Option[Seq[de.nccloud.common.v0.models.Sort]] = None ) /** * The result of a query, returning data in a projected form. * * @param data The returned data in the projected form. */ final case class QueryResponse( page: de.nccloud.common.v0.models.PageResponse, data: Map[String, _root_.io.circe.Json] ) /** * Represents a single topic, which will be consumed by the reporting service. * * @param service Info about the producing service. * @param schema The schema of the topics' provided data. */ final case class Topic( name: String, service: de.nccloud.common.v0.models.ServiceInfo, schema: Map[String, de.nccloud.reporting.api.v0.models.Field] ) final case class TopicResponse( page: de.nccloud.common.v0.models.PageResponse, data: Seq[de.nccloud.reporting.api.v0.models.Topic] ) /** * Updates one or more data fields. * * @param trackingId A unique id, which identifies the association of multiple events. This can i.e. * be a user guid, for events related to a user. It is important, that this id is * unique and immutable. * @param payload One or more fields defined in the reporting schema, which will be updated. * @param audit Audit information about this event. */ final case class UpdateEvent( trackingId: String, payload: Map[String, _root_.io.circe.Json], audit: de.nccloud.common.v0.models.Audit ) extends ReportingEvent { override val reportingEventType: ReportingEventType = ReportingEventType.UpdateEvent } /** * A stored view containing data from one or more topics. * * @param guid Globally unique identifier of the projection. * @param name Name of this projection used to reference it in a human-friendly way. The name * must be unique. * @param projection Projected fields of this view. * @param variables Definition of variables which can be passed to the view. All `scalar_type`s are * allowed, except `object`. */ final case class View( guid: _root_.java.util.UUID, name: String, projection: Map[String, de.nccloud.reporting.api.v0.models.ProjectionField], variables: Map[String, de.nccloud.reporting.api.v0.models.ScalarType] ) /** * Form for creating a new `view`. * * @param name Name of the view. * @param projection Projected fields of this view. * @param variables Definition of variables which can be passed to the view. All `scalar_type`s are * allowed, except `object`. */ final case class ViewForm( name: String, projection: Map[String, de.nccloud.reporting.api.v0.models.ProjectionField], variables: Map[String, de.nccloud.reporting.api.v0.models.ScalarType] ) final case class ViewResponse( page: de.nccloud.common.v0.models.PageResponse, data: Seq[de.nccloud.reporting.api.v0.models.View] ) /** * Provides future compatibility in clients - in the future, when a type is added * to the union ReportingEvent, 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 ReportingEventUndefinedType( description: String ) extends ReportingEvent { override val reportingEventType: ReportingEventType = ReportingEventType.UNDEFINED(description) } /** * A type for a topic's schema field. */ sealed trait ScalarType extends _root_.scala.Product with _root_.scala.Serializable object ScalarType { case object Boolean extends ScalarType { override def toString = "boolean" } case object DateIso8601 extends ScalarType { override def toString = "date-iso8601" } case object DateTimeIso8601 extends ScalarType { override def toString = "date-time-iso8601" } case object Decimal extends ScalarType { override def toString = "decimal" } case object Double extends ScalarType { override def toString = "double" } case object Integer extends ScalarType { override def toString = "integer" } case object Long extends ScalarType { override def toString = "long" } case object String extends ScalarType { override def toString = "string" } case object Uuid extends ScalarType { override def toString = "uuid" } /** * Note: when a type is set to `object`, the additional fields **must** be defined * in the schema. */ case object Object extends ScalarType { override def toString = "object" } /** * 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 ScalarType /** * 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[ScalarType] = scala.List(Boolean, DateIso8601, DateTimeIso8601, Decimal, Double, Integer, Long, String, Uuid, Object) private val byName: Map[String, ScalarType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ScalarType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ScalarType] = byName.get(value.toLowerCase) } } package de.nccloud.reporting.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 de.nccloud.common.v0.models.json._ import de.nccloud.reporting.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 jsonDecoderReportingApiScalarType: Decoder[de.nccloud.reporting.api.v0.models.ScalarType] = Decoder.decodeString.map(de.nccloud.reporting.api.v0.models.ScalarType(_)) implicit val jsonEncoderReportingApiScalarType: Encoder[de.nccloud.reporting.api.v0.models.ScalarType] = Encoder.encodeString.contramap[de.nccloud.reporting.api.v0.models.ScalarType](_.toString) implicit def decodeReportingApiDeleteEvent: Decoder[DeleteEvent] = Decoder.instance { c => for { trackingId <- c.downField("tracking_id").as[String] audit <- c.downField("audit").as[de.nccloud.common.v0.models.Audit] } yield { DeleteEvent( trackingId = trackingId, audit = audit ) } } implicit def encodeReportingApiDeleteEvent: Encoder[DeleteEvent] = Encoder.instance { t => Json.fromFields(Seq( Some("tracking_id" -> t.trackingId.asJson), Some("audit" -> t.audit.asJson) ).flatten) } implicit def decodeReportingApiField: Decoder[Field] = Decoder.instance { c => for { __type__ <- c.downField("type").as[de.nccloud.reporting.api.v0.models.ScalarType] isArray <- c.downField("is_array").as[Boolean] fields <- c.downField("fields").as[Option[Map[String, de.nccloud.reporting.api.v0.models.Field]]] label <- c.downField("label").as[Option[String]] description <- c.downField("description").as[Option[String]] } yield { Field( `type` = __type__, isArray = isArray, fields = fields, label = label, description = description ) } } implicit def encodeReportingApiField: Encoder[Field] = Encoder.instance { t => Json.fromFields(Seq( Some("type" -> t.`type`.asJson), Some("is_array" -> t.isArray.asJson), t.fields.map(t => "fields" -> t.asJson), t.label.map(t => "label" -> t.asJson), t.description.map(t => "description" -> t.asJson) ).flatten) } implicit def decodeReportingApiFilter: Decoder[Filter] = Decoder.instance { c => for { expression <- c.downField("expression").as[Option[String]] variables <- c.downField("variables").as[Option[Map[String, _root_.io.circe.Json]]] } yield { Filter( expression = expression, variables = variables ) } } implicit def encodeReportingApiFilter: Encoder[Filter] = Encoder.instance { t => Json.fromFields(Seq( t.expression.map(t => "expression" -> t.asJson), t.variables.map(t => "variables" -> t.asJson) ).flatten) } implicit def decodeReportingApiProjectionField: Decoder[ProjectionField] = Decoder.instance { c => for { label <- c.downField("label").as[Option[String]] description <- c.downField("description").as[Option[String]] source <- c.downField("source").as[Option[de.nccloud.reporting.api.v0.models.ProjectionFieldSource]] fields <- c.downField("fields").as[Option[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]]] } yield { ProjectionField( label = label, description = description, source = source, fields = fields ) } } implicit def encodeReportingApiProjectionField: Encoder[ProjectionField] = Encoder.instance { t => Json.fromFields(Seq( t.label.map(t => "label" -> t.asJson), t.description.map(t => "description" -> t.asJson), t.source.map(t => "source" -> t.asJson), t.fields.map(t => "fields" -> t.asJson) ).flatten) } implicit def decodeReportingApiProjectionFieldSource: Decoder[ProjectionFieldSource] = Decoder.instance { c => for { topic <- c.downField("topic").as[String] path <- c.downField("path").as[String] filter <- c.downField("filter").as[String] } yield { ProjectionFieldSource( topic = topic, path = path, filter = filter ) } } implicit def encodeReportingApiProjectionFieldSource: Encoder[ProjectionFieldSource] = Encoder.instance { t => Json.fromFields(Seq( Some("topic" -> t.topic.asJson), Some("path" -> t.path.asJson), Some("filter" -> t.filter.asJson) ).flatten) } implicit def decodeReportingApiQueryForm: Decoder[QueryForm] = Decoder.instance { c => for { view <- c.downField("view").as[Option[String]] projection <- c.downField("projection").as[Option[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]]] variables <- c.downField("variables").as[Map[String, _root_.io.circe.Json]] sort <- c.downField("sort").as[Option[Seq[de.nccloud.common.v0.models.Sort]]] } yield { QueryForm( view = view, projection = projection, variables = variables, sort = sort ) } } implicit def encodeReportingApiQueryForm: Encoder[QueryForm] = Encoder.instance { t => Json.fromFields(Seq( t.view.map(t => "view" -> t.asJson), t.projection.map(t => "projection" -> t.asJson), Some("variables" -> t.variables.asJson), t.sort.map(t => "sort" -> t.asJson) ).flatten) } implicit def decodeReportingApiQueryResponse: Decoder[QueryResponse] = Decoder.instance { c => for { page <- c.downField("page").as[de.nccloud.common.v0.models.PageResponse] data <- c.downField("data").as[Map[String, _root_.io.circe.Json]] } yield { QueryResponse( page = page, data = data ) } } implicit def encodeReportingApiQueryResponse: Encoder[QueryResponse] = Encoder.instance { t => Json.fromFields(Seq( Some("page" -> t.page.asJson), Some("data" -> t.data.asJson) ).flatten) } implicit def decodeReportingApiTopic: Decoder[Topic] = Decoder.instance { c => for { name <- c.downField("name").as[String] service <- c.downField("service").as[de.nccloud.common.v0.models.ServiceInfo] schema <- c.downField("schema").as[Map[String, de.nccloud.reporting.api.v0.models.Field]] } yield { Topic( name = name, service = service, schema = schema ) } } implicit def encodeReportingApiTopic: Encoder[Topic] = Encoder.instance { t => Json.fromFields(Seq( Some("name" -> t.name.asJson), Some("service" -> t.service.asJson), Some("schema" -> t.schema.asJson) ).flatten) } implicit def decodeReportingApiTopicResponse: Decoder[TopicResponse] = Decoder.instance { c => for { page <- c.downField("page").as[de.nccloud.common.v0.models.PageResponse] data <- c.downField("data").as[Seq[de.nccloud.reporting.api.v0.models.Topic]] } yield { TopicResponse( page = page, data = data ) } } implicit def encodeReportingApiTopicResponse: Encoder[TopicResponse] = Encoder.instance { t => Json.fromFields(Seq( Some("page" -> t.page.asJson), Some("data" -> t.data.asJson) ).flatten) } implicit def decodeReportingApiUpdateEvent: Decoder[UpdateEvent] = Decoder.instance { c => for { trackingId <- c.downField("tracking_id").as[String] payload <- c.downField("payload").as[Map[String, _root_.io.circe.Json]] audit <- c.downField("audit").as[de.nccloud.common.v0.models.Audit] } yield { UpdateEvent( trackingId = trackingId, payload = payload, audit = audit ) } } implicit def encodeReportingApiUpdateEvent: Encoder[UpdateEvent] = Encoder.instance { t => Json.fromFields(Seq( Some("tracking_id" -> t.trackingId.asJson), Some("payload" -> t.payload.asJson), Some("audit" -> t.audit.asJson) ).flatten) } implicit def decodeReportingApiView: Decoder[View] = Decoder.instance { c => for { guid <- c.downField("guid").as[_root_.java.util.UUID] name <- c.downField("name").as[String] projection <- c.downField("projection").as[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]] variables <- c.downField("variables").as[Map[String, de.nccloud.reporting.api.v0.models.ScalarType]] } yield { View( guid = guid, name = name, projection = projection, variables = variables ) } } implicit def encodeReportingApiView: Encoder[View] = Encoder.instance { t => Json.fromFields(Seq( Some("guid" -> t.guid.asJson), Some("name" -> t.name.asJson), Some("projection" -> t.projection.asJson), Some("variables" -> t.variables.asJson) ).flatten) } implicit def decodeReportingApiViewForm: Decoder[ViewForm] = Decoder.instance { c => for { name <- c.downField("name").as[String] projection <- c.downField("projection").as[Map[String, de.nccloud.reporting.api.v0.models.ProjectionField]] variables <- c.downField("variables").as[Map[String, de.nccloud.reporting.api.v0.models.ScalarType]] } yield { ViewForm( name = name, projection = projection, variables = variables ) } } implicit def encodeReportingApiViewForm: Encoder[ViewForm] = Encoder.instance { t => Json.fromFields(Seq( Some("name" -> t.name.asJson), Some("projection" -> t.projection.asJson), Some("variables" -> t.variables.asJson) ).flatten) } implicit def decodeReportingApiViewResponse: Decoder[ViewResponse] = Decoder.instance { c => for { page <- c.downField("page").as[de.nccloud.common.v0.models.PageResponse] data <- c.downField("data").as[Seq[de.nccloud.reporting.api.v0.models.View]] } yield { ViewResponse( page = page, data = data ) } } implicit def encodeReportingApiViewResponse: Encoder[ViewResponse] = Encoder.instance { t => Json.fromFields(Seq( Some("page" -> t.page.asJson), Some("data" -> t.data.asJson) ).flatten) } implicit def decodeReportingApiReportingEvent: Decoder[ReportingEvent] = Decoder.instance { c => c.get[Option[String]]("type") match { case Right(Some(s)) if s == "update_event" => c.as[de.nccloud.reporting.api.v0.models.UpdateEvent] case Right(Some(s)) if s == "delete_event" => c.as[de.nccloud.reporting.api.v0.models.DeleteEvent] case Right(Some(s)) => Right(de.nccloud.reporting.api.v0.models.ReportingEventUndefinedType(s)) case _ => Left(DecodingFailure("Union[ReportingEvent] requires a discriminator named 'type' - this field was not found in the Json", c.history)) } } implicit def encodeReportingApiReportingEvent: Encoder[ReportingEvent] = Encoder.instance { case t: de.nccloud.reporting.api.v0.models.UpdateEvent => t.asJson.mapObject(obj => ("type", Json.fromString("update_event")) +: obj) case t: de.nccloud.reporting.api.v0.models.DeleteEvent => t.asJson.mapObject(obj => ("type", Json.fromString("delete_event")) +: obj) case other => sys.error(s"The type[${other.getClass.getName}] has no JSON encoder") } } } package de.nccloud.reporting.api.v0 { import org.http4s.client.blaze._ import io.circe.syntax._ object Constants { val Namespace = "de.nccloud.reporting.api.v0" val UserAgent = "apibuilder app.apibuilder.io/nccloud/reporting-api/0.0.1/http4s_0_15" val Version = "0.0.1" val VersionMajor = 0 } class Client( val baseUrl: org.http4s.Uri, auth: scala.Option[de.nccloud.reporting.api.v0.Authorization] = None, defaultHeaders: Seq[(String, String)] = Nil, asyncHttpClient: org.http4s.client.Client = Client.defaultAsyncHttpClient ) extends interfaces.Client { import org.http4s.Response import de.nccloud.common.v0.models.json._ import de.nccloud.reporting.api.v0.models.json._ def closeAsyncHttpClient(): Unit = { asyncHttpClient.shutdownNow() } def queryResponses: QueryResponses = QueryResponses def topics: Topics = Topics def views: Views = Views object QueryResponses extends QueryResponses { override def post( queryForm: de.nccloud.reporting.api.v0.models.QueryForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.QueryResponse] = { val urlPath = Seq("query") val (payload, formPayload) = (Some(queryForm), None) _executeRequest[de.nccloud.reporting.api.v0.models.QueryForm, de.nccloud.reporting.api.v0.models.QueryResponse]("POST", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.QueryResponse]("de.nccloud.reporting.api.v0.models.QueryResponse", r) case r if r.status.code == 400 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.GenericErrorsResponse(r)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 400")) } } } object Topics extends Topics { override def get( offset: Int = 0, limit: Int = 25, sortBy: String = "name", direction: de.nccloud.common.v0.models.SortDirection = de.nccloud.common.v0.models.SortDirection.Asc, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.TopicResponse] = { val urlPath = Seq("topics") val queryParameters = Seq( Some("offset" -> offset.toString), Some("limit" -> limit.toString), Some("sort_by" -> sortBy), Some("direction" -> direction.toString) ).flatten _executeRequest[Unit, de.nccloud.reporting.api.v0.models.TopicResponse]("GET", path = urlPath, queryParameters = queryParameters, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.TopicResponse]("de.nccloud.reporting.api.v0.models.TopicResponse", r) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200")) } } override def getByGuid( guid: String, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.Topic] = { val urlPath = Seq("topics", guid) _executeRequest[Unit, de.nccloud.reporting.api.v0.models.Topic]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.Topic]("de.nccloud.reporting.api.v0.models.Topic", r) case r if r.status.code == 404 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.UnitResponse(r.status.code)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 404")) } } } object Views extends Views { override def get( offset: Int = 0, limit: Int = 25, sortBy: String = "name", direction: de.nccloud.common.v0.models.SortDirection = de.nccloud.common.v0.models.SortDirection.Asc, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.ViewResponse] = { val urlPath = Seq("views") val queryParameters = Seq( Some("offset" -> offset.toString), Some("limit" -> limit.toString), Some("sort_by" -> sortBy), Some("direction" -> direction.toString) ).flatten _executeRequest[Unit, de.nccloud.reporting.api.v0.models.ViewResponse]("GET", path = urlPath, queryParameters = queryParameters, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.ViewResponse]("de.nccloud.reporting.api.v0.models.ViewResponse", r) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200")) } } override def getByGuid( guid: _root_.java.util.UUID, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] = { val urlPath = Seq("views", guid.toString) _executeRequest[Unit, de.nccloud.reporting.api.v0.models.View]("GET", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.View]("de.nccloud.reporting.api.v0.models.View", r) case r if r.status.code == 404 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.UnitResponse(r.status.code)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 404")) } } override def post( viewForm: de.nccloud.reporting.api.v0.models.ViewForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] = { val urlPath = Seq("views") val (payload, formPayload) = (Some(viewForm), None) _executeRequest[de.nccloud.reporting.api.v0.models.ViewForm, de.nccloud.reporting.api.v0.models.View]("POST", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 201 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.View]("de.nccloud.reporting.api.v0.models.View", r) case r if r.status.code == 400 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.GenericErrorsResponse(r)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 201, 400")) } } override def putByGuid( guid: _root_.java.util.UUID, viewForm: de.nccloud.reporting.api.v0.models.ViewForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] = { val urlPath = Seq("views", guid.toString) val (payload, formPayload) = (Some(viewForm), None) _executeRequest[de.nccloud.reporting.api.v0.models.ViewForm, de.nccloud.reporting.api.v0.models.View]("PUT", path = urlPath, body = payload, formBody = formPayload, requestHeaders = requestHeaders) { case r if r.status.code == 200 => _root_.de.nccloud.reporting.api.v0.Client.parseJson[de.nccloud.reporting.api.v0.models.View]("de.nccloud.reporting.api.v0.models.View", r) case r if r.status.code == 400 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.GenericErrorsResponse(r)) case r if r.status.code == 404 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.UnitResponse(r.status.code)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 400, 404")) } } override def deleteByGuid( guid: _root_.java.util.UUID, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[Unit] = { val urlPath = Seq("views", guid.toString) _executeRequest[Unit, Unit]("DELETE", path = urlPath, requestHeaders = requestHeaders) { case r if r.status.code == 200 => scalaz.concurrent.Task.now(()) case r if r.status.code == 404 => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.UnitResponse(r.status.code)) case r => scalaz.concurrent.Task.fail(new de.nccloud.reporting.api.v0.errors.FailedRequest(r.status.code, s"Unsupported response code[${r.status.code}]. Expected: 200, 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: scalaz.concurrent.Task[org.http4s.Request]): scalaz.concurrent.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 => scalaz.concurrent.Task[U] )(implicit encoder: io.circe.Encoder[T]): scalaz.concurrent.Task[U] = { import org.http4s.QueryParamEncoder._ val m = org.http4s.Method.fromString(method) match { case scalaz.\/-(m) => m case scalaz.-\/(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(scalaz.concurrent.Task.now(reqAndMaybeAuth))(reqAndMaybeAuth.withBody) else body.fold(scalaz.concurrent.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]): scalaz.concurrent.Task[T] = r.attemptAs[T].run.flatMap { case scalaz.\/-(value) => scalaz.concurrent.Task.now(value) case scalaz.-\/(error) => scalaz.concurrent.Task.fail(new de.nccloud.reporting.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 queryResponses: de.nccloud.reporting.api.v0.QueryResponses def topics: de.nccloud.reporting.api.v0.Topics def views: de.nccloud.reporting.api.v0.Views } } trait QueryResponses { def post( queryForm: de.nccloud.reporting.api.v0.models.QueryForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.QueryResponse] } trait Topics { def get( offset: Int = 0, limit: Int = 25, sortBy: String = "name", direction: de.nccloud.common.v0.models.SortDirection = de.nccloud.common.v0.models.SortDirection.Asc, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.TopicResponse] def getByGuid( guid: String, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.Topic] } trait Views { def get( offset: Int = 0, limit: Int = 25, sortBy: String = "name", direction: de.nccloud.common.v0.models.SortDirection = de.nccloud.common.v0.models.SortDirection.Asc, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.ViewResponse] def getByGuid( guid: _root_.java.util.UUID, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] def post( viewForm: de.nccloud.reporting.api.v0.models.ViewForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] def putByGuid( guid: _root_.java.util.UUID, viewForm: de.nccloud.reporting.api.v0.models.ViewForm, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[de.nccloud.reporting.api.v0.models.View] def deleteByGuid( guid: _root_.java.util.UUID, requestHeaders: Seq[(String, String)] = Nil ): scalaz.concurrent.Task[Unit] } package errors { import de.nccloud.common.v0.models.json._ import de.nccloud.reporting.api.v0.models.json._ final case class GenericErrorsResponse( response: org.http4s.Response, message: Option[String] = None ) extends Exception(message.getOrElse(s"${response.status.code}: ${response.body}")) { lazy val genericErrors = _root_.de.nccloud.reporting.api.v0.Client.parseJson[Seq[de.nccloud.common.v0.models.GenericError]]("Seq[de.nccloud.common.v0.models.GenericError]", response) } final case class UnitResponse(status: Int) extends Exception(s"HTTP $status") 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) } }