Skip to content
Snippets Groups Projects
Commit c91b426c authored by Aidan's avatar Aidan
Browse files

Add HTTP cache to API methods

parent f7d58c6c
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
import java.nio.ByteBuffer
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import okhttp3.Cache
import java.io.File
import java.util.concurrent.TimeUnit
class ApiMethods {
companion object {
var UserAgent = "Grayjay Android";
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()
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) {
val body = events.toByteArray().toRequestBody(MEDIA_TYPE_OCTET_STREAM)
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