Skip to content

Commit

Permalink
优化请求流程
Browse files Browse the repository at this point in the history
  • Loading branch information
zhzc0x committed Apr 5, 2024
1 parent b7957ef commit 5ee7793
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 83 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ GET请求
val job = CxHttp.get("https://www.baidu.com")
//此处可指定协程,不指定默认使用CxHttpHelper.scope
.scope(this).launch { response ->
if(response.body != null){
if (response.body != null) {
println("resultGet1: ${response.body<String>()}")
} else {
// TODO: Can do some exception handling
Expand Down Expand Up @@ -171,8 +171,8 @@ HookRequest
HookResponse

```kotlin
val mutexLock = Mutex()
CxHttpHelper.hookResponse { response ->
val mutexLock = Mutex()
CxHttpHelper.hookResponse { response ->
//可以加锁防止多次重复刷新
if (!mutexLock.isLocked) {
mutexLock.withLock {
Expand All @@ -194,7 +194,7 @@ HookResponse
HookResult(Hook统一请求结果CxHttpResult<*>)

```kotlin
CxHttpHelper.hookResult { result: CxHttpResult<*> ->
CxHttpHelper.hookResult { result: CxHttpResult<*> ->
result as MyHttpResult
if (result.code == 401) {
println("hookResult: token失效,准备刷新并重试")
Expand Down
40 changes: 20 additions & 20 deletions library/src/main/kotlin/cxhttp/CxHttp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cxhttp

import cxhttp.annotation.InternalAPI
import cxhttp.converter.ResponseConverter
import cxhttp.response.CxHttpResult
import cxhttp.response.Response
Expand All @@ -10,14 +11,13 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.io.IOException


/**
* Coroutine Extensions Http(协程扩展Http)
*
* */
class CxHttp private constructor(internal val request: Request, private val block: suspend Request.() -> Unit) {

companion object{
companion object {

fun get(url: String, block: suspend Request.() -> Unit = {}): CxHttp {
return request(url, Request.Method.GET.value, block)
Expand Down Expand Up @@ -53,14 +53,14 @@ class CxHttp private constructor(internal val request: Request, private val bloc

}

@CxHttpHelper.InternalAPI
@InternalAPI
var scope: CoroutineScope = CxHttpHelper.scope
private set
@CxHttpHelper.InternalAPI
@InternalAPI
var respConverter: ResponseConverter = CxHttpHelper.converter
private set

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
fun scope(scope: CoroutineScope) = apply {
this.scope = scope
}
Expand All @@ -69,74 +69,74 @@ class CxHttp private constructor(internal val request: Request, private val bloc
* 设置ResponseConverter,自定义转换Response to CxHttpResult,默认使用CxHttpHelper.converter
*
* */
@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
fun respConverter(respConverter: ResponseConverter) = apply {
this.respConverter = respConverter
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
inline fun launch(crossinline responseBlock: suspend CoroutineScope.(Response) -> Unit) = scope.launch {
responseBlock(await())
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
inline fun <reified T, reified RESULT: CxHttpResult<T>> launchResult(
crossinline resultBlock: suspend CoroutineScope.(RESULT) -> Unit) = scope.launch {
resultBlock(awaitResult())
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
inline fun <reified T, reified RESULT: CxHttpResult<List<T>>> launchResultList(
crossinline resultBlock: suspend CoroutineScope.(RESULT) -> Unit) = scope.launch {
resultBlock(awaitResultList())
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend fun await(): Response = withContext(Dispatchers.IO) {
awaitImpl()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend inline fun <reified T, reified RESULT: CxHttpResult<T>> awaitResult(): RESULT = withContext(Dispatchers.IO) {
awaitImpl().result<T, RESULT>()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend inline fun <reified T, reified RESULT: CxHttpResult<List<T>>> awaitResultList(): RESULT = withContext(Dispatchers.IO) {
awaitImpl().resultList<T, RESULT>()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
fun async(): Deferred<Response> = scope.async(Dispatchers.IO) {
awaitImpl()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
inline fun <reified T, reified RESULT: CxHttpResult<T>> asyncResult(): Deferred<RESULT> = scope.async(Dispatchers.IO) {
awaitImpl().result<T, RESULT>()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
inline fun <reified T, reified RESULT: CxHttpResult<List<T>>> asyncResultList(): Deferred<RESULT> = scope.async(Dispatchers.IO) {
awaitImpl().resultList<T, RESULT>()
}

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend fun asFlow(): Flow<Response> = flow {
emit(awaitImpl())
}.flowOn(Dispatchers.IO)

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend inline fun <reified T, reified RESULT: CxHttpResult<T>> resultAsFlow(): Flow<RESULT> = flow {
emit(awaitImpl().result<T, RESULT>())
}.flowOn(Dispatchers.IO)

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
suspend inline fun <reified T, reified RESULT: CxHttpResult<List<T>>> resultListAsFlow(): Flow<RESULT> = flow {
emit(awaitImpl().resultList<T, RESULT>())
}.flowOn(Dispatchers.IO)

@CxHttpHelper.InternalAPI
@InternalAPI
suspend fun awaitImpl(): Response {
var response = try {
if (!request.reCall) {//避免重新请求时多次调用
Expand All @@ -159,4 +159,4 @@ class CxHttp private constructor(internal val request: Request, private val bloc
return response
}

}
}
15 changes: 1 addition & 14 deletions library/src/main/kotlin/cxhttp/CxHttpHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cxhttp

import cxhttp.annotation.InternalAPI
import cxhttp.call.CxHttpCall
import cxhttp.call.Okhttp3Call
import cxhttp.converter.CxHttpConverter
Expand Down Expand Up @@ -152,18 +153,4 @@ object CxHttpHelper {
@InternalAPI
data class FailInfo(val code: Int, var msg: String)

@RequiresOptIn(
level = RequiresOptIn.Level.ERROR,
message = "This API is internal in CxHttp and should not be used. It could be removed or changed without notice."
)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.TYPEALIAS,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR
)
internal annotation class InternalAPI

}
15 changes: 15 additions & 0 deletions library/src/main/kotlin/cxhttp/annotation/InternalAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cxhttp.annotation

@RequiresOptIn(
level = RequiresOptIn.Level.ERROR,
message = "This API is internal in CxHttp and should not be used. It could be removed or changed without notice."
)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.TYPEALIAS,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR
)
internal annotation class InternalAPI
4 changes: 2 additions & 2 deletions library/src/main/kotlin/cxhttp/converter/ResponseConverter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cxhttp.converter

import cxhttp.CxHttpHelper
import cxhttp.annotation.InternalAPI
import cxhttp.response.CxHttpResult
import cxhttp.response.Response
import java.lang.reflect.Type
Expand All @@ -13,7 +13,7 @@ interface ResponseConverter {

fun <T, RESULT: CxHttpResult<List<T>>> convertResultList(body: Response.Body, resultType: Class<RESULT>, tType: Type): RESULT

@CxHttpHelper.InternalAPI
@InternalAPI
fun <RESULT: CxHttpResult<*>> convertResult(code: String, msg: String, data: Any? = null, resultType: Class<RESULT>): RESULT {
val httpResult = try {
val constructor = resultType.getConstructor(String::class.java, String::class.java, Any::class.java)
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/kotlin/cxhttp/hook/HookInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ internal object HookInstance: HookRequest, HookResponse, HookResult {

}

}
}
2 changes: 1 addition & 1 deletion library/src/main/kotlin/cxhttp/hook/HookRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ fun interface HookRequest {

suspend fun hook(request: Request)

}
}
6 changes: 3 additions & 3 deletions library/src/main/kotlin/cxhttp/hook/HookResponse.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cxhttp.hook

import cxhttp.CxHttpHelper
import cxhttp.annotation.InternalAPI
import cxhttp.request.Request
import cxhttp.response.Response

Expand All @@ -10,11 +10,11 @@ import cxhttp.response.Response
* */
interface HookResponse {

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
val Response.request: Request
get() = client.request

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
fun Response.setReCall() {
this.reCall = true
}
Expand Down
9 changes: 4 additions & 5 deletions library/src/main/kotlin/cxhttp/hook/HookResult.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package cxhttp.hook

import cxhttp.CxHttpHelper
import cxhttp.annotation.InternalAPI
import cxhttp.request.Request
import cxhttp.response.CxHttpResult

interface HookResult {


@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
val CxHttpResult<*>.request: Request
get() = response.client.request

@OptIn(CxHttpHelper.InternalAPI::class)
@OptIn(InternalAPI::class)
fun CxHttpResult<*>.setReCall() {
response.reCall = true
}

}
}
5 changes: 3 additions & 2 deletions library/src/main/kotlin/cxhttp/response/CxHttpResult.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cxhttp.response

import cxhttp.CxHttpHelper
import cxhttp.annotation.InternalAPI

/**
* CxHttp所有请求返回的结果基类,T为任意类型,默认实现 @see HttpResult
Expand All @@ -12,11 +13,11 @@ import cxhttp.CxHttpHelper
abstract class CxHttpResult<T>(private val cxCode: String,
private val cxMsg: String,
private val cxData: T?) {
@CxHttpHelper.InternalAPI
@InternalAPI
lateinit var response: Response
val success: Boolean = cxCode == CxHttpHelper.SUCCESS_CODE
}

data class HttpResult<T>(val code: String,
val msg: String,
val data: T?): CxHttpResult<T>(code, msg, data)
val data: T?): CxHttpResult<T>(code, msg, data)
Loading

0 comments on commit 5ee7793

Please sign in to comment.