Skip to content

Commit

Permalink
adds some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
hayribakici committed Jul 12, 2024
1 parent 6bb7031 commit 275f680
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/src/spotify_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ abstract class SpotifyApiBase {
/// Use own [logger] is also possible for e.g. saving logs into a file etc.
void enableLogging(bool enable,
{LoggingDetail loggingDetail = LoggingDetail.simple, Logger? logger}) {
_spotifyClient.enableLogging(enable, logger: logger);
_spotifyClient.enableLogging(enable);
_spotifyClient.loggingDetail = loggingDetail;
_spotifyClient.logger = logger;
}

/// Expands shortened spotify [url]
Expand Down
45 changes: 27 additions & 18 deletions lib/src/spotify_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ part of '../spotify.dart';
class SpotifyClient with http.BaseClient {
final FutureOr<oauth2.Client> _inner;

late Logger _logger;

bool _enableLogging = false;
void enableLogging(bool enable, {Logger? logger}) {

/// [entable]s logging of the reuqests and responses with the Spotify-API.
void enableLogging(bool enable) {
_enableLogging = enable;
_logger = logger ?? Logger();
}

late Logger _logger;
set logger(value) {
// don't allow to initialize a Logger when logging is disabled.
if (!_enableLogging) {
throw StateError('[enableLogging] must be set to [true]');
}
logger = value ?? Logger();
}

LoggingDetail _detail = LoggingDetail.simple;

/// Retrieve how verbose the logging currently is. Default's to [LoggingDetail.simple].
get loggingDetail => _detail;

/// Set the level of verbosity of the logging.
set loggingDetail(value) => _detail = value;

SpotifyClient(this._inner);
Expand All @@ -39,7 +51,6 @@ class SpotifyClient with http.BaseClient {
if (!_enableLogging) {
return await (await _inner).get(url, headers: headers);
}
var output = StringBuffer();
try {
// Log GET request details
_logger.i(_logString('🚀 🌐 GET Request 🌐 🚀', url));
Expand All @@ -56,9 +67,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log GET error
output.writeln('❌ ❗ GET Request Failed ❗ ❌');
output.writeln('❗ Error Message: $error');
_logger.e(output);
_logger.e(_logError('GET', error));
rethrow; // Rethrow the error after logging
}
}
Expand Down Expand Up @@ -136,14 +145,11 @@ class SpotifyClient with http.BaseClient {
return await (await _inner)
.delete(url, headers: headers, body: body, encoding: encoding);
}
var output = StringBuffer();
try {
// Log delete request details
_logger.i(_logString('🚀 🌐 DELETE Request 🌐 🚀', url,
headers: headers, body: body));

output.clear();

// Perform the delete request
final response = await (await _inner)
.delete(url, headers: headers, body: body, encoding: encoding);
Expand All @@ -155,7 +161,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log delete error
_logger.e('❌ ❗ DELETE ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('DELETE', error));
rethrow; // Rethrow the error after logging
}
}
Expand Down Expand Up @@ -185,7 +191,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log post error
_logger.e('❌ ❗ POST ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('POST', error));
rethrow; // Rethrow the error after logging
}
}
Expand Down Expand Up @@ -215,7 +221,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log patch error
_logger.e('❌ ❗ PATCH ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('PATCH', error));
rethrow; // Rethrow the error after logging
}
}
Expand Down Expand Up @@ -245,7 +251,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log put error
_logger.e('❌ ❗ PUT ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('PUT', error));
rethrow; // Rethrow the error after logging
}
}
Expand All @@ -269,7 +275,7 @@ class SpotifyClient with http.BaseClient {
return response;
} catch (error) {
// Log head error
_logger.e('❌ ❗ HEAD ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('HEAD', error));
rethrow; // Rethrow the error after logging
}
}
Expand All @@ -293,7 +299,7 @@ class SpotifyClient with http.BaseClient {
return response.body;
} catch (error) {
// Log read error
_logger.e('❌ ❗ READ ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError("READ", error));
rethrow; // Rethrow the error after logging
}
}
Expand All @@ -320,7 +326,7 @@ class SpotifyClient with http.BaseClient {
return response.bodyBytes;
} catch (error) {
// Log readBytes error
_logger.e('❌ ❗ ReadBytes ERROR ❗ ❌\n❗ Error Message: $error');
_logger.e(_logError('ReadBytes', error));
rethrow; // Rethrow the error after logging
}
}
Expand Down Expand Up @@ -362,6 +368,9 @@ class SpotifyClient with http.BaseClient {
}
}

String _logError(String request, Object error) =>
'❌ ❗ $request Request Failed ❗ ❌\n❗ Error Message: $error';

/// Sets how much information is displayed in the http logging
enum LoggingDetail {
/// Simple tier logging:
Expand Down

0 comments on commit 275f680

Please sign in to comment.