Browse Source

更改Tag相关函数

drake 3 years ago
parent
commit
a070c33b50

+ 13 - 13
net/src/main/java/com/drake/net/Net.kt

@@ -21,7 +21,7 @@ package com.drake.net
 import android.util.Log
 import com.drake.net.interfaces.ProgressListener
 import com.drake.net.request.*
-import com.drake.net.tag.NetLabel
+import com.drake.net.tag.NetTag
 import okhttp3.Request
 import java.io.PrintWriter
 import java.io.StringWriter
@@ -44,7 +44,7 @@ object Net {
     ) = UrlRequest().apply {
         setPath(path)
         method = Method.GET
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -62,7 +62,7 @@ object Net {
     ) = BodyRequest().apply {
         setPath(path)
         method = Method.POST
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -80,7 +80,7 @@ object Net {
     ) = UrlRequest().apply {
         setPath(path)
         method = Method.HEAD
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -98,7 +98,7 @@ object Net {
     ) = UrlRequest().apply {
         setPath(path)
         method = Method.OPTIONS
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -116,7 +116,7 @@ object Net {
     ) = UrlRequest().apply {
         setPath(path)
         method = Method.TRACE
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -134,7 +134,7 @@ object Net {
     ) = BodyRequest().apply {
         setPath(path)
         method = Method.DELETE
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -152,7 +152,7 @@ object Net {
     ) = BodyRequest().apply {
         setPath(path)
         method = Method.PUT
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
 
@@ -170,7 +170,7 @@ object Net {
     ) = BodyRequest().apply {
         setPath(path)
         method = Method.PATCH
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }
     //</editor-fold>
@@ -194,7 +194,7 @@ object Net {
         val iterator = NetConfig.runningCalls.iterator()
         while (iterator.hasNext()) {
             val call = iterator.next().get() ?: continue
-            if (id == call.request().label<NetLabel.RequestId>()?.value) {
+            if (id == call.request().tagOf<NetTag.RequestId>()?.value) {
                 call.cancel()
                 iterator.remove()
                 return true
@@ -213,7 +213,7 @@ object Net {
         var hasCancel = false
         while (iterator.hasNext()) {
             val call = iterator.next().get() ?: continue
-            val value = call.request().label<NetLabel.RequestGroup>()?.value
+            val value = call.request().tagOf<NetTag.RequestGroup>()?.value
             if (group == value) {
                 call.cancel()
                 iterator.remove()
@@ -234,7 +234,7 @@ object Net {
         NetConfig.runningCalls.forEach {
             val request = it.get()?.request() ?: return@forEach
             if (request.id == id) {
-                request.addUploadListener(progressListener)
+                request.uploadListeners().add(progressListener)
             }
         }
     }
@@ -262,7 +262,7 @@ object Net {
         NetConfig.runningCalls.forEach {
             val request = it.get()?.request() ?: return@forEach
             if (request.id == id) {
-                request.addDownloadListener(progressListener)
+                request.downloadListeners().add(progressListener)
             }
         }
     }

+ 8 - 8
net/src/main/java/com/drake/net/NetCoroutine.kt

@@ -28,7 +28,7 @@ inline fun <reified M> CoroutineScope.Get(
         setPath(path)
         method = Method.GET
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -50,7 +50,7 @@ inline fun <reified M> CoroutineScope.Post(
         setPath(path)
         method = Method.POST
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -72,7 +72,7 @@ inline fun <reified M> CoroutineScope.Head(
         setPath(path)
         method = Method.HEAD
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -94,7 +94,7 @@ inline fun <reified M> CoroutineScope.Options(
         setPath(path)
         method = Method.OPTIONS
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -116,7 +116,7 @@ inline fun <reified M> CoroutineScope.Trace(
         setPath(path)
         method = Method.TRACE
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -138,7 +138,7 @@ inline fun <reified M> CoroutineScope.Delete(
         setPath(path)
         method = Method.DELETE
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -160,7 +160,7 @@ inline fun <reified M> CoroutineScope.Put(
         setPath(path)
         method = Method.PUT
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }
@@ -182,7 +182,7 @@ inline fun <reified M> CoroutineScope.Patch(
         setPath(path)
         method = Method.PATCH
         setGroup(coroutineContext[CoroutineExceptionHandler])
-        setTag(tag)
+        tag(tag)
         block?.invoke(this)
     }.execute()
 }

+ 4 - 4
net/src/main/java/com/drake/net/interceptor/LogRecordInterceptor.kt

@@ -2,11 +2,11 @@ package com.drake.net.interceptor
 
 import android.util.Log
 import com.drake.net.log.LogRecorder
-import com.drake.net.request.label
 import com.drake.net.request.logRecord
 import com.drake.net.request.logString
+import com.drake.net.request.tagOf
 import com.drake.net.response.logString
-import com.drake.net.tag.NetLabel
+import com.drake.net.tag.NetTag
 import okhttp3.Interceptor
 import okhttp3.Request
 import okhttp3.Response
@@ -35,9 +35,9 @@ open class LogRecordInterceptor(
 
     override fun intercept(chain: Interceptor.Chain): Response {
         val request = chain.request()
-        request.label<NetLabel.LogRecord>()
+        request.tagOf<NetTag.LogRecord>()
 
-        if (request.logRecord == false) {
+        if (!request.logRecord) {
             return chain.proceed(request)
         }
 

+ 6 - 6
net/src/main/java/com/drake/net/okhttp/OkHttpExtension.kt

@@ -17,8 +17,8 @@
 package com.drake.net.okhttp
 
 import com.drake.net.interceptor.NetOkHttpInterceptor
-import com.drake.net.request.label
-import com.drake.net.tag.NetLabel
+import com.drake.net.request.tagOf
+import com.drake.net.tag.NetTag
 import okhttp3.OkHttpClient
 
 /**
@@ -39,12 +39,12 @@ fun OkHttpClient.toNetOkhttp() = run {
 fun OkHttpClient.cancelId(id: Any?) {
     id ?: return
     dispatcher.runningCalls().forEach {
-        if (id === it.request().label<NetLabel.RequestId>()?.value) {
+        if (id === it.request().tagOf<NetTag.RequestId>()?.value) {
             it.cancel()
         }
     }
     dispatcher.queuedCalls().forEach {
-        if (id === it.request().label<NetLabel.RequestId>()?.value) {
+        if (id === it.request().tagOf<NetTag.RequestId>()?.value) {
             it.cancel()
         }
     }
@@ -57,12 +57,12 @@ fun OkHttpClient.cancelId(id: Any?) {
 fun OkHttpClient.cancelGroup(group: Any?) {
     group ?: return
     dispatcher.runningCalls().forEach {
-        if (group === it.request().label<NetLabel.RequestGroup>()?.value) {
+        if (group === it.request().tagOf<NetTag.RequestGroup>()?.value) {
             it.cancel()
         }
     }
     dispatcher.queuedCalls().forEach {
-        if (group === it.request().label<NetLabel.RequestGroup>()?.value) {
+        if (group === it.request().tagOf<NetTag.RequestGroup>()?.value) {
             it.cancel()
         }
     }

+ 51 - 44
net/src/main/java/com/drake/net/request/BaseRequest.kt

@@ -24,7 +24,7 @@ import com.drake.net.interfaces.NetCallback
 import com.drake.net.interfaces.ProgressListener
 import com.drake.net.okhttp.toNetOkhttp
 import com.drake.net.response.convert
-import com.drake.net.tag.NetLabel
+import com.drake.net.tag.NetTag
 import com.drake.net.utils.runMain
 import okhttp3.*
 import okhttp3.HttpUrl.Companion.toHttpUrl
@@ -65,6 +65,22 @@ abstract class BaseRequest {
     }
     //</editor-fold>
 
+    //<editor-fold desc="ID">
+    /**
+     * 唯一的Id
+     */
+    fun setId(id: Any?) {
+        okHttpRequest.id = id
+    }
+
+    /**
+     * 分组
+     */
+    fun setGroup(group: Any?) {
+        okHttpRequest.group = group
+    }
+    //</editor-fold>
+
     //<editor-fold desc="URL">
 
     /**
@@ -116,7 +132,9 @@ abstract class BaseRequest {
     fun setQuery(name: String, value: String?, encoded: Boolean = false) {
         if (encoded) {
             httpUrl.setEncodedQueryParameter(name, value)
-        } else httpUrl.setQueryParameter(name, value)
+        } else {
+            httpUrl.setQueryParameter(name, value)
+        }
     }
 
     /**
@@ -177,27 +195,27 @@ abstract class BaseRequest {
 
     //</editor-fold>
 
-    //<editor-fold desc="Tag">
-
+    //<editor-fold desc="Extra">
     /**
-     * 唯一的Id
+     * 添加标签
+     * 使用`Request.tag(name)`得到指定标签
+     *
+     * @param name 标签名称
+     * @param tag 标签
      */
-    fun setId(id: Any?) {
-        okHttpRequest.setId(id)
+    fun setExtra(name: String, tag: Any?) {
+        okHttpRequest.setExtra(name, tag)
     }
 
-    /**
-     * 分组
-     */
-    fun setGroup(group: Any?) {
-        okHttpRequest.setGroup(group)
-    }
+    //</editor-fold>
+
+    //<editor-fold desc="Tag">
 
     /**
      * 使用Any::class作为键名添加标签
      * 使用Request.tag()返回标签
      */
-    fun setTag(tag: Any?) {
+    fun tag(tag: Any?) {
         okHttpRequest.tag(tag)
     }
 
@@ -205,7 +223,7 @@ abstract class BaseRequest {
      * 使用[type]作为键名添加标签
      * 使用Request.label<T>()或者Request.tag(type)返回标签
      */
-    fun <T> setTag(type: Class<in T>, tag: T?) {
+    fun <T> tag(type: Class<in T>, tag: T?) {
         okHttpRequest.tag(type, tag)
     }
 
@@ -213,27 +231,8 @@ abstract class BaseRequest {
      * 使用[T]作为键名添加标签
      * 使用Request.label<T>()或者Request.tag(type)返回标签
      */
-    inline fun <reified T> setLabel(tag: T?) {
-        okHttpRequest.setLabel(tag)
-    }
-
-    /**
-     * 添加标签
-     * 使用`Request.tag(name)`得到指定标签
-     *
-     * @param name 标签名称
-     * @param tag 标签
-     */
-    fun setTag(name: String, tag: Any?) {
-        okHttpRequest.setTag(name, tag)
-    }
-
-    /**
-     * 为请求附着针对Kotlin的Type信息
-     */
-    @OptIn(ExperimentalStdlibApi::class)
-    inline fun <reified T> setKType() {
-        okHttpRequest.setKType(typeOf<T>())
+    inline fun <reified T> tagOf(tag: T?) {
+        okHttpRequest.tagOf(tag)
     }
 
     //</editor-fold>
@@ -292,35 +291,35 @@ abstract class BaseRequest {
      * @see setDownloadDir
      */
     fun setDownloadFileName(name: String?) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileName(name))
+        okHttpRequest.tagOf(NetTag.DownloadFileName(name))
     }
 
     /**
      * 下载保存的目录, 也支持包含文件名称的完整路径, 如果使用完整路径则无视setDownloadFileName设置
      */
     fun setDownloadDir(name: String?) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileDir(name))
+        okHttpRequest.tagOf(NetTag.DownloadFileDir(name))
     }
 
     /**
      * 下载保存的目录, 也支持包含文件名称的完整路径, 如果使用完整路径则无视setDownloadFileName设置
      */
     fun setDownloadDir(name: File?) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileDir(name))
+        okHttpRequest.tagOf(NetTag.DownloadFileDir(name))
     }
 
     /**
      * 如果服务器返回 "Content-MD5"响应头和制定路径已经存在的文件MD5相同是否直接返回File
      */
     fun setDownloadMd5Verify(enabled: Boolean = true) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileMD5Verify(enabled))
+        okHttpRequest.tagOf(NetTag.DownloadFileMD5Verify(enabled))
     }
 
     /**
      * 假设下载文件路径已存在同名文件是否重命名, 例如`file_name(1).apk`
      */
     fun setDownloadFileNameConflict(enabled: Boolean = true) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileConflictRename(enabled))
+        okHttpRequest.tagOf(NetTag.DownloadFileConflictRename(enabled))
     }
 
     /**
@@ -328,7 +327,7 @@ abstract class BaseRequest {
      * 例如下载的文件名如果是中文, 服务器传输给你的会是被URL编码的字符串. 你使用URL解码后才是可读的中文名称
      */
     fun setDownloadFileNameDecode(enabled: Boolean = true) {
-        okHttpRequest.setLabel(NetLabel.DownloadFileNameDecode(enabled))
+        okHttpRequest.tagOf(NetTag.DownloadFileNameDecode(enabled))
     }
 
     /**
@@ -338,7 +337,7 @@ abstract class BaseRequest {
      *      下载文件名: install.apk, 临时文件名: install.apk.net-download
      */
     fun setDownloadTempFile(enabled: Boolean = true) {
-        okHttpRequest.setLabel(NetLabel.DownloadTempFile(enabled))
+        okHttpRequest.tagOf(NetTag.DownloadTempFile(enabled))
     }
 
     /**
@@ -354,7 +353,15 @@ abstract class BaseRequest {
      * 是否启用日志记录器
      */
     fun setLogRecord(enabled: Boolean) {
-        okHttpRequest.setLogRecord(enabled)
+        okHttpRequest.logRecord = enabled
+    }
+
+    /**
+     * 为请求附着针对Kotlin的Type信息
+     */
+    @OptIn(ExperimentalStdlibApi::class)
+    inline fun <reified T> setKType() {
+        okHttpRequest.kType = typeOf<T>()
     }
 
     /**

+ 143 - 0
net/src/main/java/com/drake/net/request/RequestBuilder.kt

@@ -0,0 +1,143 @@
+package com.drake.net.request
+
+import com.drake.net.NetConfig
+import com.drake.net.convert.NetConverter
+import com.drake.net.interfaces.ProgressListener
+import com.drake.net.tag.NetTag
+import okhttp3.Headers
+import okhttp3.OkHttpUtils
+import okhttp3.Request
+import java.util.concurrent.ConcurrentLinkedQueue
+import kotlin.reflect.KType
+
+//<editor-fold desc="Group">
+/**
+ * 请求的Id
+ */
+var Request.Builder.id: Any?
+    get() = tagOf<NetTag.RequestId>()
+    set(value) {
+        tagOf(NetTag.RequestId(value))
+    }
+
+/**
+ * 请求的分组名
+ * Group和Id本质上都是任意对象Any. 但是Net网络请求中自动取消的操作都是通过Group分组. 如果你覆盖可能会导致自动取消无效
+ * 在设计理念上分组可以重复. Id不行
+ */
+var Request.Builder.group: Any?
+    get() = tagOf<NetTag.RequestGroup>()
+    set(value) {
+        tagOf(NetTag.RequestGroup(value))
+    }
+//</editor-fold>
+
+/**
+ * 是否输出网络请求日志
+ * 该属性和[NetConfig.logEnabled]有所区别
+ * @see [com.drake.net.interceptor.LogRecordInterceptor]
+ */
+var Request.Builder.logRecord: Boolean
+    get() = tagOf<NetTag.LogRecord>()?.enabled ?: false
+    set(value) {
+        tagOf(NetTag.LogRecord(value))
+    }
+
+/**
+ * KType属于Kotlin特有的Type, 某些kotlin独占框架可能会使用到. 例如 kotlin.serialization
+ */
+var Request.Builder.kType: KType?
+    get() = tagOf<NetTag.RequestKType>()?.value
+    set(value) {
+        tagOf(NetTag.RequestKType(value))
+    }
+
+/**
+ * 全部的请求头
+ */
+fun Request.Builder.headers(): Headers.Builder {
+    return OkHttpUtils.headers(this)
+}
+//</editor-fold>
+
+//<editor-fold desc="Extra">
+/**
+ * 设置键值对的tag
+ */
+fun Request.Builder.setExtra(name: String, value: Any?) = apply {
+    val extras = extras()
+    if (value == null) {
+        extras.remove(name)
+    } else {
+        extras[name] = value
+    }
+}
+
+/**
+ * 全部键值对标签
+ */
+fun Request.Builder.extras(): HashMap<String, Any?> {
+    return tagOf<NetTag.Extras>() ?: kotlin.run {
+        val tag = NetTag.Extras()
+        tagOf(tag)
+        tag
+    }
+}
+
+//</editor-fold>
+
+//<editor-fold desc="Tag">
+
+/**
+ * 返回OkHttp的tag(通过Class区分的tag)
+ */
+inline fun <reified T> Request.Builder.tagOf(): T? {
+    return tags()[T::class.java] as? T
+}
+
+/**
+ * 设置OkHttp的tag(通过Class区分的tag)
+ */
+inline fun <reified T> Request.Builder.tagOf(value: T) = apply {
+    tag(T::class.java, value)
+}
+
+/**
+ * 标签集合
+ */
+fun Request.Builder.tags(): MutableMap<Class<*>, Any?> {
+    return OkHttpUtils.tags(this)
+}
+//</editor-fold>
+
+//<editor-fold desc="Progress">
+/**
+ * 全部的上传监听器
+ */
+fun Request.Builder.uploadListeners(): ConcurrentLinkedQueue<ProgressListener> {
+    return tagOf<NetTag.UploadListeners>() ?: kotlin.run {
+        val tag = NetTag.UploadListeners()
+        tagOf(tag)
+        tag
+    }
+}
+
+/**
+ * 全部的下载监听器
+ */
+fun Request.Builder.downloadListeners(): ConcurrentLinkedQueue<ProgressListener> {
+    return tagOf<NetTag.DownloadListeners>() ?: kotlin.run {
+        val tag = NetTag.DownloadListeners()
+        tagOf(tag)
+        tag
+    }
+}
+//</editor-fold>
+
+
+/**
+ * 设置转换器
+ */
+fun Request.Builder.setConverter(converter: NetConverter) = apply {
+    tag(NetConverter::class.java, converter)
+}

+ 58 - 201
net/src/main/java/com/drake/net/request/RequestExtension.kt

@@ -22,21 +22,23 @@ import com.drake.net.body.peekString
 import com.drake.net.body.value
 import com.drake.net.convert.NetConverter
 import com.drake.net.interfaces.ProgressListener
-import com.drake.net.tag.NetLabel
-import okhttp3.*
+import com.drake.net.tag.NetTag
+import okhttp3.FormBody
+import okhttp3.MultipartBody
+import okhttp3.OkHttpUtils
+import okhttp3.Request
 import java.net.URLDecoder
 import java.util.concurrent.ConcurrentLinkedQueue
 import kotlin.reflect.KType
 
-//<editor-fold desc="请求属性">
-
+//<editor-fold desc="ID">
 /**
  * 请求的Id
  */
 var Request.id: Any?
-    get() = label<NetLabel.RequestId>()
+    get() = tagOf<NetTag.RequestId>()
     set(value) {
-        setLabel(NetLabel.RequestId(value))
+        tagOf(NetTag.RequestId(value))
     }
 
 /**
@@ -45,205 +47,131 @@ var Request.id: Any?
  * 在设计理念上分组可以重复. Id不行
  */
 var Request.group: Any?
-    get() = label<NetLabel.RequestGroup>()
+    get() = tagOf<NetTag.RequestGroup>()
     set(value) {
-        setLabel(NetLabel.RequestGroup(value))
+        tagOf(NetTag.RequestGroup(value))
     }
+//</editor-fold>
 
 /**
  * 是否输出网络请求日志
  * 该属性和[NetConfig.logEnabled]有所区别
  * @see [com.drake.net.interceptor.LogRecordInterceptor]
  */
-var Request.logRecord: Boolean?
-    get() = label<NetLabel.LogRecord>()?.enabled
+var Request.logRecord: Boolean
+    get() = tagOf<NetTag.LogRecord>()?.enabled ?: true
     set(value) {
-        setLabel(value?.let { NetLabel.LogRecord(it) })
+        tagOf(NetTag.LogRecord(value))
     }
 
 /**
  * KType属于Kotlin特有的Type, 某些kotlin独占框架可能会使用到. 例如 kotlin.serialization
  */
 var Request.kType: KType?
-    get() = label<NetLabel.RequestKType>()?.value
+    get() = tagOf<NetTag.RequestKType>()?.value
     set(value) {
-        setLabel(NetLabel.RequestKType(value))
+        tagOf(NetTag.RequestKType(value))
     }
 
-//</editor-fold>
-
-//<editor-fold desc="Request.Builder">
-
-/**
- * 设置请求Id
- */
-fun Request.Builder.setId(id: Any?) = apply {
-    setLabel(NetLabel.RequestId(id))
-}
-
-/**
- * 设置请求分组
- */
-fun Request.Builder.setGroup(group: Any?) = apply {
-    setLabel(NetLabel.RequestGroup(group))
-}
-
-/**
- * 设置是否记录日志
- */
-fun Request.Builder.setLogRecord(enabled: Boolean) = apply {
-    setLabel(NetLabel.LogRecord(enabled))
-}
-
-/**
- * 设置KType
- */
-fun Request.Builder.setKType(type: KType) = apply {
-    setLabel(NetLabel.RequestKType(type))
-}
-
-/**
- * 全部的请求头
- */
-fun Request.Builder.headers(): Headers.Builder {
-    return OkHttpUtils.headers(this)
-}
-//</editor-fold>
-
-//<editor-fold desc="标签">
 
+//<editor-fold desc="Extra">
 /**
  * 返回键值对的标签
  * 键值对标签即OkHttp中的实际tag(在Net中叫label)中的一个Map集合
  */
-fun Request.tag(name: String): Any? {
-    return label<NetLabel.Tags>()?.get(name)
-}
-
-/**
- * 设置键值对的标签
- */
-fun Request.setTag(name: String, value: Any?) {
-    val tags = tags()
-    if (value == null) {
-        tags.remove(name)
-    } else {
-        tags[name] = value
-    }
-}
-
-/**
- * 设置键值对的tag
- */
-fun Request.Builder.setTag(name: String, value: Any?) = apply {
-    val tags = tags()
-    if (value == null) {
-        tags.remove(name)
-    } else {
-        tags[name] = value
-    }
+fun Request.extra(name: String): Any? {
+    return tagOf<NetTag.Extras>()?.get(name)
 }
 
 /**
  * 全部键值对标签
  */
-fun Request.tags(): HashMap<String, Any?> {
-    var tags = label<NetLabel.Tags>()
-    if (tags == null) {
-        tags = NetLabel.Tags()
-        setLabel(tags)
-    }
-    return tags
-}
-
-/**
- * 全部键值对标签
- */
-fun Request.Builder.tags(): HashMap<String, Any?> {
-    var tags = label<NetLabel.Tags>()
-    if (tags == null) {
-        tags = NetLabel.Tags()
-        setLabel(tags)
+fun Request.extras(): HashMap<String, Any?> {
+    val tags = tags()
+    return tags[NetTag.Extras::class.java] as NetTag.Extras? ?: kotlin.run {
+        val tag = NetTag.Extras()
+        tags[NetTag.Extras::class.java] = tag
+        tag
     }
-    return tags
 }
+//</editor-fold>
 
+//<editor-fold desc="Tag">
 /**
  * 返回OkHttp的tag(通过Class区分的tag)
  */
-inline fun <reified T> Request.label(): T? {
+inline fun <reified T> Request.tagOf(): T? {
     return tag(T::class.java)
 }
 
-/**
- * 返回OkHttp的tag(通过Class区分的tag)
- */
-inline fun <reified T> Request.Builder.label(): T? {
-    return labels()[T::class.java] as? T
-}
-
 /**
  * 设置OkHttp的tag(通过Class区分的tag)
  */
-inline fun <reified T> Request.setLabel(value: T) = apply {
-    val labels = labels()
-    if (value == null) {
-        labels.remove(T::class.java)
-    } else {
-        labels[T::class.java] = value
-    }
+inline fun <reified T> Request.tagOf(value: T) = apply {
+    tags()[T::class.java] = value
 }
 
 /**
- * 设置OkHttp的tag(通过Class区分的tag)
+ * 标签集合
  */
-inline fun <reified T> Request.Builder.setLabel(value: T) = apply {
-    tag(T::class.java, value)
+fun Request.tags(): MutableMap<Class<*>, Any?> {
+    return OkHttpUtils.tags(this)
 }
 
+//</editor-fold>
+
+//<editor-fold desc="Progress">
 /**
- * 标签集合
+ * 全部的上传监听器
  */
-fun Request.labels(): MutableMap<Class<*>, Any?> {
-    return OkHttpUtils.tags(this)
+fun Request.uploadListeners(): ConcurrentLinkedQueue<ProgressListener> {
+    return tagOf<NetTag.UploadListeners>() ?: kotlin.run {
+        val tag = NetTag.UploadListeners()
+        tagOf(tag)
+        tag
+    }
 }
 
 /**
- * 标签集合
+ * 全部的下载监听器
  */
-fun Request.Builder.labels(): MutableMap<Class<*>, Any?> {
-    return OkHttpUtils.tags(this)
+fun Request.downloadListeners(): ConcurrentLinkedQueue<ProgressListener> {
+    return tagOf<NetTag.DownloadListeners>() ?: kotlin.run {
+        val tag = NetTag.DownloadListeners()
+        tagOf(tag)
+        tag
+    }
 }
 
 //</editor-fold>
 
-//<editor-fold desc="下载">
+//<editor-fold desc="Download">
 /**
  * 当指定下载目录存在同名文件是覆盖还是进行重命名, 重命名规则是: $文件名_($序号).$后缀
  */
 fun Request.downloadConflictRename(): Boolean {
-    return label<NetLabel.DownloadFileConflictRename>()?.enabled == true
+    return tagOf<NetTag.DownloadFileConflictRename>()?.enabled == true
 }
 
 /**
  * 是否进行校验文件md5, 如果校验则匹配上既马上返回文件而不会进行下载
  */
 fun Request.downloadMd5Verify(): Boolean {
-    return label<NetLabel.DownloadFileMD5Verify>()?.enabled == true
+    return tagOf<NetTag.DownloadFileMD5Verify>()?.enabled == true
 }
 
 /**
  * 下载文件目录
  */
 fun Request.downloadFileDir(): String {
-    return label<NetLabel.DownloadFileDir>()?.dir ?: NetConfig.app.filesDir.absolutePath
+    return tagOf<NetTag.DownloadFileDir>()?.dir ?: NetConfig.app.filesDir.absolutePath
 }
 
 /**
  * 下载文件名
  */
 fun Request.downloadFileName(): String? {
-    return label<NetLabel.DownloadFileName>()?.name
+    return tagOf<NetTag.DownloadFileName>()?.name
 }
 
 /**
@@ -251,7 +179,7 @@ fun Request.downloadFileName(): String? {
  * 例如下载的文件名如果是中文, 服务器传输给你的会是被URL编码的字符串. 你使用URL解码后才是可读的中文名称
  */
 fun Request.downloadFileNameDecode(): Boolean {
-    return label<NetLabel.DownloadFileNameDecode>()?.enabled == true
+    return tagOf<NetTag.DownloadFileNameDecode>()?.enabled == true
 }
 
 /**
@@ -261,71 +189,7 @@ fun Request.downloadFileNameDecode(): Boolean {
  *      下载文件名: install.apk, 临时文件名: install.apk.net-download
  */
 fun Request.downloadTempFile(): Boolean {
-    return label<NetLabel.DownloadTempFile>()?.enabled == true
-}
-//</editor-fold>
-
-//<editor-fold desc="进度监听">
-/**
- * 全部的上传监听器
- */
-fun Request.uploadListeners(): ConcurrentLinkedQueue<ProgressListener> {
-    var uploadListeners = label<NetLabel.UploadListeners>()
-    if (uploadListeners == null) {
-        uploadListeners = NetLabel.UploadListeners()
-        setLabel(uploadListeners)
-    }
-    return uploadListeners
-}
-
-/**
- * 全部的上传监听器
- */
-fun Request.Builder.uploadListeners(): ConcurrentLinkedQueue<ProgressListener> {
-    var uploadListeners = label<NetLabel.UploadListeners>()
-    if (uploadListeners == null) {
-        uploadListeners = NetLabel.UploadListeners()
-        setLabel(uploadListeners)
-    }
-    return uploadListeners
-}
-
-/**
- * 全部的下载监听器
- */
-fun Request.downloadListeners(): ConcurrentLinkedQueue<ProgressListener> {
-    var downloadListeners = label<NetLabel.DownloadListeners>()
-    if (downloadListeners == null) {
-        downloadListeners = NetLabel.DownloadListeners()
-        setLabel(downloadListeners)
-    }
-    return downloadListeners
-}
-
-/**
- * 全部的下载监听器
- */
-fun Request.Builder.downloadListeners(): ConcurrentLinkedQueue<ProgressListener> {
-    var downloadListeners = label<NetLabel.DownloadListeners>()
-    if (downloadListeners == null) {
-        downloadListeners = NetLabel.DownloadListeners()
-        setLabel(downloadListeners)
-    }
-    return downloadListeners
-}
-
-/**
- * 添加上传监听器
- */
-fun Request.addUploadListener(progressListener: ProgressListener) {
-    uploadListeners().add(progressListener)
-}
-
-/**
- * 添加下载监听器
- */
-fun Request.addDownloadListener(progressListener: ProgressListener) {
-    downloadListeners().add(progressListener)
+    return tagOf<NetTag.DownloadTempFile>()?.enabled == true
 }
 //</editor-fold>
 
@@ -333,14 +197,7 @@ fun Request.addDownloadListener(progressListener: ProgressListener) {
  * 返回请求包含的转换器
  */
 fun Request.converter(): NetConverter {
-    return label<NetConverter>() ?: NetConfig.converter
-}
-
-/**
- * 设置转换器
- */
-fun Request.Builder.setConverter(converter: NetConverter) = apply {
-    setLabel(converter)
+    return tagOf<NetConverter>() ?: NetConfig.converter
 }
 
 /**

+ 2 - 2
net/src/main/java/com/drake/net/tag/NetLabel.kt → net/src/main/java/com/drake/net/tag/NetTag.kt

@@ -21,8 +21,8 @@ import java.io.File
 import java.util.concurrent.ConcurrentLinkedQueue
 import kotlin.reflect.KType
 
-sealed class NetLabel {
-    class Tags : HashMap<String, Any?>()
+sealed class NetTag {
+    class Extras : HashMap<String, Any?>()
 
     inline class RequestId(val value: Any?)
     inline class RequestGroup(val value: Any?)