Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to null-safety. #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Examples-Level ###
api_values.dart
2 changes: 1 addition & 1 deletion example/applied_methods_example/album_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() async {
));

print('Album Name: ${albumGetInfo.name} |'
' Album Artist: ${albumGetInfo.artist.name}');
' Album Artist: ${albumGetInfo.artist?.name}');

albumGetInfo.tracks?.forEach((Track track) {
print('Track Title: ${track.name} | Track Duration: ${track.duration}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() async {
print('#######################artist.getCorrection#########################');

// artist.getCorrection
(await artistInstance.getCorrection())?.forEach((Artist artist) {
(await artistInstance.getCorrection()).forEach((Artist artist) {
print('Artist correction: ${artist.name}');
});

Expand Down
4 changes: 2 additions & 2 deletions example/applied_methods_example/track_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() async {
print('######################track.getCorrection###########################');

// track.getCorrection
(await trackInstance.getCorrection())?.forEach((Track track) {
(await trackInstance.getCorrection()).forEach((Track track) {
print('Track Correction Name: ${track.name} |'
' Track Correction URL: ${track.url}');
});
Expand Down Expand Up @@ -94,7 +94,7 @@ void main() async {
await trackInstance.scrobble(timestamp: DateTime.now());
// YAY. IT WORKS!

scrobbleResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
scrobbleResponse.scrobbleResponses.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

Expand Down
4 changes: 2 additions & 2 deletions example/applied_methods_example/user_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ void main() async {
print('#####################user.getWeeklyAlbumChart#######################');

// user.getTopTracks
(await userInstance.getTopTracks())?.forEach((Track track) {
(await userInstance.getTopTracks()).forEach((Track track) {
print('Top Tracks Name: ${track.name} | Top Tracks URL: ${track.url} |'
' Duration: ${track.duration}');
});

print('#####################user.getWeeklyArtistChart######################');

// user.getWeeklyAlbumChart
(await userInstance.getTopTracks())?.forEach((Track track) {
(await userInstance.getTopTracks()).forEach((Track track) {
print('Top Tracks Name: ${track.name} | Top Tracks URL: ${track.url} |'
' Duration: ${track.duration}');
});
Expand Down
4 changes: 2 additions & 2 deletions example/methods_example/album_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main() async {
));

print('Album Name: ${albumGetInfo.name} |'
' Album Artist: ${albumGetInfo.artist.name}');
' Album Artist: ${albumGetInfo.artist?.name}');

albumGetInfo.tracks?.forEach((Track track) {
print('Track Title: ${track.name} | Track Duration: ${track.duration}');
Expand All @@ -60,7 +60,7 @@ void main() async {
// album.getTopTags
(await scrobblenaut.album.getTopTags(album: 'Your Name.', artist: 'RADWIMPS'))
?.forEach((Tag tag) {
print('Tag Name: ${tag.name} | Tag URL: ${tag.url}');
print('Tag Name: ${tag.name} | Tag URL: ${tag.url}');
});

print('#########################album.removeTag############################');
Expand Down
2 changes: 1 addition & 1 deletion example/methods_example/artist_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() async {

// artist.getCorrection
(await scrobblenaut.artist.getCorrection(artist: 'RADWIMPS'))
?.forEach((Artist artist) {
.forEach((Artist artist) {
print('Artist correction: ${artist.name}');
});

Expand Down
2 changes: 1 addition & 1 deletion example/methods_example/geo_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() async {

// geo.getTopTracks
(await scrobblenaut.geo.getTopTracks(country: 'canada'))
?.forEach((Track track) {
.forEach((Track track) {
print('Top Track Name: ${track.name} | Top Track URL : ${track.url} | '
'Top Track Duration: ${track.duration}');
// Check if the duration is correct.
Expand Down
7 changes: 2 additions & 5 deletions example/methods_example/library_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import '../api_values.dart';
void main() async {
print('####################################################################');

final lastFMAuth = await LastFM.authenticate(
final lastFMAuth = LastFM.noAuth(
apiKey: APIValues.API,
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
);

final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);
Expand All @@ -26,6 +22,7 @@ void main() async {

// library.getArtist
(await scrobblenaut.library.getArtists(user: 'nebulino'))
?.artist
?.forEach((Artist artist) {
print('Top Artist Name: ${artist.name} | Top Artist URL : ${artist.url}');
});
Expand Down
12 changes: 6 additions & 6 deletions example/methods_example/scrobble_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() async {
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
// sessionKey: APIValues.sessionKey,
);

// final lastFMnoAuth = await LastFM.noAuth(apiKey: APIValues.API);
Expand All @@ -32,7 +32,7 @@ void main() async {
timestamp: DateTime.now());
// YAY. IT WORKS!

response.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
response.scrobbleResponses.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

Expand All @@ -43,17 +43,17 @@ void main() async {
final anotherResponse =
await scrobblenaut.track.scrobbleFromObject(scrobble: scrobble);

anotherResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
anotherResponse.scrobbleResponses.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

print('Multiple scrobble.');
final scrobble2 = Scrobble(track: 'Missing', artist: 'HoneyComeBear');

final lastResponse =
await scrobblenaut.track.multiScrobble(scrobbleList: [scrobble, scrobble2]);
final lastResponse = await scrobblenaut.track
.multiScrobble(scrobbleList: [scrobble, scrobble2]);

lastResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
lastResponse.scrobbleResponses.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

Expand Down
6 changes: 3 additions & 3 deletions example/methods_example/track_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() async {
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
// sessionKey: APIValues.sessionKey,
);

final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);
Expand All @@ -35,7 +35,7 @@ void main() async {

// track.getCorrection
(await scrobblenaut.track.getCorrection(track: 'TOMOYO', artist: 'Zekk'))
?.forEach((Track track) {
.forEach((Track track) {
print('Track Correction Name: ${track.name} |'
' Track Correction URL: ${track.url}');
});
Expand Down Expand Up @@ -103,7 +103,7 @@ void main() async {
timestamp: DateTime.now());
// YAY. IT WORKS!

scrobbleResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
scrobbleResponse.scrobbleResponses.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

Expand Down
10 changes: 3 additions & 7 deletions example/methods_example/user_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import '../api_values.dart';
void main() async {
print('####################################################################');

final lastFMAuth = await LastFM.authenticate(
final lastFMAuth = LastFM.noAuth(
apiKey: APIValues.API,
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
);

final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);
Expand Down Expand Up @@ -89,7 +85,7 @@ void main() async {

// user.getTopTracks
(await scrobblenaut.user.getTopTracks(user: 'nebulino'))
?.forEach((Track track) {
.forEach((Track track) {
print('Top Tracks Name: ${track.name} | Top Tracks URL: ${track.url} |'
' Duration: ${track.duration}');
});
Expand All @@ -98,7 +94,7 @@ void main() async {

// user.getWeeklyAlbumChart
(await scrobblenaut.user.getTopTracks(user: 'nebulino'))
?.forEach((Track track) {
.forEach((Track track) {
print('Top Tracks Name: ${track.name} | Top Tracks URL: ${track.url} |'
' Duration: ${track.duration}');
});
Expand Down
6 changes: 3 additions & 3 deletions example/scrobblenaut_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'api_values.dart';

// Just an example of use.
void main() async {
var lastFM = await LastFM.noAuth(
var lastFM = LastFM.noAuth(
apiKey: APIValues.API,
);

Expand All @@ -25,7 +25,7 @@ void main() async {
));

print('Album Name: ${albumGetInfo.name} |'
' Album Artist: ${albumGetInfo.artist.name}');
' Album Artist: ${albumGetInfo.artist?.name}');

albumGetInfo.tracks?.forEach((Track track) {
print('Track Title: ${track.name} | Track Duration: ${track.duration}');
Expand All @@ -35,7 +35,7 @@ void main() async {
apiKey: APIValues.API,
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password
password: APIValues.password,
);

scrobblenaut = Scrobblenaut(lastFM: lastFM);
Expand Down
61 changes: 28 additions & 33 deletions lib/src/core/lastfm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Copyright (c) 2020 Nebulino //
// //

import 'package:meta/meta.dart';
import 'package:scrobblenaut/src/core/session_key_generator.dart';
import 'package:scrobblenaut/src/helpers/utils.dart';
import 'package:scrobblenaut/src/tools/spaceship.dart';
Expand All @@ -13,41 +12,38 @@ class LastFM {
SpaceShip _client;

final String _apiKey;
final String _apiSecret;
final String _sessionKey;
final String _username;
final String _passwordHash;
final String? _apiSecret;
final String? _sessionKey;
final String? _username;
final String? _passwordHash;
final bool _isAuth;

/// Default constructor.
LastFM._(this._apiKey, this._apiSecret, this._sessionKey, this._username,
this._passwordHash, this._isAuth) {
_client = SpaceShip(
base_url: 'https://ws.audioscrobbler.com/2.0/',
);
}
this._passwordHash, this._isAuth)
: _client = SpaceShip(
base_url: 'https://ws.audioscrobbler.com/2.0/',
);

/// Default kind of API usage.
/// You can use methods that does not required authentication.
LastFM.noAuth({
@required String apiKey,
String proxy,
required String apiKey,
String? proxy,
}) : this._(apiKey, null, null, null, null, false);

/// It creates a LastFM object with auth mode.
static Future<LastFM> authenticate({
@required String apiKey,
@required String apiSecret,
@required String username,
@required String password,
String sessionKey,
String proxy,
required String apiKey,
required String apiSecret,
required String username,
required String password,
String? sessionKey,
String? proxy,
}) async {
final passwordHash = generateMD5(password);

if ((apiKey != null && apiSecret != null) &&
sessionKey == null &&
(username != null && password != null)) {
if (sessionKey == null) {
final session = await SessionKeyGenerator(
LastFM._(
apiKey,
Expand Down Expand Up @@ -83,15 +79,14 @@ class LastFM {
}

static Future<LastFM> authenticateWithPasswordHash({
@required String apiKey,
@required String apiSecret,
@required String username,
@required String passwordHash,
String sessionKey,
required String apiKey,
required String apiSecret,
required String username,
required String passwordHash,
String? sessionKey,
String? proxy,
}) async {
if ((apiKey != null && apiSecret != null) &&
sessionKey == null &&
(username != null && passwordHash != null)) {
if (sessionKey == null) {
final session = await SessionKeyGenerator(
LastFM._(
apiKey,
Expand Down Expand Up @@ -133,16 +128,16 @@ class LastFM {
String get apiKey => _apiKey;

/// It returns the apiSecret used.
String get apiSecret => _apiSecret;
String? get apiSecret => _apiSecret;

/// It returns, if authenticated, the session key.
String get sessionKey => _sessionKey;
String? get sessionKey => _sessionKey;

/// It returns, if authenticated, the username.
String get username => _username;
String? get username => _username;

/// It returns, if authenticated, the passwordHash.
String get passwordHash => _passwordHash;
String? get passwordHash => _passwordHash;

/// True if authenticated.
bool get isAuth => _isAuth;
Expand Down
Loading