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

app/test/admin/moderation_transparency_metrics_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121
String? email,
2222
String? caseId,
2323
}) async {
24-
await withHttpPubApiClient(
24+
await withRetryPubApiClient(
2525
(client) async {
2626
await client.postReport(account_api.ReportForm(
2727
email: email ?? '[email protected]',

app/test/frontend/handlers/report_test.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void main() {
7878

7979
group('Report API test', () {
8080
testWithProfile('unauthenticated email missing', fn: () async {
81-
await withHttpPubApiClient(
81+
await withRetryPubApiClient(
8282
(client) async {
8383
await expectApiException(
8484
client.postReport(ReportForm(
@@ -97,7 +97,7 @@ void main() {
9797
await withFakeAuthRequestContext('[email protected]', () async {
9898
final sessionId = requestContext.sessionData?.sessionId;
9999
final csrfToken = requestContext.csrfToken;
100-
await withHttpPubApiClient(
100+
await withRetryPubApiClient(
101101
sessionId: sessionId,
102102
csrfToken: csrfToken,
103103
(client) async {
@@ -117,7 +117,7 @@ void main() {
117117
});
118118

119119
testWithProfile('subject missing', fn: () async {
120-
await withHttpPubApiClient(
120+
await withRetryPubApiClient(
121121
(client) async {
122122
await expectApiException(
123123
client.postReport(ReportForm(
@@ -134,7 +134,7 @@ void main() {
134134
});
135135

136136
testWithProfile('subject is invalid', fn: () async {
137-
await withHttpPubApiClient(
137+
await withRetryPubApiClient(
138138
(client) async {
139139
await expectApiException(
140140
client.postReport(ReportForm(
@@ -152,7 +152,7 @@ void main() {
152152
});
153153

154154
testWithProfile('package missing', fn: () async {
155-
await withHttpPubApiClient(
155+
await withRetryPubApiClient(
156156
(client) async {
157157
await expectApiException(
158158
client.postReport(ReportForm(
@@ -170,7 +170,7 @@ void main() {
170170
});
171171

172172
testWithProfile('version missing', fn: () async {
173-
await withHttpPubApiClient(
173+
await withRetryPubApiClient(
174174
(client) async {
175175
await expectApiException(
176176
client.postReport(ReportForm(
@@ -188,7 +188,7 @@ void main() {
188188
});
189189

190190
testWithProfile('publisher missing', fn: () async {
191-
await withHttpPubApiClient(
191+
await withRetryPubApiClient(
192192
(client) async {
193193
await expectApiException(
194194
client.postReport(ReportForm(
@@ -209,7 +209,7 @@ void main() {
209209
await withFakeAuthRequestContext('[email protected]', () async {
210210
final sessionId = requestContext.sessionData?.sessionId;
211211
final csrfToken = requestContext.csrfToken;
212-
await withHttpPubApiClient(
212+
await withRetryPubApiClient(
213213
sessionId: sessionId,
214214
csrfToken: csrfToken,
215215
(client) async {
@@ -229,7 +229,7 @@ void main() {
229229
});
230230

231231
testWithProfile('unauthenticated report success', fn: () async {
232-
await withHttpPubApiClient(
232+
await withRetryPubApiClient(
233233
(client) async {
234234
final msg = await client.postReport(ReportForm(
235235
@@ -256,7 +256,7 @@ void main() {
256256
await withFakeAuthRequestContext('[email protected]', () async {
257257
final sessionId = requestContext.sessionData?.sessionId;
258258
final csrfToken = requestContext.csrfToken;
259-
await withHttpPubApiClient(
259+
await withRetryPubApiClient(
260260
sessionId: sessionId,
261261
csrfToken: csrfToken,
262262
(client) async {
@@ -307,7 +307,7 @@ void main() {
307307
}
308308

309309
testWithProfile('failure: case does not exists', fn: () async {
310-
await withHttpPubApiClient(
310+
await withRetryPubApiClient(
311311
(client) async {
312312
await expectApiException(
313313
client.postReport(ReportForm(
@@ -326,7 +326,7 @@ void main() {
326326

327327
testWithProfile('failure: case is not closed', fn: () async {
328328
await _prepareApplied(status: ModerationStatus.pending);
329-
await withHttpPubApiClient(
329+
await withRetryPubApiClient(
330330
(client) async {
331331
await expectApiException(
332332
client.postReport(ReportForm(
@@ -345,7 +345,7 @@ void main() {
345345

346346
testWithProfile('failure: subject is not on the case', fn: () async {
347347
await _prepareApplied();
348-
await withHttpPubApiClient(
348+
await withRetryPubApiClient(
349349
(client) async {
350350
await expectApiException(
351351
client.postReport(ReportForm(
@@ -370,7 +370,7 @@ void main() {
370370
);
371371

372372
// first report: success
373-
await withHttpPubApiClient(
373+
await withRetryPubApiClient(
374374
(client) async {
375375
final msg = await client.postReport(ReportForm(
376376
@@ -400,7 +400,7 @@ void main() {
400400
);
401401

402402
// second report: rejected
403-
await withHttpPubApiClient((client) async {
403+
await withRetryPubApiClient((client) async {
404404
await expectApiException(
405405
client.postReport(ReportForm(
406406
@@ -421,7 +421,7 @@ void main() {
421421
logSubject: 'package-version:oxygen/1.2.0',
422422
);
423423

424-
await withFakeAuthHttpPubApiClient(
424+
await withFakeAuthRetryPubApiClient(
425425
426426
(client) async {
427427
final msg = await client.postReport(ReportForm(

0 commit comments

Comments
 (0)