public class Connection extends Object implements Closeable
Make sure to close this connection to avoid resources leak.
| Modifier and Type | Field and Description |
|---|---|
static String |
COMET_SDK_API_HEADER |
static int |
CONNECTION_SHUTDOWN_TIMEOUT_MS |
static int |
READ_TIMEOUT_MS |
static int |
REQUEST_TIMEOUT_MS |
| Constructor and Description |
|---|
Connection(@NonNull String cometBaseUrl,
@NonNull String apiKey,
int maxAuthRetries,
@NonNull org.slf4j.Logger logger)
Creates new instance with specified parameters.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this connection immediately by releasing underlying resources.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
downloadAsync(@NonNull File file,
@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params)
Allows downloading remote assets to the provided file asynchronously.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
downloadAsync(@NonNull OutputStream outputStream,
@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params)
Allows downloading data from remote endpoint and write it into provided
OutputStream asynchronously. |
Optional<String> |
sendGetWithRetries(@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params)
Allows sending synchronous GET request to the specified endpoint with given request parameters.
|
Optional<String> |
sendGetWithRetries(@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params,
boolean throwOnFailure)
Allows sending synchronous GET request to the specified endpoint with given request parameters.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(byte[] bytes,
@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params,
Map<FormParamName,Object> formParams)
Allows asynchronous sending of provided byte array as POST request to the specified endpoint.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(@NonNull File file,
@NonNull String endpoint,
@NonNull Map<QueryParamName,String> queryParams,
Map<FormParamName,Object> formParams)
Allows asynchronous posting the content of the file as multipart form data to the specified endpoint.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(@NonNull String endpoint,
@NonNull Map<QueryParamName,String> params,
@NonNull Map<FormParamName,Object> formParams)
Allows asynchronous FORM submission to the specified endpoint.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(@NonNull String json,
@NonNull String endpoint)
Allows asynchronous sending of text as JSON encoded body of the POST request.
|
Optional<String> |
sendPostWithRetries(@NonNull String json,
@NonNull String endpoint,
boolean throwOnFailure)
Allows sending POST to the specified endpoint with body as JSON string.
|
void |
waitAndClose(@NonNull Duration timeout)
Allows to properly close this connection after all scheduled posts request are executed or if timeout expired.
|
public static final int READ_TIMEOUT_MS
public static final int REQUEST_TIMEOUT_MS
public static final int CONNECTION_SHUTDOWN_TIMEOUT_MS
public static final String COMET_SDK_API_HEADER
public Connection(@NonNull
@NonNull String cometBaseUrl,
@NonNull
@NonNull String apiKey,
int maxAuthRetries,
@NonNull
@NonNull org.slf4j.Logger logger)
cometBaseUrl - the base URL of the Comet server's endpoints.apiKey - the API key to authorize Comet API accessmaxAuthRetries - the maximum number of retries per failed request.logger - the Logger to collect log records.public Optional<String> sendGetWithRetries(@NonNull @NonNull String endpoint, @NonNull @NonNull Map<QueryParamName,String> params)
maxAuthRetries attempts.endpoint - the request path of the endpointparams - the map with request parameters.Optional of response body.public Optional<String> sendGetWithRetries(@NonNull @NonNull String endpoint, @NonNull @NonNull Map<QueryParamName,String> params, boolean throwOnFailure) throws CometApiException
maxAuthRetries attempts.
If request failed the CometApiException will be thrown with related details if throwOnFailure
parameter set.
endpoint - the request path of the endpointparams - the map with request parameters.throwOnFailure - if true then CometApiException will be thrown on failure.
Otherwise, the empty Optional returned.Optional of response body or empty Optional.CometApiException - if failed and throwOnFailure parameter set.public Optional<String> sendPostWithRetries(@NonNull @NonNull String json, @NonNull @NonNull String endpoint, boolean throwOnFailure) throws CometApiException
maxAuthRetries attempts. If failed empty Optional will be returned or CometApiException
will be thrown if maxAuthRetries attempts exceeded.json - the JSON string to be posted.endpoint - the relative path to the endpointthrowOnFailure - the flag to indicate if exception should be thrown on failure of request execution.Optional of response body.CometApiException - if throwOnFailure set to true and request was failed.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(@NonNull
@NonNull String json,
@NonNull
@NonNull String endpoint)
json - the JSON encoded text.endpoint - the relative path to the endpoint.ListenableFuture which can be used to monitor status of the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(@NonNull
@NonNull File file,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<QueryParamName,String> queryParams,
Map<FormParamName,Object> formParams)
file - the file to be included.endpoint - the relative path to the endpoint.queryParams - the request query parametersformParams - the form parametersListenableFuture which can be used to monitor status of the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(byte[] bytes,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<QueryParamName,String> params,
Map<FormParamName,Object> formParams)
bytes - the data arrayendpoint - the relative path to the endpoint.params - the request parameters map.formParams - the form parametersListenableFuture which can be used to monitor status of the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<QueryParamName,String> params,
@NonNull
@NonNull Map<FormParamName,Object> formParams)
endpoint - the relative path to the endpoint.params - the request parameters map.formParams - the form parametersListenableFuture which can be used to monitor status of the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> downloadAsync(@NonNull
@NonNull File file,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<QueryParamName,String> params)
file - the File instance to collect received data.endpoint - the request path of the endpoint.params - the map with request parameters.ListenableFuture which can be used to monitor status of the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> downloadAsync(@NonNull
@NonNull OutputStream outputStream,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<QueryParamName,String> params)
OutputStream asynchronously.
The provided OutputStream will be closed when response from server completed either successfully or
failed.
outputStream - the OutputStream to write received data into.endpoint - the request path of the endpoint.params - the map with request parameters.ListenableFuture which can be used to monitor status of the request execution.public void close()
throws IOException
Please note that some asynchronous post request can still be not processed, which will result in errors. Use this method with great caution and only if you are not expecting any request still be unprocessed. For all other cases it is recommended to use waitAndClose method.
close in interface Closeableclose in interface AutoCloseableIOException - if an I/O error occurs.public void waitAndClose(@NonNull
@NonNull Duration timeout)
throws IOException
timeout - the maximum duration to wait before closing connection.IOException - if an I/O error occurs.Copyright © 2022. All rights reserved.