Search in sources :

Example 1 with NightfallRequestTimeoutException

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);
}
Also used : Call(okhttp3.Call) NightfallClientException(ai.nightfall.scan.model.NightfallClientException) NightfallRequestTimeoutException(ai.nightfall.scan.model.NightfallRequestTimeoutException) InitializeFileUploadRequest(ai.nightfall.scan.model.InitializeFileUploadRequest) ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) Request(okhttp3.Request) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) UploadFileChunkRequest(ai.nightfall.scan.model.UploadFileChunkRequest) CompleteFileUploadRequest(ai.nightfall.scan.model.CompleteFileUploadRequest) IOException(java.io.IOException) NightfallAPIException(ai.nightfall.scan.model.NightfallAPIException) ScanTextResponse(ai.nightfall.scan.model.ScanTextResponse) NightfallErrorResponse(ai.nightfall.scan.model.NightfallErrorResponse) Response(okhttp3.Response) ScanFileResponse(ai.nightfall.scan.model.ScanFileResponse) NightfallErrorResponse(ai.nightfall.scan.model.NightfallErrorResponse) RequestBody(okhttp3.RequestBody)

Aggregations

CompleteFileUploadRequest (ai.nightfall.scan.model.CompleteFileUploadRequest)1 InitializeFileUploadRequest (ai.nightfall.scan.model.InitializeFileUploadRequest)1 NightfallAPIException (ai.nightfall.scan.model.NightfallAPIException)1 NightfallClientException (ai.nightfall.scan.model.NightfallClientException)1 NightfallErrorResponse (ai.nightfall.scan.model.NightfallErrorResponse)1 NightfallRequestTimeoutException (ai.nightfall.scan.model.NightfallRequestTimeoutException)1 ScanFileRequest (ai.nightfall.scan.model.ScanFileRequest)1 ScanFileResponse (ai.nightfall.scan.model.ScanFileResponse)1 ScanTextRequest (ai.nightfall.scan.model.ScanTextRequest)1 ScanTextResponse (ai.nightfall.scan.model.ScanTextResponse)1 UploadFileChunkRequest (ai.nightfall.scan.model.UploadFileChunkRequest)1 IOException (java.io.IOException)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1