0

I am trying to code Http Request with Kotlin Coroutines as explained in

this guide and that other guide

As far as I can see, my code is quite close to both example:

package com.tolearn.service

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.http.HttpResponse.BodyHandlers
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton


@Singleton
class DemoService {

    fun postDemo(key: String, msg: String){

        suspend fun getCoroutine(){
    
            val someData = getData()
    
            print(someData)
        }
    
        suspend fun getData(): String {
    
    
            val client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_2)
                    .build();
    
            val request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:3000/employees"))
                    .build();
    
            val response = client.sendAsync(request, BodyHandlers.ofString());
    
            return response.await().body() ***here I get Unresolved reference: await
            //return response.get().body() ***this line works but I understand I should prefer use previous line with "awai()" instead of "get()"
        }
    }
}

My final goal is expose an endpoint (Controller) which will call another Endpoint throw java.net.http.HttpClient inside of a Suspend function. My Microservice is totally stateless and nedd to be faster and less heavy as possible. It is my first time coding Kotlin Coroutine. Based on theorical study, I understand Coroutine is cheaper than thread and I did a good choice for Coroutine. Nevertheless, I am new to it and am struggling with doubts like Error/Exceptions handle and, more immedially why I am getting Unresolved reference: await

So my straight question is: why return response.await().body() caused Unresolved reference: await? Should I replace by response.get().body()?

Any other suggestion or tip will be highly appreciatted.

*** edit

here is my build.gradle. I have just added kotlinx-coroutines-jdk8:1.4.2

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
    id("com.google.protobuf") version "0.8.13"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.grpc:micronaut-grpc-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut.kafka:micronaut-kafka")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.2")

    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    testImplementation("io.micronaut:micronaut-http-client")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

sourceSets {
    main {
        java {
            srcDirs("build/generated/source/proto/main/grpc")
            //srcDirs 'build/generated/source/proto/main/grpckt'
            srcDirs("build/generated/source/proto/main/java")
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.14.0" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.33.1" }
        //grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0" }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            //grpckt {}
        }
    }
}
Jim C
  • 3,957
  • 25
  • 85
  • 162
  • 1
    `await()` is an extension function, so you need to import it the same way you would import a class. If it's not available to import, make sure you have the `kotlinx-coroutines-jdk8` library in your project dependencies. That's where the extension function comes from. – Tenfour04 Dec 16 '20 at 21:25
  • I added kotlinx-coroutines-jdk8. Thanks. But I am still getting Unresolved reference: await. Do I have to import await somehow? A bit weird since response é filled in by client.sendAsync. Kindly can you see my imports if I miss something? – Jim C Dec 16 '20 at 21:37
  • 1
    Yes, you need to import the extension function itself. In IntelliJ or Android Studio, you can put your cursor on it and press Alt+Enter so it will suggest to do it automatically. – Tenfour04 Dec 16 '20 at 22:05
  • That is the issue: IntelliJ it is not suggesting any import. – Jim C Dec 16 '20 at 22:31
  • Kindly can you past here the import declaration? – Jim C Dec 16 '20 at 22:31
  • 1
    You can import it with `import kotlinx.coroutines.future.await` – Rubydesic Jan 07 '21 at 16:06
  • @Rubydesic thanks, if you add as answer I can pick it up – Jim C Jan 08 '21 at 14:20

1 Answers1

1

Ensure you have the kotlinx-coroutines-jdk8 library on your classpath, and then use the following import statement:

import kotlinx.coroutines.future.await 
Rubydesic
  • 3,386
  • 12
  • 27