Class HttpUrlConnectionHttpCore

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

public class HttpUrlConnectionHttpCore extends HttpCore
Implementation of HttpCore using Java's built-in HttpURLConnection.
  • Constructor Details

    • HttpUrlConnectionHttpCore

      public HttpUrlConnectionHttpCore()
      Constructs a new HttpUrlConnectionHttpCore instance.
  • Method Details

    • getNumberOfBytesToReadAtOnce

      protected int getNumberOfBytesToReadAtOnce()
      Returns the number of bytes to read at once from the input stream.
      Returns:
      The number of bytes to read at once.
    • send

      public <T> CompletableFuture<T> send(ApiRequest<T> request)
      Description copied from class: HttpCore
      Sends an API request and returns a CompletableFuture that will be completed with the response.
      Specified by:
      send in class HttpCore
      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 void close()
      Description copied from class: HttpCore
      Closes the HTTP core, releasing any resources it holds.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class HttpCore
    • createConnection

      protected HttpURLConnection createConnection(String url, String method, List<RequestHeader> headers) throws IOException
      Creates a new HttpURLConnection for the given URL and method, applying the specified headers.
      Parameters:
      url - the URL to connect to
      method - the HTTP method to use (e.g., "GET", "POST", etc.)
      headers - the list of request headers to apply to the connection
      Returns:
      a new HttpURLConnection instance configured with the specified URL, method, and headers
      Throws:
      IOException - if an I/O error occurs while opening the connection
    • writeBody

      protected void writeBody(Kirara kirara, HttpURLConnection connection, Object body) throws IOException
      Writes the body of the request to the connection's output stream.
      Parameters:
      kirara - the Kirara instance used for serialization
      connection - the HttpURLConnection to write the body to
      body - the body of the request, which can be of various types (e.g., byte[], String, or any object)
      Throws:
      IOException - if an I/O error occurs while writing to the output stream
    • readResponse

      protected <T> T readResponse(Kirara kirara, HttpURLConnection connection, Class<T> responseClass)
      Reads the response from the connection and converts it to the specified response class.
      Type Parameters:
      T - the type of the response expected from the API
      Parameters:
      kirara - the Kirara instance used for deserialization
      connection - the HttpURLConnection to read the response from
      responseClass - the class of the expected response type
      Returns:
      an instance of the specified response class containing the response data