Skip to content

Commit

Permalink
feat: impl search song, artist, album
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Nov 11, 2023
1 parent 9ea60c6 commit 83f62e1
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 74 deletions.
77 changes: 77 additions & 0 deletions lib/src/endpoints/search.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:jiosaavn/src/client.dart';
import 'package:jiosaavn/src/collection/endpoints.dart';
import 'package:jiosaavn/src/models/album.dart';
import 'package:jiosaavn/src/models/artist.dart';
import 'package:jiosaavn/src/models/search.dart';
import 'package:jiosaavn/src/models/song.dart';

class SearchEndpoint extends BaseClient {
SearchEndpoint([super.options]);
Expand All @@ -14,4 +17,78 @@ class SearchEndpoint extends BaseClient {

return AllSearchResponse.fromCustomJson(result);
}

Future<SongSearchResponse> songs(
String query, {
int page = 0,
int limit = 10,
}) async {
// api v4 does not contain media_preview_url
final response = await request(
call: endpoints.search.songs,
queryParameters: {
"q": query,
"p": page,
"n": limit,
},
);

final req = SongSearchRequest.fromJson(response);

return SongSearchResponse(
results: req.results.map((e) => SongResponse.fromSongRequest(e)).toList(),
start: req.start,
total: req.total,
);
}

Future<AlbumSearchResponse> albums(
String query, {
int page = 0,
int limit = 10,
}) async {
// api v4 does not contain media_preview_url
final response = await request(
call: endpoints.search.albums,
queryParameters: {
"q": query,
"p": page,
"n": limit,
},
);

final req = AlbumSearchRequest.fromJson(response);

return AlbumSearchResponse(
results:
req.results.map((e) => AlbumResponse.fromAlbumRequest(e)).toList(),
start: req.start,
total: req.total,
);
}

Future<ArtistSearchResponse> artists(
String query, {
int page = 0,
int limit = 10,
}) async {
// api v4 does not contain media_preview_url
final response = await request(
call: endpoints.search.artists,
queryParameters: {
"q": query,
"p": page,
"n": limit,
},
);

final req = ArtistSearchRequest.fromJson(response);

return ArtistSearchResponse(
results:
req.results.map((e) => ArtistResponse.fromArtistRequest(e)).toList(),
start: req.start,
total: req.total,
);
}
}
96 changes: 48 additions & 48 deletions lib/src/models/artist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,59 +170,59 @@ class ArtistUrls {
@JsonSerializable()
class ArtistRequest extends Artist {
String? artistId;
String subtitle;
String? subtitle;

@JsonKey(name: "follower_count")
String followerCount;
String? followerCount;

@JsonKey(name: "is_verified")
bool? isVerified;

String dominantLanguage;
String? dominantLanguage;

String dominantType;
String? dominantType;

static List<SongRequest> _fromTopSongs(List topSongs) {
static List<SongRequest>? _fromTopSongs(List? topSongs) {
return topSongs
.map((song) => SongRequest.fromArtistTopSong(song))
?.map((song) => SongRequest.fromArtistTopSong(song))
.toList()
.cast<SongRequest>();
}

@JsonKey(fromJson: ArtistRequest._fromTopSongs)
List<SongRequest> topSongs;
List<SongRequest>? topSongs;

List<AlbumRequest> topAlbums;
List<AlbumRequest>? topAlbums;

String bio;
String dob;
String fb;
String twitter;
String wiki;
String? bio;
String? dob;
String? fb;
String? twitter;
String? wiki;
ArtistUrls? urls;

List<String> availableLanguages;
List<String>? availableLanguages;

@JsonKey(name: "fan_count")
String fanCount;
String? fanCount;

ArtistRequest({
this.artistId,
required this.subtitle,
required this.followerCount,
this.subtitle,
this.followerCount,
this.isVerified,
required this.dominantLanguage,
required this.dominantType,
required this.topSongs,
required this.topAlbums,
required this.bio,
required this.dob,
required this.fb,
required this.twitter,
required this.wiki,
this.dominantLanguage,
this.dominantType,
this.topSongs,
this.topAlbums,
this.bio,
this.dob,
this.fb,
this.twitter,
this.wiki,
this.urls,
required this.availableLanguages,
required this.fanCount,
this.availableLanguages,
this.fanCount,
required super.id,
required super.name,
required super.ctr,
Expand Down Expand Up @@ -251,24 +251,24 @@ class ArtistResponse {
List<DownloadLink>? image;

@JsonKey(name: "follower_count")
String followerCount;
String? followerCount;

@JsonKey(name: "fan_count")
String fanCount;
String? fanCount;

@JsonKey(name: "is_verified")
bool? isVerified;

String dominantLanguage;
String? dominantLanguage;
String? dominantType;

String dominantType;
String bio;
String dob;
String fb;
String twitter;
String wiki;
String? bio;
String? dob;
String? fb;
String? twitter;
String? wiki;

List<String> availableLanguages;
List<String>? availableLanguages;

@JsonKey(name: "is_radio_present")
bool? isRadioPresent;
Expand All @@ -279,17 +279,17 @@ class ArtistResponse {
this.url,
this.role,
this.image,
required this.followerCount,
required this.fanCount,
this.followerCount,
this.fanCount,
this.isVerified,
required this.dominantLanguage,
required this.dominantType,
required this.bio,
required this.dob,
required this.fb,
required this.twitter,
required this.wiki,
required this.availableLanguages,
this.dominantLanguage,
this.dominantType,
this.bio,
this.dob,
this.fb,
this.twitter,
this.wiki,
this.availableLanguages,
this.isRadioPresent,
});

Expand Down
52 changes: 26 additions & 26 deletions lib/src/models/artist.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/endpoints/search.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:jiosaavn/src/endpoints/search.dart';
import 'package:jiosaavn/src/models/album.dart';
import 'package:jiosaavn/src/models/artist.dart';
import 'package:jiosaavn/src/models/search.dart';
import 'package:jiosaavn/src/models/song.dart';
import 'package:test/test.dart';

void main(List<String> args) {
Expand All @@ -11,5 +14,23 @@ void main(List<String> args) {

expect(res, isA<AllSearchResponse>());
});

test("Song search", () async {
final res = await search.songs("Malibu - Miley Cyrus");

expect(res, isA<SongSearchResponse>());
});

test("Album search", () async {
final res = await search.albums("Scaled and Icy");

expect(res, isA<AlbumSearchResponse>());
});

test("Artist search", () async {
final res = await search.artists("Twenty One Pilots");

expect(res, isA<ArtistSearchResponse>());
});
});
}

0 comments on commit 83f62e1

Please sign in to comment.