searchGroups method Null safety
Searches for groups with the given parameters
Returns an empty List if no results
Implementation
Future<List<Group>> searchGroups(
{bool useLogin = false,
String name = '',
List<String> ids = const []}) async {
var res;
if (token != '' && useLogin) {
res = await http.get(
Uri.parse(
'https://api.mangadex.org/group?name=$name${(ids.isNotEmpty) ? '&ids[]=${ids.join('&ids[]=')}' : ''}'),
headers: {
HttpHeaders.authorizationHeader: 'Bearer $token',
HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'
});
} else {
res = await http.get(
Uri.parse(
'https://api.mangadex.org/group?name=$name${(ids.isNotEmpty) ? '&ids[]=${ids.join('&ids[]=')}' : ''}'),
headers: {HttpHeaders.userAgentHeader: 'mangadex_dart_api/1.0'});
}
if (res.statusCode == 403 && res.headers['X-Captcha-Sitekey'] != null) {
throw CaptchaException(res.headers['X-Captcha-Sitekey'].toString(),
message:
'You need to solve a captcha, check `.sitekey` for the sitekey.');
}
var data = jsonDecode(res.body)['results'];
// ignore: omit_local_variable_types
List<Group> groups = [];
for (var group in data) {
var r = group['data'];
// ignore: omit_local_variable_types
List<User> members = [];
for (var member in r['attributes']['members']) {
members.add(
User(id: member['id'], username: member['attributes']['username']));
}
groups.add(Group(
id: r['id'],
name: r['attributes']['name'],
isLocked: r['attributes']['locked'],
createdAt: r['attributes']['createdAt'],
updatedAt: r['attributes']['updatedAt'],
description: r['attributes']['description'],
website: r['attributes']['website'],
ircChannel: r['attributes']['ircChannel'],
ircServer: r['attributes']['ircServer'],
discord: r['attributes']['discord'],
contactEmail: r['attributes']['contactEmail'],
leader: User(
id: r['attributes']['leader']['id'],
username: r['attributes']['leader']['attributes']['username']),
members: members));
}
return groups;
}