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

Merged

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%

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

Some nits about the comments

example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
example_dart/lib/queued_interceptor_crsftoken.dart Outdated Show resolved Hide resolved
also, fixed the baseUrl + path combination more clearly, reflecting @AlexV525 review.

baseUrl does not contain '/' and '/' is included starting from the path.

L72 had a duplicated host, so I removed it.
@seunghwanly
Copy link
Contributor Author

Some nits about the comments

I've incorporated the suggestions from your review into 66bab27. Thank you for your feedback :)

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

Thanks both @seunghwanly and @Passer-by for your continuous contributions!

@AlexV525 AlexV525 merged commit e7edb97 into cfug:main Jan 11, 2025
1 of 3 checks passed
@seunghwanly
Copy link
Contributor Author

@AlexV525 @Passer-by

Quick question!

Since Future.wait runs its tasks in the same isolate, wouldn't 'Concurrency' be a more suitable term in this case? Additionally, the implemented code seems to handle the tasks one by one.

void _handleQueue<T, V extends _BaseHandler>(
_TaskQueue<T, V> taskQueue,
T data,
V handler,
void Function(T, V) callback,
) {
final task = _InterceptorParams<T, V>(data, handler);
task.handler._processNextInQueue = () {
if (taskQueue.queue.isNotEmpty) {
final next = taskQueue.queue.removeFirst();
assert(next.handler._processNextInQueue != null);
callback(next.data, next.handler);
} else {
taskQueue.processing = false;
}
};
taskQueue.queue.add(task);
if (!taskQueue.processing) {
taskQueue.processing = true;
final task = taskQueue.queue.removeFirst();
try {
callback(task.data, task.handler);
} catch (e) {
task.handler._processNextInQueue!();
}
}
}
}

}

// To prevent repeated requests to the 'Authentication Server'
// to update our 'access_token' with parallel requests,
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 wrote 'parallel' here

@AlexV525
Copy link
Member

I'm not sure about the word but it seems reasonable.

@Passer-by
Copy link
Contributor

Would using 'Independent Request' be better here?

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.

3 participants