Skip to content

Commit 0d737eb

Browse files
committed
Remove init and use validator for each individual request
1 parent 37048e1 commit 0d737eb

1 file changed

Lines changed: 31 additions & 38 deletions

File tree

library/src/main/java/com/proxerme/library/connection/ProxerConnection.java

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,33 @@ public class ProxerConnection {
4646
private static final String RESPONSE_ERROR = "error";
4747
private static final String RESPONSE_ERROR_MESSAGE = "msg";
4848
private static final String VALIDATOR_ID = "default-validator";
49+
private static final ResponseValidator defaultValidator = new ResponseValidator() {
50+
@Override
51+
public boolean validate(@NonNull Response response) throws Exception {
52+
JSONObject json = response.asJsonObject();
53+
54+
if (json.has(RESPONSE_ERROR)) {
55+
if (json.getInt(RESPONSE_ERROR) == 0) {
56+
return true;
57+
} else {
58+
if (json.has(RESPONSE_ERROR_MESSAGE)) {
59+
throw new ProxerException(PROXER,
60+
json.getString(RESPONSE_ERROR_MESSAGE));
61+
} else {
62+
throw new ProxerException(UNKNOWN);
63+
}
64+
}
65+
} else {
66+
return false;
67+
}
68+
}
4969

70+
@NonNull
71+
@Override
72+
public String id() {
73+
return VALIDATOR_ID;
74+
}
75+
};
5076
private static LinkedList<Thread> parseThreads = new LinkedList<>();
5177

5278
/**
@@ -106,40 +132,6 @@ public static void cancel(@ConnectionTag int tag) {
106132
Bridge.cancelAll().tag(String.valueOf(tag)).commit();
107133
}
108134

109-
/**
110-
* Does some initialization steps. You *must* call this method somewhere in your lifecycle. A
111-
* good place might be the onCreate method of your main Activity.
112-
*/
113-
public static void init() {
114-
Bridge.config().validators(new ResponseValidator() {
115-
@Override
116-
public boolean validate(@NonNull Response response) throws Exception {
117-
JSONObject json = response.asJsonObject();
118-
119-
if (json.has(RESPONSE_ERROR)) {
120-
if (json.getInt(RESPONSE_ERROR) == 0) {
121-
return true;
122-
} else {
123-
if (json.has(RESPONSE_ERROR_MESSAGE)) {
124-
throw new ProxerException(PROXER,
125-
json.getString(RESPONSE_ERROR_MESSAGE));
126-
} else {
127-
throw new ProxerException(UNKNOWN);
128-
}
129-
}
130-
} else {
131-
return false;
132-
}
133-
}
134-
135-
@NonNull
136-
@Override
137-
public String id() {
138-
return VALIDATOR_ID;
139-
}
140-
});
141-
}
142-
143135
/**
144136
* Cleans up references and left open connections. You should call this method somewhere in the
145137
* lifecycle, but don't have to. A good place might be the onDestroy method of your main
@@ -300,7 +292,7 @@ public NewsRequest(@IntRange(from = 1) int page) {
300292
@NonNull
301293
@Override
302294
protected RequestBuilder buildRequest() {
303-
return Bridge.get(UrlHolder.getNewsUrl(page));
295+
return Bridge.get(UrlHolder.getNewsUrl(page)).validators(defaultValidator);
304296
}
305297

306298
@ConnectionTag
@@ -332,7 +324,8 @@ protected RequestBuilder buildRequest() {
332324
Form loginCredentials = new Form().add(FORM_USERNAME, user.getUsername())
333325
.add(FORM_PASSWORD, user.getPassword());
334326

335-
return Bridge.post(UrlHolder.getLoginUrl()).body(loginCredentials);
327+
return Bridge.post(UrlHolder.getLoginUrl()).body(loginCredentials)
328+
.validators(defaultValidator);
336329
}
337330

338331
@ConnectionTag
@@ -358,7 +351,7 @@ public static class LogoutRequest extends ProxerRequest<Void> {
358351
@NonNull
359352
@Override
360353
protected RequestBuilder buildRequest() {
361-
return Bridge.get(UrlHolder.getLogoutUrl());
354+
return Bridge.get(UrlHolder.getLogoutUrl()).validators(defaultValidator);
362355
}
363356

364357
@ConnectionTag
@@ -387,7 +380,7 @@ public ConferencesRequest(@IntRange(from = 1) int page) {
387380
@NonNull
388381
@Override
389382
protected RequestBuilder buildRequest() {
390-
return Bridge.get(UrlHolder.getConferencesUrl(page));
383+
return Bridge.get(UrlHolder.getConferencesUrl(page)).validators(defaultValidator);
391384
}
392385

393386
@Override

0 commit comments

Comments
 (0)