Skip to content

Commit

Permalink
feat: implement song endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Nov 11, 2023
1 parent 59513b7 commit cdc55c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
32 changes: 32 additions & 0 deletions lib/src/endpoints/song.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:dio/dio.dart';
import 'package:jiosaavn/src/client.dart';
import 'package:jiosaavn/src/collection/endpoints.dart';
import 'package:jiosaavn/src/models/song.dart';

class SongEndpoint extends BaseClient {
SongEndpoint([super.options]);

Future<List<SongResponse>> detailsById(List<String> ids) async {
// api v4 does not contain media_preview_url
final response = await request(call: endpoints.songs.id, queryParameters: {
"pids": ids.join(','),
});

if (response["songs"] == null || response["songs"]?.isNotEmpty == false)
throw DioException(
requestOptions: RequestOptions(
baseUrl: options?.baseUrl,
queryParameters: options?.queryParameters,
),
error: "No songs found",
type: DioExceptionType.badResponse,
);

return (response["songs"] as List)
.map(
(song) => SongResponse.fromSongRequest(SongRequest.fromJson(song)),
)
.toList()
.cast<SongResponse>();
}
}
2 changes: 1 addition & 1 deletion lib/src/models/song.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SongRequest {
String language;
String origin;

@JsonKey(name: "play_count")
@JsonKey(name: "play_count", fromJson: SongRequest._toString)
String? playCount;

@JsonKey(name: "copyright_text")
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/song.g.dart

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

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

void main(List<String> args) {
final song = SongEndpoint();

group("Song Endpoint => ", () {
test("Get song Details by Ids", () async {
final res = await song.detailsById(["5WXAlMNt", "csaEsVWV"]);

expect(res, isA<List<SongResponse>>());
expect(res, hasLength(2));
});
});
}

0 comments on commit cdc55c9

Please sign in to comment.