Skip to content

Commit

Permalink
Add GIS web demo, refactor the other web samples a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Sep 20, 2022
1 parent 29967de commit 63a5782
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
18 changes: 6 additions & 12 deletions test_integration/web/drive_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import 'package:http/browser_client.dart';

import 'web_shared.dart';

final _textArea = querySelector('textarea') as TextAreaElement;

final _loginButton = querySelector('#login') as ButtonElement;
final _uploadInput = querySelector('#upload') as FileUploadInputElement;

Expand All @@ -33,14 +31,14 @@ Future<void> _login() async {
logLevel: 'debug',
);
} on AuthenticationException catch (e) {
_log(e.error);
logToTextArea(e.error);
return;
}

_loginButton.disabled = _credentials != null;
_uploadInput.disabled = _credentials == null;

_log([
logToTextArea([
'logged in!',
jsonEncode(_credentials),
].join('\n'));
Expand All @@ -50,7 +48,7 @@ Future<void> _upload() async {
final files = _uploadInput.files;

if (files == null) {
_log('no file!');
logToTextArea('no file!');
return;
}

Expand All @@ -65,24 +63,20 @@ Future<void> _upload() async {
final options = drive.UploadOptions.resumable;
final file = files.single;

_log('starting upload');
logToTextArea('starting upload');

final newFile = await api.create(
drive.File(name: 'Google APIs test file on ${DateTime.now()}'),
uploadMedia: drive.Media(file.read(options.chunkSize), file.size),
uploadOptions: options,
);

_log(jsonEncode(newFile));
logToTextArea(jsonEncode(newFile));
} finally {
client.close();
}
}

void _log(Object value) {
_textArea.text = '$value\n\n\n${_textArea.text!}';
}

extension on File {
Stream<Uint8List> read(int chunkSize) async* {
if (chunkSize <= 0) {
Expand All @@ -99,7 +93,7 @@ extension on File {
final bytes = reader.result as Uint8List;
loaded += bytes.length;
yield bytes;
_log(
logToTextArea(
'Finished $loaded of $size - '
'${(100 * loaded / size).toStringAsFixed(2)}%',
);
Expand Down
58 changes: 58 additions & 0 deletions test_integration/web/google_identity_demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'dart:html' show ButtonElement, querySelector;

import 'package:googleapis_auth/auth_browser.dart';

import 'web_shared.dart';

String? _accessToken;

final _revokeButton = querySelector('#revoke') as ButtonElement;

void main() {
final loginButton = querySelector('#login') as ButtonElement;
loginButton.onClick.listen((event) => _login());

final loginCodeButton = querySelector('#loginCode') as ButtonElement;
loginCodeButton.onClick.listen((event) => _loginCode());

_revokeButton.onClick.listen((event) async {
if (_accessToken == null) return;

await revokeConsent(_accessToken!);

_accessToken = null;
});
}

Future<void> _login() async {
final response = await requestAccessCredentials(
clientId: clientId().identifier,
scopes: ['openid', 'email'],
prompt: 'consent',
// ignore: deprecated_member_use
logLevel: 'debug',
);

logToTextArea([
response.accessToken.toJson(),
response.scopes,
].join('\n'));

_accessToken = response.accessToken.data;

_revokeButton.disabled = false;
}

Future<void> _loginCode() async {
final response = await requestAuthorizationCode(
clientId: clientId().identifier,
scopes: ['openid', 'email'],
// ignore: deprecated_member_use
logLevel: 'debug',
);

logToTextArea([
response.code,
response.scopes,
].join('\n'));
}
17 changes: 17 additions & 0 deletions test_integration/web/google_identity_demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<script src="google_identity_demo.dart.js" defer async ></script>
</head>
<body>

<p>
<button id="login">login</button></p>
<button id="loginCode">login with code</button></p>

<button id="revoke" disabled>revoke</button></p>

<textarea cols="80" rows="30"> </textarea>

</body>
</html>
10 changes: 2 additions & 8 deletions test_integration/web/implicit_browser_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'web_shared.dart';

late final BrowserOAuth2Flow _flow;

final _textArea = querySelector('textarea') as TextAreaElement;

Future<void> main() async {
_flow = await createImplicitBrowserFlow(
clientId(),
Expand All @@ -28,7 +26,7 @@ Future<void> main() async {
Future<void> _hybridFlow() async {
final creds = await _flow.runHybridFlow();

_log([
logToTextArea([
'cred json',
prettyJsonEncode(creds.credentials),
'',
Expand All @@ -46,12 +44,8 @@ Future<void> _login() async {
],
);

_log([
logToTextArea([
'cred json',
prettyJsonEncode(creds),
].join('\n'));
}

void _log(String value) {
_textArea.text = value;
}
3 changes: 2 additions & 1 deletion test_integration/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<body>

<ul>
<li><a href="implicit_browser_flow.html">Demo of DEPRECATED Google browser auth APIs</a></li>
<li><a href="drive.html">Demo of Google Drive APIs</a></li>
<li><a href="google_identity_demo.html">Demo of Google Identity Services (GIS) browser auth APIs</a></li>
<li><a href="implicit_browser_flow.html">Demo of DEPRECATED Google browser auth APIs</a></li>
</ul>

</body>
Expand Down
8 changes: 8 additions & 0 deletions test_integration/web/web_shared.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:html';

import 'package:googleapis_auth/auth_browser.dart';

const _clientId = String.fromEnvironment('clientId');
Expand All @@ -8,3 +10,9 @@ ClientId clientId() {
}
return ClientId(_clientId);
}

final _textArea = querySelector('textarea') as TextAreaElement;

void logToTextArea(Object value) {
_textArea.text = '$value\n\n\n${_textArea.text!}';
}

0 comments on commit 63a5782

Please sign in to comment.