Skip to content

Commit

Permalink
Parsing JSON body in advance. Solves apicollective#391 (apicollective…
Browse files Browse the repository at this point in the history
…#392)

* Moved error models to client class and removed effect

* Added errors object within the client to avoid namespace conflicts

* Formatting

* Fixed error scope in client

* Parsing json body before returning. Solves apicollective#391

* Lifted body into F for backward compatibility and renamed decoded body to 'body'

* Inlined exceptionclass and simplified pattern match
  • Loading branch information
amarrella authored Apr 5, 2018
1 parent 2cb737a commit 2713662
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private lazy val defaultAsyncHttpClient = PooledHttp1Client()
""")
override val canSerializeUuid = true
override val implicitArgs: Option[String] = None
override val asyncSuccess: String = "now"
override def asyncSuccess: String = "now"
override val requestUriMethod: Option[String] = None //Some("getUri.toJavaNetURI")
override val expectsInjectedWsClient = false
override def formatBaseUrl(url: Option[String]): String = s"val baseUrl: org.http4s.Uri" + url.fold("")(u => s" = org.http4s.Uri.unsafeFromString(${ScalaUtil.wrapInQuotes(u)})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ class ScalaClientMethodGenerator (
if (response.isUnit) {
s"case r => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r.${config.responseStatusMethod}))"
} else {
s"case r => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r))"
config match {
case _: ScalaClientMethodConfigs.Http4s017 | _: ScalaClientMethodConfigs.Http4s015 =>
s"case r => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r))"
case _ =>
val json = config.toJson("r", response.datatype.name)
s"case r => $json.flatMap(body => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r, None, body)))"
}
}
}
case None => {
Expand Down Expand Up @@ -145,7 +151,13 @@ class ScalaClientMethodGenerator (
Some(s"case r if r.${config.responseStatusMethod} == $statusCode => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r.${config.responseStatusMethod}))")

} else {
Some(s"case r if r.${config.responseStatusMethod} == $statusCode => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r))")
config match {
case _: ScalaClientMethodConfigs.Http4s017 | _: ScalaClientMethodConfigs.Http4s015 =>
Some(s"case r if r.${config.responseStatusMethod} == $statusCode => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r))")
case _ =>
val json = config.toJson("r", response.datatype.name)
Some(s"case r if r.${config.responseStatusMethod} == $statusCode => $json.flatMap(body => ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncFailure}(new errors.${response.errorClassName}(r, None, body)))")
}
}
}
}
Expand All @@ -169,6 +181,27 @@ class ScalaClientMethodGenerator (
}
}

private def errorClasses(): Seq[String] =
ssd.resources.flatMap(_.operations).flatMap(_.responses).filter(r => !r.isSuccess).map { response =>
require(!response.isSuccess)
if (response.isUnit) {
unitExceptionClass(response.errorClassName)
} else {
val variableName = response.errorVariableName
val className = response.errorClassName
val responseDataType = response.datatype.name

val body = variableName.map(v => s"{\n lazy val $v = ${http4sConfig.wrappedAsyncType("Sync").getOrElse(http4sConfig.asyncType)}.${http4sConfig.asyncSuccess}(body)\n}").getOrElse("")

Seq(
s"case class $className(",
s" response: ${config.responseClass},",
s" message: Option[String] = None" + variableName.map(v => s",\n body: $responseDataType").getOrElse(""),
s""") extends Exception(message.getOrElse(response.${config.responseStatusMethod} + ": " + response.${config.responseBodyMethod}))$body"""
).mkString("\n")
}
}.distinct.sorted

override protected def modelErrorClasses(): Seq[String] =
config match {
case _ @ (_:ScalaClientMethodConfigs.Http4s017 | _:ScalaClientMethodConfigs.Http4s015) => super.modelErrorClasses()
Expand All @@ -178,7 +211,7 @@ class ScalaClientMethodGenerator (
def modelErrors(): String =
config match {
case _ @ (_:ScalaClientMethodConfigs.Http4s017 | _:ScalaClientMethodConfigs.Http4s015) => ""
case _ => s"\n\nobject errors {\n\n${super.modelErrorClasses().mkString("\n\n")}\n}"
case _ => s"\n\nobject errors {\n\n${errorClasses().mkString("\n\n")}\n}"
}

}
Expand Down

0 comments on commit 2713662

Please sign in to comment.