Class HttpUrlConnectionHttpCore
java.lang.Object
enterprises.iwakura.kirara.core.HttpCore
enterprises.iwakura.kirara.core.impl.HttpUrlConnectionHttpCore
- All Implemented Interfaces:
Closeable,AutoCloseable
Implementation of
HttpCore using Java's built-in HttpURLConnection.-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new HttpUrlConnectionHttpCore instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the HTTP core, releasing any resources it holds.protected HttpURLConnectioncreateConnection(String url, String method, List<RequestHeader> headers) Creates a newHttpURLConnectionfor the given URL and method, applying the specified headers.protected intReturns the number of bytes to read at once from the input stream.protected <T> TreadResponse(Kirara kirara, HttpURLConnection connection, Class<T> responseClass) Reads the response from the connection and converts it to the specified response class.<T> CompletableFuture<T> send(ApiRequest<T> request) Sends an API request and returns a CompletableFuture that will be completed with the response.protected voidwriteBody(Kirara kirara, HttpURLConnection connection, Object body) Writes the body of the request to the connection's output stream.Methods inherited from class enterprises.iwakura.kirara.core.HttpCore
convertBodyToBytes, convertBytesToResponse, getExecutor, handleKiraraSupportedResponse
-
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
Description copied from class:HttpCoreSends an API request and returns a CompletableFuture that will be completed with the response. -
close
public void close()Description copied from class:HttpCoreCloses the HTTP core, releasing any resources it holds. -
createConnection
protected HttpURLConnection createConnection(String url, String method, List<RequestHeader> headers) throws IOException Creates a newHttpURLConnectionfor the given URL and method, applying the specified headers.- Parameters:
url- the URL to connect tomethod- the HTTP method to use (e.g., "GET", "POST", etc.)headers- the list of request headers to apply to the connection- Returns:
- a new
HttpURLConnectioninstance 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 serializationconnection- the HttpURLConnection to write the body tobody- 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
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 deserializationconnection- the HttpURLConnection to read the response fromresponseClass- the class of the expected response type- Returns:
- an instance of the specified response class containing the response data
-