-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Hide a specific header from dio logger #315
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cbe8ca0
Hide a specific header from dio logger
Ilushnik d5aea35
Formatting new code
Ilushnik 35f0d8a
Exclude a specific header from TalkerHttpLogger
Ilushnik 45b6256
Test description change
Ilushnik 1ba0c4b
hideHeaderValuesForKeys renamed to hiddenHeaders in talker_dio_logger…
Ilushnik 47c7e9f
Merge branch 'Frezyx:master' into master
Ilushnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
packages/talker_http_logger/example/talker_http_logger_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import 'package:http_interceptor/http_interceptor.dart'; | ||
import 'package:talker_http_logger/talker_http_logger.dart'; | ||
import 'package:talker_http_logger/talker_http_logger_settings.dart'; | ||
|
||
void main(List<String> args) async { | ||
final client = InterceptedClient.build(interceptors: [ | ||
TalkerHttpLogger(), | ||
TalkerHttpLogger( | ||
settings: TalkerHttpLoggerSettings( | ||
hideHeaderValuesForKeys: {'Authorization'})), | ||
]); | ||
|
||
await client.get("https://google.com".toUri()); | ||
await client.get("https://google.com".toUri(), headers: { | ||
"firstHeader": "firstHeaderValue", | ||
"authorization": "bearer super_secret_token", | ||
"lastHeader": "lastHeaderValue", | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,26 @@ import 'dart:convert'; | |
|
||
import 'package:http_interceptor/http_interceptor.dart'; | ||
import 'package:talker/talker.dart'; | ||
import 'package:talker_http_logger/talker_http_logger_settings.dart'; | ||
|
||
class TalkerHttpLogger extends InterceptorContract { | ||
TalkerHttpLogger({Talker? talker}) { | ||
TalkerHttpLogger( | ||
{Talker? talker, this.settings = const TalkerHttpLoggerSettings()}) { | ||
_talker = talker ?? Talker(); | ||
} | ||
|
||
late Talker _talker; | ||
|
||
/// [TalkerHttpLogger] settings and customization | ||
TalkerHttpLoggerSettings settings; | ||
|
||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that this feature is same for talker_http and talker_dio but can we create separated PR with |
||
@override | ||
Future<BaseRequest> interceptRequest({ | ||
required BaseRequest request, | ||
}) async { | ||
final message = '${request.url}'; | ||
_talker.logCustom(HttpRequestLog(message, request: request)); | ||
_talker.logCustom( | ||
HttpRequestLog(message, request: request, settings: settings)); | ||
return request; | ||
} | ||
|
||
|
@@ -38,15 +44,19 @@ class TalkerHttpLogger extends InterceptorContract { | |
} | ||
|
||
const encoder = JsonEncoder.withIndent(' '); | ||
const _hiddenValue = '*****'; | ||
|
||
class HttpRequestLog extends TalkerLog { | ||
HttpRequestLog( | ||
String title, { | ||
required this.request, | ||
this.settings = const TalkerHttpLoggerSettings(), | ||
}) : super(title); | ||
|
||
final BaseRequest request; | ||
|
||
final TalkerHttpLoggerSettings settings; | ||
|
||
@override | ||
AnsiPen get pen => (AnsiPen()..xterm(219)); | ||
|
||
|
@@ -62,6 +72,15 @@ class HttpRequestLog extends TalkerLog { | |
|
||
try { | ||
if (headers.isNotEmpty) { | ||
if (settings.hideHeaderValuesForKeys.isNotEmpty) { | ||
headers.updateAll((key, value) { | ||
return settings.hideHeaderValuesForKeys | ||
.map((v) => v.toLowerCase()) | ||
.contains(key.toLowerCase()) | ||
? _hiddenValue | ||
: value; | ||
}); | ||
} | ||
final prettyHeaders = encoder.convert(headers); | ||
msg += '\nHeaders: $prettyHeaders'; | ||
} | ||
|
@@ -76,10 +95,13 @@ class HttpResponseLog extends TalkerLog { | |
HttpResponseLog( | ||
String title, { | ||
required this.response, | ||
this.settings = const TalkerHttpLoggerSettings(), | ||
}) : super(title); | ||
|
||
final BaseResponse response; | ||
|
||
final TalkerHttpLoggerSettings settings; | ||
|
||
@override | ||
AnsiPen get pen => (AnsiPen()..xterm(46)); | ||
|
||
|
@@ -111,10 +133,13 @@ class HttpErrorLog extends TalkerLog { | |
HttpErrorLog( | ||
String title, { | ||
required this.response, | ||
this.settings = const TalkerHttpLoggerSettings(), | ||
}) : super(title); | ||
|
||
final BaseResponse response; | ||
|
||
final TalkerHttpLoggerSettings settings; | ||
|
||
@override | ||
AnsiPen get pen => AnsiPen()..red(); | ||
|
||
|
9 changes: 9 additions & 0 deletions
9
packages/talker_http_logger/lib/talker_http_logger_settings.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class TalkerHttpLoggerSettings { | ||
const TalkerHttpLoggerSettings({ | ||
this.hideHeaderValuesForKeys = const <String>{}, | ||
}); | ||
|
||
/// Header values for the specified keys in the Set will be replaced with *****. | ||
/// Case insensitive | ||
final Set<String> hideHeaderValuesForKeys; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hideHeaderValuesForKeys
is huge and hard name for this fieldCan we rename it to
hiddenHeaders
?