parent
61b2b16776
commit
23d701417d
@ -0,0 +1,21 @@
|
||||
package com.common.commonlib.net.interceptor
|
||||
|
||||
import okhttp3.Interceptor
|
||||
|
||||
/**
|
||||
* 通用拦截器基类
|
||||
*
|
||||
* @author Alex Wang
|
||||
* @since 2022-2-24
|
||||
*/
|
||||
abstract class CommonInterceptor : Interceptor {
|
||||
/**
|
||||
* 获取拦截器类型
|
||||
*/
|
||||
abstract fun getType(): String
|
||||
|
||||
/**
|
||||
* 获取拦截器key
|
||||
*/
|
||||
abstract fun getKey(): String
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package com.common.commonlib.net.manager
|
||||
|
||||
import com.common.commonlib.net.interceptor.CommonInterceptor
|
||||
|
||||
/**
|
||||
* 通用拦截器管理类
|
||||
* 用于请求的通用拦截器管理
|
||||
*
|
||||
* @author Alex Wang
|
||||
* @since 2022-2-24
|
||||
*/
|
||||
object CommonInterceptorManager {
|
||||
const val TAG = "CommonInterceptorManager"
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
*/
|
||||
private val mInterceptors: HashMap<String, HashMap<Any, CommonInterceptor>> = HashMap()
|
||||
|
||||
/**
|
||||
* 需要被预置的拦截器
|
||||
*/
|
||||
val presetRequireTypes: ArrayList<String> = ArrayList()
|
||||
|
||||
/**
|
||||
* 通过类型获取该类型拦截器列表
|
||||
*
|
||||
* @param type 拦截器类型
|
||||
*
|
||||
* @return 拦截器列表
|
||||
*/
|
||||
fun getInterceptorsByType(type: String): Map<Any, CommonInterceptor>? {
|
||||
return mInterceptors[type]
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型获取该类型拦截器列表
|
||||
*
|
||||
* @param type 拦截器类型
|
||||
*
|
||||
* @return 拦截器列表
|
||||
*/
|
||||
fun getInterceptorsByType(type: String, key: String): CommonInterceptor? {
|
||||
return mInterceptors[type]?.get(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需要预置类型的拦截器
|
||||
*/
|
||||
fun getInterceptorsByRequiredTypeList(): HashMap<Any, CommonInterceptor> {
|
||||
return getInterceptorsByTypeList(presetRequireTypes)
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过类型列表获取通用拦截器
|
||||
* @param typeList 拦截器类型
|
||||
*
|
||||
* @return 拦截器map
|
||||
*/
|
||||
fun getInterceptorsByTypeList(typeList: ArrayList<String>): HashMap<Any, CommonInterceptor> {
|
||||
val result = HashMap<Any, CommonInterceptor>()
|
||||
for (item in typeList) {
|
||||
if (!getInterceptorsByType(item).isNullOrEmpty()) {
|
||||
result.putAll(getInterceptorsByType(item)!!)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新的通用拦截器
|
||||
*
|
||||
* @param interceptor 拦截器
|
||||
*/
|
||||
fun addInterceptorByType(interceptor: CommonInterceptor, presetRequired: Boolean) {
|
||||
val type = interceptor.getType()
|
||||
val key = interceptor.getKey()
|
||||
var map = mInterceptors[type]
|
||||
if (map.isNullOrEmpty()) {
|
||||
map = HashMap()
|
||||
mInterceptors[type] = map
|
||||
}
|
||||
map[key] = interceptor
|
||||
if (presetRequired && !presetRequireTypes.contains(type)) {
|
||||
presetRequireTypes.add(type)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除通用拦截器
|
||||
*
|
||||
* @param type 请求类型
|
||||
* @param key 拦截器key
|
||||
*/
|
||||
fun removeInterceptorByType(type: String, key: String) {
|
||||
val map = mInterceptors[type]
|
||||
if (!map.isNullOrEmpty()) {
|
||||
map.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除通用拦截器
|
||||
*
|
||||
* @param type 请求类型
|
||||
*/
|
||||
fun removeInterceptorByType(type: String) {
|
||||
mInterceptors.remove(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除通用拦截器
|
||||
*
|
||||
* @param interceptor 拦截器
|
||||
*/
|
||||
fun removeInterceptorByType(interceptor: CommonInterceptor) {
|
||||
removeInterceptorByType(interceptor.getType(), interceptor.getKey())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue