forked from agzam/vyatsu-schedule
39 lines
1.1 KiB
Kotlin
39 lines
1.1 KiB
Kotlin
package agzam.vyatsu.data.remote
|
|||
|
|
|
||
|
|
import android.util.Log
|
||
|
|
import kotlinx.coroutines.Dispatchers
|
||
|
|
import kotlinx.coroutines.withContext
|
||
|
|
import okhttp3.OkHttpClient
|
||
|
|
import okhttp3.Request
|
||
|
|
import java.util.concurrent.TimeUnit
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author Agzam
|
||
|
|
*/
|
||
|
|
object VyatsuHttpClient {
|
||
|
|
|
||
|
|
private const val TAG = "HTTP"
|
||
|
|
|
||
|
|
private val client: OkHttpClient = OkHttpClient.Builder()
|
||
|
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
||
|
|
.build()
|
||
|
|
|
||
|
|
suspend fun getText(url: String): String = withContext(Dispatchers.IO) {
|
||
|
|
client.newCall(Request.Builder().url(url).build()).execute().use { resp ->
|
||
|
|
Log.d(TAG, "GET $url -> ${resp.code}")
|
||
|
|
check(resp.isSuccessful) { "HTTP ${resp.code} for $url" }
|
||
|
|
resp.body!!.string()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
suspend fun getBytes(url: String): ByteArray = withContext(Dispatchers.IO) {
|
||
|
|
client.newCall(Request.Builder().url(url).build()).execute().use { resp ->
|
||
|
|
Log.d(TAG, "GET $url -> ${resp.code}")
|
||
|
|
check(resp.isSuccessful) { "HTTP ${resp.code} for $url" }
|
||
|
|
resp.body!!.bytes()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|