Skip to content

Get Started

matyrobbrt edited this page Jan 23, 2023 · 1 revision

After you've added the library as a dependency let's see how you can start using this library.

Creating the CurseForgeAPI instance

All of the requests to the CFCore API need to be authorised with an API Key, and are made through a CurseForgeAPI instance, which can be created using the Builder.

final CurseForgeAPI cfApi = CurseForgeAPI.builder()
    .apiKey(API_KEY) // this is our API Key 
    .uploadApiToken(UPLOAD_API_TOKEN) // a token for the Upload API, can be null
    .build();

Making Requests

Blocking Requests

Blocking requests can be made using CurseForgeAPI#makeRequest, using a Request configured with the endpoint URL, the response decoder and, optionally, a body. The returned Response contains an optional value (representing the response to the request), and the status code that the request returned.

Non-Blocking Requests

Blocking requests can be made using CurseForgeAPI#makeAsyncRequest, using a Request configured with the endpoint URL, the response decoder and, optionally, a body. The returned AsyncRequest can be sent using #queue(Consumer). The success consumer will be invoked with the response, for handling it.
For example, the following method will asynchronously get the mod with the project ID 570544 from CurseForge, and print its name:

final AsyncRequest<Response<Mod>> request = cfApi.makeAsyncRequest(Requests.getMod(570544));
request.queue(response -> {
    response.ifPresent(mod -> System.out.println(mod.name()));
});

The Requests class

Needing to remember request structures or needing to check the docs every time you want to request something can become annoying. This is why the Requests class has been created. It contains static factory methods used for creating Requests that do / return different things.
For example, the following method will get the mod with the project ID 570544 from CurseForge, and print its name:

final Response<Mod> modResponse = cfApi.makeRequest(Requests.getMod(570544));
modResponse.ifPresent(mod -> System.out.println(mod.name()));

IRequestHelper

IRequestHelpers are helper classes used for making direct requests, instead of using CurseForgeAPI#makeRequest (or #makeAsyncRequest) and the Requests class for generating them.
A CurseForgeAPI object holds 2 request helpers: A RequestHelper and an AsyncRequestHelper, which can be got using #getHelper(), respectively #getAsyncHelper().
Example of using RequestHelper in order to make a request that will print the name of all the categories that exist for the game with the ID 432 (Minecraft):

final RequestHelper helper = cfApi.getHelper();
final Response<List<Category>> categoriesResponse = helper.getCategories(Constants.GameIDs.MINECRAFT); // 432 can be used as well
categoriesResponse.ifPresent(categories -> categories.forEach(category -> System.out.println(category.name())));