Package dev.mayuna.simpleapi
Interface WrappedApi
-
public interface WrappedApi
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T> voidapplyHeadersToHttpRequestBuilder(ApiRequest<T> apiRequest, java.net.http.HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders)Applies theRequestHeaders to the givenHttpRequest.Builder.
If theApiRequest.applyHeadersToHttpRequestBuilder(HttpRequest.Builder, RequestHeader[])is overridden, this method may not be called, depending on the implementation.default <T> java.lang.StringcomputeEndpoint(ApiRequest<T> apiRequest)Computers the endpoint of thisApiRequest.
If theApiRequest.getComputedEndpoint()is overridden, this method may not be called, depending on the implementation.default java.net.http.HttpClientcreateHttpClientInstance()Creates a newHttpClientinstance that will be used for sending requests.
If theApiRequest.createHttpClientInstance()is overridden, this method may not be called, depending on the implementation.default java.net.http.HttpRequest.BuildercreateHttpRequestBuilderInstance()Creates a newHttpRequest.Builderinstance that will be used for building requests.
If theApiRequest.createHttpRequestBuilderInstance()is overridden, this method may not be called, depending on the implementation.default <T> TcreateInstanceOfResponseClass(java.lang.Class<T> responseClass)Creates a new instance of the response class.
If theApiRequest.createInstanceOfResponseClass()is overridden, this method may not be called, depending on the implementation.default RequestHeader[]getDefaultRequestHeaders()Gets the defaultRequestHeaders for this API.
Useful if you want to set some headers for all requests.java.lang.StringgetDefaultUrl()Gets the default URL for this API.default <T> ThandleResponse(ApiRequest<T> apiRequest, java.net.http.HttpResponse<?> httpResponse)Handles the givenHttpResponse.
If theApiRequest.handleResponse(HttpResponse)is overridden, this method may not be called, depending on the implementation.default <T> voidonAfterApiRequest(ApiRequest<T> request)It is called after the request is sent but before the response is handled.
If the request send fails with exception, this method is not called.default <T> voidonAfterHandledApiRequest(ApiRequest<T> request, T response)It is called after the request's response is handled.
If the request send and/or request's response handler fails with exception, this method is not called.default <T> voidonApiRequest(ApiRequest<T> request)Is called before the request is sent.default <T> voidonException(ApiRequest<T> request, java.lang.Throwable throwable)It is called when any exception occurs when sending the request or handling the response.
Note: This method does not catch any exceptions.
All exceptions are re-thrown after this method is called, if therethrowExceptions()returnstrue.default booleanrethrowExceptions()Determines if exceptions should be re-thrown afteronException(ApiRequest, Throwable)is called.
Also, if this method returns false, all responses that failed to be sent or handled will benull.default voidrunAsync(java.lang.Runnable runnable)This method is used for async requests.default <T> Tsend(ApiRequest<T> apiRequest)Sends the request synchronously.
If theApiRequest.send()is overridden, this method may not be called, depending on the implementation.
-
-
-
Method Detail
-
getDefaultUrl
java.lang.String getDefaultUrl()
Gets the default URL for this API.- Returns:
- The default URL for this API.
-
getDefaultRequestHeaders
default RequestHeader[] getDefaultRequestHeaders()
Gets the defaultRequestHeaders for this API.
Useful if you want to set some headers for all requests.- Returns:
- Nullable array of
RequestHeaders.
-
computeEndpoint
default <T> java.lang.String computeEndpoint(ApiRequest<T> apiRequest)
Computers the endpoint of thisApiRequest.
If theApiRequest.getComputedEndpoint()is overridden, this method may not be called, depending on the implementation.- Type Parameters:
T- The type of the response.- Parameters:
apiRequest- TheApiRequestto compute the endpoint of.- Returns:
- The computed endpoint of this
ApiRequestwith allPathParameters replaces andRequestQuerys added.
-
applyHeadersToHttpRequestBuilder
default <T> void applyHeadersToHttpRequestBuilder(ApiRequest<T> apiRequest, java.net.http.HttpRequest.Builder httpRequestBuilder, RequestHeader[] requestHeaders)
Applies theRequestHeaders to the givenHttpRequest.Builder.
If theApiRequest.applyHeadersToHttpRequestBuilder(HttpRequest.Builder, RequestHeader[])is overridden, this method may not be called, depending on the implementation.- Type Parameters:
T- The type of the response.- Parameters:
apiRequest- TheApiRequestto apply theRequestHeaders to.httpRequestBuilder- TheHttpRequest.Builderto apply theRequestHeaders to.requestHeaders- TheRequestHeaders to apply.
-
createHttpClientInstance
default java.net.http.HttpClient createHttpClientInstance()
Creates a newHttpClientinstance that will be used for sending requests.
If theApiRequest.createHttpClientInstance()is overridden, this method may not be called, depending on the implementation.- Returns:
- The created
HttpClientinstance.
-
createHttpRequestBuilderInstance
default java.net.http.HttpRequest.Builder createHttpRequestBuilderInstance()
Creates a newHttpRequest.Builderinstance that will be used for building requests.
If theApiRequest.createHttpRequestBuilderInstance()is overridden, this method may not be called, depending on the implementation.- Returns:
- The created
HttpRequest.Builderinstance.
-
runAsync
default void runAsync(java.lang.Runnable runnable)
This method is used for async requests. You may override this method to change the way async requests are sent, for example, using a thread pool, etc.- Parameters:
runnable- The runnable to run.
-
createInstanceOfResponseClass
default <T> T createInstanceOfResponseClass(java.lang.Class<T> responseClass) throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessExceptionCreates a new instance of the response class.
If theApiRequest.createInstanceOfResponseClass()is overridden, this method may not be called, depending on the implementation.- Type Parameters:
T- The type of the response.- Parameters:
responseClass- The response class to create an instance of.- 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> T handleResponse(ApiRequest<T> apiRequest, java.net.http.HttpResponse<?> httpResponse) throws java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException
Handles the givenHttpResponse.
If theApiRequest.handleResponse(HttpResponse)is overridden, this method may not be called, depending on the implementation.- Type Parameters:
T- The type of the response.- Parameters:
apiRequest- TheApiRequestto handle the response for.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.
-
send
default <T> T send(ApiRequest<T> apiRequest) throws java.io.IOException, java.lang.InterruptedException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException
Sends the request synchronously.
If theApiRequest.send()is overridden, this method may not be called, depending on the implementation.- Type Parameters:
T- The type of the response.- Parameters:
apiRequest- The request to send.- 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.
-
onApiRequest
default <T> void onApiRequest(ApiRequest<T> request)
Is called before the request is sent.- Type Parameters:
T- The type of the response.- Parameters:
request- The request.
-
onAfterApiRequest
default <T> void onAfterApiRequest(ApiRequest<T> request)
It is called after the request is sent but before the response is handled.
If the request send fails with exception, this method is not called.- Type Parameters:
T- The type of the response.- Parameters:
request- The request.
-
onAfterHandledApiRequest
default <T> void onAfterHandledApiRequest(ApiRequest<T> request, T response)
It is called after the request's response is handled.
If the request send and/or request's response handler fails with exception, this method is not called.- Type Parameters:
T- The type of the response.- Parameters:
request- The request.response- The response.
-
onException
default <T> void onException(ApiRequest<T> request, java.lang.Throwable throwable)
It is called when any exception occurs when sending the request or handling the response.
Note: This method does not catch any exceptions.
All exceptions are re-thrown after this method is called, if therethrowExceptions()returnstrue.- Type Parameters:
T- The type of the response.- Parameters:
request- The request.throwable- The exception.
-
rethrowExceptions
default boolean rethrowExceptions()
Determines if exceptions should be re-thrown afteronException(ApiRequest, Throwable)is called.
Also, if this method returns false, all responses that failed to be sent or handled will benull.- Returns:
- If exceptions should be re-thrown after
onException(ApiRequest, Throwable)is called.
-
-