Package com.morpheusdata.core.util
Class HttpApiClient
java.lang.Object
com.morpheusdata.core.util.HttpApiClient
Utility methods for calling external APIs in a standardized way.
- Since:
- 0.8.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Options for making an HTTP request.static interface
-
Field Summary
Modifier and TypeFieldDescriptionprotected static org.slf4j.Logger
The logger for this class.The network proxy to use for requests.Sets a throttle rate (in milliseconds) between HTTP Calls. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddRequiredHeader
(Map<CharSequence, CharSequence> headers, String name, String value) Add a required header to the headers map if it does not already exist.Make a POST request to an API with the specified URL, path, and credentials.callApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) Make a POST request to an API with the specified URL, path, credentials, andHttpApiClient.RequestOptions
.callApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) Make a request to an API with the specified URL, path, credentials,HttpApiClient.RequestOptions
, and HTTP request method.callJsonApi
(String url, String path) Make a POST request to a JSON API with the specified URL and path.callJsonApi
(String url, String path, HttpApiClient.RequestOptions opts) Make a POST request to a JSON API with the specified URL, path, andHttpApiClient.RequestOptions
.callJsonApi
(String url, String path, HttpApiClient.RequestOptions opts, String method) Make a request to a JSON API with the specified URL, path,HttpApiClient.RequestOptions
and HTTP method.callJsonApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) Make a POST request to a JSON API with the specified URL, path, username, password, andHttpApiClient.RequestOptions
.callJsonApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) Make a request to a JSON API with the specified URL, path, username, password,HttpApiClient.RequestOptions
, and HTTP method.ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse>
callStreamApi
(String url, String path, String username, String password, byte[] body, HttpApiClient.RequestOptions opts, String method) Executes an HTTP request with streaming capabilities to a specified URL and path.ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse>
callStreamApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) Executes an HTTP request with streaming capabilities to a specified URL and path.ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse>
callStreamApi
(String url, String path, String username, String password, InputStream body, HttpApiClient.RequestOptions opts, String method) Executes an HTTP request with streaming capabilities to a specified URL and path.ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse>
callStreamApi
(String url, String path, String username, String password, String body, HttpApiClient.RequestOptions opts, String method) Executes an HTTP request with streaming capabilities to a specified URL and path.callXmlApi
(String url, String path, HttpApiClient.RequestOptions opts) Deprecated.callXmlApi
(String url, String path, HttpApiClient.RequestOptions opts, String method) Deprecated.callXmlApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) Deprecated.callXmlApi
(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) Deprecated.void
Shutdown the client connection manager
-
Field Details
-
throttleRate
Sets a throttle rate (in milliseconds) between HTTP Calls. This is used to slow down queries to the remote server. -
networkProxy
The network proxy to use for requests. -
log
protected static org.slf4j.Logger logThe logger for this class.
-
-
Constructor Details
-
HttpApiClient
public HttpApiClient()
-
-
Method Details
-
callApi
public ServiceResponse callApi(String url, String path, String username, String password) throws URISyntaxException, Exception Make a POST request to an API with the specified URL, path, and credentials. This method is intended for simple POST requests.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://").path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callApi
public ServiceResponse callApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) throws URISyntaxException, Exception Make a POST request to an API with the specified URL, path, credentials, andHttpApiClient.RequestOptions
.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body.- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callApi
public ServiceResponse callApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Make a request to an API with the specified URL, path, credentials,HttpApiClient.RequestOptions
, and HTTP request method.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body. The Request body can be one ofMap
,String
, byte[], orInputStream
.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callStreamApi
public ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse> callStreamApi(String url, String path, String username, String password, String body, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Executes an HTTP request with streaming capabilities to a specified URL and path. This method is intended for scenarios where large amounts of data need to be transferred, such as uploading or downloading files, without fully loading the data into memory. The method handles authentication and custom request options, and returns the raw HTTP response for further processing.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.body
- The request body to be sent to the server.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing theCloseableHttpResponse
from the server. This response can be used to read the server's response, including status codes, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callStreamApi
public ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse> callStreamApi(String url, String path, String username, String password, InputStream body, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Executes an HTTP request with streaming capabilities to a specified URL and path. This method is intended for scenarios where large amounts of data need to be transferred, such as uploading or downloading files, without fully loading the data into memory. The method handles authentication and custom request options, and returns the raw HTTP response for further processing.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.body
- The request body to be sent to the server.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing theCloseableHttpResponse
from the server. This response can be used to read the server's response, including status codes, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callStreamApi
public ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse> callStreamApi(String url, String path, String username, String password, byte[] body, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Executes an HTTP request with streaming capabilities to a specified URL and path. This method is intended for scenarios where large amounts of data need to be transferred, such as uploading or downloading files, without fully loading the data into memory. The method handles authentication and custom request options, and returns the raw HTTP response for further processing.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.body
- The request body to be sent to the server.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing theCloseableHttpResponse
from the server. This response can be used to read the server's response, including status codes, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callStreamApi
public ServiceResponse<org.apache.http.client.methods.CloseableHttpResponse> callStreamApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Executes an HTTP request with streaming capabilities to a specified URL and path. This method is intended for scenarios where large amounts of data need to be transferred, such as uploading or downloading files, without fully loading the data into memory. The method handles authentication and custom request options, and returns the raw HTTP response for further processing.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query params included in the URL will be maintained when appending the path and query params supplied in other arguments in this method.path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body. The Request body can be one ofMap
,String
, byte[], orInputStream
.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing theCloseableHttpResponse
from the server. This response can be used to read the server's response, including status codes, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callJsonApi
Make a POST request to a JSON API with the specified URL and path.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://").path
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callJsonApi
public ServiceResponse callJsonApi(String url, String path, HttpApiClient.RequestOptions opts) throws URISyntaxException, Exception Make a POST request to a JSON API with the specified URL, path, andHttpApiClient.RequestOptions
.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query paramspath
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body.- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callJsonApi
public ServiceResponse callJsonApi(String url, String path, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Make a request to a JSON API with the specified URL, path,HttpApiClient.RequestOptions
and HTTP method.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query paramspath
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callJsonApi
public ServiceResponse callJsonApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) throws URISyntaxException, Exception Make a POST request to a JSON API with the specified URL, path, username, password, andHttpApiClient.RequestOptions
.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query paramspath
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body.- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callJsonApi
public ServiceResponse callJsonApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Make a request to a JSON API with the specified URL, path, username, password,HttpApiClient.RequestOptions
, and HTTP method.- Parameters:
url
- The base URL of the server to which the request is sent. This should include the protocol (e.g., "https://"). Any path or query paramspath
- The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.username
- The username used for basic authentication with the server. This can be null if authentication is not required.password
- The password used for basic authentication with the server. This should match the provided username. Can be null if authentication is not required.opts
- TheHttpApiClient.RequestOptions
object containing various options for the request, such as headers, query parameters, and the request body.method
- The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".- Returns:
- A
ServiceResponse
containing the response from the server. This response includes the status code, headers, and content. - Throws:
URISyntaxException
- If the provided URL or path is invalid or cannot be correctly parsed into a URI.Exception
- If an error occurs during the execution of the request, such as an I/O error, authentication failure, or invalid response.
-
callXmlApi
@Deprecated(since="1.1.5") public ServiceResponse callXmlApi(String url, String path, HttpApiClient.RequestOptions opts) throws URISyntaxException, Exception Deprecated.- Throws:
URISyntaxException
Exception
-
callXmlApi
@Deprecated(since="1.1.5") public ServiceResponse callXmlApi(String url, String path, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Deprecated.- Throws:
URISyntaxException
Exception
-
callXmlApi
@Deprecated(since="1.1.5") public ServiceResponse callXmlApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts) throws URISyntaxException, Exception Deprecated.- Throws:
URISyntaxException
Exception
-
callXmlApi
@Deprecated(since="1.1.5") public ServiceResponse callXmlApi(String url, String path, String username, String password, HttpApiClient.RequestOptions opts, String method) throws URISyntaxException, Exception Deprecated.- Throws:
URISyntaxException
Exception
-
addRequiredHeader
public Map<CharSequence,CharSequence> addRequiredHeader(Map<CharSequence, CharSequence> headers, String name, String value) Add a required header to the headers map if it does not already exist.- Parameters:
headers
-name
-value
-- Returns:
-
shutdownClient
public void shutdownClient()Shutdown the client connection manager
-