|
@@ -28,6 +28,7 @@ import com.drake.net.NetConfig
|
|
|
import com.drake.net.convert.NetConverter
|
|
|
import com.drake.net.interfaces.ProgressListener
|
|
|
import com.drake.net.tag.NetTag
|
|
|
+import kotlinx.coroutines.CoroutineExceptionHandler
|
|
|
import okhttp3.OkHttpUtils
|
|
|
import okhttp3.Request
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue
|
|
@@ -35,7 +36,10 @@ import kotlin.reflect.KType
|
|
|
|
|
|
//<editor-fold desc="ID">
|
|
|
/**
|
|
|
- * 请求的Id
|
|
|
+ * 请求Id
|
|
|
+ * Group和Id在使用场景上有所区别, 预期上Group允许重复赋值给多个请求, Id仅允许赋值给一个请求, 但实际上都允许重复赋值
|
|
|
+ * 在作用域中发起请求时会默认使用协程错误处理器作为Group: `setGroup(coroutineContext[CoroutineExceptionHandler])`
|
|
|
+ * 如果你覆盖Group会导致协程结束不会自动取消请求
|
|
|
*/
|
|
|
var Request.id: Any?
|
|
|
get() = tagOf<NetTag.RequestId>()?.value
|
|
@@ -44,9 +48,10 @@ var Request.id: Any?
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 请求的分组名
|
|
|
- * Group和Id本质上都是任意对象Any. 但是Net网络请求中自动取消的操作都是通过Group分组. 如果你覆盖可能会导致自动取消无效
|
|
|
- * 在设计理念上分组可以重复. Id不行
|
|
|
+ * 请求分组
|
|
|
+ * Group和Id在使用场景上有所区别, 预期上Group允许重复赋值给多个请求, Id仅允许赋值给一个请求, 但实际上都允许重复赋值
|
|
|
+ * 在作用域中发起请求时会默认使用协程错误处理器作为Group: `setGroup(coroutineContext[CoroutineExceptionHandler])`
|
|
|
+ * 如果你覆盖Group会导致协程结束不会自动取消请求
|
|
|
*/
|
|
|
var Request.group: Any?
|
|
|
get() = tagOf<NetTag.RequestGroup>()?.value
|
|
@@ -56,7 +61,8 @@ var Request.group: Any?
|
|
|
//</editor-fold>
|
|
|
|
|
|
/**
|
|
|
- * KType属于Kotlin特有的Type, 某些kotlin独占框架可能会使用到. 例如 kotlin.serialization
|
|
|
+ * 为请求附着KType信息
|
|
|
+ * KType属于Kotlin特有的Type, 某些Kotlin框架可能会使用到, 例如 kotlin.serialization
|
|
|
*/
|
|
|
var Request.kType: KType?
|
|
|
get() = tagOf<NetTag.RequestKType>()?.value
|
|
@@ -67,15 +73,14 @@ var Request.kType: KType?
|
|
|
|
|
|
//<editor-fold desc="Extra">
|
|
|
/**
|
|
|
- * 返回键值对的标签
|
|
|
- * 键值对标签即OkHttp中的实际tag(在Net中叫label)中的一个Map集合
|
|
|
+ * 读取额外信息
|
|
|
*/
|
|
|
fun Request.extra(name: String): Any? {
|
|
|
return tagOf<NetTag.Extras>()?.get(name)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 全部键值对标签
|
|
|
+ * 全部额外信息
|
|
|
*/
|
|
|
fun Request.extras(): HashMap<String, Any?> {
|
|
|
val tags = tags()
|
|
@@ -89,7 +94,7 @@ fun Request.extras(): HashMap<String, Any?> {
|
|
|
|
|
|
//<editor-fold desc="Tag">
|
|
|
/**
|
|
|
- * 返回OkHttp的tag(通过Class区分的tag)
|
|
|
+ * 读取OkHttp的tag(通过Class区分的tag)
|
|
|
*/
|
|
|
inline fun <reified T> Request.tagOf(): T? {
|
|
|
return tag(T::class.java)
|
|
@@ -107,7 +112,7 @@ inline fun <reified T> Request.tagOf(value: T?) = apply {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 标签集合
|
|
|
+ * 全部tag
|
|
|
*/
|
|
|
fun Request.tags(): MutableMap<Class<*>, Any?> {
|
|
|
return OkHttpUtils.tags(this)
|
|
@@ -142,14 +147,16 @@ fun Request.downloadListeners(): ConcurrentLinkedQueue<ProgressListener> {
|
|
|
|
|
|
//<editor-fold desc="Download">
|
|
|
/**
|
|
|
- * 当指定下载目录存在同名文件是覆盖还是进行重命名, 重命名规则是: $文件名_($序号).$后缀
|
|
|
+ * 下载文件路径存在同名文件时是覆盖或创建新文件(添加序号)
|
|
|
+ * 重命名规则是: $文件名_($序号).$后缀, 例如`file_name(1).apk`
|
|
|
*/
|
|
|
fun Request.downloadConflictRename(): Boolean {
|
|
|
return tagOf<NetTag.DownloadFileConflictRename>()?.value == true
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 是否进行校验文件md5, 如果校验则匹配上既马上返回文件而不会进行下载
|
|
|
+ * 下载文件MD5校验
|
|
|
+ * 如果服务器响应头`Content-MD5`值和指定路径已经存在的文件MD5相同, 则跳过下载直接返回该File
|
|
|
*/
|
|
|
fun Request.downloadMd5Verify(): Boolean {
|
|
|
return tagOf<NetTag.DownloadFileMD5Verify>()?.value == true
|
|
@@ -180,8 +187,8 @@ fun Request.downloadFileNameDecode(): Boolean {
|
|
|
/**
|
|
|
* 下载是否使用临时文件
|
|
|
* 避免下载失败后覆盖同名文件或者无法判别是否已下载完整, 仅在下载完整以后才会显示为原有文件名
|
|
|
- * 临时文件命名规则: 文件名 + .net-download
|
|
|
- * 下载文件名: install.apk, 临时文件名: install.apk.net-download
|
|
|
+ * 临时文件命名规则: 文件名 + .downloading
|
|
|
+ * 下载文件名: install.apk, 临时文件名: install.apk.downloading
|
|
|
*/
|
|
|
fun Request.downloadTempFile(): Boolean {
|
|
|
return tagOf<NetTag.DownloadTempFile>()?.value == true
|