@@ -4,19 +4,21 @@ import jp.studyplus.android.sdk.BuildConfig
44import kotlinx.coroutines.CompletableDeferred
55import kotlinx.coroutines.Deferred
66import okhttp3.*
7+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
8+ import okhttp3.RequestBody.Companion.toRequestBody
79import org.json.JSONObject
810import java.io.IOException
911
1012internal class ApiService (private val client : OkHttpClient ) {
1113 fun post (auth : String , json : String ): Deferred <Long ?> {
1214 val body = createPostBody(json)
1315 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()
2022
2123 return execute(client.newCall(request))
2224 }
@@ -27,10 +29,9 @@ internal class ApiService(private val client: OkHttpClient) {
2729 }
2830}
2931
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( )
3133
32- internal fun createPostBody (json : String ) =
33- RequestBody .create(JSON_MEDIA_TYPE , json)
34+ internal fun createPostBody (json : String ) = json.toRequestBody(JSON_MEDIA_TYPE )
3435
3536internal fun execute (call : Call ): Deferred <Long ?> {
3637 val deferred = CompletableDeferred <Long ?>()
@@ -51,7 +52,7 @@ internal fun execute(call: Call): Deferred<Long?> {
5152
5253 override fun onResponse (call : Call , response : Response ) {
5354 if (response.isSuccessful) {
54- val parsedJson = JSONObject (response.body() ?.string() ? : EMPTY_JSON )
55+ val parsedJson = JSONObject (response.body?.string() ? : EMPTY_JSON )
5556 val recordId = parsedJson.optLong(RECORD_ID , INVALID_RECORD_ID )
5657
5758 if (recordId != INVALID_RECORD_ID ) {
0 commit comments