Class UnirestClient

java.lang.Object
com.moesif.api.http.client.UnirestClient
All Implemented Interfaces:
HttpClient

public class UnirestClient extends Object implements HttpClient
  • Constructor Details

    • UnirestClient

      public UnirestClient()
  • Method Details

    • getSharedInstance

      public static HttpClient getSharedInstance()
      Singleton access to the shared instance
      Returns:
      A shared instance of UnirestClient
    • setUserAgent

      public static void setUserAgent(String userAgent)
      Set a custom User-Agent string for HTTP requests
      Parameters:
      userAgent - The custom User-Agent string to use
    • executeAsStringAsync

      public void executeAsStringAsync(HttpRequest request, APICallBack<HttpResponse> callBack)
      Execute a given HttpRequest to get string response back
      Specified by:
      executeAsStringAsync in interface HttpClient
      Parameters:
      request - The given HttpRequest to execute
      callBack - Async callback for events
    • executeAsBinaryAsync

      public void executeAsBinaryAsync(HttpRequest request, APICallBack<HttpResponse> callBack)
      Execute a given HttpRequest to get binary response back
      Specified by:
      executeAsBinaryAsync in interface HttpClient
      Parameters:
      request - The given HttpRequest to execute
      callBack - Async callback for events
    • executeAsBinaryAsync

      public void executeAsBinaryAsync(HttpRequest request, APICallBack<HttpResponse> callBack, boolean debug) throws com.fasterxml.jackson.core.JsonProcessingException
      Specified by:
      executeAsBinaryAsync in interface HttpClient
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • executeAsBinary

      public HttpResponse executeAsBinary(HttpRequest request) throws APIException
      Execute a given HttpRequest to get binary response back
      Specified by:
      executeAsBinary in interface HttpClient
      Parameters:
      request - The given HttpRequest to execute
      Returns:
      the HTTP response
      Throws:
      APIException - on error executing request
    • executeAsString

      public HttpResponse executeAsString(HttpRequest request) throws APIException
      Execute a given HttpRequest to get string response back
      Specified by:
      executeAsString in interface HttpClient
      Parameters:
      request - The given HttpRequest to execute
      Returns:
      the HTTP response
      Throws:
      APIException - on error executing request
    • publishResponse

      protected static void publishResponse(com.mashape.unirest.http.HttpResponse<?> response, HttpRequest request, APICallBack<HttpResponse> completionBlock, com.mashape.unirest.http.exceptions.UnirestException uniException)
      Publishes success or failure result as HttpResponse from a HttpRequest
      Parameters:
      response - The http response to publish
      request - The http request
      completionBlock - The success and failure code block reference to invoke the delegate
      uniException - The reported errors for getting the http response
    • publishResponse

      protected static void publishResponse(com.mashape.unirest.http.HttpResponse<?> response, HttpRequest request, APICallBack<HttpResponse> completionBlock, com.mashape.unirest.http.exceptions.UnirestException uniException, boolean debug) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • convertResponse

      public static HttpResponse convertResponse(com.mashape.unirest.http.HttpResponse<?> response)
      Converts a given Unirest http response into our internal http response model
      Parameters:
      response - The given unirest http response
      Returns:
      The converted http response
    • convertResponse

      public static HttpResponse convertResponse(com.mashape.unirest.http.HttpResponse<?> response, boolean debug) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • convertRequest

      protected static com.mashape.unirest.request.HttpRequest convertRequest(HttpRequest request)
      Converts a given internal http request into unirest http request model
      Parameters:
      request - The given http request in internal format
      Returns:
      The converted unirest http request
    • convertHttpMetod

      protected static com.mashape.unirest.http.HttpMethod convertHttpMetod(HttpMethod method)
      Converts a given internal http method enumeration into unirest http method unirest
      Parameters:
      method - The given http method enum in internal format
      Returns:
      The converted unirest http method enum
    • get

      public HttpRequest get(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters, String _username, String _password)
      Create a simple HTTP GET request with basic authentication
      Specified by:
      get in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the query params
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • get

      public HttpRequest get(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters)
      Create a simple HTTP GET request
      Specified by:
      get in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the query params
      Returns:
      the request object
    • post

      public HttpRequest post(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters)
      Create an HTTP POST request with parameters
      Specified by:
      post in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the query params
      Returns:
      the request object
    • post

      public HttpRequest post(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters, String _username, String _password)
      Create an HTTP POST request with parameters and with basic authentication
      Specified by:
      post in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the query params
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • postBody

      public HttpBodyRequest postBody(String _queryUrl, Map<String,String> _headers, String _body)
      Create an HTTP POST request with body
      Specified by:
      postBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      Returns:
      the request object
    • postBody

      public HttpBodyRequest postBody(String _queryUrl, Map<String,String> _headers, String _body, String _username, String _password)
      Create an HTTP POST request with body and with basic authentication
      Specified by:
      postBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • put

      public HttpRequest put(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters)
      Create an HTTP PUT request with parameters
      Specified by:
      put in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      Returns:
      the request object
    • put

      public HttpRequest put(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters, String _username, String _password)
      Create an HTTP PUT request with parameters and with basic authentication
      Specified by:
      put in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • putBody

      public HttpBodyRequest putBody(String _queryUrl, Map<String,String> _headers, String _body)
      Create an HTTP PUT request with body
      Specified by:
      putBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      Returns:
      the request object
    • putBody

      public HttpBodyRequest putBody(String _queryUrl, Map<String,String> _headers, String _body, String _username, String _password)
      Create an HTTP PUT request with body and with basic authentication
      Specified by:
      putBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • patch

      public HttpRequest patch(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters)
      Create an HTTP PATCH request with parameters
      Specified by:
      patch in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      Returns:
      the request object
    • patch

      public HttpRequest patch(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters, String _username, String _password)
      Create an HTTP PATCH request with parameters and with basic authentication
      Specified by:
      patch in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • patchBody

      public HttpBodyRequest patchBody(String _queryUrl, Map<String,String> _headers, String _body)
      Create an HTTP PATCH request with body
      Specified by:
      patchBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      Returns:
      the request object
    • patchBody

      public HttpBodyRequest patchBody(String _queryUrl, Map<String,String> _headers, String _body, String _username, String _password)
      Create an HTTP PATCH request with body and with basic authentication
      Specified by:
      patchBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • delete

      public HttpRequest delete(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters)
      Create an HTTP DELETE request with parameters
      Specified by:
      delete in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      Returns:
      the request object
    • delete

      public HttpRequest delete(String _queryUrl, Map<String,String> _headers, Map<String,Object> _parameters, String _username, String _password)
      Create an HTTP DELETE request with parameters and with basic authentication
      Specified by:
      delete in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _parameters - the request parameters
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object
    • deleteBody

      public HttpBodyRequest deleteBody(String _queryUrl, Map<String,String> _headers, String _body)
      Create an HTTP DELETE request with body
      Specified by:
      deleteBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      Returns:
      the request object
    • deleteBody

      public HttpBodyRequest deleteBody(String _queryUrl, Map<String,String> _headers, String _body, String _username, String _password)
      Create an HTTP DELETE request with body and with basic authentication
      Specified by:
      deleteBody in interface HttpClient
      Parameters:
      _queryUrl - the URL of the HTTP request
      _headers - the HTTP request headers as map
      _body - the request body
      _username - for basic auth header
      _password - for basic auth header
      Returns:
      the request object