-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from alyssaruth/ktor-1
Ktor 1
- Loading branch information
Showing
40 changed files
with
613 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package http | ||
|
||
interface ApiResponse<T> { | ||
val statusCode: Int | ||
} | ||
|
||
data class SuccessResponse<T>(override val statusCode: Int, val body: T) : ApiResponse<T> | ||
|
||
data class FailureResponse<T>( | ||
override val statusCode: Int, | ||
val errorCode: String?, | ||
val errorMessage: String? | ||
) : ApiResponse<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package http | ||
|
||
import dto.HealthCheckResponse | ||
import kong.unirest.HttpMethod | ||
|
||
class HealthCheckApi(private val httpClient: HttpClient) { | ||
fun doHealthCheck() { | ||
httpClient.doCall<HealthCheckResponse>(HttpMethod.GET, "/health-check") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package http | ||
|
||
import dto.ClientErrorResponse | ||
import java.util.* | ||
import kong.unirest.HttpMethod | ||
import kong.unirest.HttpResponse | ||
import kong.unirest.JsonObjectMapper | ||
import kong.unirest.Unirest | ||
import logging.Severity | ||
import util.Globals.baseUrl | ||
import utils.InjectedThings.logger | ||
|
||
class HttpClient { | ||
val jsonObjectMapper = JsonObjectMapper() | ||
|
||
inline fun <reified T : Any?> doCall( | ||
method: HttpMethod, | ||
route: String, | ||
payload: Any? = null, | ||
): ApiResponse<T> { | ||
val requestId = UUID.randomUUID() | ||
val requestJson = payload?.let { jsonObjectMapper.writeValue(payload) } | ||
|
||
logger.info( | ||
"http.request", | ||
"$method $route", | ||
"requestId" to requestId, | ||
"requestBody" to requestJson, | ||
) | ||
|
||
val request = Unirest.request(method.toString(), "${baseUrl}${route}") | ||
payload?.let { request.body(requestJson) } | ||
val response = request.asString() | ||
|
||
if (response.isSuccess) { | ||
logResponse(Severity.INFO, requestId, route, method, requestJson, response) | ||
val body = jsonObjectMapper.readValue(response.body, T::class.java) | ||
return SuccessResponse(response.status, body) | ||
} else { | ||
val errorResponse = tryParseErrorResponse(response) | ||
logResponse( | ||
Severity.ERROR, | ||
requestId, | ||
route, | ||
method, | ||
requestJson, | ||
response, | ||
errorResponse, | ||
) | ||
return FailureResponse( | ||
response.status, | ||
errorResponse?.errorCode, | ||
errorResponse?.errorMessage, | ||
) | ||
} | ||
} | ||
|
||
fun tryParseErrorResponse(response: HttpResponse<String>) = | ||
try { | ||
jsonObjectMapper.readValue(response.body, ClientErrorResponse::class.java) | ||
} catch (e: Exception) { | ||
null | ||
} | ||
|
||
fun logResponse( | ||
level: Severity, | ||
requestId: UUID, | ||
route: String, | ||
method: HttpMethod, | ||
requestJson: String?, | ||
response: HttpResponse<String>, | ||
errorResponse: ClientErrorResponse? = null, | ||
) { | ||
logger.log( | ||
level, | ||
"http.response", | ||
"Received ${response.status} for $method $route", | ||
null, | ||
mapOf( | ||
"requestId" to requestId, | ||
"requestBody" to requestJson, | ||
"responseCode" to response.status, | ||
"responseBody" to response.body?.toString(), | ||
"clientErrorCode" to errorResponse?.errorCode, | ||
"clientErrorMessage" to errorResponse?.errorMessage, | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package util | ||
|
||
import http.HealthCheckApi | ||
import http.HttpClient | ||
|
||
object Globals { | ||
private val httpClient = HttpClient() | ||
val baseUrl = "http://localhost:8080" | ||
var healthCheckApi = HealthCheckApi(httpClient) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.