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.