Class HttpApiClient

java.lang.Object
com.morpheusdata.core.util.HttpApiClient

public class HttpApiClient extends Object
Utility methods for calling external APIs in a standardized way.
Since:
0.8.0
  • Field Details

    • throttleRate

      public Long throttleRate
      Sets a throttle rate (in milliseconds) between HTTP Calls. This is used to slow down queries to the remote server.
    • networkProxy

      public NetworkProxy networkProxy
      The network proxy to use for requests.
    • log

      protected static org.slf4j.Logger log
      The 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, and HttpApiClient.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 - The HttpApiClient.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 - The HttpApiClient.RequestOptions object containing various options for the request, such as headers, query parameters, and the request body. The Request body can be one of Map, String, byte[], or InputStream.
      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 - The HttpApiClient.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 the CloseableHttpResponse 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 - The HttpApiClient.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 the CloseableHttpResponse 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 - The HttpApiClient.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 the CloseableHttpResponse 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 - The HttpApiClient.RequestOptions object containing various options for the request, such as headers, query parameters, and the request body. The Request body can be one of Map, String, byte[], or InputStream.
      method - The HTTP method to be used for the request, one of "HEAD", "GET", "POST", "PUT", "PATCH", or "DELETE".
      Returns:
      A ServiceResponse containing the CloseableHttpResponse 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

      public ServiceResponse callJsonApi(String url, String path) throws URISyntaxException, Exception
      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

      Make a POST request to a JSON API with the specified URL, path, and HttpApiClient.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
      path - The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.
      opts - The HttpApiClient.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 params
      path - The specific path or endpoint on the server to which the request is made. This path is appended to the base URL.
      opts - The HttpApiClient.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, and HttpApiClient.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
      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 - The HttpApiClient.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 params
      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 - The HttpApiClient.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.
      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