Class OpaRestClient


  • public class OpaRestClient
    extends java.lang.Object
    Class wrapping Java's HttpClient in OPA and JSON requests
    • Constructor Summary

      Constructors 
      Constructor Description
      OpaRestClient​(OpaConfiguration opaConfiguration, java.net.http.HttpClient httpClient, com.fasterxml.jackson.databind.ObjectMapper objectMapper)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.net.http.HttpRequest.Builder getBasicRequestBuilder​(java.lang.String endpoint)
      Create HttpRequest.Builder with configured url using provided endpoint
      <T> com.bisnode.opa.client.rest.JsonBodyHandler<T> getJsonBodyHandler​(com.fasterxml.jackson.databind.JavaType responseType)
      Gets JsonBodyHandler that will deserialize JSON to desired class type
      java.net.http.HttpRequest.BodyPublisher getJsonBodyPublisher​(java.lang.Object body)
      Gets HttpRequest.BodyPublisher that is capable of serializing to JSON
      <T> java.net.http.HttpResponse<T> sendRequest​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
      Sends provided request and returns response mapped using HttpResponse.BodyHandler
      • Methods inherited from class java.lang.Object

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

      • OpaRestClient

        public OpaRestClient​(OpaConfiguration opaConfiguration,
                             java.net.http.HttpClient httpClient,
                             com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    • Method Detail

      • getBasicRequestBuilder

        public java.net.http.HttpRequest.Builder getBasicRequestBuilder​(java.lang.String endpoint)
        Create HttpRequest.Builder with configured url using provided endpoint
        Parameters:
        endpoint - desired opa endpoint
        Throws:
        OpaClientException - if URL or endpoint is invalid
      • getJsonBodyPublisher

        public java.net.http.HttpRequest.BodyPublisher getJsonBodyPublisher​(java.lang.Object body)
                                                                     throws com.fasterxml.jackson.core.JsonProcessingException
        Gets HttpRequest.BodyPublisher that is capable of serializing to JSON
        Parameters:
        body - object to be serialized
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • getJsonBodyHandler

        public <T> com.bisnode.opa.client.rest.JsonBodyHandler<T> getJsonBodyHandler​(com.fasterxml.jackson.databind.JavaType responseType)
        Gets JsonBodyHandler that will deserialize JSON to desired class type
        Type Parameters:
        T - desired response type
        Parameters:
        responseType - desired response type
      • sendRequest

        public <T> java.net.http.HttpResponse<T> sendRequest​(java.net.http.HttpRequest request,
                                                             java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
                                                      throws java.io.IOException,
                                                             java.lang.InterruptedException
        Sends provided request and returns response mapped using HttpResponse.BodyHandler
        Type Parameters:
        T - Type of returned body
        Parameters:
        request - request to be sent
        bodyHandler - handler that indicates how to transform incoming body
        Returns:
        response from HttpRequest
        Throws:
        java.io.IOException - is propagated from HttpClient
        java.lang.InterruptedException - is propagated from HttpClient