forked from liujingxing/rxhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxHttpPlugins.java
More file actions
143 lines (120 loc) · 4.49 KB
/
RxHttpPlugins.java
File metadata and controls
143 lines (120 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package rxhttp;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import io.reactivex.functions.Function;
import io.reactivex.internal.util.ExceptionHelper;
import rxhttp.wrapper.cahce.CacheManager;
import rxhttp.wrapper.cahce.CacheMode;
import rxhttp.wrapper.cahce.CacheStrategy;
import rxhttp.wrapper.cahce.InternalCache;
import rxhttp.wrapper.callback.IConverter;
import rxhttp.wrapper.converter.GsonConverter;
import rxhttp.wrapper.param.Param;
/**
* RxHttp 插件类
* User: ljx
* Date: 2019-07-14
* Time: 11:24
*/
public class RxHttpPlugins {
private static Function<? super Param, ? extends Param> mOnParamAssembly;
private static Function<? super String, String> decoder;
private static IConverter converter = GsonConverter.create();
private static List<String> excludeCacheKeys = Collections.emptyList();
private static InternalCache cache;
private static CacheStrategy cacheStrategy = new CacheStrategy(CacheMode.ONLY_NETWORK);
//设置公共参数装饰
public static void setOnParamAssembly(@Nullable Function<? super Param, ? extends Param> onParamAssembly) {
mOnParamAssembly = onParamAssembly;
}
//设置转换器,可用于对Http返回的String 字符串解密
/**
* @deprecated please user {@link #setResultDecoder(Function)} instead
* @param decoder 数据转换器
*/
@Deprecated
public static void setOnConverter(@Nullable Function<? super String, String> decoder) {
setResultDecoder(decoder);
}
//设置解码/解密器,可用于对Http返回的String 字符串解码/解密
public static void setResultDecoder(@Nullable Function<? super String, String> decoder) {
RxHttpPlugins.decoder = decoder;
}
public static void setConverter(@NonNull IConverter converter) {
if (converter == null)
throw new IllegalArgumentException("converter can not be null");
RxHttpPlugins.converter = converter;
}
public static IConverter getConverter() {
return converter;
}
/**
* <P>对Param参数添加一层装饰,可以在该层做一些与业务相关工作,
* <P>例如:添加公共参数/请求头信息
*
* @param source Param
* @return 装饰后的参数
*/
public static Param onParamAssembly(Param source) {
if (source == null || !source.isAssemblyEnabled()) return source;
Function<? super Param, ? extends Param> f = mOnParamAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
/**
* 对字符串进行解码/解密
*
* @param source String字符串
* @return 解码/解密后字符串
*/
public static String onResultDecoder(String source) {
Function<? super String, String> f = decoder;
if (f != null) {
return apply(f, source);
}
return source;
}
@NonNull
private static <T, R> R apply(@NonNull Function<T, R> f, @NonNull T t) {
try {
return f.apply(t);
} catch (Throwable ex) {
throw ExceptionHelper.wrapOrThrow(ex);
}
}
public static CacheStrategy getCacheStrategy() {
if (cacheStrategy == null) {
cacheStrategy = new CacheStrategy(CacheMode.ONLY_NETWORK);
}
return new CacheStrategy(cacheStrategy);
}
public static InternalCache getCache() {
return cache;
}
public static void setCache(File directory, long maxSize) {
setCache(directory, maxSize, CacheMode.ONLY_NETWORK, -1);
}
public static void setCache(File directory, long maxSize, long cacheValidTime) {
setCache(directory, maxSize, CacheMode.ONLY_NETWORK, cacheValidTime);
}
public static void setCache(File directory, long maxSize, CacheMode cacheMode) {
setCache(directory, maxSize, cacheMode, -1);
}
public static void setCache(File directory, long maxSize, CacheMode cacheMode, long cacheValidTime) {
CacheManager rxHttpCache = new CacheManager(directory, maxSize);
RxHttpPlugins.cache = rxHttpCache.internalCache;
RxHttpPlugins.cacheStrategy = new CacheStrategy(cacheMode, cacheValidTime);
}
public static void setExcludeCacheKeys(String... keys) {
excludeCacheKeys = Arrays.asList(keys);
}
public static List<String> getExcludeCacheKeys() {
return excludeCacheKeys;
}
}