/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.6 * User agent: apibuilder app.apibuilder.io/flow/postman-generator-attributes/0.0.6/http4s_0_17 */ package io.flow.postman.generator.attributes.v0.models { /** * Attribute instructs the generator to add Basic Auth info to the whole Postman * collection. Attribute should be defined on the root level of the ApiBuilder * specification * * @param username Specifies the username to be used in Postman Collection auth definition. It may * be also a variable reference. * @param password Specified the password to be used in Postman Collection auth definition. It may * be also a variable reference. Leave this string empty, when only a username is * needed. */ final case class BasicAuth( username: String, password: String ) /** * Attribute instructs the generator to add a setup step with the referenced entity * creation. This attribute should be attached to the field (that holds an * identifier) inside model or to the resource path (that holds one path param). * * @param relatedServiceNamespace ApiBuilder service's namespace, in which a target operation is specified. * @param resourceType Resource type, that holds a referenced operation. * @param operationMethod Referenced operation method. * @param operationPath Operation path corresponding with the selected {{operation_method}}. * @param identifierField A field name. When the referenced operation is called, identifier_field is used * to extract the corresponding field from the response JSON. If the response JSON * is an array, then a value is extracted from the first object. * @param queryParams Optionally, the specified query params will be used to execute a referenced * operation. * @param deleteOperationPath Optionally, the referenced entity's DELETE operation path can be put here. If * specified, the generator will add a cleanup step for the referenced object. */ final case class ObjectReference( relatedServiceNamespace: String, resourceType: String, operationMethod: io.apibuilder.spec.v0.models.Method, operationPath: String, identifierField: String, queryParams: _root_.scala.Option[Map[String, String]] = None, deleteOperationPath: _root_.scala.Option[String] = None ) /** * Attribute instructs the generator to substitute a field value (when generating * examples) with specified value or variable reference. Attribute should be * attached to a field inside model. * * @param substitute Value, which will be put into the field marked by this attribute. It may be also * a variable reference. */ final case class ValueSubstitute( substitute: String ) /** * A set of attribute names, that are recognized by Postman Generator */ sealed trait AttributeName extends _root_.scala.Product with _root_.scala.Serializable object AttributeName { case object PostmanBasicAuth extends AttributeName { override def toString = "postman-basic-auth" } case object ObjectReference extends AttributeName { override def toString = "object-reference" } case object ValueSubstitute extends AttributeName { override def toString = "value-substitute" } /** * 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 AttributeName /** * 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[AttributeName] = scala.List(PostmanBasicAuth, ObjectReference, ValueSubstitute) private val byName: Map[String, AttributeName] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): AttributeName = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[AttributeName] = byName.get(value.toLowerCase) } } package io.flow.postman.generator.attributes.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 io.apibuilder.spec.v0.models.json._ import io.flow.postman.generator.attributes.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 jsonDecoderPostmanGeneratorAttributesAttributeName: Decoder[io.flow.postman.generator.attributes.v0.models.AttributeName] = Decoder.decodeString.map(io.flow.postman.generator.attributes.v0.models.AttributeName(_)) implicit val jsonEncoderPostmanGeneratorAttributesAttributeName: Encoder[io.flow.postman.generator.attributes.v0.models.AttributeName] = Encoder.encodeString.contramap[io.flow.postman.generator.attributes.v0.models.AttributeName](_.toString) implicit def decodePostmanGeneratorAttributesBasicAuth: Decoder[BasicAuth] = Decoder.instance { c => for { username <- c.downField("username").as[String] password <- c.downField("password").as[String] } yield { BasicAuth( username = username, password = password ) } } implicit def encodePostmanGeneratorAttributesBasicAuth: Encoder[BasicAuth] = Encoder.instance { t => Json.fromFields(Seq( Some("username" -> t.username.asJson), Some("password" -> t.password.asJson) ).flatten) } implicit def decodePostmanGeneratorAttributesObjectReference: Decoder[ObjectReference] = Decoder.instance { c => for { relatedServiceNamespace <- c.downField("related_service_namespace").as[String] resourceType <- c.downField("resource_type").as[String] operationMethod <- c.downField("operation_method").as[io.apibuilder.spec.v0.models.Method] operationPath <- c.downField("operation_path").as[String] identifierField <- c.downField("identifier_field").as[String] queryParams <- c.downField("query_params").as[Option[Map[String, String]]] deleteOperationPath <- c.downField("delete_operation_path").as[Option[String]] } yield { ObjectReference( relatedServiceNamespace = relatedServiceNamespace, resourceType = resourceType, operationMethod = operationMethod, operationPath = operationPath, identifierField = identifierField, queryParams = queryParams, deleteOperationPath = deleteOperationPath ) } } implicit def encodePostmanGeneratorAttributesObjectReference: Encoder[ObjectReference] = Encoder.instance { t => Json.fromFields(Seq( Some("related_service_namespace" -> t.relatedServiceNamespace.asJson), Some("resource_type" -> t.resourceType.asJson), Some("operation_method" -> t.operationMethod.asJson), Some("operation_path" -> t.operationPath.asJson), Some("identifier_field" -> t.identifierField.asJson), t.queryParams.map(t => "query_params" -> t.asJson), t.deleteOperationPath.map(t => "delete_operation_path" -> t.asJson) ).flatten) } implicit def decodePostmanGeneratorAttributesValueSubstitute: Decoder[ValueSubstitute] = Decoder.instance { c => for { substitute <- c.downField("substitute").as[String] } yield { ValueSubstitute( substitute = substitute ) } } implicit def encodePostmanGeneratorAttributesValueSubstitute: Encoder[ValueSubstitute] = Encoder.instance { t => Json.fromFields(Seq( Some("substitute" -> t.substitute.asJson) ).flatten) } } } package io.flow.postman.generator.attributes.v0 { import org.http4s.client.blaze._ import io.circe.syntax._ object Constants { val Namespace = "io.flow.postman.generator.attributes.v0" val UserAgent = "apibuilder app.apibuilder.io/flow/postman-generator-attributes/0.0.6/http4s_0_17" val Version = "0.0.6" val VersionMajor = 0 } class Client( val baseUrl: org.http4s.Uri, auth: scala.Option[io.flow.postman.generator.attributes.v0.Authorization] = None, defaultHeaders: Seq[(String, String)] = Nil, asyncHttpClient: org.http4s.client.Client = Client.defaultAsyncHttpClient ) extends interfaces.Client { import org.http4s.Response import io.apibuilder.spec.v0.models.json._ import io.flow.postman.generator.attributes.v0.models.json._ def closeAsyncHttpClient(): Unit = { asyncHttpClient.shutdownNow() } 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 io.flow.postman.generator.attributes.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 } } package errors { 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) } }