@@ -4,19 +4,21 @@ import jp.studyplus.android.sdk.BuildConfig
4
4
import kotlinx.coroutines.CompletableDeferred
5
5
import kotlinx.coroutines.Deferred
6
6
import okhttp3.*
7
+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
8
+ import okhttp3.RequestBody.Companion.toRequestBody
7
9
import org.json.JSONObject
8
10
import java.io.IOException
9
11
10
12
internal class ApiService (private val client : OkHttpClient ) {
11
13
fun post (auth : String , json : String ): Deferred <Long ?> {
12
14
val body = createPostBody(json)
13
15
val request = Request .Builder ()
14
- .header(" Accept" , HEADER_JSON )
15
- .header(" Content-type" , HEADER_JSON )
16
- .addHeader(" Authorization" , auth)
17
- .url(" $ENDPOINT /v1/study_records" )
18
- .post(body)
19
- .build()
16
+ .header(" Accept" , HEADER_JSON )
17
+ .header(" Content-type" , HEADER_JSON )
18
+ .addHeader(" Authorization" , auth)
19
+ .url(" $ENDPOINT /v1/study_records" )
20
+ .post(body)
21
+ .build()
20
22
21
23
return execute(client.newCall(request))
22
24
}
@@ -27,10 +29,9 @@ internal class ApiService(private val client: OkHttpClient) {
27
29
}
28
30
}
29
31
30
- private val JSON_MEDIA_TYPE = MediaType .parse( " application/json; charset=utf-8" )
32
+ private val JSON_MEDIA_TYPE = " application/json; charset=utf-8" .toMediaTypeOrNull( )
31
33
32
- internal fun createPostBody (json : String ) =
33
- RequestBody .create(JSON_MEDIA_TYPE , json)
34
+ internal fun createPostBody (json : String ) = json.toRequestBody(JSON_MEDIA_TYPE )
34
35
35
36
internal fun execute (call : Call ): Deferred <Long ?> {
36
37
val deferred = CompletableDeferred <Long ?>()
@@ -51,7 +52,7 @@ internal fun execute(call: Call): Deferred<Long?> {
51
52
52
53
override fun onResponse (call : Call , response : Response ) {
53
54
if (response.isSuccessful) {
54
- val parsedJson = JSONObject (response.body() ?.string() ? : EMPTY_JSON )
55
+ val parsedJson = JSONObject (response.body?.string() ? : EMPTY_JSON )
55
56
val recordId = parsedJson.optLong(RECORD_ID , INVALID_RECORD_ID )
56
57
57
58
if (recordId != INVALID_RECORD_ID ) {
0 commit comments