Can you tell me what exactly you are trying to accomplish?
To me it looks like you are doing the following:
- Looking up for all your burned assignments which have no been resurrected
- And looking up the subjects which belong to the assignments and returning the subject characters.
If this is the case, I can go over what I would do.
But before we start I want to address the 403
s you are getting.
The reason you are getting 403
is because you are hitting the rate limiter we have in place (10 requests per second and 60 requests per minute). The code you are using is doing a request for each assignment to retrieve the subject. If you have a lot of burned items (can be in the multiple 1000s given your level), you are going to exceed the rate limit really quickly.
Anyway, back to achieving your goal.
I am going to demonstrate two ways to do it. The first way I will use your existing code and alter it so it is more efficient. The second way is how I would personally write out the code to achieve your goal.
How to achieve your goal using your existing code (jQuery)
var apiKey = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
url = 'https://www.wanikani.com/api/v2/',
endpoint = 'assignments',
parameters = '?srs_stages=9';
$.ajax({
url: url + endpoint + parameters,
dataType: 'json',
method: 'GET',
headers : { 'Authorization' : 'Bearer ' + apiKey },
}).done(function(responseBody) {
var subject_ids = responseBody.data.map(a => a.data.subject_id).join(),
endpoint2 = 'subjects',
parameters2 = '?ids=' + subject_ids;
$.ajax({
url: url + endpoint2 + parameters2,
dataType: 'json',
method: 'GET',
headers : { 'Authorization' : 'Bearer ' + apiKey },
}).done(function(responseBody2) {
var subject_characters = responseBody2.data.map(s => s.data.characters).join(', ');
console.log(subject_characters);
}).fail(function(error) {
alert(error);
});
}).fail(function(error) {
alert(error);
});
How to achieve your goal using my way (Javascript)
var apiBaseUrl = 'https://www.wanikani.com/api/v2/';
var apiToken = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
var requestHeaders =
new Headers({
'Wanikani-Revision': '20170710',
Authorization: 'Bearer ' + apiToken,
});
var assignmentsApiEndpointPath = 'assignments';
var assignmentsFilters = '?srs_stages=9';
var assignmentsApiEndpoint =
new Request(apiBaseUrl + assignmentsApiEndpointPath + assignmentsFilters, {
method: 'GET',
headers: requestHeaders
});
fetch(assignmentsApiEndpoint)
.then(function(response) { return response.json(); })
.then(function(responseBody) { return responseBody.data.map(a => a.data.subject_id).join(); })
.then(function(subject_ids) {
var subjectsApiEndpointPath = 'subjects';
var subjectsFilters = '?ids=' + subject_ids;
var subjectsApiEndpoint =
new Request(apiBaseUrl + subjectsApiEndpointPath + subjectsFilters, {
method: 'GET',
headers: requestHeaders
});
fetch(subjectsApiEndpoint)
.then(function(response) { return response.json(); })
.then(function(responseBody) { console.log(responseBody.data.map(s => s.data.characters).join(', ')); });
});
The key thing in both examples: only two requests are made to the API. This keeps you under the rate limit.
What I did is collect all the subject_ids
from the assignments response, join them in a comma-delimited string, and leverage the ids
filter on the subjects endpoint to get the subjects related to the assignments.
If you are going to be hitting the API a lot to look for subjects, it is best to cache the subjects locally and do a find on the cache rather that hitting the API every time you want the subject data.
Ideally, you would know a programming language. But if you rather not program, I suggest using something like Insomnia, which is free and excellent.