-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApiService.kt
42 lines (37 loc) · 1.3 KB
/
ApiService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package data
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import model.GenerateContentRequest
import model.GenerateContentResponse
class ApiService {
companion object {
const val BASE_URL = "https://generativelanguage.googleapis.com/v1beta/models"
}
@OptIn(ExperimentalSerializationApi::class)
private val client = HttpClient {
install(ContentNegotiation) {
json(
Json {
isLenient = true
ignoreUnknownKeys = true
explicitNulls = false
}
)
}
}
suspend fun getGeneratedContent(request: GenerateContentRequest): GenerateContentResponse {
return client.post("$BASE_URL/gemini-pro:generateContent") {
contentType(ContentType.Application.Json)
url { parameters.append("key", "YOUR_API_KEY") }
setBody(request)
}.body<GenerateContentResponse>()
}
}