Add channel search request and object
This commit is contained in:
parent
27e5fd2c79
commit
6b40f9e76f
3 changed files with 60 additions and 1 deletions
|
|
@ -216,4 +216,16 @@ class Twitch {
|
|||
debugPrint('Channel does not exist');
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a list of channel query objects closest matching the given query string.
|
||||
static Future<List<ChannelQuery>> searchChannels({required String query, required Map<String, String>? headers}) async {
|
||||
final response = await http.get(Uri.parse('https://api.twitch.tv/helix/search/channels?first=8&query=$query'), headers: headers);
|
||||
if (response.statusCode == 200) {
|
||||
final channelData = jsonDecode(response.body)['data'] as List;
|
||||
|
||||
return channelData.map((e) => ChannelQuery.fromJson(e)).toList();
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import 'package:json_annotation/json_annotation.dart';
|
|||
|
||||
part 'channel.g.dart';
|
||||
|
||||
// Object for Twitch channels.
|
||||
// Object for Twitch channel GET request.
|
||||
@JsonSerializable(createToJson: false, fieldRename: FieldRename.snake)
|
||||
class Channel {
|
||||
final String broadcasterId;
|
||||
|
|
@ -27,3 +27,35 @@ class Channel {
|
|||
|
||||
factory Channel.fromJson(Map<String, dynamic> json) => _$ChannelFromJson(json);
|
||||
}
|
||||
|
||||
// Object for Twitch channel search query.
|
||||
@JsonSerializable(createToJson: false, fieldRename: FieldRename.snake)
|
||||
class ChannelQuery {
|
||||
final String broadcasterLanguage;
|
||||
final String broadcasterLogin;
|
||||
final String displayName;
|
||||
final String gameId;
|
||||
final String gameName;
|
||||
final String id;
|
||||
final bool isLive;
|
||||
final List<String> tagIds;
|
||||
final String thumbnailUrl;
|
||||
final String title;
|
||||
final String startedAt;
|
||||
|
||||
const ChannelQuery({
|
||||
required this.broadcasterLanguage,
|
||||
required this.broadcasterLogin,
|
||||
required this.displayName,
|
||||
required this.gameId,
|
||||
required this.gameName,
|
||||
required this.id,
|
||||
required this.isLive,
|
||||
required this.tagIds,
|
||||
required this.thumbnailUrl,
|
||||
required this.title,
|
||||
required this.startedAt,
|
||||
});
|
||||
|
||||
factory ChannelQuery.fromJson(Map<String, dynamic> json) => _$ChannelQueryFromJson(json);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,3 +16,18 @@ Channel _$ChannelFromJson(Map<String, dynamic> json) => Channel(
|
|||
title: json['title'] as String,
|
||||
delay: json['delay'] as int,
|
||||
);
|
||||
|
||||
ChannelQuery _$ChannelQueryFromJson(Map<String, dynamic> json) => ChannelQuery(
|
||||
broadcasterLanguage: json['broadcaster_language'] as String,
|
||||
broadcasterLogin: json['broadcaster_login'] as String,
|
||||
displayName: json['display_name'] as String,
|
||||
gameId: json['game_id'] as String,
|
||||
gameName: json['game_name'] as String,
|
||||
id: json['id'] as String,
|
||||
isLive: json['is_live'] as bool,
|
||||
tagIds:
|
||||
(json['tag_ids'] as List<dynamic>).map((e) => e as String).toList(),
|
||||
thumbnailUrl: json['thumbnail_url'] as String,
|
||||
title: json['title'] as String,
|
||||
startedAt: json['started_at'] as String,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue