user3201336
user3201336

Reputation: 355

Best practice on handling null API responses from AWS API calls

Lets say I invoke AWS APIs in my java/kotlin code. Example on getHostedZone route53 API below.

  1. Should I check if response == null first or is response guaranteed to be non null? Is null pointer check handling needed A or B?

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
            }
        }
  1. Similarly, I am assuming that hostedZone and config can be null based on the model documentation - GetHostedZoneResponse and HostedZone and checking 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.

  1. Can I expect valid exceptions to be thrown in case of timeouts or other failures from AWS and not check for null (I understand there are known exceptions that can be handled for aws api calls respectively.) ? This question applies to most of the aws service api calls I believe and I couldn't find a satisfactory answer or best practice from the documentation.

Upvotes: 0

Views: 27

Answers (1)

smac2020
smac2020

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

Related Questions