Sheheryar Umair
11/22/2023, 6:26 AMimport io.ktor.client.HttpClient
import io.ktor.client.request.forms.*
import <http://io.ktor.client.request.post|io.ktor.client.request.post>
import io.ktor.http.*
import io.ktor.util.*
import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import <http://io.micronaut.http.annotation.Post|io.micronaut.http.annotation.Post>
import io.micronaut.http.multipart.CompletedFileUpload
import io.micronaut.security.annotation.Secured
import io.micronaut.security.rules.SecurityRule
@Secured(SecurityRule.IS_ANONYMOUS)
@Controller("/pdf-extract")
class PdfService(private val ktorClient: HttpClient) {
@Post(value = "/", consumes = [MediaType.MULTIPART_FORM_DATA], produces = [MediaType.APPLICATION_JSON])
suspend fun pdfService(@Body files: CompletedFileUpload): String {
val response = uploadFileToExternalService(files)
return if (response.code() == 200) {
"Response from API: ${response.body()}"
} else {
"Failed to fetch data from API"
}
}
@OptIn(InternalAPI::class)
suspend fun uploadFileToExternalService(file: CompletedFileUpload): HttpResponse<String> {
return try {
val response = <http://ktorClient.post|ktorClient.post><String>("<http://127.0.0.1:8000/file/>") {
body = MultiPartFormDataContent(formData {
append("file", file.inputStream, Headers.build {
append(HttpHeaders.ContentType, file.contentType.toString())
append(HttpHeaders.ContentDisposition, "filename=${file.name}")
})
})
}
HttpResponse.ok(response)
} catch (e: Exception) {
HttpResponse.serverError("Failed to upload file: ${e.message}")
}
}
}
Sheheryar Umair
11/22/2023, 6:28 AMFailed to inject value for parameter [ktorClient] of class: dk.controllers.PdfService\r\n\r\nMessage: No bean of type [io.ktor.client.HttpClient] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).\r\nPath Taken: new PdfService(HttpClient ktorClient) --> new PdfService([HttpClient ktorClient])"
Aleksei Tirman [JB]
11/22/2023, 9:14 AMSheheryar Umair
11/22/2023, 9:36 AM