@@ -4,21 +4,24 @@ import android.content.Context
44import coil.request.ImageRequest
55import com.google.gson.Gson
66import com.google.gson.reflect.TypeToken
7+ import com.home.reader.api.dto.Credentials
78import com.home.reader.api.dto.Issue
89import com.home.reader.api.dto.Result
910import com.home.reader.api.dto.Series
11+ import com.home.reader.api.dto.Token
12+ import okhttp3.MediaType.Companion.toMediaType
1013import okhttp3.OkHttpClient
1114import okhttp3.Request
15+ import okhttp3.RequestBody.Companion.toRequestBody
1216import okhttp3.Response
1317import java.io.InputStreamReader
1418import java.lang.reflect.Type
1519
16- class ApiProcessor (host : String , port : Int , private val context : Context ) {
17-
18- private val baseUrl = " http://$host :$port "
20+ class ApiProcessor (private val host : String , private val context : Context ) {
1921 private val client = OkHttpClient ()
2022
2123 private companion object {
24+ val CONTENT_TYPE = " application/json" .toMediaType()
2225
2326 val SERIES_RESULT_TYPE : Type = TypeToken .getParameterized(
2427 List ::class .java,
@@ -30,14 +33,27 @@ class ApiProcessor(host: String, port: Int, private val context: Context) {
3033 Issue ::class .java
3134 ).type
3235
36+ const val AUTH = " /customer/login"
3337 const val ALL_SERIES = " /comics/series"
3438 const val ISSUES_OF_SERIES = " /comics/series/%s/issues"
3539 }
3640
41+ fun login (username : String , password : String ): Result <Token > {
42+ val credentials = Credentials (username, password)
43+ val body = Gson ().toJson(credentials).toRequestBody(CONTENT_TYPE )
44+
45+ val request = Request .Builder ()
46+ .post(body)
47+ .url(" $host$AUTH " )
48+ .build()
49+
50+ return client.newCall(request).execute().toResult<Token >()
51+ }
52+
3753 fun getSeries (token : String ): Result <List <Series >> {
3854 val request = Request .Builder ()
3955 .get()
40- .url(" $baseUrl $ALL_SERIES " )
56+ .url(" $host $ALL_SERIES " )
4157 .addAuthorizationHeader(token)
4258 .build()
4359
@@ -51,7 +67,7 @@ class ApiProcessor(host: String, port: Int, private val context: Context) {
5167 size : String = "origin"
5268 ): ImageRequest {
5369 return ImageRequest .Builder (context = context)
54- .data(" $baseUrl /file/$issueId /$page ?size=$size " )
70+ .data(" $host /file/$issueId /$page ?size=$size " )
5571 .addHeader(" Authorization" , " Bearer $token " )
5672 .build()
5773 }
@@ -60,7 +76,7 @@ class ApiProcessor(host: String, port: Int, private val context: Context) {
6076 val path = ISSUES_OF_SERIES .format(seriesId)
6177 val request = Request .Builder ()
6278 .get()
63- .url(" $baseUrl $path " )
79+ .url(" $host $path " )
6480 .addAuthorizationHeader(token)
6581 .build()
6682
@@ -80,4 +96,7 @@ class ApiProcessor(host: String, port: Int, private val context: Context) {
8096 return Result (value)
8197 }
8298
99+ private inline fun <reified T > Response.toResult (): Result <T > {
100+ return this .toResult(TypeToken .get(T ::class .java).type)
101+ }
83102}
0 commit comments