11package jp.studyplus.android.sdk.internal.api
22
33import jp.studyplus.android.sdk.BuildConfig
4- import kotlinx.coroutines.CompletableDeferred
5- import kotlinx.coroutines.Deferred
64import okhttp3.*
75import okhttp3.MediaType.Companion.toMediaTypeOrNull
86import okhttp3.RequestBody.Companion.toRequestBody
97import org.json.JSONObject
108import java.io.IOException
119
10+ internal interface PostCallback {
11+ fun onSuccess (recordId : Long? )
12+ fun onFailure (e : IOException )
13+ }
14+
1215internal class ApiService (private val client : OkHttpClient ) {
13- fun post (auth : String , json : String ): Deferred < Long ?> {
16+ fun post (auth : String , json : String , callback : PostCallback ) {
1417 val body = createPostBody(json)
1518 val request = Request .Builder ()
1619 .header(" Accept" , HEADER_JSON )
@@ -20,7 +23,7 @@ internal class ApiService(private val client: OkHttpClient) {
2023 .post(body)
2124 .build()
2225
23- return execute(client.newCall( request) )
26+ execute(client, request, callback )
2427 }
2528
2629 companion object {
@@ -30,41 +33,35 @@ internal class ApiService(private val client: OkHttpClient) {
3033}
3134
3235private val JSON_MEDIA_TYPE = " application/json; charset=utf-8" .toMediaTypeOrNull()
36+ private const val EMPTY_JSON = " {}"
37+ private const val RECORD_ID = " record_id"
38+ private const val INVALID_RECORD_ID = - 1L
3339
3440internal fun createPostBody (json : String ) = json.toRequestBody(JSON_MEDIA_TYPE )
3541
36- internal fun execute (call : Call ): Deferred <Long ?> {
37- val deferred = CompletableDeferred <Long ?>()
38- deferred.invokeOnCompletion {
39- if (deferred.isCancelled) {
40- call.cancel()
41- }
42- }
43-
44- call.enqueue(object : Callback {
45- private val EMPTY_JSON = " {}"
46- private val RECORD_ID = " record_id"
47- private val INVALID_RECORD_ID = - 1L
48-
42+ internal fun execute (client : OkHttpClient , request : Request , callback : PostCallback ) {
43+ client.newCall(request).enqueue(object : Callback {
4944 override fun onFailure (call : Call , e : IOException ) {
50- deferred.completeExceptionally (e)
45+ callback.onFailure (e)
5146 }
5247
5348 override fun onResponse (call : Call , response : Response ) {
54- if (response.isSuccessful) {
55- val parsedJson = JSONObject (response.body?.string() ? : EMPTY_JSON )
56- val recordId = parsedJson.optLong(RECORD_ID , INVALID_RECORD_ID )
49+ if (! response.isSuccessful) {
50+ callback.onFailure(IOException (response.toString()))
51+ return
52+ }
5753
58- if (recordId != INVALID_RECORD_ID ) {
59- deferred.complete(recordId)
60- } else {
61- deferred.complete(null )
62- }
54+ val recordId = parseResponse(response)
55+ if (recordId != INVALID_RECORD_ID ) {
56+ callback.onSuccess(recordId)
6357 } else {
64- deferred.completeExceptionally( IOException (response.toString()) )
58+ callback.onSuccess( null )
6559 }
6660 }
6761 })
62+ }
6863
69- return deferred
64+ private fun parseResponse (response : Response ): Long {
65+ val parsedJson = JSONObject (response.body?.string() ? : EMPTY_JSON )
66+ return parsedJson.optLong(RECORD_ID , INVALID_RECORD_ID )
7067}
0 commit comments