Skip to content

Commit

Permalink
feat: add spotify friend endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jan 5, 2024
1 parent 5509cae commit df8ee98
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
100 changes: 100 additions & 0 deletions lib/models/spotify_friends.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'package:json_annotation/json_annotation.dart';

part 'spotify_friends.g.dart';

@JsonSerializable(createToJson: false)
class SpotifyFriend {
final String uri;
final String name;
final String imageUrl;

const SpotifyFriend({
required this.uri,
required this.name,
required this.imageUrl,
});

factory SpotifyFriend.fromJson(Map<String, dynamic> json) =>
_$SpotifyFriendFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyActivityArtist {
final String uri;
final String name;

const SpotifyActivityArtist({required this.uri, required this.name});

factory SpotifyActivityArtist.fromJson(Map<String, dynamic> json) =>
_$SpotifyActivityArtistFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyActivityAlbum {
final String uri;
final String name;

const SpotifyActivityAlbum({required this.uri, required this.name});

factory SpotifyActivityAlbum.fromJson(Map<String, dynamic> json) =>
_$SpotifyActivityAlbumFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyActivityContext {
final String uri;
final String name;
final num index;

const SpotifyActivityContext({
required this.uri,
required this.name,
required this.index,
});

factory SpotifyActivityContext.fromJson(Map<String, dynamic> json) =>
_$SpotifyActivityContextFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyActivityTrack {
final String uri;
final String name;
final String imageUrl;
final SpotifyActivityArtist artist;
final SpotifyActivityAlbum album;
final SpotifyActivityContext context;

const SpotifyActivityTrack({
required this.uri,
required this.name,
required this.imageUrl,
required this.artist,
required this.album,
required this.context,
});

factory SpotifyActivityTrack.fromJson(Map<String, dynamic> json) =>
_$SpotifyActivityTrackFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyFriendActivity {
SpotifyFriend user;
SpotifyActivityTrack track;

SpotifyFriendActivity({required this.user, required this.track});

factory SpotifyFriendActivity.fromJson(Map<String, dynamic> json) =>
_$SpotifyFriendActivityFromJson(json);
}

@JsonSerializable(createToJson: false)
class SpotifyFriends {
List<SpotifyFriendActivity> friends;

SpotifyFriends({required this.friends});

factory SpotifyFriends.fromJson(Map<String, dynamic> json) =>
_$SpotifyFriendsFromJson(json);
}
65 changes: 65 additions & 0 deletions lib/models/spotify_friends.g.dart

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

14 changes: 14 additions & 0 deletions lib/services/custom_spotify_endpoints/spotify_endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:spotify/spotify.dart';
import 'package:spotube/models/spotify_friends.dart';

class CustomSpotifyEndpoints {
static const _baseUrl = 'https://api.spotify.com/v1';
Expand Down Expand Up @@ -162,4 +163,17 @@ class CustomSpotifyEndpoints {
result["tracks"].map((track) => Track.fromJson(track)).toList(),
);
}

Future<SpotifyFriends> getFriendActivity() async {
final res = await _client.get(
Uri.parse("https://guc-spclient.spotify.com/presence-view/v1/buddylist"),
headers: {
"content-type": "application/json",
"authorization": "Bearer $accessToken",
"accept": "application/json",
},
);

return SpotifyFriends.fromJson(jsonDecode(res.body));
}
}

0 comments on commit df8ee98

Please sign in to comment.