Class HttpAsyncClientImpl

  • All Implemented Interfaces:
    AsyncHttpClient

    public class HttpAsyncClientImpl
    extends java.lang.Object
    implements AsyncHttpClient
    AsyncHttpClient implemented with Apache's HttpAsyncClient.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.apache.http.nio.client.HttpAsyncClient httpClient
      HTTP client instance
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpAsyncClientImpl​(org.apache.http.nio.client.HttpAsyncClient httpClientIn)
      Init a connection to a given Salt API endpoint.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private SaltException createSaltException​(org.apache.http.HttpResponse response)
      Create the appropriate exception for the given HTTP response.
      private <T> java.util.concurrent.CompletionStage<T> executeRequest​(org.apache.http.nio.client.HttpAsyncClient httpClient, org.apache.http.client.methods.HttpUriRequest httpRequest, JsonParser<T> parser)
      Executes a prepared HTTP request using the given client.
      <T> java.util.concurrent.CompletionStage<T> get​(java.net.URI uri, java.util.Map<java.lang.String,​java.lang.String> headers, JsonParser<T> parser)
      Send a GET request and parse the result into object of given type.
      <T> java.util.concurrent.CompletionStage<T> post​(java.net.URI uri, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String data, JsonParser<T> parser)
      Send a POST request and parse the result into object of given type.
      private <T> org.apache.http.client.methods.HttpUriRequest prepareRequest​(java.net.URI uri, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String jsonData)
      Prepares the HTTP request object creating a POST or GET request depending on if data is supplied or not.
      private <T> java.util.concurrent.CompletionStage<T> request​(java.net.URI uri, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String data, JsonParser<T> parser)
      Perform HTTP request and parse the result into a given result type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • httpClient

        private final org.apache.http.nio.client.HttpAsyncClient httpClient
        HTTP client instance
    • Constructor Detail

      • HttpAsyncClientImpl

        public HttpAsyncClientImpl​(org.apache.http.nio.client.HttpAsyncClient httpClientIn)
        Init a connection to a given Salt API endpoint.
        Parameters:
        httpClientIn - the HTTP client
    • Method Detail

      • post

        public <T> java.util.concurrent.CompletionStage<T> post​(java.net.URI uri,
                                                                java.util.Map<java.lang.String,​java.lang.String> headers,
                                                                java.lang.String data,
                                                                JsonParser<T> parser)
        Send a POST request and parse the result into object of given type.
        Specified by:
        post in interface AsyncHttpClient
        Type Parameters:
        T - return type the response json will be parsed into
        Parameters:
        uri - uri to make the http request to
        headers - headers to pass to the request
        data - the data to send (in JSON format)
        parser - parser to use for the response json
        Returns:
        CompletionStage holding object of the given return type T
      • get

        public <T> java.util.concurrent.CompletionStage<T> get​(java.net.URI uri,
                                                               java.util.Map<java.lang.String,​java.lang.String> headers,
                                                               JsonParser<T> parser)
        Send a GET request and parse the result into object of given type.
        Specified by:
        get in interface AsyncHttpClient
        Type Parameters:
        T - return type the response json will be parsed into
        Parameters:
        uri - uri to make the http request to
        headers - headers to pass to the request
        parser - parser to use for the response json
        Returns:
        CompletionStage holding object of the given return type T
      • request

        private <T> java.util.concurrent.CompletionStage<T> request​(java.net.URI uri,
                                                                    java.util.Map<java.lang.String,​java.lang.String> headers,
                                                                    java.lang.String data,
                                                                    JsonParser<T> parser)
        Perform HTTP request and parse the result into a given result type.
        Parameters:
        data - the data to send with the request
        Returns:
        CompletionStage holding object of type T
      • prepareRequest

        private <T> org.apache.http.client.methods.HttpUriRequest prepareRequest​(java.net.URI uri,
                                                                                 java.util.Map<java.lang.String,​java.lang.String> headers,
                                                                                 java.lang.String jsonData)
        Prepares the HTTP request object creating a POST or GET request depending on if data is supplied or not.
        Parameters:
        jsonData - json POST data, will use GET if null
        Returns:
        HttpUriRequest object the prepared request
      • executeRequest

        private <T> java.util.concurrent.CompletionStage<T> executeRequest​(org.apache.http.nio.client.HttpAsyncClient httpClient,
                                                                           org.apache.http.client.methods.HttpUriRequest httpRequest,
                                                                           JsonParser<T> parser)
        Executes a prepared HTTP request using the given client.
        Parameters:
        httpClient - the client to use for the request
        httpRequest - the prepared request to perform
        Returns:
        CompletionStage holding object of type T
      • createSaltException

        private SaltException createSaltException​(org.apache.http.HttpResponse response)
        Create the appropriate exception for the given HTTP response.
        Parameters:
        response - HTTP response
        Returns:
        SaltException instance