2018-01-12 10:01:25 +01:00
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
2018-02-06 21:41:30 +01:00
|
|
|
import android.content.Context
|
2018-09-30 17:00:28 +02:00
|
|
|
import jp.juggler.subwaytooter.App1
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.log.LogCategory
|
|
|
|
import okhttp3.*
|
2020-12-08 14:48:23 +01:00
|
|
|
import ru.gildor.coroutines.okhttp.await
|
2018-01-12 10:01:25 +01:00
|
|
|
|
|
|
|
// okhttpそのままだとモックしづらいので
|
|
|
|
// リクエストを投げてレスポンスを得る部分をインタフェースにまとめる
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
interface SimpleHttpClient {
|
2020-12-08 14:48:23 +01:00
|
|
|
|
|
|
|
var onCallCreated: (Call) -> Unit
|
|
|
|
|
2020-12-08 21:24:42 +01:00
|
|
|
// fun getResponse(
|
|
|
|
// request: Request,
|
|
|
|
// tmpOkhttpClient: OkHttpClient? = null
|
|
|
|
// ): Response
|
2020-12-08 14:48:23 +01:00
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
suspend fun getResponse(
|
|
|
|
request: Request,
|
2023-01-13 13:22:25 +01:00
|
|
|
tmpOkhttpClient: OkHttpClient? = null,
|
2021-06-20 15:12:25 +02:00
|
|
|
): Response
|
2020-12-08 14:48:23 +01:00
|
|
|
|
|
|
|
fun getWebSocket(
|
2021-06-20 15:12:25 +02:00
|
|
|
request: Request,
|
2023-01-13 13:22:25 +01:00
|
|
|
webSocketListener: WebSocketListener,
|
2021-06-20 15:12:25 +02:00
|
|
|
): WebSocket
|
2018-01-12 10:01:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 21:41:30 +01:00
|
|
|
class SimpleHttpClientImpl(
|
2021-06-20 15:12:25 +02:00
|
|
|
val context: Context,
|
2023-01-13 13:22:25 +01:00
|
|
|
private val okHttpClient: OkHttpClient,
|
2018-02-06 21:41:30 +01:00
|
|
|
) : SimpleHttpClient {
|
2020-12-08 14:48:23 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
val log = LogCategory("SimpleHttpClientImpl")
|
|
|
|
}
|
|
|
|
|
|
|
|
override var onCallCreated: (Call) -> Unit = {}
|
|
|
|
|
2020-12-08 21:24:42 +01:00
|
|
|
// override fun getResponse(
|
|
|
|
// request: Request,
|
|
|
|
// tmpOkhttpClient: OkHttpClient?
|
|
|
|
// ): Response {
|
|
|
|
// App1.getAppState(context).networkTracker.checkNetworkState()
|
|
|
|
// val call = (tmpOkhttpClient ?: this.okHttpClient).newCall(request)
|
|
|
|
// onCallCreated(call)
|
|
|
|
// return call.execute()
|
|
|
|
// }
|
2020-12-08 14:48:23 +01:00
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
override suspend fun getResponse(
|
|
|
|
request: Request,
|
2023-01-13 13:22:25 +01:00
|
|
|
tmpOkhttpClient: OkHttpClient?,
|
2021-06-20 15:12:25 +02:00
|
|
|
): Response {
|
|
|
|
App1.getAppState(context).networkTracker.checkNetworkState()
|
|
|
|
val call = (tmpOkhttpClient ?: this.okHttpClient).newCall(request)
|
|
|
|
onCallCreated(call)
|
|
|
|
return call.await()
|
|
|
|
}
|
2020-12-08 14:48:23 +01:00
|
|
|
|
|
|
|
override fun getWebSocket(
|
2021-06-20 15:12:25 +02:00
|
|
|
request: Request,
|
2023-01-13 13:22:25 +01:00
|
|
|
webSocketListener: WebSocketListener,
|
2021-06-20 15:12:25 +02:00
|
|
|
): WebSocket {
|
2020-12-08 14:48:23 +01:00
|
|
|
App1.getAppState(context).networkTracker.checkNetworkState()
|
|
|
|
return okHttpClient.newWebSocket(request, webSocketListener)
|
|
|
|
}
|
2018-01-12 10:01:25 +01:00
|
|
|
}
|