Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
blockvote committed Jun 3, 2024
1 parent 23fd359 commit 02561fd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ import org.slf4j.LoggerFactory
class OpenApiValidator(val specUrlOrPayload: String) {
val validator = OpenApiInteractionValidator.createFor(specUrlOrPayload).build()

fun validate(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent): ValidationReport {
fun validate(
request: APIGatewayProxyRequestEvent,
response: APIGatewayProxyResponseEvent,
): ValidationReport {
return validator.validate(request.toRequest(), response.toResponse())
.also { if (it.hasErrors()) log.error("error validating request and response against $specUrlOrPayload - $it") }
}

fun assertValid(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent) {
fun assertValid(
request: APIGatewayProxyRequestEvent,
response: APIGatewayProxyResponseEvent,
) {
return validate(request, response).let {
if (it.hasErrors()) {
throw ApiInteractionInvalid(
specUrlOrPayload,
request,
response,
it
it,
)
}
}
Expand All @@ -37,38 +43,45 @@ class OpenApiValidator(val specUrlOrPayload: String) {
throw ApiInteractionInvalid(
spec = specUrlOrPayload,
request = request,
validationReport = it
validationReport = it,
)
}
}

fun assertValidResponse(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent) =
request.toRequest().let { r ->
validator.validateResponse(r.path, r.method, response.toResponse()).let {
if (it.hasErrors()) {
throw ApiInteractionInvalid(
spec = specUrlOrPayload,
request = request,
validationReport = it
)
}
fun assertValidResponse(
request: APIGatewayProxyRequestEvent,
response: APIGatewayProxyResponseEvent,
) = request.toRequest().let { r ->
validator.validateResponse(r.path, r.method, response.toResponse()).let {
if (it.hasErrors()) {
throw ApiInteractionInvalid(
spec = specUrlOrPayload,
request = request,
validationReport = it,
)
}
}
}

class ApiInteractionInvalid(val spec: String, val request: APIGatewayProxyRequestEvent, val response: APIGatewayProxyResponseEvent? = null, val validationReport: ValidationReport) :
RuntimeException("Error validating request and response against $spec - $validationReport")
class ApiInteractionInvalid(
val spec: String,
val request: APIGatewayProxyRequestEvent,
val response: APIGatewayProxyResponseEvent? = null,
val validationReport: ValidationReport,
) : RuntimeException("Error validating request and response against $spec - $validationReport")

private fun APIGatewayProxyRequestEvent.toRequest(): Request {
val builder = when (httpMethod.toLowerCase()) {
"get" -> SimpleRequest.Builder.get(path)
"post" -> SimpleRequest.Builder.post(path)
"put" -> SimpleRequest.Builder.put(path)
"patch" -> SimpleRequest.Builder.patch(path)
"delete" -> SimpleRequest.Builder.delete(path)
"options" -> SimpleRequest.Builder.options(path)
"head" -> SimpleRequest.Builder.head(path)
else -> throw IllegalArgumentException("Unsupported method $httpMethod")
}
val builder =
when (httpMethod.toLowerCase()) {
"get" -> SimpleRequest.Builder.get(path)
"post" -> SimpleRequest.Builder.post(path)
"put" -> SimpleRequest.Builder.put(path)
"patch" -> SimpleRequest.Builder.patch(path)
"delete" -> SimpleRequest.Builder.delete(path)
"options" -> SimpleRequest.Builder.options(path)
"head" -> SimpleRequest.Builder.head(path)
else -> throw IllegalArgumentException("Unsupported method $httpMethod")
}
headers?.forEach { builder.withHeader(it.key, it.value) }
queryStringParameters?.forEach { builder.withQueryParam(it.key, it.value) }
builder.withBody(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,33 @@ class ValidatingRequestRouterWrapper(
val delegate: RequestHandler,
specUrlOrPayload: String,
private val additionalRequestValidationFunctions: List<(APIGatewayProxyRequestEvent) -> Unit> = emptyList(),
private val additionalResponseValidationFunctions: List<(APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent) -> Unit> = emptyList()
private val additionalResponseValidationFunctions: List<
(
APIGatewayProxyRequestEvent,
APIGatewayProxyResponseEvent,
) -> Unit,
> = emptyList(),
) {
private val openApiValidator = OpenApiValidator(specUrlOrPayload)

fun handleRequest(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent =
fun handleRequest(
input: APIGatewayProxyRequestEvent,
context: Context,
): APIGatewayProxyResponseEvent =
handleRequest(input = input, context = context, skipRequestValidation = false, skipResponseValidation = false)

fun handleRequestSkippingRequestAndResponseValidation(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent =
fun handleRequestSkippingRequestAndResponseValidation(
input: APIGatewayProxyRequestEvent,
context: Context,
): APIGatewayProxyResponseEvent =
handleRequest(input = input, context = context, skipRequestValidation = true, skipResponseValidation = true)

private fun handleRequest(input: APIGatewayProxyRequestEvent, context: Context, skipRequestValidation: Boolean, skipResponseValidation: Boolean): APIGatewayProxyResponseEvent {
private fun handleRequest(
input: APIGatewayProxyRequestEvent,
context: Context,
skipRequestValidation: Boolean,
skipResponseValidation: Boolean,
): APIGatewayProxyResponseEvent {
if (!skipRequestValidation) {
try {
openApiValidator.assertValidRequest(input)
Expand Down Expand Up @@ -58,7 +74,10 @@ class ValidatingRequestRouterWrapper(
additionalRequestValidationFunctions.forEach { it(requestEvent) }
}

private fun runAdditionalResponseValidations(requestEvent: APIGatewayProxyRequestEvent, responseEvent: APIGatewayProxyResponseEvent) {
private fun runAdditionalResponseValidations(
requestEvent: APIGatewayProxyRequestEvent,
responseEvent: APIGatewayProxyResponseEvent,
) {
additionalResponseValidationFunctions.forEach { it(requestEvent, responseEvent) }
}

Expand Down

0 comments on commit 02561fd

Please sign in to comment.