-
Notifications
You must be signed in to change notification settings - Fork 129
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
Support @Tag #586
Support @Tag #586
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #586 +/- ##
========================================
Coverage 94.17% 94.17%
========================================
Files 11 11
Lines 481 481
========================================
Hits 453 453
Misses 28 28 ☔ View full report in Codecov by Sentry. |
Hey, Thanx for the PR, however, I'm not sure I understand what this Can you add some concrete examples and/or tests please? |
ok. i will add it later. Let me start with a small example . some path need check token to trigger refresh token when token is out of date. we can determine the path in interceptor: class TokenInterceptor extends RequestInterceptor{
request( ) {
if (path.startWith("/xxx/xx")
|| path.startWith("/xxx/xx")
|| path.startWith("/xxx/xx")
|| path.startWith("/xxx/xx")
// ...... more
){
// wait to refresh token
await ensureTokenIsValid();
request.headers.add["Auth": token];
}
}
} If those token-dependent requests are tagged, then no judgment is required。 if (request.tags["needToken"] != null){
// wait to refresh token
await ensureTokenIsValid();
request.headers.add["Auth": token];
} |
Why don't you simply catch an expired token, refresh it and repeat the call once more?
|
thx, it looks more simple,but the class AuthInterceptor implements chopper.ResponseInterceptor {
@override
FutureOr<chopper.Response<dynamic>> onResponse(chopper.Response<dynamic> response) async{
if(response.statusCode == 401){
await refreshToken();
// resend
response = await chopperClient.send<BodyType>(response.base.request as chopper.Request);
}
return response;
}
} i noticed that 👍🏻 after #547 interceptor can get type. |
It's nice to add meta information to a request but I personally never needed something like this. Like @techouse says in most cases it can be solved with a interceptor. But on the other side it can't really hurt 😄 |
hi guys, i refactored tag, make tag as a Object instead of Map<String, dynamic>. and i add my showcase in Add tag
if want to filter null value or empty String for some url. we can make an class IncludeBodyNullOrEmptyTag {
bool includeNull = false;
bool includeEmpty = false;
IncludeBodyNullOrEmptyTag(this.includeNull, this.includeEmpty);
}
@get(path: '/include')
Future<Response> includeBodyNullOrEmptyTag(
{@Tag()
IncludeBodyNullOrEmptyTag tag = const IncludeBodyNullOrEmptyTag()}); get tag via class TagConverter extends JsonConverter {
FutureOr<Request> convertRequest(Request request) {
final tag = request.tag;
if (tag is IncludeBodyNullOrEmptyTag) {
if (request.body is Map) {
final Map body = request.body as Map;
final Map bodyCopy = {};
for (final MapEntry entry in body.entries) {
if (!tag.includeNull && entry.value == null) continue;
if (!tag.includeEmpty && entry.value == "") continue;
bodyCopy[entry.key] = entry.value;
}
request = request.copyWith(body: bodyCopy);
}
}
}
} |
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.
LGTM
LGTM |
Add tag
@Tag
parameter annotation for setting tag on the underlying ChopperRequest
object. These can be readin
Converter
s orInterceptor
s for tracing, analytics, varying behavior, and more.if want to filter null value or empty String for some url. we can make an
IncludeBodyNullOrEmptyTag
Object as Tag.get tag via
request.tag
inConverter
orInterceptor
: