Skip to content

Commit

Permalink
Replacing regular with enhanced switches in GitHubAPIConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico committed Aug 2, 2023
1 parent b38ddf4 commit 3880807
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/main/java/usi/si/seart/github/GitHubAPIConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,12 @@ public FetchCallback.Result doWithRetry(RetryContext context) throws Exception {
String body = response.body().string();
JsonElement element = conversionService.convert(body, JsonElement.class);

switch (series) {
case SUCCESSFUL:
return new Result(status, headers, element);
case INFORMATIONAL:
case REDIRECTION:
return new Result(status, headers, JsonNull.INSTANCE);
case CLIENT_ERROR:
return handleClientError(status, headers, element.getAsJsonObject());
case SERVER_ERROR:
return handleServerError(status, element.getAsJsonObject());
default:
}

throw new IllegalStateException("This line should never be reached");
return switch (series) {
case SUCCESSFUL -> new Result(status, headers, element);
case CLIENT_ERROR -> handleClientError(status, headers, element.getAsJsonObject());
case SERVER_ERROR -> handleServerError(status, element.getAsJsonObject());
case INFORMATIONAL, REDIRECTION -> new Result(status, headers, JsonNull.INSTANCE);
};
}

private FetchCallback.Result handleServerError(HttpStatus status, JsonObject json) {
Expand All @@ -375,19 +367,15 @@ private FetchCallback.Result handleClientError(
) throws InterruptedException {
ErrorResponse errorResponse = conversionService.convert(json, ErrorResponse.class);
switch (status) {
case UNAUTHORIZED:
/*
* Here we should not call `replaceTokenIfExpired()`
* since it would lead to an infinite loop,
* because we are checking the Rate Limit API
* with the very same unauthorized token.
*/
gitHubTokenManager.replaceToken();
break;
case TOO_MANY_REQUESTS:
TimeUnit.MINUTES.sleep(5);
break;
case FORBIDDEN:
/*
* Here we should not call `replaceTokenIfExpired()`
* since it would lead to an infinite loop,
* because we are checking the Rate Limit API
* with the very same unauthorized token.
*/
case UNAUTHORIZED -> gitHubTokenManager.replaceToken();
case TOO_MANY_REQUESTS -> TimeUnit.MINUTES.sleep(5);
case FORBIDDEN -> {
/*
* Response status code 403, two possibilities:
* (1) The rate limit for the current token is exceeded
Expand All @@ -404,16 +392,17 @@ private FetchCallback.Result handleClientError(
throw new IllegalStateException(message);
} else if (remaining == 0) {
gitHubTokenManager.replaceTokenIfExpired();
break;
} else {
/*
* Case (2) encountered, so we propagate error upwards
* @see fetchLastPageNumberFromHeader
*/
return new Result(status, headers, json);
}
default:
}
default -> {
// TODO: 30.07.23 Add any other special logic here
}
}
throw new HttpClientErrorException(status, errorResponse.getMessage());
}
Expand Down

0 comments on commit 3880807

Please sign in to comment.