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
Modifier and TypeMethodDescriptiondefault voidapplyHeadersToHttpRequestBuilder(HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders) Applies theRequestHeaders to the givenHttpRequest.Builder.static <T> ApiRequestBuilder<T>builder(WrappedApi wrappedApi, Class<T> responseClass) default HttpClientCreates a newHttpClientinstance.default HttpRequest.BuilderCreates a newHttpRequest.Builderinstance.default TCreates a new instance of the response class.default HttpResponse.BodyHandler<?>Gets theHttpResponse.BodyHandlerof thisApiRequest.default HttpRequest.BodyPublisherGets theHttpRequest.BodyPublisherof thisApiRequest.default StringGets the computed endpoint of thisApiRequest.@NotNull StringGets the endpoint of thisApiRequest.default @Nullable PathParameter[]Gets thePathParameters of thisApiRequest.default @Nullable RequestHeader[]Gets theRequestHeaders of thisApiRequest.@NotNull RequestMethodGets theRequestMethodof thisApiRequest.default @Nullable RequestQuery[]Gets theRequestQuerys of thisApiRequest.Gets theClassof the response.@Nullable StringgetUrl()Gets the URL of thisApiRequest.@NotNull WrappedApiGets theWrappedApiof thisApiRequest.default ThandleResponse(HttpResponse<?> httpResponse) Handles the givenHttpResponse.default Tsend()Sends the request synchronously.default CompletableFuture<T>Sends the request asynchronously usingWrappedApi.runAsync(Runnable).
-
Method Details
-
builder
- Type Parameters:
T- The type of the response.- Parameters:
wrappedApi- TheWrappedApito use.responseClass- TheClassof the response.- Returns:
- The created
ApiRequestBuilder.
-
getWrappedApi
Gets theWrappedApiof thisApiRequest.- Returns:
- The
WrappedApiof thisApiRequest.
-
getResponseClass
Gets theClassof the response.- Returns:
- The
Classof the response.
-
getUrl
Gets the URL of thisApiRequest.- Returns:
- The URL of this
ApiRequest. If null, the default URL of theWrappedApiwill be used.
-
getEndpoint
Gets the endpoint of thisApiRequest.- Returns:
- The endpoint of this
ApiRequest.
-
getRequestMethod
Gets theRequestMethodof thisApiRequest.- Returns:
- The
RequestMethodof thisApiRequest.
-
getPathParameters
Gets thePathParameters of thisApiRequest.- Returns:
- The
PathParameters of thisApiRequest.
-
getRequestQueries
Gets theRequestQuerys of thisApiRequest.- Returns:
- The
RequestQuerys of thisApiRequest.
-
getRequestHeaders
Gets theRequestHeaders of thisApiRequest.- Returns:
- The
RequestHeaders of thisApiRequest.
-
getBodyPublisher
Gets theHttpRequest.BodyPublisherof thisApiRequest.- Returns:
- The
HttpRequest.BodyPublisherof thisApiRequest.
-
getBodyHandler
Gets theHttpResponse.BodyHandlerof thisApiRequest.- Returns:
- The
HttpResponse.BodyHandlerof thisApiRequest.
-
getComputedEndpoint
Gets the computed endpoint of thisApiRequest.- Returns:
- The computed endpoint of this
ApiRequestwith allPathParameters replaces andRequestQuerys added.
-
applyHeadersToHttpRequestBuilder
default void applyHeadersToHttpRequestBuilder(HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders) Applies theRequestHeaders to the givenHttpRequest.Builder.- Parameters:
httpRequestBuilder- TheHttpRequest.Builderto apply theRequestHeaders to.requestHeaders- TheRequestHeaders to apply.
-
createHttpClientInstance
Creates a newHttpClientinstance.- Returns:
- The created
HttpClientinstance.
-
createHttpRequestBuilderInstance
Creates a newHttpRequest.Builderinstance.- Returns:
- The created
HttpRequest.Builderinstance.
-
createInstanceOfResponseClass
default T createInstanceOfResponseClass() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessExceptionCreates a new instance of the response class.- Returns:
- The created instance of the response class.
- Throws:
NoSuchMethodException- Is thrown if the response class does not have a default constructor.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.InstantiationException- Is thrown if the response class is abstract or interface.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-
handleResponse
default T handleResponse(HttpResponse<?> httpResponse) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException Handles the givenHttpResponse.- Parameters:
httpResponse- TheHttpResponseto handle.- Returns:
- The instance of the response class.
- Throws:
NoSuchMethodException- Is thrown if the response class does not have a default constructor.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.InstantiationException- Is thrown if the response class is abstract or interface.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-
sendAsync
Sends the request asynchronously usingWrappedApi.runAsync(Runnable).- Returns:
- A
CompletableFuturethat will be completed with the instance of response class.
-
send
default T send() throws IOException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessExceptionSends the request synchronously.- Returns:
- The instance of the response class.
- Throws:
IOException- If an I/O error occurs.InterruptedException- If the operation is interrupted.NoSuchMethodException- Is thrown if the response class does not have a default constructor.InvocationTargetException- Is thrown if the constructor of the response class throws an exception.InstantiationException- Is thrown if the response class is abstract or interface.IllegalAccessException- Is thrown if the constructor of the response class is not accessible.
-