Reputation: 355
Lets say I invoke AWS APIs in my java/kotlin code. Example on getHostedZone route53 API below.
A:
val request = GetHostedZoneRequest.builder().id(hostedZoneId).build()
val response = route53Client.getHostedZone(request)
if (response.hostedZone().config().privateZone() == false) {
// my private zone handling logic
}
(or) B:
val request = GetHostedZoneRequest.builder().id(hostedZoneId).build()
val response = route53Client.getHostedZone(request)
if(response != null) {
if (response.hostedZone()?.config()?.privateZone() == false) {
// my private zone handling logic
}
}
hostedZone()?.config()?.privateZone()
is the right way. Is this assumption correct ?Because I find my IDE not warning about these null checks, I am wondering if these checks are an overkill or not. Under the hood, are these null checks done as expected during an API call.
Upvotes: 0
Views: 27
Reputation: 10734
Looks like you want to use Route53 in kotlin programming langauge. You code like this
request = GetHostedZoneRequest.builder().id(hostedZoneId).build()
val response = route53Client.getHostedZone(request)
This is not using the AWS SDK for Kotlin. Kotlin SDK does not use builders. You are actually using the Java V2 SDK implemented in Kotlin.
The AWS SDK for Kotlin supports full Kotlin features such as coroutines. The Java SDK does not.
Here is an example of using the AWS SDK for Kotlin for a Route53 service call.
suspend fun getHealthStatus(healthCheckIdVal: String?) {
val statusRequest =
GetHealthCheckStatusRequest {
healthCheckId = healthCheckIdVal
}
Route53Client { region = "AWS_GLOBAL" }.use { route53Client ->
val response = route53Client.getHealthCheckStatus(statusRequest)
response.healthCheckObservations?.forEach { observation ->
println("(The health check observation status is ${observation.statusReport?.status}")
}
}
}
As far as responses are concerned, when using the Kotlin SDK. you are wrapping the service call within a use
block. This provides some advantages:
The use
block automatically handles the lifecycle of the Route53Client
instance, ensuring that it is properly closed and the resources are released when the block is exited, even if an exception is thrown. This is a safer and more declarative way of managing the client's lifecycle, compared to manually creating and closing the client.
The AWS SDK for Kotlin's data model classes, such as GetHealthCheckStatusRequest
and the response objects, are designed to be null-safe. This means that you don't have to manually check for null values when accessing the data, as the SDK will handle that for you. This can help reduce boilerplate code and make the logic more focused on the actual business requirements.
The use of the suspend
function allows you to take advantage of Kotlin's coroutine features, which can simplify asynchronous programming and make the code more readable and maintainable. By using coroutines, you can avoid complex callback-based code or the need for explicit threading and synchronization.
Read the AWS Kotlin Developer Guide for more information.
What is the AWS SDK for Kotlin?
Upvotes: 0