Skip to content

Commit a5dc0d2

Browse files
authored
Refactor of the withHttpPubApiClient method(s). (#8495)
1 parent 74d9811 commit a5dc0d2

14 files changed

+82
-67
lines changed

app/lib/fake/backend/fake_auth_provider.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,20 @@ Future<String> _acquireCsrfToken({
416416

417417
/// Creates a pub.dev API client and executes [fn], making sure that the HTTP
418418
/// resources are freed after the callback finishes.
419+
/// The callback [fn] is retried on the transient network errors.
419420
///
420421
/// The [email] is used to create an HTTP session and the related CSRF token is
421422
/// extracted from the session, both are sent alongside the requests.
422-
Future<R> withFakeAuthHttpPubApiClient<R>(
423+
Future<R> withFakeAuthRetryPubApiClient<R>(
423424
Future<R> Function(PubApiClient client) fn, {
424425
required String email,
425426
List<String>? scopes,
427+
428+
/// The base URL of the pub server.
426429
String? pubHostedUrl,
427-
Set<String>? experimental,
430+
431+
/// The enabled experiments that will be part of the experimental cookie.
432+
Set<String>? experiments,
428433
}) async {
429434
final sessionId = await _acquireFakeSessionId(
430435
email: email,
@@ -436,11 +441,11 @@ Future<R> withFakeAuthHttpPubApiClient<R>(
436441
pubHostedUrl: pubHostedUrl,
437442
);
438443

439-
return await withHttpPubApiClient(
444+
return await withRetryPubApiClient(
440445
sessionId: sessionId,
441446
csrfToken: csrfToken,
442447
pubHostedUrl: pubHostedUrl,
443-
experimental: experimental,
448+
experiments: experiments,
444449
fn,
445450
);
446451
}

app/lib/tool/test_profile/importer.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Future<void> importProfile({
4545
// create publishers
4646
for (final p in profile.publishers) {
4747
final firstMemberEmail = p.members.first.email;
48-
await withFakeAuthHttpPubApiClient(
48+
await withFakeAuthRetryPubApiClient(
4949
email: firstMemberEmail,
5050
scopes: [webmasterScope],
5151
pubHostedUrl: pubHostedUrl,
@@ -94,8 +94,8 @@ Future<void> importProfile({
9494
await source.getArchiveBytes(rv.package, rv.version);
9595
bytes = await _mayCleanupTarModeBits(bytes);
9696
try {
97-
await withHttpPubApiClient(
98-
bearerToken: createFakeAuthTokenForEmail(uploaderEmail,
97+
await withRetryPubApiClient(
98+
authToken: createFakeAuthTokenForEmail(uploaderEmail,
9999
audience: activeConfiguration.pubClientAudience),
100100
pubHostedUrl: pubHostedUrl,
101101
(client) => client.uploadPackageBytes(bytes),
@@ -120,7 +120,7 @@ Future<void> importProfile({
120120
final packageName = testPackage.name;
121121
final activeEmail = lastActiveUploaderEmails[packageName];
122122

123-
await withFakeAuthHttpPubApiClient(
123+
await withFakeAuthRetryPubApiClient(
124124
email: activeEmail!,
125125
pubHostedUrl: pubHostedUrl,
126126
(client) async {
@@ -151,8 +151,8 @@ Future<void> importProfile({
151151
);
152152

153153
if (testPackage.isFlutterFavorite ?? false) {
154-
await withHttpPubApiClient(
155-
bearerToken:
154+
await withRetryPubApiClient(
155+
authToken:
156156
createFakeServiceAccountToken(email: adminUserEmail ?? activeEmail),
157157
pubHostedUrl: pubHostedUrl,
158158
(client) async {
@@ -170,7 +170,7 @@ Future<void> importProfile({
170170
final createLikeCounts = <String, int>{};
171171
// create users
172172
for (final u in profile.users) {
173-
await withFakeAuthHttpPubApiClient(
173+
await withFakeAuthRetryPubApiClient(
174174
email: u.email,
175175
pubHostedUrl: pubHostedUrl,
176176
(client) async {
@@ -194,7 +194,7 @@ Future<void> importProfile({
194194

195195
for (var i = 0; i < likesMissing; i++) {
196196
final userEmail = 'like-$i@pub.dev';
197-
await withFakeAuthHttpPubApiClient(
197+
await withFakeAuthRetryPubApiClient(
198198
email: userEmail,
199199
pubHostedUrl: pubHostedUrl,
200200
(client) async {

app/lib/tool/utils/pub_api_client.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,32 @@ class _FakeTimeClient implements http.Client {
9696
/// resources are freed after the callback finishes.
9797
/// The callback [fn] is retried on the transient network errors.
9898
///
99-
/// If [bearerToken], [sessionId] or [csrfToken] is specified, the corresponding
99+
/// If [authToken], [sessionId] or [csrfToken] is specified, the corresponding
100100
/// HTTP header will be sent alongside the request.
101-
Future<R> withHttpPubApiClient<R>(
101+
Future<R> withRetryPubApiClient<R>(
102+
/// The callback function that may be retried on transient errors.
102103
Future<R> Function(PubApiClient client) fn, {
103-
String? bearerToken,
104+
/// The token to use as the `Authorization` header in the format of `Bearer <token>`.
105+
String? authToken,
106+
107+
/// The session id that will be part of the session cookie.
104108
String? sessionId,
109+
110+
/// The CSRF token that will be the value of the CSRF header (`x-pub-csrf-token`).
105111
String? csrfToken,
112+
113+
/// The base URL of the pub server.
106114
String? pubHostedUrl,
107-
Set<String>? experimental,
115+
116+
/// The enabled experiments that will be part of the experimental cookie.
117+
Set<String>? experiments,
108118
}) async {
109119
final httpClient = httpClientWithAuthorization(
110-
tokenProvider: () async => bearerToken,
120+
tokenProvider: () async => authToken,
111121
sessionIdProvider: () async => sessionId,
112122
csrfTokenProvider: () async => csrfToken,
113123
cookieProvider: () async => {
114-
if (experimental != null) experimentalCookieName: experimental.join(':'),
124+
if (experiments != null) experimentalCookieName: experiments.join(':'),
115125
},
116126
client: http.Client(),
117127
);

app/test/account/consent_backend_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main() {
2525
group('Uploader invite', () {
2626
Future<String?> inviteUploader(
2727
{String adminEmail = '[email protected]'}) async {
28-
await withFakeAuthHttpPubApiClient(
28+
await withFakeAuthRetryPubApiClient(
2929
email: adminEmail,
3030
pubHostedUrl: activeConfiguration.primarySiteUri.toString(),
3131
(client) async {
@@ -309,7 +309,7 @@ void main() {
309309

310310
group('Sanity check', () {
311311
testWithProfile('consent parameter length', fn: () async {
312-
await withFakeAuthHttpPubApiClient(email: adminAtPubDevEmail, (c) async {
312+
await withFakeAuthRetryPubApiClient(email: adminAtPubDevEmail, (c) async {
313313
await expectApiException(
314314
c.consentInfo('abcd' * 500),
315315
status: 400,

app/test/admin/exported_api_sync_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ void main() {
1919
List<String>? packages,
2020
bool forceWrite = false,
2121
}) async {
22-
await withHttpPubApiClient(
23-
bearerToken: siteAdminToken,
22+
await withRetryPubApiClient(
23+
authToken: siteAdminToken,
2424
(api) async {
2525
await api.adminInvokeAction(
2626
'exported-api-sync',

app/test/admin/moderate_package_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import '../shared/test_services.dart';
3333
void main() {
3434
group('Moderate package', () {
3535
Future<ModerationCase> _report(String package) async {
36-
await withHttpPubApiClient(
36+
await withRetryPubApiClient(
3737
(client) async {
3838
await client.postReport(ReportForm(
3939

app/test/admin/moderate_package_version_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import '../shared/test_services.dart';
3333
void main() {
3434
group('Moderate package version', () {
3535
Future<ModerationCase> _report(String package, String version) async {
36-
await withHttpPubApiClient(
36+
await withRetryPubApiClient(
3737
(client) async {
3838
await client.postReport(ReportForm(
3939

app/test/admin/moderate_publisher_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import '../shared/test_services.dart';
2424
void main() {
2525
group('Moderate Publisher', () {
2626
Future<ModerationCase> _report(String publisherId) async {
27-
await withHttpPubApiClient(
27+
await withRetryPubApiClient(
2828
(client) async {
2929
await client.postReport(ReportForm(
3030

app/test/admin/moderate_user_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import '../shared/test_services.dart';
2828
void main() {
2929
group('Moderate User', () {
3030
Future<ModerationCase> _report(String package) async {
31-
await withHttpPubApiClient(
31+
await withRetryPubApiClient(
3232
(client) async {
3333
await client.postReport(account_api.ReportForm(
3434

app/test/admin/moderation_case_resolve_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main() {
1818
String? appealCaseId,
1919
required bool? apply,
2020
}) async {
21-
await withHttpPubApiClient(
21+
await withRetryPubApiClient(
2222
(client) async {
2323
await client.postReport(ReportForm(
2424

0 commit comments

Comments
 (0)