Package dev.mayuna.simpleapi
Interface APIRequest
-
public interface APIRequest
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classAPIRequest.Builder
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default java.net.http.HttpResponse.BodyHandler<?>getBodyHandler()This method must return expected API response body.
Example: You know, that the API will respond with Raw JSON, so you returnHttpResponse.BodyHandlers.ofString()default java.net.http.HttpRequest.BodyPublishergetBodyPublisher()ReturnsHttpRequest.BodyPublisherwith request body.
Example: You are sending JSON in POST Request, so you pass your JSON intoHttpRequest.BodyPublishers.ofString(String)methoddefault java.lang.StringgetContentType()Returns value which will be used inContent-Typeheader.@NonNull java.lang.StringgetEndpoint()Returns API endpoint.default java.lang.StringgetFinalEndpoint()You should not override this method.
This method is used for inserting specified queries ingetQueries()method@NonNull java.lang.StringgetMethod()HTTP Method which will be used for requesting
Example:GET
This method is required.default PathParameter[]getPathParameters()Returns array ofPathParameters which will be used for replacing path parameters in APIs endpoint.
Example: You have/users/{user_id}/statsAPI endpoint, so you must declarePathParameterwith parameteruser_idand your desired replacementdefault Query[]getQueries()default voidprocessHttpRequestBuilder(java.net.http.HttpRequest.Builder httpRequestBuilder)This method is called when requesting.
-
-
-
Method Detail
-
getEndpoint
@NonNull @NonNull java.lang.String getEndpoint()
Returns API endpoint. Should start with/
Example:/users/{user_id}/stats
This method is required.- Returns:
- Non-null API Endpoint
-
getMethod
@NonNull @NonNull java.lang.String getMethod()
HTTP Method which will be used for requesting
Example:GET
This method is required.- Returns:
- HTTP Request method
-
getPathParameters
default PathParameter[] getPathParameters()
Returns array ofPathParameters which will be used for replacing path parameters in APIs endpoint.
Example: You have/users/{user_id}/statsAPI endpoint, so you must declarePathParameterwith parameteruser_idand your desired replacement- Returns:
- Nullable
PathParameterarray
-
getQueries
default Query[] getQueries()
Returns array ofQuery(ies) which will be added after APIs endpoint.
Example:
return new Query[] { new Query("name", "first_value"), new Query("other_name", "second_value") }
Which will result in:?name=first_value&other_name=second_value- Returns:
- Nullable
Queryarray
-
processHttpRequestBuilder
default void processHttpRequestBuilder(@NonNull java.net.http.HttpRequest.Builder httpRequestBuilder)This method is called when requesting. You can specify special headers viaHttpRequest.Builder.header(String, String)method.- Parameters:
httpRequestBuilder- Non-nullHttpRequest.Builderobject
-
getBodyHandler
@NonNull default java.net.http.HttpResponse.BodyHandler<?> getBodyHandler()
This method must return expected API response body.
Example: You know, that the API will respond with Raw JSON, so you returnHttpResponse.BodyHandlers.ofString()- Returns:
- Non-null
HttpResponse.BodyHandlerobject
-
getContentType
default java.lang.String getContentType()
Returns value which will be used inContent-Typeheader. Null for noContent-Typeheader- Returns:
- Content-type
-
getBodyPublisher
@NonNull default java.net.http.HttpRequest.BodyPublisher getBodyPublisher()
ReturnsHttpRequest.BodyPublisherwith request body.
Example: You are sending JSON in POST Request, so you pass your JSON intoHttpRequest.BodyPublishers.ofString(String)method- Returns:
- Non-null
HttpRequest.BodyPublisher
-
getFinalEndpoint
default java.lang.String getFinalEndpoint()
You should not override this method.
This method is used for inserting specified queries ingetQueries()method- Returns:
- Final API endpoint which will used for requesting
-
-