[Android] NoNonsense WaniKani Client - client library in Kotlin

Hello all.

So I made a client implementation of the API in Kotlin because I wanted it for some personal project.

This is probably only interesting for other developers.

You can use it in your Android (or plain Java for that matter) projects by adding this to your gradle dependencies:

implementation("com.nononsenseapps.wanikani:wanikani-client:2.0.0")

The source is available at https://gitlab.com/spacecowboy/wanikani-client under the Apache 2 license.

Currently it only offers suspend functions (which means you need to use coroutines in your app) but if there’s any interest I might add regular blocking functions too.

In any case, here’s a quick example of how you might use it:

import com.nononsenseapps.wanikani.client.getWaniKaniClient
import com.nononsenseapps.wanikani.client.response.SummaryLesson
import com.nononsenseapps.wanikani.client.response.SummaryReview
import kotlinx.coroutines.runBlocking
import java.time.Instant

fun currentLessonsAndReviews(apiKey: String) {
    val client = getWaniKaniClient(apiKey)

    runBlocking {
        val user = client.getUser().data

        println("Hello ${user.username}, you are level ${user.level}")

        val summary = client.getSummary().data

        val lessonsAvailableNow = summary.lessons
            .filter { it.availableAt < Instant.now() }
            .fold(0) { total: Int, summaryLesson: SummaryLesson ->
                total + summaryLesson.subjectIds.count()
            }

        println("You have $lessonsAvailableNow lessons waiting for you")

        val reviewsAvailableNow = summary.reviews
            .filter { it.availableAt < Instant.now() }
            .fold(0) { total: Int, summaryReview: SummaryReview ->
                total + summaryReview.subjectIds.count()
            }

        println("You have $reviewsAvailableNow reviews waiting for you")

        val moreReviewsAt = summary.nextReviewsAt

        if (moreReviewsAt == null) {
            println("No more reviews after that - you're done!")
        } else {
            println("More reviews coming up at $moreReviewsAt")
        }
    }
}

The output would be similar to

Hello cowboyprogrammer, you are level 26
You have 48 lessons waiting for you
You have 94 reviews waiting for you
More reviews coming up at 2020-06-13T19:00:00Z

I’ve implemented the entire API and tested it against the examples given in the docs but I’ve noticed that real responses are not necessarily exactly what the examples imply so there might be a few things to fix.

Hope it’s useful for someone. Bug reports and contributions are most welcome.

Maven Central

2 Likes

This is great! I don’t do development with the WK API, but since I vastly prefer Kotlin over Java, I’m happy to see this implementation anyway. If I end up making anything with the WK API, I’ll be sure to use this!

1 Like

Thank you for making this Library, and thumbs up for using Coroutines, This is great!
I’ll be sure to use this when I’m going to talk with Wanikani API. :nerd_face:

2 Likes

Version 0.0.4

Updated library for latest breaking API changes. Also took the opportunity to add some additional type safety so the Stages Object in Kotlin does not have separate interval and interval_unit fields - instead it has a single Duration object.

2 Likes

Version 2.0.0

  • Replaced the audio types enum with a simple string.

Latest round of new vocabulary introduced audio encoded in webm-format. To prevent further breakage, I changed the enum to a String so if any future formats are added then they don’t break the library.

This is a breaking change (if you ever depended on this field) hence the major version bump.

1 Like