husnjak / IGDB-API-JVM

Wrapper for IGDBs API written in Kotlin should work for all JVM based languages, tested in Kotlin & Java
MIT License
37 stars 10 forks source link
apicalypse igdb kotlin wrapper

Maven Central

IGDB API-JVM (V4)

A Kotlin wrapper for the IGDB.com Video Game Database API.

IMPORTANT

This wrapper is compatible with ONLY their newest release V4.

About IGDB

One of the principles behind IGDB.com is accessibility of data. We wish to share the data with anyone who wants to build cool video game oriented websites, apps and services. This means that the information you contribute to IGDB.com can be used by other projects as well.

Thus, you are not only contributing to the value of this site but to thousands of other projects as well. We are looking forward to see what exciting game related projects you come up with. Happy coding!

More info here:

Information about the Querying language APICalypse:

About the wrapper

This wrapper is written in Kotlin which uses the JVM and works with both Kotlin & Java projects. I have not tested it on other JVM languages but it should work for these languages as well. The examples below showcase this wrapper in both Kotlin and Java.

Feel free to test it on other languages yourselves 😀

The Wrapper can handle both the IGDB generated classes and JSON (Strings), I have chosen to make the API's Generated classes (Protocol Buffers) the standard way because it will make it easier to use as you don't have to create your own classes to hold the information.

Installation and setup

Maven

<dependency>
  <groupId>io.github.husnjak</groupId>
  <artifactId>igdb-api-jvm</artifactId>
  <version>1.2.0</version>
</dependency>

Gradle

dependencies {
    implementation 'io.github.husnjak:igdb-api-jvm:1.2.0'
}

Optional for Android (SDK: 19+). Add internet permissions in the manifest.

<uses-permission android:name="android.permission.INTERNET" />

Using your Twitch Developer Credentials

// The instance stores the token in the object untill a new one is requested TwitchToken token = tAuth.getTwitchToken()

``` kotlin
// Kotlin example
val token = TwitchAuthenticator.requestTwitchToken("CLIENT_ID", "CLIENT_SECRET")

// The instance stores the token in the object untill a new one is requested
TwitchAuthenticator.twitchToken

The TwitchToken object looks like this

{
  "access_token": "ACCESS_TOKEN",
  "expires_in": 5135439,
  "token_type": "bearer"
}

Android projects

Do not use the TwitchAuthenticator in your Android applications, you don't want to create multiple access_tokens for each device.
It is recommended to create your token on a server and then use a proxy api to call the IGDB api, where you append the Bearer token for each request.

IGDB provides a free AWS CloudFormation template that you can deploy for this purpose with instructions on how to use it.

How to use the wrapper

The wrapper has two "wrapping" functions, and a lot of helper functions (one for each endpoint)
The two main functions called apiProtoRequest and apiJsonRequest and they handle all the requests to the api.
The class APICalypse handles the new querying language, so that you don't need to care about structure and syntax as much.

NOTE
These examples above are only here to show you how to use the "manual" part of the wrapper. This wrapper comes with complete functions for each endpoint in the API, so you don't have to deal with the manual implementation.

There are two functions for each endpoint, one for classes and one for json, for quick access. The difference between them is the name see the examples below:

// Example of functions in Kotlin
IGDBWrapper.games(APICalypse())
IGDBWrapper.platforms(APICalypse())
IGDBWrapper.genres(APICalypse())
...
IGDBWrapper.jsonGames(APICalypse())
IGDBWrapper.jsonPlatforms(APICalypse())
IGDBWrapper.jsonGenres(APICalypse())
...
// Example of functions in Java
ProtoRequestKt.games(IGDBWrapper.INSTANCE, new APICalypse())
ProtoRequestKt.platforms(IGDBWrapper.INSTANCE, new APICalypse())
ProtoRequestKt.genres(IGDBWrapper.INSTANCE, new APICalypse())
...
JsonRequestKt.jsonGames(IGDBWrapper.INSTANCE, new APICalypse())
JsonRequestKt.jsonPlatforms(IGDBWrapper.INSTANCE, new APICalypse())
JsonRequestKt.jsonGenres(IGDBWrapper.INSTANCE, new APICalypse())
...

ImageBuilder

To simplify the process of building the image URLs for IGDBs images there is a function called imageBuilder which is a helping tool in requesting the perfect sized images for your project. The function requires you to get the image_id then set your desired size (resolution), set your desired image format (default is set to PNG).

// Kotlin Example
val image_id = "mnljdjtrh44x4snmierh"
val imageURL = imageBuilder(image_id, ImageSize.SCREENSHOT_HUGE, ImageType.PNG)

/*
* Result: 
* imageURL = https://images.igdb.com/igdb/image/upload/t_screenshot_huge/mnljdjtrh44x4snmierh.png
*/
// Java Example
String image_id = "mnljdjtrh44x4snmierh";
String imageURL = ImageBuilderKt.imageBuilder(image_id, ImageSize.SCREENSHOT_HUGE, ImageType.PNG)

/*
* Result: 
* imageURL = https://images.igdb.com/igdb/image/upload/t_screenshot_huge/mnljdjtrh44x4snmierh.png
*/

More information about images can be found here

Exceptions

The wrapper throws an RequestException on every exception from the API. This exception hold three things:

Code Examples