-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtest_integration.dart
128 lines (102 loc) · 3.07 KB
/
test_integration.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:_discoveryapis_commons/_discoveryapis_commons.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:http/http.dart';
import 'package:yaml/yaml.dart';
import 'shared.dart';
const _configFile = 'config.yaml';
Object? readConfig(String key) {
final configFile = File(_configFile);
if (!configFile.existsSync()) {
throw StateError('"$_configFile" does not exist.');
}
final yaml = loadYaml(
configFile.readAsStringSync(),
sourceUrl: Uri.parse(_configFile),
);
if (yaml is! YamlMap) {
throw StateError('"$_configFile" does not contain a Map.');
}
if (!yaml.containsKey(key)) {
throw ArgumentError('"$_configFile" does not have a value for "$key".');
}
return yaml[key];
}
Future<T> withClientFromUserCredentials<T>(
List<String> scopes,
Future<T> Function(AuthClient) action, {
Client? customClient,
}) async {
final client = customClient ?? Client();
final credentials = await _credentials(client, scopes);
try {
return await action(authenticatedClient(client, credentials));
} on DetailedApiRequestError catch (e) {
print(e);
print(prettyJsonEncode(e.jsonResponse));
rethrow;
} finally {
client.close();
}
}
Future<T> withClientFromDefaultCredentials<T>(
List<String> scopes,
Future<T> Function(AuthClient) action,
) async {
final client = await clientViaApplicationDefaultCredentials(
scopes: scopes,
);
try {
return await action(client);
} finally {
client.close();
}
}
ClientId openClientId() => ClientId.fromJson(
(loadYamlNode(File('client_id.yaml').readAsStringSync()) as Map)
.cast<String, dynamic>(),
);
Future<AccessCredentials> _credentials(
Client client,
List<String> scopes,
) async {
final clientId = openClientId();
final credentialsFile = File('credentials.json');
AccessCredentials? credentials;
if (credentialsFile.existsSync()) {
final json =
jsonDecode(credentialsFile.readAsStringSync()) as Map<String, dynamic>;
credentials = AccessCredentials.fromJson(json);
if (credentials.accessToken.hasExpired ||
!credentials.scopes.toSet().containsAll(scopes)) {
credentials = null;
stderr.writeln(
'Cached credentials have expired or the requested scopes mismatch - '
'doing auth!',
);
}
}
credentials ??= await obtainAccessCredentialsViaUserConsent(
clientId,
scopes,
client,
_prompt,
listenPort: 8181,
);
credentialsFile.writeAsStringSync(prettyJsonEncode(credentials));
stderr.writeln(
'* Credentials expire in '
'${credentials.accessToken.expiry.difference(DateTime.now())} *',
);
return credentials;
}
void _prompt(String url) {
stderr.writeln('Please go to the following URL and grant access:');
stderr.writeln(' => $url');
stderr.writeln();
}