Package enterprises.iwakura.kirara.core
Class Kirara
java.lang.Object
enterprises.iwakura.kirara.core.Kirara
- All Implemented Interfaces:
Closeable,AutoCloseable
The base class for Kirara library. Extend this class inorder to create a wrapper for a specific API.
You may use
You may use
createRequest(String, String, Class) for creating API requests.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringThe base URL of the API to which requests will be sent.protected List<RequestHeader> Default request headers that will be included in every API request.protected final HttpCoreThe HTTP core used to send requests and receive responses.protected final SerializerThe serializer used to convert objects to and from JSON or other formats. -
Constructor Summary
ConstructorsConstructorDescriptionKirara(HttpCore httpCore, Serializer serializer) Constructs a new Kirara instance with the specified HTTP core and serializer.Kirara(HttpCore httpCore, Serializer serializer, String apiUrl) Constructs a new Kirara instance with the specified HTTP core, serializer, and API URL. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the HTTP core.protected <R extends ApiRequest<T>,T>
RcreateRequest(String method, String endpoint, Class<T> responseClass) Constructs aApiRequestclass with the specified method, endpoint, and response class.<T> voidonException(ApiRequest<T> request, Throwable exception) Invoked when an exception occurs during the request processing or response handling.<T> voidonRequest(ApiRequest<T> request) Invoked just before sending a request.<T> voidonResponse(ApiRequest<T> request, T response) Invoked when a response is received.
-
Field Details
-
httpCore
The HTTP core used to send requests and receive responses. -
serializer
The serializer used to convert objects to and from JSON or other formats. -
apiUrl
The base URL of the API to which requests will be sent. All API request's endpoints will be appended to this URL. -
defaultRequestHeaders
Default request headers that will be included in every API request. This can be used to set common headers like "User-Agent", "Accept", etc
-
-
Constructor Details
-
Kirara
Constructs a new Kirara instance with the specified HTTP core and serializer.- Parameters:
httpCore- the HTTP core used to send requestsserializer- the serializer used for request and response bodies
-
Kirara
Constructs a new Kirara instance with the specified HTTP core, serializer, and API URL.- Parameters:
httpCore- the HTTP core used to send requestsserializer- the serializer used for request and response bodiesapiUrl- the base URL of the API to which requests will be sent
-
-
Method Details
-
createRequest
protected <R extends ApiRequest<T>,T> R createRequest(String method, String endpoint, Class<T> responseClass) Constructs aApiRequestclass with the specified method, endpoint, and response class. This method is used to create a request that can be sent to the API.- Type Parameters:
R- the type of the request, extendingApiRequestT- the type of the response expected from the API- Parameters:
method- the HTTP method to use (e.g., "GET", "POST", etc.)endpoint- the API endpoint to which the request will be sentresponseClass- the class of the expected response type- Returns:
- a new instance of
ApiRequestwith the specified parameters
-
close
Closes the HTTP core.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs while closing the connection
-
onRequest
Invoked just before sending a request.- Type Parameters:
T- the type of the response expected from the API- Parameters:
request- the API request being sent
-
onResponse
Invoked when a response is received.- Type Parameters:
T- the type of the response expected from the API- Parameters:
request- the API request that was sentresponse- the response received from the API
-
onException
Invoked when an exception occurs during the request processing or response handling.- Type Parameters:
T- the type of the response expected from the API- Parameters:
request- the API request that was being processedexception- the exception that occurred
-