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-01-12 10:01:25 +01:00
|
|
|
import okhttp3.*
|
2018-09-30 17:00:28 +02:00
|
|
|
import jp.juggler.subwaytooter.App1
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.LogCategory
|
2018-01-12 10:01:25 +01:00
|
|
|
|
|
|
|
// okhttpそのままだとモックしづらいので
|
|
|
|
// リクエストを投げてレスポンスを得る部分をインタフェースにまとめる
|
|
|
|
|
|
|
|
interface CurrentCallCallback {
|
|
|
|
fun onCallCreated(call : Call)
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
interface SimpleHttpClient {
|
2018-01-12 10:01:25 +01:00
|
|
|
var currentCallCallback : CurrentCallCallback?
|
2018-09-01 02:49:57 +02:00
|
|
|
|
|
|
|
fun getResponse(
|
|
|
|
request : Request,
|
|
|
|
tmpOkhttpClient : OkHttpClient? = null
|
|
|
|
) : Response
|
|
|
|
|
2018-02-06 21:41:30 +01:00
|
|
|
fun getWebSocket(
|
|
|
|
request : Request,
|
|
|
|
webSocketListener : WebSocketListener
|
|
|
|
) : WebSocket
|
2018-01-12 10:01:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 21:41:30 +01:00
|
|
|
class SimpleHttpClientImpl(
|
2018-09-30 17:00:28 +02:00
|
|
|
val context : Context,
|
2018-09-01 02:49:57 +02:00
|
|
|
private val okHttpClient : OkHttpClient
|
2018-02-06 21:41:30 +01:00
|
|
|
) : SimpleHttpClient {
|
|
|
|
|
2018-09-01 02:49:57 +02:00
|
|
|
|
2018-02-06 21:41:30 +01:00
|
|
|
companion object {
|
|
|
|
val log = LogCategory("SimpleHttpClientImpl")
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:01:25 +01:00
|
|
|
override var currentCallCallback : CurrentCallCallback? = null
|
|
|
|
|
2018-09-01 02:49:57 +02:00
|
|
|
override fun getResponse(
|
|
|
|
request : Request,
|
|
|
|
tmpOkhttpClient : OkHttpClient?
|
|
|
|
) : Response {
|
2018-09-30 17:00:28 +02:00
|
|
|
App1.getAppState(context).networkTracker.checkNetworkState()
|
2018-09-01 02:49:57 +02:00
|
|
|
val call = (tmpOkhttpClient ?: this.okHttpClient).newCall(request)
|
2018-01-12 10:01:25 +01:00
|
|
|
currentCallCallback?.onCallCreated(call)
|
|
|
|
return call.execute()
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
override fun getWebSocket(
|
|
|
|
request : Request,
|
|
|
|
webSocketListener : WebSocketListener
|
|
|
|
) : WebSocket {
|
2018-09-30 17:00:28 +02:00
|
|
|
App1.getAppState(context).networkTracker.checkNetworkState()
|
2018-01-21 13:46:36 +01:00
|
|
|
return okHttpClient.newWebSocket(request, webSocketListener)
|
2018-01-12 10:01:25 +01:00
|
|
|
}
|
2018-02-06 21:41:30 +01:00
|
|
|
|
|
|
|
|
2018-01-12 10:01:25 +01:00
|
|
|
}
|