-
Notifications
You must be signed in to change notification settings - Fork 190
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
Changes from all commits
e51153d
68440de
95dc732
ec84830
9b6f85c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
@@ -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? | ||
|
||
|
@@ -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/" | ||
|
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. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} |
There was a problem hiding this comment.
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