Package dev.mayuna.simpleapi
Interface WrappedApi
public interface WrappedApi
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> voidapplyHeadersToHttpRequestBuilder(ApiRequest<T> apiRequest, 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> 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 HttpClientCreates 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 HttpRequest.BuilderCreates 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(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[]Gets the defaultRequestHeaders for this API.
Useful if you want to set some headers for all requests.Gets the default URL for this API.default DurationGets the default timeout duration for the API requests.default <T> ThandleResponse(ApiRequest<T> apiRequest, 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, 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 booleanDetermines 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 voidThis 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 Details
-
getDefaultUrl
String getDefaultUrl()Gets the default URL for this API.- Returns:
- The default URL for this API.
-
getDefaultRequestHeaders
Gets the defaultRequestHeaders for this API.
Useful if you want to set some headers for all requests.- Returns:
- Nullable array of
RequestHeaders.
-
computeEndpoint
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, 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
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
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.
-
getTimeoutDuration
Gets the default timeout duration for the API requests.- Returns:
- The default timeout duration for the API requests.
-
runAsync
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(Class<T> responseClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException Creates 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:
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> T handleResponse(ApiRequest<T> apiRequest, HttpResponse<?> httpResponse) throws InvocationTargetException, NoSuchMethodException, InstantiationException, 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:
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.
-
send
default <T> T send(ApiRequest<T> apiRequest) throws IOException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, 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:
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.
-
onApiRequest
Is called before the request is sent.- Type Parameters:
T- The type of the response.- Parameters:
request- The request.
-
onAfterApiRequest
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
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
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.
-