Skip to content

Commit

Permalink
Active waiting in case when Polygon didn't give a correct API respons…
Browse files Browse the repository at this point in the history
…e added
  • Loading branch information
perveevm committed Apr 24, 2022
1 parent 14c9f44 commit 3e4931a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.perveevm</groupId>
<artifactId>polygon-api</artifactId>
<version>0.6</version>
<version>0.6.1</version>

<properties>
<maven.compiler.source>15</maven.compiler.source>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ru/perveevm/polygon/api/PolygonSession.java
Expand Up @@ -818,7 +818,22 @@ private JsonElement sendAPIRequest(final String methodName, final List<NameValue
throws PolygonSessionException {
String json = sendAPIRequestPlain(methodName, parameters);

JSONResponse jsonResponse = gson.fromJson(json, JSONResponse.class);
JSONResponse jsonResponse;
long timeout = 100;
while (true) {
try {
jsonResponse = gson.fromJson(json, JSONResponse.class);
break;
} catch (JsonSyntaxException e) {
try {
Thread.currentThread().wait(timeout);
} catch (InterruptedException ie) {
throw new PolygonSessionException("Session thread was interrupted", e);
}
timeout *= 2;
}
}

if (jsonResponse.getStatus() == JSONResponseStatus.FAILED) {
throw new PolygonSessionFailedRequestException(BASE_URL + methodName, parameters,
jsonResponse.getComment());
Expand Down

0 comments on commit 3e4931a

Please sign in to comment.