HttpClient: Skip the builder for requests
* New Kotlin-friendly syntax as part of okhttp 5.x Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -22,7 +22,9 @@ package com.aurora.store.data.network
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.aurora.gplayapi.data.models.PlayResponse
|
import com.aurora.gplayapi.data.models.PlayResponse
|
||||||
import com.aurora.gplayapi.network.IHttpClient
|
import com.aurora.gplayapi.network.IHttpClient
|
||||||
import com.aurora.store.BuildConfig
|
import com.aurora.store.BuildConfig.APPLICATION_ID
|
||||||
|
import com.aurora.store.BuildConfig.VERSION_CODE
|
||||||
|
import com.aurora.store.BuildConfig.VERSION_NAME
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
@@ -53,20 +55,21 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
|||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun post(url: String, headers: Map<String, String>, requestBody: RequestBody): PlayResponse {
|
fun post(url: String, headers: Map<String, String>, requestBody: RequestBody): PlayResponse {
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(url)
|
url = url.toHttpUrl(),
|
||||||
.headers(headers.toHeaders())
|
headers = headers.toHeaders(),
|
||||||
.method(POST, requestBody)
|
method = POST,
|
||||||
.build()
|
body = requestBody
|
||||||
|
)
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun call(url: String, headers: Map<String, String> = emptyMap()): Response {
|
fun call(url: String, headers: Map<String, String> = emptyMap()): Response {
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(url)
|
url = url.toHttpUrl(),
|
||||||
.headers(headers.toHeaders())
|
headers = headers.toHeaders(),
|
||||||
.build()
|
)
|
||||||
return okHttpClient.newCall(request).execute()
|
return okHttpClient.newCall(request).execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,24 +79,24 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
|||||||
headers: Map<String, String>,
|
headers: Map<String, String>,
|
||||||
params: Map<String, String>
|
params: Map<String, String>
|
||||||
): PlayResponse {
|
): PlayResponse {
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(buildUrl(url, params))
|
url = buildUrl(url, params),
|
||||||
.headers(headers.toHeaders())
|
headers = headers.toHeaders(),
|
||||||
.method(POST, "".toRequestBody(null))
|
method = POST,
|
||||||
.build()
|
body = "".toRequestBody(null)
|
||||||
|
)
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun postAuth(url: String, body: ByteArray): PlayResponse {
|
override fun postAuth(url: String, body: ByteArray): PlayResponse {
|
||||||
|
val headers = mapOf("User-Agent" to "${APPLICATION_ID}-${VERSION_NAME}-${VERSION_CODE}")
|
||||||
val requestBody = body.toRequestBody("application/json".toMediaType(), 0, body.size)
|
val requestBody = body.toRequestBody("application/json".toMediaType(), 0, body.size)
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(url)
|
url = url.toHttpUrl(),
|
||||||
.header(
|
headers = headers.toHeaders(),
|
||||||
"User-Agent",
|
method = POST,
|
||||||
"${BuildConfig.APPLICATION_ID}-${BuildConfig.VERSION_NAME}-${BuildConfig.VERSION_CODE}"
|
body = requestBody
|
||||||
)
|
)
|
||||||
.method(POST, requestBody)
|
|
||||||
.build()
|
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,23 +116,21 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
|||||||
headers: Map<String, String>,
|
headers: Map<String, String>,
|
||||||
params: Map<String, String>
|
params: Map<String, String>
|
||||||
): PlayResponse {
|
): PlayResponse {
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(buildUrl(url, params))
|
url = buildUrl(url, params),
|
||||||
.headers(headers.toHeaders())
|
headers = headers.toHeaders(),
|
||||||
.method(GET, null)
|
method = GET
|
||||||
.build()
|
)
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getAuth(url: String): PlayResponse {
|
override fun getAuth(url: String): PlayResponse {
|
||||||
val request = Request.Builder()
|
val headers = mapOf("User-Agent" to "${APPLICATION_ID}-${VERSION_NAME}-${VERSION_CODE}")
|
||||||
.url(url)
|
val request = Request(
|
||||||
.header(
|
url = url.toHttpUrl(),
|
||||||
"User-Agent",
|
headers = headers.toHeaders(),
|
||||||
"${BuildConfig.APPLICATION_ID}-${BuildConfig.VERSION_NAME}-${BuildConfig.VERSION_CODE}"
|
method = GET
|
||||||
)
|
)
|
||||||
.method(GET, null)
|
|
||||||
.build()
|
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,11 +140,11 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
|||||||
headers: Map<String, String>,
|
headers: Map<String, String>,
|
||||||
paramString: String
|
paramString: String
|
||||||
): PlayResponse {
|
): PlayResponse {
|
||||||
val request = Request.Builder()
|
val request = Request(
|
||||||
.url(url + paramString)
|
url = "$url$paramString".toHttpUrl(),
|
||||||
.headers(headers.toHeaders())
|
headers = headers.toHeaders(),
|
||||||
.method(GET, null)
|
method = GET
|
||||||
.build()
|
)
|
||||||
return processRequest(request)
|
return processRequest(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +168,7 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
|||||||
return PlayResponse(
|
return PlayResponse(
|
||||||
isSuccessful = response.isSuccessful,
|
isSuccessful = response.isSuccessful,
|
||||||
code = response.code,
|
code = response.code,
|
||||||
responseBytes = response.body?.bytes() ?: byteArrayOf(),
|
responseBytes = response.body.bytes(),
|
||||||
errorString = if (!response.isSuccessful) response.message else String()
|
errorString = if (!response.isSuccessful) response.message else String()
|
||||||
).also {
|
).also {
|
||||||
_responseCode.value = response.code
|
_responseCode.value = response.code
|
||||||
|
|||||||
Reference in New Issue
Block a user