3
3
import androidx .annotation .NonNull ;
4
4
import java .io .File ;
5
5
import java .io .IOException ;
6
- import java .util .Arrays ;
7
- import java .util .Collections ;
8
- import java .util .List ;
9
6
import okhttp3 .Cache ;
10
7
import okhttp3 .Interceptor ;
11
8
import okhttp3 .OkHttpClient ;
@@ -24,8 +21,7 @@ public final class OkHttpConnectionFactory {
24
21
@ NonNull private static final Cache NET_CACHE = new Cache (new File (CommonsApplication .getInstance ().getCacheDir (),
25
22
CACHE_DIR_NAME ), NET_CACHE_SIZE );
26
23
27
- @ NonNull
28
- private static final OkHttpClient CLIENT = createClient ();
24
+ @ NonNull private static final OkHttpClient CLIENT = createClient ();
29
25
30
26
@ NonNull public static OkHttpClient getClient () {
31
27
return CLIENT ;
@@ -44,7 +40,7 @@ private static OkHttpClient createClient() {
44
40
45
41
private static HttpLoggingInterceptor getLoggingInterceptor () {
46
42
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor ()
47
- .setLevel (Level .BASIC );
43
+ .setLevel (Level .BASIC );
48
44
49
45
httpLoggingInterceptor .redactHeader ("Authorization" );
50
46
httpLoggingInterceptor .redactHeader ("Cookie" );
@@ -53,10 +49,7 @@ private static HttpLoggingInterceptor getLoggingInterceptor() {
53
49
}
54
50
55
51
private static class CommonHeaderRequestInterceptor implements Interceptor {
56
-
57
- @ Override
58
- @ NonNull
59
- public Response intercept (@ NonNull final Chain chain ) throws IOException {
52
+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
60
53
final Request request = chain .request ().newBuilder ()
61
54
.header ("User-Agent" , CommonsApplication .getInstance ().getUserAgent ())
62
55
.build ();
@@ -65,44 +58,25 @@ public Response intercept(@NonNull final Chain chain) throws IOException {
65
58
}
66
59
67
60
public static class UnsuccessfulResponseInterceptor implements Interceptor {
68
- private static final List <String > DO_NOT_INTERCEPT = Collections .singletonList (
69
- "api.php?format=json&formatversion=2&errorformat=plaintext&action=upload&ignorewarnings=1" );
70
61
71
62
private static final String ERRORS_PREFIX = "{\" error" ;
72
63
73
- @ Override
74
- @ NonNull
75
- public Response intercept (@ NonNull final Chain chain ) throws IOException {
64
+ @ Override @ NonNull public Response intercept (@ NonNull final Chain chain ) throws IOException {
76
65
final Response rsp = chain .proceed (chain .request ());
77
-
78
- // Do not intercept certain requests and let the caller handle the errors
79
- if (isExcludedUrl (chain .request ())) {
80
- return rsp ;
81
- }
82
66
if (rsp .isSuccessful ()) {
83
67
try (final ResponseBody responseBody = rsp .peekBody (ERRORS_PREFIX .length ())) {
84
- if (ERRORS_PREFIX .equals (responseBody .string ())) {
68
+ if (ERRORS_PREFIX .equals (responseBody .string ())){
85
69
try (final ResponseBody body = rsp .body ()) {
86
70
throw new IOException (body .string ());
87
71
}
88
72
}
89
- } catch (final IOException e ) {
73
+ }catch (final IOException e ){
90
74
Timber .e (e );
91
75
}
92
76
return rsp ;
93
77
}
94
78
throw new HttpStatusException (rsp );
95
79
}
96
-
97
- private boolean isExcludedUrl (final Request request ) {
98
- final String requestUrl = request .url ().toString ();
99
- for (final String url : DO_NOT_INTERCEPT ) {
100
- if (requestUrl .contains (url )) {
101
- return true ;
102
- }
103
- }
104
- return false ;
105
- }
106
80
}
107
81
108
82
private OkHttpConnectionFactory () {
0 commit comments