Skip to content
Snippets Groups Projects
Commit 44edd69e authored by Kelvin's avatar Kelvin
Browse files

Merge branch 'ad-polycentric-caching' into 'master'

Add HTTP cache to API methods and add max timeout

See merge request !10
parents f7d58c6c c91b426c
No related branches found
No related tags found
1 merge request!10Add HTTP cache to API methods and add max timeout
...@@ -16,17 +16,34 @@ import userpackage.Protocol ...@@ -16,17 +16,34 @@ import userpackage.Protocol
import java.nio.ByteBuffer import java.nio.ByteBuffer
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
import okhttp3.Cache
import java.io.File
import java.util.concurrent.TimeUnit
class ApiMethods { class ApiMethods {
companion object { companion object {
var UserAgent = "Grayjay Android"; var UserAgent = "Grayjay Android";
private const val TAG = "ApiMethods" private const val TAG = "ApiMethods"
private val client = OkHttpClient() private var client: OkHttpClient = OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.SECONDS)
.build()
fun initCache(cacheDir: File) {
client = OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.SECONDS)
.cache(Cache(
directory = File(cacheDir, "http_cache"),
maxSize = 25L * 1024L * 1024L // 25 MiB
))
.build()
}
private val MEDIA_TYPE_OCTET_STREAM = "application/octet-stream".toMediaType() private val MEDIA_TYPE_OCTET_STREAM = "application/octet-stream".toMediaType()
fun getRequestBuilder(url: String): Request.Builder = Request.Builder().url(url).header("x-polycentric-user-agent", UserAgent) fun getRequestBuilder(url: String): Request.Builder = Request.Builder().url(url).header("x-polycentric-user-agent", UserAgent)
suspend fun postEvents(server: String, events: Protocol.Events): Unit = withContext(Dispatchers.IO) { suspend fun postEvents(server: String, events: Protocol.Events): Unit = withContext(Dispatchers.IO) {
val body = events.toByteArray().toRequestBody(MEDIA_TYPE_OCTET_STREAM) val body = events.toByteArray().toRequestBody(MEDIA_TYPE_OCTET_STREAM)
val request = getRequestBuilder("$server/events").post(body).build() val request = getRequestBuilder("$server/events").post(body).build()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment