Make analyzer happy.

This commit is contained in:
David Iglesias Teixeira
2021-03-04 20:28:40 -08:00
parent 0796f293b2
commit ab9eb3b5e5
5 changed files with 41 additions and 39 deletions

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// ignore_for_file: public_member_api_docs
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -28,11 +26,13 @@ void main() {
); );
} }
/// The main widget of this demo.
class SignInDemo extends StatefulWidget { class SignInDemo extends StatefulWidget {
@override @override
State createState() => SignInDemoState(); State createState() => SignInDemoState();
} }
/// The state of the main widget.
class SignInDemoState extends State<SignInDemo> { class SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount _currentUser; GoogleSignInAccount _currentUser;
String _contactText; String _contactText;
@ -56,14 +56,16 @@ class SignInDemoState extends State<SignInDemo> {
_contactText = 'Loading contact info...'; _contactText = 'Loading contact info...';
}); });
final peopleApi = final PeopleServiceApi peopleApi =
PeopleServiceApi(await _googleSignIn.authenticatedClient()); PeopleServiceApi(await _googleSignIn.authenticatedClient());
final response = await peopleApi.people.connections.list( final ListConnectionsResponse response =
await peopleApi.people.connections.list(
'people/me', 'people/me',
personFields: 'names', personFields: 'names',
); );
final firstNamedContactName = _pickFirstNamedContact(response.connections); final String firstNamedContactName =
_pickFirstNamedContact(response.connections);
setState(() { setState(() {
if (firstNamedContactName != null) { if (firstNamedContactName != null) {
@ -77,12 +79,12 @@ class SignInDemoState extends State<SignInDemo> {
String _pickFirstNamedContact(List<Person> connections) { String _pickFirstNamedContact(List<Person> connections) {
return connections return connections
?.firstWhere( ?.firstWhere(
(person) => person.names != null, (Person person) => person.names != null,
orElse: () => null, orElse: () => null,
) )
?.names ?.names
?.firstWhere( ?.firstWhere(
(name) => name.displayName != null, (Name name) => name.displayName != null,
orElse: () => null, orElse: () => null,
) )
?.displayName; ?.displayName;

View File

@ -2,9 +2,6 @@ name: extension_google_sign_in_example
description: Example of Google Sign-In plugin and googleapis. description: Example of Google Sign-In plugin and googleapis.
dependencies: dependencies:
flutter:
sdk: flutter
google_sign_in: ^5.0.0
extension_google_sign_in_as_googleapis_auth: extension_google_sign_in_as_googleapis_auth:
# When depending on this package from a real application you should use: # When depending on this package from a real application you should use:
# extension_google_sign_in_as_googleapis_auth: ^x.y.z # extension_google_sign_in_as_googleapis_auth: ^x.y.z
@ -12,14 +9,13 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on # The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version. # the parent directory to use the current plugin's version.
path: ../ path: ../
flutter:
sdk: flutter
google_sign_in: ^5.0.0
googleapis: ^1.0.0 googleapis: ^1.0.0
dev_dependencies: dev_dependencies:
pedantic: ^1.10.0 pedantic: ^1.10.0
integration_test:
path: ../../../integration_test
flutter_driver:
sdk: flutter
flutter: flutter:
uses-material-design: true uses-material-design: true

View File

@ -6,16 +6,16 @@
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis_auth/auth.dart' as googleapis_auth; import 'package:googleapis_auth/auth.dart' as gapis;
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
/// Extension on [GoogleSignIn] that adds an `authenticatedClient` method. /// Extension on [GoogleSignIn] that adds an `authenticatedClient` method.
/// ///
/// This method can be used to retrieve an authenticated [googleapis_auth.AuthClient] /// This method can be used to retrieve an authenticated [gapis.AuthClient]
/// client that can be used with the rest of the `googleapis` libraries. /// client that can be used with the rest of the `googleapis` libraries.
extension GoogleApisGoogleSignInAuth on GoogleSignIn { extension GoogleApisGoogleSignInAuth on GoogleSignIn {
/// Retrieve a `googleapis` authenticated client. /// Retrieve a `googleapis` authenticated client.
Future<googleapis_auth.AuthClient?> authenticatedClient({ Future<gapis.AuthClient?> authenticatedClient({
@visibleForTesting GoogleSignInAuthentication? debugAuthentication, @visibleForTesting GoogleSignInAuthentication? debugAuthentication,
@visibleForTesting List<String>? debugScopes, @visibleForTesting List<String>? debugScopes,
}) async { }) async {
@ -25,17 +25,17 @@ extension GoogleApisGoogleSignInAuth on GoogleSignIn {
if (oathTokenString == null) { if (oathTokenString == null) {
return null; return null;
} }
final credentials = googleapis_auth.AccessCredentials( final gapis.AccessCredentials credentials = gapis.AccessCredentials(
googleapis_auth.AccessToken( gapis.AccessToken(
'Bearer', 'Bearer',
oathTokenString, oathTokenString,
// We don't know when the token expires, so we assume "never" // We don't know when the token expires, so we assume "never"
DateTime.now().toUtc().add(Duration(days: 365)), DateTime.now().toUtc().add(const Duration(days: 365)),
), ),
null, // We don't have a refreshToken null, // We don't have a refreshToken
debugScopes ?? this.scopes, debugScopes ?? scopes,
); );
return googleapis_auth.authenticatedClient(http.Client(), credentials); return gapis.authenticatedClient(http.Client(), credentials);
} }
} }

View File

@ -6,22 +6,21 @@
name: extension_google_sign_in_as_googleapis_auth name: extension_google_sign_in_as_googleapis_auth
description: A bridge package between google_sign_in and googleapis_auth, to create Authenticated Clients from google_sign_in user credentials. description: A bridge package between google_sign_in and googleapis_auth, to create Authenticated Clients from google_sign_in user credentials.
version: 2.0.0 version: 2.0.1
homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sign_in_as_googleapis_auth repository: https://github.com/flutter/packages/tree/master/packages/extension_google_sign_in_as_googleapis_auth
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
google_sign_in: ^5.0.0 google_sign_in: ^5.0.0
googleapis_auth: ^1.0.0 googleapis_auth: ^1.0.0
meta: ^1.3.0
http: ^0.13.0 http: ^0.13.0
meta: ^1.3.0
dev_dependencies: dev_dependencies:
pedantic: ^1.10.0
test: ^1.16.3
flutter_test: flutter_test:
sdk: flutter sdk: flutter
pedantic: ^1.10.0
environment: environment:
sdk: ">=2.12.0-259.9.beta <3.0.0" sdk: ">=2.12.0-259.9.beta <3.0.0"

View File

@ -5,37 +5,42 @@
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis_auth/auth.dart' as auth; import 'package:googleapis_auth/auth.dart' as gapis;
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart'; import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:test/fake.dart';
const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; const String SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null';
const DEBUG_FAKE_SCOPES = <String>['some-scope', 'another-scope']; const List<String> DEBUG_FAKE_SCOPES = <String>['some-scope', 'another-scope'];
const SIGN_IN_FAKE_SCOPES = <String>['some-scope', 'another-scope']; const List<String> SIGN_IN_FAKE_SCOPES = <String>[
'some-scope',
'another-scope'
];
class FakeGoogleSignIn extends Fake implements GoogleSignIn { class FakeGoogleSignIn extends Fake implements GoogleSignIn {
@override
final List<String> scopes = SIGN_IN_FAKE_SCOPES; final List<String> scopes = SIGN_IN_FAKE_SCOPES;
} }
class FakeGoogleSignInAuthentication extends Fake class FakeGoogleSignInAuthentication extends Fake
implements GoogleSignInAuthentication { implements GoogleSignInAuthentication {
@override
final String accessToken = SOME_FAKE_ACCESS_TOKEN; final String accessToken = SOME_FAKE_ACCESS_TOKEN;
} }
void main() { void main() {
GoogleSignIn signIn = FakeGoogleSignIn(); final GoogleSignIn signIn = FakeGoogleSignIn();
final authMock = FakeGoogleSignInAuthentication(); final FakeGoogleSignInAuthentication authMock =
FakeGoogleSignInAuthentication();
test('authenticatedClient returns an authenticated client', () async { test('authenticatedClient returns an authenticated client', () async {
final client = await signIn.authenticatedClient( final gapis.AuthClient client = (await signIn.authenticatedClient(
debugAuthentication: authMock, debugAuthentication: authMock,
); ))!;
expect(client, isA<auth.AuthClient>()); expect(client, isA<gapis.AuthClient>());
}); });
test('authenticatedClient uses GoogleSignIn scopes by default', () async { test('authenticatedClient uses GoogleSignIn scopes by default', () async {
final client = (await signIn.authenticatedClient( final gapis.AuthClient client = (await signIn.authenticatedClient(
debugAuthentication: authMock, debugAuthentication: authMock,
))!; ))!;
expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN));
@ -44,7 +49,7 @@ void main() {
test('authenticatedClient returned client contains the passed-in credentials', test('authenticatedClient returned client contains the passed-in credentials',
() async { () async {
final client = (await signIn.authenticatedClient( final gapis.AuthClient client = (await signIn.authenticatedClient(
debugAuthentication: authMock, debugAuthentication: authMock,
debugScopes: DEBUG_FAKE_SCOPES, debugScopes: DEBUG_FAKE_SCOPES,
))!; ))!;