Skip to content

Adds Request/Response Logging #220

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

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
23d6fa3
first attempt
hayribakici Dec 3, 2023
d0a306a
wip
hayribakici Dec 11, 2023
2331c72
next attempt
hayribakici Dec 31, 2023
ac93790
next attempt
hayribakici Dec 31, 2023
9519e85
wip
hayribakici Jan 2, 2024
a726f59
Merge branch 'master' into #186_debug_tools
hayribakici Feb 21, 2024
10d24b7
pubspec
hayribakici Feb 21, 2024
6a33448
first attempt to add interceptor
hayribakici Feb 21, 2024
9044311
wip
hayribakici Feb 23, 2024
9e6cd44
Merge branch 'master' into #186_debug_tools
hayribakici Mar 30, 2024
24818ae
wip
hayribakici Apr 5, 2024
15d14a6
wipp
hayribakici Jul 2, 2024
7e521be
wip
hayribakici Jul 5, 2024
4576435
first attempt to add level of details
hayribakici Jul 5, 2024
6c0d288
send
hayribakici Jul 6, 2024
eb4d086
Merge branch 'master' into #186_debug_tools
hayribakici Jul 6, 2024
a3ed2aa
removes interceptor
hayribakici Jul 6, 2024
8e488c8
head
hayribakici Jul 6, 2024
605918c
cleanup
hayribakici Jul 6, 2024
0de6adf
cleanup logss
hayribakici Jul 6, 2024
4cc68ea
adds name
hayribakici Jul 6, 2024
238c065
removes async
hayribakici Jul 6, 2024
7d6a272
formatting to make CI happy
hayribakici Jul 6, 2024
d4c85a2
reverse client member name
hayribakici Jul 6, 2024
3bc2d61
making dart formatter happy
hayribakici Jul 6, 2024
f77ef70
fixes tests
hayribakici Jul 6, 2024
72cdf20
allows lib users to add own logger
hayribakici Jul 7, 2024
7b0200d
format
hayribakici Jul 7, 2024
6bb7031
fixes typo
hayribakici Jul 7, 2024
275f680
adds some refactorings
hayribakici Jul 12, 2024
b0ee94f
adds better signatures
hayribakici Jul 13, 2024
f582c8e
fixes bug
hayribakici Jul 13, 2024
86f928a
removes condition
hayribakici Jul 13, 2024
387538d
formatting
hayribakici Jul 13, 2024
5e75f5f
logs only in debug mode
hayribakici Jul 17, 2024
47f307f
adds comments
hayribakici Jul 17, 2024
398663e
adds licence header
hayribakici Jul 17, 2024
1064671
Making `DefaultLogger` private
hayribakici Jul 18, 2024
c3dbe4d
making `SpotifyClient` final
hayribakici Jul 18, 2024
51aafe1
Merge branch 'master' into #186_debug_tools
hayribakici Mar 14, 2025
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
1 change: 1 addition & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void main() async {

var credentials = SpotifyApiCredentials(keyMap['id'], keyMap['secret']);
var spotify = SpotifyApi(credentials);
spotify.enableLogging(enable: true);

print('\nExpannd shortened spotify link of https://spotify.link/hRkBrwub9xb');
var longLink = await spotify.expandLink('https://spotify.link/hRkBrwub9xb');
Expand Down
6 changes: 5 additions & 1 deletion lib/spotify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ library spotify;

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:spotify/spotify.dart';
import 'package:meta/meta.dart';
import 'dart:developer';

import 'src/models/_models.dart';

export 'package:oauth2/oauth2.dart'
show AuthorizationException, ExpirationException;

export 'src/models/_models.dart';

part 'src/spotify_logger.dart';
part 'src/endpoints/albums.dart';
part 'src/endpoints/artists.dart';
part 'src/endpoints/audio_analysis.dart';
Expand All @@ -42,3 +45,4 @@ part 'src/spotify_credentials.dart';
part 'src/spotify_exception.dart';
part 'src/utils.dart';
part 'src/authorization_scope.dart';
part 'src/spotify_client.dart';
3 changes: 1 addition & 2 deletions lib/src/spotify_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class SpotifyApi extends SpotifyApiBase {
{Function(SpotifyApiCredentials)? onCredentialsRefreshed})
: super(credentials, http.Client(), onCredentialsRefreshed);

SpotifyApi.fromClient(FutureOr<oauth2.Client> super.client)
: super.fromClient();
SpotifyApi.fromClient(super.client) : super.fromClient();

SpotifyApi.fromAuthCodeGrant(super.grant, super.responseUri)
: super.fromAuthCodeGrant();
Expand Down
41 changes: 28 additions & 13 deletions lib/src/spotify_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ abstract class SpotifyApiBase {

bool _shouldWait = false;

late FutureOr<oauth2.Client> _client;
FutureOr<oauth2.Client> get client => _client;
late SpotifyClient _spotifyClient;
SpotifyClient get spotifyClient => _spotifyClient;

@visibleForTesting
FutureOr<oauth2.Client> get client => _spotifyClient._inner;

late Artists _artists;
Artists get artists => _artists;
Expand Down Expand Up @@ -62,8 +65,8 @@ abstract class SpotifyApiBase {
late Shows _shows;
Shows get shows => _shows;

SpotifyApiBase.fromClient(FutureOr<http.BaseClient> client) {
_client = client as FutureOr<oauth2.Client>;
SpotifyApiBase.fromClient(FutureOr<oauth2.Client> client) {
_spotifyClient = SpotifyClient(client);

_artists = Artists(this);
_albums = Albums(this);
Expand Down Expand Up @@ -177,6 +180,19 @@ abstract class SpotifyApiBase {
);
}

/// [enable]s logging of the requests and responses on the debug console.
/// [loggingDetail] controls the logging verbosity. Default's set
/// to [LoggingDetail.simple].
/// If required [logger] is also possible for e.g. saving logs into a file etc.
void enableLogging(
{required bool enable,
LoggingDetail loggingDetail = LoggingDetail.simple,
SpotifyLogger? logger}) {
_spotifyClient.enableLogging = enable;
_spotifyClient.loggingDetail = loggingDetail;
_spotifyClient.logger = logger;
}

/// Expands shortened spotify [url]
Future<String> expandLink(String url) async =>
_streamedHeadImpl(url, const {});
Expand All @@ -202,18 +218,18 @@ abstract class SpotifyApiBase {
return await _requestWrapper(() async {
final request = http.Request('HEAD', Uri.parse(url));
request.headers.addAll(headers);
return (await _client).send(request);
return _spotifyClient.send(request);
});
}

Future<String> _getImpl(String url, Map<String, String> headers) async {
return await _requestWrapper(() async =>
await (await _client).get(Uri.parse(url), headers: headers));
return await _requestWrapper(
() async => await _spotifyClient.get(Uri.parse(url), headers: headers));
}

Future<String> _postImpl(
String url, Map<String, String> headers, dynamic body) async {
return await _requestWrapper(() async => await (await _client)
return await _requestWrapper(() async => await _spotifyClient
.post(Uri.parse(url), headers: headers, body: body));
}

Expand All @@ -223,15 +239,14 @@ abstract class SpotifyApiBase {
final request = http.Request('DELETE', Uri.parse(url));
request.headers.addAll(headers);
request.body = body;
return await http.Response.fromStream(
await (await _client).send(request));
return await http.Response.fromStream(await _spotifyClient.send(request));
});
}

Future<String> _putImpl(
String url, Map<String, String> headers, dynamic body) async {
return await _requestWrapper(() async => await (await _client)
.put(Uri.parse(url), headers: headers, body: body));
return await _requestWrapper(() async =>
await _spotifyClient.put(Uri.parse(url), headers: headers, body: body));
}

// the reason we are using [http.BaseResponse] is because
Expand Down Expand Up @@ -267,7 +282,7 @@ abstract class SpotifyApiBase {
}

Future<SpotifyApiCredentials> getCredentials() async {
return SpotifyApiCredentials._fromClient(await _client);
return SpotifyApiCredentials._fromClient(await client);
}

String handleResponseWithBody(http.Response response) {
Expand Down
Loading