|
1 | 1 | package hirez.api; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
3 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
5 | 6 | import hirez.api.object.*; |
@@ -97,12 +98,7 @@ protected final <T> Single<T> call(Class<T> type, String method, String... argv) |
97 | 98 | } |
98 | 99 |
|
99 | 100 | 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); |
106 | 102 | } |
107 | 103 |
|
108 | 104 | protected final <T> Single<T> testAndCall(Class<T> type, String method, String... argv) { |
@@ -130,14 +126,18 @@ public final Single<CreateSession> createSession() { |
130 | 126 | public final Single<TestSession> testSession() { |
131 | 127 | return call(String.class, "testsession") |
132 | 128 | .map(TestSession::new).flatMap(response -> |
133 | | - Single.create(sink -> { |
| 129 | + Single.<TestSession>create(sink -> { |
134 | 130 | if (response.isSuccessful()) { |
135 | 131 | sink.onSuccess(response); |
136 | 132 | } else { |
137 | 133 | sink.onError(new HiRezException(response.getRawMessage())); |
138 | 134 | } |
139 | 135 | }) |
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 | + }); |
141 | 141 | } |
142 | 142 |
|
143 | 143 | public final Single<Ping> ping() { |
|
0 commit comments