@@ -6,6 +6,7 @@ import 'dart:async';
66import 'dart:convert' show json;
77import 'dart:io' ;
88
9+ import 'package:_pub_shared/utils/http.dart' ;
910import 'package:clock/clock.dart' ;
1011import 'package:gcloud/service_scope.dart' as ss;
1112import 'package:googleapis/iamcredentials/v1.dart' as iam_credentials;
@@ -264,12 +265,14 @@ class _GmailSmtpRelay extends EmailSenderBase {
264265 /// [_serviceAccountEmail] configured for _domain-wide delegation_ following:
265266 /// https://developers.google.com/identity/protocols/oauth2/service-account
266267 Future <String > _createAccessToken (String sender) async {
267- final iam = iam_credentials.IAMCredentialsApi (_authClient);
268268 final iat = clock.now ().toUtc ().millisecondsSinceEpoch ~ / 1000 - 20 ;
269269 iam_credentials.SignJwtResponse jwtResponse;
270270 try {
271- jwtResponse = await retry (
272- () => iam.projects.serviceAccounts.signJwt (
271+ jwtResponse = await withRetryHttpClient (client: _authClient, (
272+ client,
273+ ) async {
274+ final iam = iam_credentials.IAMCredentialsApi (client);
275+ return iam.projects.serviceAccounts.signJwt (
273276 iam_credentials.SignJwtRequest ()
274277 ..payload = json.encode ({
275278 'iss' : _serviceAccountEmail,
@@ -280,8 +283,8 @@ class _GmailSmtpRelay extends EmailSenderBase {
280283 'sub' : sender,
281284 }),
282285 'projects/-/serviceAccounts/$_serviceAccountEmail ' ,
283- ),
284- );
286+ );
287+ } );
285288 } on Exception catch (e, st) {
286289 _logger.severe (
287290 'Signing JWT for sending email failed, '
@@ -294,29 +297,24 @@ class _GmailSmtpRelay extends EmailSenderBase {
294297 );
295298 }
296299
297- final client = http.Client ();
298- try {
300+ return await withRetryHttpClient ((client) async {
299301 // Send a POST request with:
300302 // Content-Type: application/x-www-form-urlencoded; charset=utf-8
301- return await retry (() async {
302- final r = await client.post (
303- _googleOauth2TokenUrl,
304- body: {
305- 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
306- 'assertion' : jwtResponse.signedJwt,
307- },
303+ final r = await client.post (
304+ _googleOauth2TokenUrl,
305+ body: {
306+ 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
307+ 'assertion' : jwtResponse.signedJwt,
308+ },
309+ );
310+ if (r.statusCode != 200 ) {
311+ throw SmtpClientAuthenticationException (
312+ 'statusCode=${r .statusCode } from $_googleOauth2TokenUrl '
313+ 'while trying exchange JWT for access_token' ,
308314 );
309- if (r.statusCode != 200 ) {
310- throw SmtpClientAuthenticationException (
311- 'statusCode=${r .statusCode } from $_googleOauth2TokenUrl '
312- 'while trying exchange JWT for access_token' ,
313- );
314- }
315- return json.decode (r.body)['access_token' ] as String ;
316- });
317- } finally {
318- client.close ();
319- }
315+ }
316+ return json.decode (r.body)['access_token' ] as String ;
317+ });
320318 }
321319}
322320
0 commit comments