HTTP clients¶
Library uses the abstract HTTP client, you can write your own implementation, which will be the best for your needs, e.g. for your platform.
There are some implementations, which you can already add to your dependencies and use, see the sections below. All the client artifacts are uploaded to the Maven Central Repository.
Abstract HTTP client configuration¶
All parameters are optional. Configuration for clients is also optional, but may be useful.
val httpClientConfig: HttpClientConfig = HttpClientConfig(
connectTimeout = 30_000,
readTimeout = 30_000,
defaultHeaders = mapOf("User-Agent" to "VK SDK Kotlin/0.0.x")
)
Note about the logging
It is barely possible to help you without seeing the VK responses, so you need to log it by yourself. For this, you can provide overrided client and configure this using the logging-interceptor for OkHttp, or the logging feature for ktor.
Abstract HTTP client methods¶
To implement this interface, you should override some simple methods:
/**
* Abstract HTTP client
*/
interface HttpClient {
/**
* Apply client configuration
*
* @param config Configuration, such as read and connect timeout, etc
*/
fun applyConfig(config: HttpClientConfig)
/**
* Make GET request
*
* @param url Full request url: host, query, etc.
* @return Response
*/
fun getSync(url: String): Response?
/* And there are some other methods */
}
Where Response.kt
is very simple:
data class Response(
val code: Int,
val body: ByteArray?
) {
/* Some pre-defined methods */
}
JVM: OkHttp-based client¶
OkHttp is available only for JVM, so you can use this client e.g. in Android or in the simple Java project.
Install¶
// also, core module is required
implementation "com.petersamokhin.vksdk:http-client-jvm-okhttp:$vkSdkVersion"
Use¶
// HTTP client configuration is optional, see the snippet above
val httpClient: HttpClient = VkOkHttpClient(httpClientConfig)
The HTTP client instance is needed for the VkApiClient
initialization, but, of course, you can use it too.
Common: ktor-based client¶
ktor is a Kotlin-based HTTP client (and also contains some other modules, e.g. server), so it can be used on any plaltform where the Kotlin can be used.
Install¶
As for the core
, if the Gradle Metadata is enabled, you can simply add this this to your dependencies:
implementation "com.petersamokhin.vksdk:http-client-common-ktor:$vkSdkVersion"
Or provide the platform name otherwise. See the Artifacts section here.
And, one more dependency is required: you should choose any ktor client, e.g. CIO
:
implementation("io.ktor:ktor-client-cio:1.3.2")
Use¶
You can provide ktor HttpClient in constructor:
val httpClient: HttpClient = VkKtorHttpClient(
coroutineContext = Dispatchers.IO /* + job */,
overrideClient = HttpClient(CIO) {
engine {
requestTimeout = 30_000L
}
}
)
Otherwise, override createEngineWithConfig
method:
class CioKtorHttpClient: VkKtorHttpClient(
coroutineContext = Dispatchers.IO /* + job */
) {
override fun createEngineWithConfig(config: HttpClientConfig): HttpClientEngine? {
return CIO.create {
engine {
requestTimeout = 30_000L
}
}
}
}
And then all the things are the same.
So, now you can use this client e.g. in the common module of your multiplatform project, etc. Of course, as the OkHttp-based client, this ktor client can also be used in Java-only project.