Initialize a client and get started with sending resources by fetching a summary.
In order to access the WaniKani API you must first create an API access token. Follow WaniKani's own documentation on managing tokens for more information on what to do. Keep in mind that securely storing tokens used with this package is the responsibility of the developer.
Creating a client object is as simple as calling the default initializer, and then assigning your token.
let client = WaniKani()
client.token = "..." // Your WaniKani API access token
To send a resource, simply use WaniKani/WaniKani/send(_:pageOptions:)-1726r
. This sample is assuming you are sending from inside an async-throwing function.
let response = try await client.send(.summary)
let pipelines = response.data
Some resources respond with a paged collection of data. As noted in WaniKani's API documentation, the default maximum size of a paged response is 500 items, with Reviews and Subjects defaulting to 1000 items. In order to paginate on this data, you can do this:
var nextPage: PageOptions?
repeat {
let response = try await client.send(.subjects(), pageOptions: nextPage)
let subjects = response.data
nextPage = subjects.page.next
} while nextPage != nil
WaniKani limits all clients to 60 requests per minute. When the rate limit is exceeded, the client will throw ResponseError/rateLimitExceeded(limit:remaining:reset:)
. You can use the error's associated values in to implement a retry strategy.
do {
// Some response that will throw a rateLimitExceeded error.
let response = try await client.send(.subjects())
print(response.data)
} catch ResponseError.rateLimitExceeded(let rateLimit, let rateRemaining, let rateReset) {
print("Rate limit exceeded: will reset in \(rateReset.formatted(.relative(presentation: .numeric)))")
}