Skip to content

Commit

Permalink
refactor: simplify json response logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dwickern committed Nov 1, 2023
1 parent af316bb commit 74c3dd0
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/main/scala/controllers/ApiHelpController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package controllers

import akka.util.ByteString
import io.swagger.core.filter.SpecFilter
import io.swagger.models.Swagger
import io.swagger.util.Json
Expand All @@ -34,15 +33,13 @@ class ApiHelpController @Inject() (components: ControllerComponents, val swagger
def getResources = Action { implicit request =>
val host: String = swaggerPlugin.config.host
val resourceListing: Swagger = getResourceListing(host)
val response: String = toJsonString(resourceListing)
returnValue(response)
respond(resourceListing)
}

def getResource(path: String) = Action { implicit request =>
val host: String = swaggerPlugin.config.host
val apiListing: Swagger = getApiListing(path, host)
val response: String = toJsonString(apiListing)
returnValue(response)
respond(apiListing)
}
}

Expand Down Expand Up @@ -98,19 +95,9 @@ trait SwaggerBaseApiController {
clone
}

protected def returnValue(obj: Any): Result = {
JsonResponse(obj).withHeaders(AccessControlAllowOrigin)
}

def toJsonString(data: Any): String = {
if (data.getClass.equals(classOf[String])) {
data.asInstanceOf[String]
} else {
Json.pretty(data.asInstanceOf[AnyRef])
}
}

protected def JsonResponse(data: Any): Result = {
Results.Ok(ByteString(toJsonString(data))).as(ContentTypes.JSON)
protected def respond(spec: Swagger): Result = {
Results.Ok(Json.pretty(spec))
.as(ContentTypes.JSON)
.withHeaders(AccessControlAllowOrigin)
}
}

0 comments on commit 74c3dd0

Please sign in to comment.