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

Support handling 304 status code in onResponse flow #119

Closed

Conversation

tristan-vrt
Copy link

In Dio a 304 not modified can be configured to be a valid status:
BaseOptions(validateStatus: (status) => status != null && ((status >= 200 && status < 300) || status == 304))

This PR allows 304's to be handled in the onResponse flow.
This way (having not modified as a valid status) the usual chain of onResponse interceptors will still trigger, unlike the error flow.

@@ -30,6 +30,16 @@ class DioCacheInterceptor extends Interceptor {

final cacheOptions = _getCacheOptions(options);

if (cacheOptions.callResponseInterceptorsAfterNotModified) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this? Can we get rid of callResponseInterceptorsAfterNotModified?
If RequestOptions has a custom validateStatus which allows 304 as valid there should be nothing to do here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because using validateStatus to validate 304's is not transparent to me. You would have to look into the source code of this cache interceptor to know how 304's are handled. Ofcourse this is more an issue with dio (handling not valid responses in onError and never going back to the onResponse flow) and it breaks every other response interceptor.

@@ -83,6 +93,29 @@ class DioCacheInterceptor extends Interceptor {
return;
}

// Check if cache response should be used instead of server response
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't duplicate code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I will create a method for this.

@tristan-vrt
Copy link
Author

Updated Pull request with master and requested changes.

@tristan-vrt
Copy link
Author

I forgot to remove the field callResponseInterceptorsAfterNotModified. Updated my PR ☝️

@llfbandit
Copy link
Owner

Thanks for your contribution. This PR has been reworked internally and is now available in v3.5.0.

@llfbandit llfbandit closed this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants