Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin: allow pluggable HTTP clients #1381

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defaultTasks 'jar'

repositories {
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kotlinx" }
maven { url "https://jitpack.io" }
}
Expand All @@ -32,10 +31,10 @@ dependencies {
implementation "io.github.cdimascio:dotenv-kotlin:6.2.2"
implementation "org.ini4j:ini4j:0.5.4"

implementation "io.ktor:ktor-client:$ktorVersion"
implementation "io.ktor:ktor-client-okhttp:$ktorVersion"
implementation "io.ktor:ktor-client-json:$ktorVersion"
implementation "io.ktor:ktor-client-gson:$ktorVersion"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed for now but can add back if we decide to have a low-level ktor client to make the requests

implementation "com.google.http-client:google-http-client:$googleHttpVersion"
implementation "com.google.http-client:google-http-client-apache-v2:$googleHttpVersion"
implementation "com.google.http-client:google-http-client-gson:$googleHttpVersion"


implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-RC'
implementation 'com.google.code.gson:gson:2.8.5'
Expand Down
2 changes: 1 addition & 1 deletion kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlinVersion=1.7.10
ktorVersion=1.6.7
googleHttpVersion=1.43.3
12 changes: 6 additions & 6 deletions kotlin/src/main/com/looker/rtl/APIMethods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ open class APIMethods(val authSession: AuthSession) {
}
}

inline fun <reified T> get(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> get(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this update required by the new Kotlin version? Just curious because it doesn't look like the Kotlin version got updated.

return authSession.transport.request<T>(HttpMethod.GET, path, queryParams, body, authRequest)
}

inline fun <reified T> head(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> head(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
return authSession.transport.request<T>(HttpMethod.HEAD, path, queryParams, body, authRequest)
}

inline fun <reified T> delete(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> delete(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
return authSession.transport.request<T>(HttpMethod.DELETE, path, queryParams, body, authRequest)
}

inline fun <reified T> post(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> post(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
return authSession.transport.request<T>(HttpMethod.POST, path, queryParams, body, authRequest)
}

inline fun <reified T> put(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> put(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
return authSession.transport.request<T>(HttpMethod.PUT, path, queryParams, body, authRequest)
}

inline fun <reified T> patch(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
inline fun <reified T : Any> patch(path: String, queryParams: Values = mapOf(), body: Any? = null): SDKResponse {
return authSession.transport.request<T>(HttpMethod.PATCH, path, queryParams, body, authRequest)
}

Expand Down
12 changes: 5 additions & 7 deletions kotlin/src/main/com/looker/rtl/AuthSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

package com.looker.rtl

import io.ktor.client.request.forms.*
import io.ktor.http.*
import com.google.api.client.http.UrlEncodedContent

open class AuthSession(
open val apiSettings: ConfigurationProvider,
Expand Down Expand Up @@ -144,12 +143,11 @@ open class AuthSession(
unQuote(System.getProperty("${apiSettings.environmentPrefix}_CLIENT_ID") ?: config[client_id])
val clientSecret =
unQuote(System.getProperty("${apiSettings.environmentPrefix}_CLIENT_SECRET") ?: config[client_secret])
val body = FormDataContent(
Parameters.build {
append(client_id, clientId)
append(client_secret, clientSecret)
}
val params = mapOf(
client_id to clientId,
client_secret to clientSecret
)
val body = UrlEncodedContent(params)
val token = ok<AuthToken>(
transport.request<AuthToken>(
HttpMethod.POST,
Expand Down
1 change: 1 addition & 0 deletions kotlin/src/main/com/looker/rtl/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ val BinaryMatch = Regex(MATCH_MODE_BINARY, RegexOption.IGNORE_CASE)

const val DEFAULT_TIMEOUT = 120
const val DEFAULT_API_VERSION = "4.0" // Kotlin requires at least API 4.0
const val DEFAULT_HTTP_TRANSPORT = "apache"

typealias Values = Map<String, Any?>

Expand Down
10 changes: 5 additions & 5 deletions kotlin/src/main/com/looker/rtl/ErrorDoc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ErrorDocItem(var url: String)
/** Structure of the error code document index */
typealias ErrorCodeIndex = HashMap<String, ErrorDocItem>

interface IErrorDocLink {
interface IErrorDocLink {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the reformatting correction. Weird we had leading spaces in the interface declarations.

/** base redirector url */
var redirector: String
/** api version of the error link */
Expand All @@ -39,9 +39,9 @@ typealias ErrorCodeIndex = HashMap<String, ErrorDocItem>
var statusCode: String
/** REST API Path */
var apiPath: String
}
}

interface IErrorDoc {
interface IErrorDoc {
/** Index of all known error codes. Call load() to populate it */
var index: ErrorCodeIndex?

Expand Down Expand Up @@ -103,10 +103,10 @@ typealias ErrorCodeIndex = HashMap<String, ErrorDocItem>
* @param errorMdUrl url for the error document
*/
fun methodName(errorMdUrl: String): String
}
}

/** Class to process Looker API error payloads and retrieve error documentation */
class ErrorDoc(val sdk: APIMethods, val cdnUrl: String = ErrorCodesUrl): IErrorDoc {
class ErrorDoc(val sdk: APIMethods, val cdnUrl: String = ErrorCodesUrl) : IErrorDoc {
companion object {
/** Location of the public CDN for Looker API Error codes */
const val ErrorCodesUrl = "https://static-a.cdn.looker.app/errorcodes/"
Expand Down
47 changes: 47 additions & 0 deletions kotlin/src/main/com/looker/rtl/GsonObjectParser.kt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.looker.rtl

import com.google.api.client.util.ObjectParser
import com.google.gson.Gson
import com.google.gson.stream.JsonReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.Reader
import java.lang.reflect.Type
import java.nio.charset.Charset

/** Custom GSON based parser for deserialization. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document why this is required now?

class GsonObjectParser : ObjectParser {
override fun <T : Any?> parseAndClose(
inputStream: InputStream?,
charset: Charset?,
clazz: Class<T>?,
): T {
return doParseAndClose(InputStreamReader(inputStream, charset), clazz)
}

override fun parseAndClose(inputStream: InputStream?, charset: Charset?, type: Type?): Any {
return doParseAndClose(InputStreamReader(inputStream, charset), type)
}

override fun <T : Any?> parseAndClose(reader: Reader?, clazz: Class<T>?): T {
return doParseAndClose(reader, clazz)
}

override fun parseAndClose(reader: Reader?, type: Type?): Any {
return doParseAndClose(reader, type)
}

private fun <T : Any?> doParseAndClose(reader: Reader?, type: Type?): T {
val jsonReader = JsonReader(reader)
jsonReader.use {
return GSON.fromJson(reader, type)
}
}

companion object {
/** [Gson] instance used for serializing Kotlin data classes and deserializing responses */
val GSON: Gson = Gson().newBuilder()
.registerTypeAdapter(AuthToken::class.java, AuthTokenAdapter())
.create()
}
}
Loading
Loading