Skip to content

Commit 752a86b

Browse files
committed
feat: reload session when is expired or invalid.
And get ResponseBody's Reader - not String
1 parent 63d7b00 commit 752a86b

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

api/src/main/java/hirez/api/Endpoint.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hirez.api;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
56
import hirez.api.object.*;
@@ -97,12 +98,7 @@ protected final <T> Single<T> call(Class<T> type, String method, String... argv)
9798
}
9899

99100
private <T> T buildResponse(Response response, Class<T> type) throws IOException {
100-
String body = Objects.requireNonNull(response.body()).string();
101-
if (type.isInstance(body)) { // Safety reflection if instance is String
102-
return type.cast(body); // Safety cast avoid use @SuppressWarnings annotation
103-
} else {
104-
return mapper.readValue(body, type);
105-
}
101+
return mapper.readValue(Objects.requireNonNull(response.body()).charStream(), type);
106102
}
107103

108104
protected final <T> Single<T> testAndCall(Class<T> type, String method, String... argv) {
@@ -130,14 +126,18 @@ public final Single<CreateSession> createSession() {
130126
public final Single<TestSession> testSession() {
131127
return call(String.class, "testsession")
132128
.map(TestSession::new).flatMap(response ->
133-
Single.create(sink -> {
129+
Single.<TestSession>create(sink -> {
134130
if (response.isSuccessful()) {
135131
sink.onSuccess(response);
136132
} else {
137133
sink.onError(new HiRezException(response.getRawMessage()));
138134
}
139135
})
140-
);
136+
).onErrorResumeNext(e -> {
137+
if (e instanceof HiRezException && e.getMessage().contains("Invalid session id.")) {
138+
return createSession().flatMap($ -> testSession());
139+
} else return Single.error(e);
140+
});
141141
}
142142

143143
public final Single<Ping> ping() {

0 commit comments

Comments
 (0)