This is a library for making simple Clash of clans API GET requests.
- Add the library dependency to your project.
Maven
<dependency>
<groupId>io.github.andruid929</groupId>
<artifactId>cocapi</artifactId>
<version>1.1.0-beta.2</version>
</dependency>Gradle
implementation 'io.github.andruid929:cocoapi:1.1.0'So, either
Visit Clash of Clans API to learn how to acquire a token for yourself.
OR
If you already have an API token, proceed with:
- In your project folder, create a Java class with any name you want.
- Create a
public static Stringfield with any name of your choosing. This field can befinalor not but **MUST ** bepublicandstaticotherwise it will not be detected. - Assign your API token to this field.
- Annotate this field with @ApiToken. This annotation marks the field as the API token which is required for requests
Here's what the field should look like
@ApiToken
public static final String MY_API_TOKEN = "api.token.here";-
Any issues with the field annotated as the API token will come back to bite you, so make sure the field is not empty, null and accurately reflects your API token.
-
If you have multiple fields annotated with
@ApiToken, the program will only take the first one it finds and stop there. Field scanning is done from top to bottom. -
Once you've declared this field, you're done with the mandatory configuration. The only thing left to do is to tell the program what your configuration file is.
-
Call
Config.setConfigurationClass(YourConfigClass.class);and pass in your Java class instead ofYourConfigClass.
With that being said, you're all set to start making requests.
Exception handling
-
You can configure the program to automatically handle exceptions when they are thrown, particulary those that occur when a method is trying to return something.
By default, the program will catch an exception, log down what went wrong in the terminal then returns a default value instead of the actual value.
-
This behaviour can be changed by setting the exception handling mode:
Config.setExceptionHandlingMode(ExceptionHandleMode mode);
You can select one of three modes in ExceptionHandlingMode.java
Default, prints error message to terminal, methods return default values.
Methods return default value, no error messages logged.
Throws the exception, no value returned.
PlayerInfo.getPlayerInfo(playerId);Get info for the player specified in the playerID parameter, this is returned as a JSON string. This JSON string can be parsed manually or passed into the Player constructor.