use of ai.nightfall.scan.model.NightfallRequestTimeoutException in project nightfall-java-sdk by nightfallai.
the class NightfallClient method issueRequest.
/**
* Issues an HTTP request to the provided resource. If the request is successful, the response body will be
* deserialized into an object based on the provided <code>responseClass</code>. If the response indicates a
* rate limiting error, the request will be retried after a short sleep.
*
* @param path the HTTP resource path
* @param method the HTTP verb
* @param body the HTTP request body
* @param headers HTTP headers
* @param responseClass the class to deserialize results into
* @return an instance of the <code>responseClass</code>
* @throws NightfallClientException thrown if an unexpected error occurs while processing the request
* @throws NightfallAPIException thrown if the API returns a 4xx or 5xx error code
* @throws NightfallRequestTimeoutException thrown if the request is aborted because read/write timeout is exceeded
*/
private <E> E issueRequest(String path, String method, MediaType mediaType, byte[] body, Headers headers, Class<E> responseClass) {
String url = this.apiHost + path;
Request.Builder builder = new Request.Builder().url(url);
if (headers != null) {
builder.headers(headers);
}
if (this.implVersion != null && !this.implVersion.equals("")) {
builder.addHeader("User-Agent", "nightfall-java-sdk/" + this.implVersion);
}
builder.addHeader("Authorization", "Bearer " + this.apiKey);
RequestBody reqBody = null;
if (body != null && body.length > 0) {
reqBody = RequestBody.create(body, mediaType);
} else if (!method.equals("GET") && !method.equals("HEAD")) {
reqBody = RequestBody.create(new byte[0]);
}
builder.method(method, reqBody);
Request request = builder.build();
Call call = this.httpClient.newCall(request);
NightfallErrorResponse lastError = null;
int errorCode = 0;
for (int attempt = 0; attempt < this.retryCount; attempt++) {
try (Response response = call.execute()) {
if (!response.isSuccessful()) {
try {
lastError = objectMapper.readValue(response.body().bytes(), NightfallErrorResponse.class);
} catch (Throwable t) {
// best effort to get more info, swallow failure
}
if (response.code() == 429 && attempt < this.retryCount - 1) {
Thread.sleep(1000);
// cannot re-use the same call object
call = call.clone();
continue;
}
// cannot directly throw exception here because of Throwable catch branch; need to break
errorCode = response.code();
break;
}
if (Void.class.equals(responseClass)) {
return null;
}
return objectMapper.readValue(response.body().bytes(), responseClass);
} catch (IOException e) {
// If OkHTTP times out, allow retries
if (e.getMessage().equalsIgnoreCase("timeout") || e.getMessage().equalsIgnoreCase("read timed out")) {
if (attempt >= this.retryCount - 1) {
throw new NightfallRequestTimeoutException("request timed out");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
// swallow
}
// cannot re-use the same call object
call = call.clone();
continue;
}
throw new NightfallClientException("issuing HTTP request: " + e.getMessage());
} catch (Throwable t) {
throw new NightfallClientException("failure executing HTTP request: " + t.getMessage());
}
}
if (errorCode > 0) {
throw new NightfallAPIException("unsuccessful response", lastError, errorCode);
}
String message = "exceeded max retry count on request: " + path;
throw new NightfallAPIException(message, lastError, 429);
}
Aggregations