This project's goal is to provide an easier way to write API wrappers in Java. It abstracts away the implementation details of making calls to the API and allows you to retrieve the data in a simple and safe way.
Download the project as a JAR and execute the following Java code:
packageAbstractusAPI;
importAbstractusAPI.http.query.Endpoint;
importAbstractusAPI.http.query.QueryParameter;
importAbstractusAPI.http.request.RequestController;
importorg.json.JSONObject;
importjava.util.concurrent.CompletableFuture;
importjava.util.concurrent.ExecutionException;
publicclassTest {
publicstaticvoidmain(String[] args) throwsExecutionException, InterruptedException {
RequestControllerrequestController = newRequestController("https", "v2.jokeapi.dev");
CompletableFuture<JSONObject> jsonObject = requestController.sendRequestAsync(newEndpoint("joke", "any"));
System.out.println(jsonObject.get());;
}
}
This project supports automatic 15 minute caching using OkHttp3 built in caching. To implement your own caching pass in a OkHttpClient into the
RequestController constructor with all configurations customized. To see more about implementing your own cache read more
here.
This project comes with a basic error handling process. It handles and throws 40+ custom exceptions depending on the status code returned from the API.
If you would like to create your own validation process simply create a class the implements the RequestValidator interface pass it in as a parameter to
the RequestController constructor.
To add parameters to the request use the QueryParameter record. It takes in a key and value in it's constructor. Then, when calling the
sendRequestAsync method of RequestController add the RequestParameter as a parameter.
packageAbstractusAPI;
importAbstractusAPI.http.query.Endpoint;
importAbstractusAPI.http.query.QueryParameter;
importAbstractusAPI.http.request.RequestController;
importorg.json.JSONObject;
importjava.util.concurrent.CompletableFuture;
importjava.util.concurrent.ExecutionException;
publicclassTest {
publicstaticvoidmain(String[] args) throwsExecutionException, InterruptedException {
RequestControllerrequestController = newRequestController("https", "v2.jokeapi.dev");
CompletableFuture<JSONObject> jsonObject = requestController.sendRequestAsync(newEndpoint("joke", "any"), newQueryParameter("amount", "6"));
System.out.println(jsonObject.get());
}
}
This project also supports adding permanent parameters that will be added to each request. Simply call,
RequestController.addQueryParameter() and pass in the QueryParameter that should be added.
packageAbstractusAPI;
importAbstractusAPI.http.query.Endpoint;
importAbstractusAPI.http.query.QueryParameter;
importAbstractusAPI.http.request.RequestController;
importorg.json.JSONObject;
importjava.util.concurrent.CompletableFuture;
importjava.util.concurrent.ExecutionException;
publicclassTest {
publicstaticvoidmain(String[] args) throwsExecutionException, InterruptedException {
RequestControllerrequestController = newRequestController("https", "v2.jokeapi.dev");
requestController.addQueryParameter(newQueryParameter("amount", "6"));
CompletableFuture<JSONObject> jsonObject = requestController.sendRequestAsync(newEndpoint("joke", "any"));
System.out.println(jsonObject.get());
}
}
To asynchronously process the results there are many options since the RequestController returns a CompletableFuture.
A simple way is to create a new thread and process the results in it.
packageAbstractusAPI;
importAbstractusAPI.http.query.Endpoint;
importAbstractusAPI.http.query.QueryParameter;
importAbstractusAPI.http.request.RequestController;
importorg.json.JSONObject;
importjava.util.concurrent.CompletableFuture;
importjava.util.concurrent.ExecutionException;
publicclassTest {
publicstaticvoidmain(String[] args) throwsExecutionException, InterruptedException {
RequestControllerrequestController = newRequestController("https", "v2.jokeapi.dev");
newThread(() -> {
try {
CompletableFuture<JSONObject> future = requestController.sendRequestAsync(newEndpoint("joke", "any"));
System.out.println(future.get());
} catch (InterruptedException | ExecutionExceptione) {
e.printStackTrace();
}
}).start();
}
}Distributed under the MIT License. See LICENSE.txt for more information.
unpluggedsam - unpluggedsam990@gmail.com
Project Link: https://github.com/unpluggedsam/abstractusAPI/