search method
Search for a query
Note: This endpoint is currently unstable and may be reworked. Refer to the official docs for more https://docs.nekos.best/api/endpoints.html#get-searchqueryxtypexcategoryxamountx
Implementation
Future<List<NBResponse>> search(String query,
{String? endpoint = null, int amount = 1}) async {
// If Ratelimit isnt null, then...
if (RateLimit != null) {
// If ratelimit is yet valid, throw it to the user
if (!DateTime.now().isAfter(RateLimit!.resetsIn)) {
throw RateLimit as NBRateLimitError;
}
// else RateLimit is no longer valid, so make it null and continue
RateLimit = null;
}
if (amount > 20) throw NBArgumentError("Amount cannot be greater than 20");
if (amount < 1) throw NBArgumentError("Amount cannot be less than 1");
if (endpoint == null) {
endpoint = randomCat();
} else {
endpoint = endpoint.toLowerCase();
isValid(endpoint);
}
var type = IMAGE_CATEGORIES.contains(endpoint) ? 1 : 2;
var res = await request(
"search?amount=$amount&type=$type&query=${Uri.encodeComponent(query)}&category=$endpoint");
if (res.statusCode == 429) {
var remaining = res.headers['x-rate-limit-remaining'];
var resetsIn = res.headers['x-rate-limit-remaining'];
RateLimit = NBRateLimitError(remaining as String, resetsIn as String);
throw RateLimit as NBRateLimitError;
}
var json = jsonDecode(res.body) as Map<String, dynamic>;
var arr = <NBResponse>[];
json['results'].forEach((e) => arr.add(NBResponse(e)));
return arr;
}