Class HttpCore

java.lang.Object
enterprises.iwakura.kirara.core.HttpCore
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
HttpUrlConnectionHttpCore

public abstract class HttpCore extends Object implements Closeable
Abstract class representing the core of Kirara's HTTP backend. Used to send the actual HTTP requests. This class is designed to be extended by specific HTTP implementations (e.g., OkHttp, etc.)
The Kirara Core implements Java 8's HttpURLConnection in HttpUrlConnectionHttpCore
  • Field Details

    • executor

      protected Executor executor
      The executor to schedule API requests on.
  • Constructor Details

    • HttpCore

      public HttpCore()
      Default constructor for HttpCore. This can be overridden by subclasses if needed.
  • Method Details

    • send

      public abstract <T> CompletableFuture<T> send(ApiRequest<T> request)
      Sends an API request and returns a CompletableFuture that will be completed with the response.
      Type Parameters:
      T - The type of the response expected from the API.
      Parameters:
      request - The API request to send.
      Returns:
      A CompletableFuture that will be completed with the response.
    • close

      public abstract void close()
      Closes the HTTP core, releasing any resources it holds.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getExecutor

      public Executor getExecutor()
      Returns an executor that can be used to schedule API requests.
      Returns:
      An Executor that can be used to schedule API requests.
    • convertBodyToBytes

      protected byte[] convertBodyToBytes(Kirara kirara, Object body)
      Converts the body of an API request to a byte array. Default implementation handles byte arrays and Strings, and uses the serializer for other object types.
      Parameters:
      kirara - The Kirara instance used for serialization.
      body - The body of the API request, which can be of various types (e.g., byte[], String, or any object).
      Returns:
      A byte array representation of the body.
    • convertBytesToResponse

      protected <T> T convertBytesToResponse(Kirara kirara, byte[] response, Class<T> specifiedResponseClass, Map<String,List<String>> responseHeaders)
      Converts a byte array response to an object of the specified class using the Kirara serializer.
      Type Parameters:
      T - The type of the object to deserialize into.
      Parameters:
      kirara - The Kirara instance used for deserialization.
      response - The byte array response to convert.
      specifiedResponseClass - The class to deserialize into.
      responseHeaders - The headers associated with the response, used for content type detection.
      Returns:
      The deserialized object of the specified class.
    • handleKiraraSupportedResponse

      protected <T> T handleKiraraSupportedResponse(Kirara kirara, T response)
      Handles the response for Kirara-supported responses by setting the Kirara instance on the response.
      Type Parameters:
      T - The type of the response.
      Parameters:
      kirara - The Kirara instance associated with the request.
      response - The response to handle.
      Returns:
      The response with the Kirara instance set, if applicable.