Package dev.mayuna.simpleapi
Interface ApiRequest<T>
-
- Type Parameters:
T- The type of the response.
public interface ApiRequest<T>Represents an API request. Can send API requests.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidapplyHeadersToHttpRequestBuilder(java.net.http.HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders)Applies theRequestHeaders to the givenHttpRequest.Builder.static <T> ApiRequestBuilder<T>builder(WrappedApi wrappedApi, java.lang.Class<T> responseClass)default java.net.http.HttpClientcreateHttpClientInstance()Creates a newHttpClientinstance.default java.net.http.HttpRequest.BuildercreateHttpRequestBuilderInstance()Creates a newHttpRequest.Builderinstance.default TcreateInstanceOfResponseClass()Creates a new instance of the response class.default java.net.http.HttpResponse.BodyHandler<?>getBodyHandler()Gets theHttpResponse.BodyHandlerof thisApiRequest.default java.net.http.HttpRequest.BodyPublishergetBodyPublisher()Gets theHttpRequest.BodyPublisherof thisApiRequest.default java.lang.StringgetComputedEndpoint()Gets the computed endpoint of thisApiRequest.@NotNull java.lang.StringgetEndpoint()Gets the endpoint of thisApiRequest.default @Nullable PathParameter[]getPathParameters()Gets thePathParameters of thisApiRequest.default @Nullable RequestHeader[]getRequestHeaders()Gets theRequestHeaders of thisApiRequest.@NotNull RequestMethodgetRequestMethod()Gets theRequestMethodof thisApiRequest.default @Nullable RequestQuery[]getRequestQueries()Gets theRequestQuerys of thisApiRequest.@NotNull java.lang.Class<T>getResponseClass()Gets theClassof the response.default java.util.concurrent.ThreadFactorygetThreadFactory()Gets theThreadFactoryto use for sending the async request.@Nullable java.lang.StringgetUrl()Gets the URL of thisApiRequest.@NotNull WrappedApigetWrappedApi()Gets theWrappedApiof thisApiRequest.default ThandleResponse(java.net.http.HttpResponse<?> httpResponse)Handles the givenHttpResponse.default Tsend()Sends the request synchronously.default java.util.concurrent.CompletableFuture<T>sendAsync()Sends the request asynchronously.
-
-
-
Method Detail
-
builder
static <T> ApiRequestBuilder<T> builder(WrappedApi wrappedApi, java.lang.Class<T> responseClass)
- Type Parameters:
T- The type of the response.- Parameters:
wrappedApi- TheWrappedApito use.responseClass- TheClassof the response.- Returns:
- The created
ApiRequestBuilder.
-
getWrappedApi
@NotNull @NotNull WrappedApi getWrappedApi()
Gets theWrappedApiof thisApiRequest.- Returns:
- The
WrappedApiof thisApiRequest.
-
getResponseClass
@NotNull @NotNull java.lang.Class<T> getResponseClass()
Gets theClassof the response.- Returns:
- The
Classof the response.
-
getUrl
@Nullable @Nullable java.lang.String getUrl()
Gets the URL of thisApiRequest.- Returns:
- The URL of this
ApiRequest. If null, the default URL of theWrappedApiwill be used.
-
getEndpoint
@NotNull @NotNull java.lang.String getEndpoint()
Gets the endpoint of thisApiRequest.- Returns:
- The endpoint of this
ApiRequest.
-
getRequestMethod
@NotNull @NotNull RequestMethod getRequestMethod()
Gets theRequestMethodof thisApiRequest.- Returns:
- The
RequestMethodof thisApiRequest.
-
getPathParameters
@Nullable default @Nullable PathParameter[] getPathParameters()
Gets thePathParameters of thisApiRequest.- Returns:
- The
PathParameters of thisApiRequest.
-
getRequestQueries
@Nullable default @Nullable RequestQuery[] getRequestQueries()
Gets theRequestQuerys of thisApiRequest.- Returns:
- The
RequestQuerys of thisApiRequest.
-
getRequestHeaders
@Nullable default @Nullable RequestHeader[] getRequestHeaders()
Gets theRequestHeaders of thisApiRequest.- Returns:
- The
RequestHeaders of thisApiRequest.
-
getBodyPublisher
@NonNull default java.net.http.HttpRequest.BodyPublisher getBodyPublisher()
Gets theHttpRequest.BodyPublisherof thisApiRequest.- Returns:
- The
HttpRequest.BodyPublisherof thisApiRequest.
-
getBodyHandler
@NotNull default java.net.http.HttpResponse.BodyHandler<?> getBodyHandler()
Gets theHttpResponse.BodyHandlerof thisApiRequest.- Returns:
- The
HttpResponse.BodyHandlerof thisApiRequest.
-
getComputedEndpoint
default java.lang.String getComputedEndpoint()
Gets the computed endpoint of thisApiRequest.- Returns:
- The computed endpoint of this
ApiRequestwith allPathParameters replaces andRequestQuerys added.
-
applyHeadersToHttpRequestBuilder
default void applyHeadersToHttpRequestBuilder(java.net.http.HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders)Applies theRequestHeaders to the givenHttpRequest.Builder.- Parameters:
httpRequestBuilder- TheHttpRequest.Builderto apply theRequestHeaders to.requestHeaders- TheRequestHeaders to apply.
-
createHttpClientInstance
default java.net.http.HttpClient createHttpClientInstance()
Creates a newHttpClientinstance.- Returns:
- The created
HttpClientinstance.
-
createHttpRequestBuilderInstance
default java.net.http.HttpRequest.Builder createHttpRequestBuilderInstance()
Creates a newHttpRequest.Builderinstance.- Returns:
- The created
HttpRequest.Builderinstance.
-
createInstanceOfResponseClass
default T createInstanceOfResponseClass() throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessException
Creates a new instance of the response class.- Returns:
- The created instance of the response class.
- Throws:
java.lang.NoSuchMethodException- Is thrown if the response class does not have a default constructor.java.lang.reflect.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.java.lang.InstantiationException- Is thrown if the response class is abstract or interface.java.lang.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-
handleResponse
default T handleResponse(java.net.http.HttpResponse<?> httpResponse) throws java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException
Handles the givenHttpResponse.- Parameters:
httpResponse- TheHttpResponseto handle.- Returns:
- The instance of the response class.
- Throws:
java.lang.NoSuchMethodException- Is thrown if the response class does not have a default constructor.java.lang.reflect.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.java.lang.InstantiationException- Is thrown if the response class is abstract or interface.java.lang.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-
getThreadFactory
default java.util.concurrent.ThreadFactory getThreadFactory()
Gets theThreadFactoryto use for sending the async request.- Returns:
- The
ThreadFactoryto use for sending the async request.
-
sendAsync
default java.util.concurrent.CompletableFuture<T> sendAsync()
Sends the request asynchronously.- Returns:
- A
CompletableFuturethat will be completed with the instance of response class.
-
send
default T send() throws java.io.IOException, java.lang.InterruptedException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException
Sends the request synchronously.- Returns:
- The instance of the response class.
- Throws:
java.io.IOException- If an I/O error occurs.java.lang.InterruptedException- If the operation is interrupted.java.lang.NoSuchMethodException- Is thrown if the response class does not have a default constructor.java.lang.reflect.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.java.lang.InstantiationException- Is thrown if the response class is abstract or interface.java.lang.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-
-