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

Fix: example of queued_interceptor_csrftoken.dart #2345

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

seunghwanly
Copy link
Contributor

@seunghwanly seunghwanly commented Dec 24, 2024

Referred to #2342

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

The example's misconception led users to misunderstand QueuedInterceptor. Also, with @Passer-by 's support, I added a token update policy.

#2342 (comment)

/example directory has been updated to /example_dart
@seunghwanly seunghwanly requested a review from a team as a code owner December 24, 2024 03:06
)}',
);
if (response.statusCode == null || response.statusCode! ~/ 100 != 2) {
return handler.reject(error);
Copy link
Contributor

Choose a reason for hiding this comment

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

tokenRefreshDio need close

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added closing lines for tokenRefreshDio

@Passer-by
Copy link
Contributor

@seunghwanly tokenRefreshDio seems to be one-time use, can it be closed after use?

@seunghwanly
Copy link
Contributor Author

tokenRefreshDio seems to be one-time use, can it be closed after use?

Apologies, I couldn't get it right. tokenRefreshDio is used in this if-condition block and closed. Could you guide me on where to fix it or point to specific lines?

if (requestedAccessToken == tokenManager.accessToken) {
final tokenRefreshDio = Dio()
..options.baseUrl = 'https://httpbun.com/';
final response = await tokenRefreshDio.post(
'https://httpbun.com/mix/s=201/b64=${base64.encode(
jsonEncode(AuthenticationServer.generate()).codeUnits,
)}',
);
if (response.statusCode == null || response.statusCode! ~/ 100 != 2) {
tokenRefreshDio.close();
return handler.reject(error);
}
final body = jsonDecode(response.data) as Map<String, Object?>;
if (!body.containsKey('access_token')) {
tokenRefreshDio.close();
return handler.reject(error);
}
final token = body['access_token'] as String;
tokenManager.setAccessToken(token, error.requestOptions.hashCode);
tokenRefreshDio.close();
}

@Passer-by
Copy link
Contributor

tokenRefreshDio好像是一次性的,用完之后可以关闭吗?

抱歉,我没能搞定。tokenRefreshDio 用在这个 if 条件块中,并且已关闭。您能指导我在哪里修复它或指出具体行吗?

if (requestedAccessToken == tokenManager.accessToken) {
final tokenRefreshDio = Dio()
..options.baseUrl = 'https://httpbun.com/';
final response = await tokenRefreshDio.post(
'https://httpbun.com/mix/s=201/b64=${base64.encode(
jsonEncode(AuthenticationServer.generate()).codeUnits,
)}',
);
if (response.statusCode == null || response.statusCode! ~/ 100 != 2) {
tokenRefreshDio.close();
return handler.reject(error);
}
final body = jsonDecode(response.data) as Map<String, Object?>;
if (!body.containsKey('access_token')) {
tokenRefreshDio.close();
return handler.reject(error);
}
final token = body['access_token'] as String;
tokenManager.setAccessToken(token, error.requestOptions.hashCode);
tokenRefreshDio.close();
}


        if (requestedAccessToken == tokenManager.accessToken) {
          final tokenRefreshDio = Dio()
            ..options.baseUrl = 'https://httpbun.com/';
          
          final response = await tokenRefreshDio.post(
            'https://httpbun.com/mix/s=201/b64=${base64.encode(
              jsonEncode(AuthenticationServer.generate()).codeUnits,
            )}',
          );

          tokenRefreshDio.close();

          if (response.statusCode == null || response.statusCode! ~/ 100 != 2) {
            return handler.reject(error);
          }

          final body = jsonDecode(response.data) as Map<String, Object?>;
          if (!body.containsKey('access_token')) {
            return handler.reject(error);
          }

          final token = body['access_token'] as String;
          tokenManager.setAccessToken(token, error.requestOptions.hashCode);
        }

Would this be simpler?

`tokenRefreshDio` closed in every condition but it could be closed right after request.
@seunghwanly
Copy link
Contributor Author

@Passer-by Thanks for the support!

Copy link
Contributor

@Passer-by Passer-by left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
Overall Coverage 🟢 84.71% 🟢 84.71% ⚪ 0%

Minimum allowed coverage is 0%, this run produced 84.71%

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