Skip to content

Commit

Permalink
[master] release 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gcappon committed Feb 25, 2022
1 parent 573b57b commit 24072ad
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 49 deletions.
9 changes: 3 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
## [1.1.0] - 2021/04/11

Release 1.1.0.
Fixed issue #1 (see https://github.com/gcappon/fitbitter/issues/1).
Now tokens are stored using `flutter_secure_storage` instead of `get_it` and `shared_preferences`.
## [1.1.1] - 2022/02/25

Formatted according to `flutter format .`.

## [1.0.0] - 2021/04/11
## [1.0.0] - 2022/02/25

Release 1.0.0.
Added docs at https://gcappon.github.com/fitbitter/
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# fitbitter

<img src="https://github.com/gcappon/fitbitter/blob/gh-pages/fitbitter-logo.png" width="250" height="250">
<img src="https://i.postimg.cc/W4pq6xhF/fitbitter-logo.png" width="250" height="250">

A Flutter package to make your life easier when dealing with Fitbit APIs.

## Buy me a coffee
Expand Down
14 changes: 7 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
//This specifies the app entrypoint
initialRoute: HomePage.route,
//This maps names to the set of routes within the app
routes: {
HomePage.route: (context) => HomePage(),
},
//This specifies the app entrypoint
initialRoute: HomePage.route,
//This maps names to the set of routes within the app
routes: {
HomePage.route: (context) => HomePage(),
},
);
} //build
}//MyApp
} //MyApp
5 changes: 3 additions & 2 deletions example/lib/screens/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class HomePage extends StatelessWidget {
ElevatedButton(
onPressed: () async {
await FitbitConnector.unauthorize(
clientID: Strings.fitbitClientID,
clientSecret: Strings.fitbitClientSecret,);
clientID: Strings.fitbitClientID,
clientSecret: Strings.fitbitClientSecret,
);
},
child: Text('Tap to unauthorize'),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.0"
version: "1.1.1"
fixnum:
dependency: transitive
description:
Expand Down
52 changes: 32 additions & 20 deletions lib/src/fitbitConnector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:fitbitter/src/urls/fitbitAuthAPIURL.dart';
/// (see [FitbitConnector.isTokenValid] for more details).
class FitbitConnector {

/// The secure storage where to store the Fitbit tokens
static final storage = const FlutterSecureStorage();

Expand All @@ -37,15 +36,19 @@ class FitbitConnector {
Dio dio = Dio();
Response response;

//Get access token
final fitbitRefreshToken = await FitbitConnector.storage.read(key: 'fitbitRefreshToken');
if(fitbitRefreshToken == null){
//Get access token
final fitbitRefreshToken =
await FitbitConnector.storage.read(key: 'fitbitRefreshToken');
if (fitbitRefreshToken == null) {
return;
}//if
} //if

// Generate the fitbit url
final fitbitUrl = FitbitAuthAPIURL.refreshToken(
userID: userID, clientID: clientID, clientSecret: clientSecret, fitbitRefreshToken: fitbitRefreshToken);
userID: userID,
clientID: clientID,
clientSecret: clientSecret,
fitbitRefreshToken: fitbitRefreshToken);

// Post refresh query to Fitbit API
response = await dio.post(
Expand All @@ -66,8 +69,10 @@ class FitbitConnector {
// Overwrite the tokens into the shared preferences
final accessToken = response.data['access_token'] as String;
final refreshToken = response.data['refresh_token'] as String;
await FitbitConnector.storage.write(key: 'fitbitAccessToken', value: accessToken);
await FitbitConnector.storage.write(key: 'fitbitRefreshToken', value: refreshToken);
await FitbitConnector.storage
.write(key: 'fitbitAccessToken', value: accessToken);
await FitbitConnector.storage
.write(key: 'fitbitRefreshToken', value: refreshToken);
//GetIt.instance<SharedPreferences>()
// .setString('fitbitAccessToken', accessToken);
//GetIt.instance<SharedPreferences>()
Expand All @@ -81,13 +86,15 @@ class FitbitConnector {
Dio dio = Dio();
late Response response;

//Get access token
final fitbitAccessToken = await FitbitConnector.storage.read(key: 'fitbitAccessToken');
if(fitbitAccessToken == null){
//Get access token
final fitbitAccessToken =
await FitbitConnector.storage.read(key: 'fitbitAccessToken');
if (fitbitAccessToken == null) {
return false;
}//if
} //if

final fitbitUrl = FitbitAuthAPIURL.isTokenValid(fitbitAccessToken: fitbitAccessToken);
final fitbitUrl =
FitbitAuthAPIURL.isTokenValid(fitbitAccessToken: fitbitAccessToken);

//Get the response
try {
Expand Down Expand Up @@ -169,8 +176,10 @@ class FitbitConnector {
final refreshToken = response.data['refresh_token'] as String;
userID = response.data['user_id'] as String?;

await FitbitConnector.storage.write(key: 'fitbitAccessToken', value: accessToken);
await FitbitConnector.storage.write(key: 'fitbitRefreshToken', value: refreshToken);
await FitbitConnector.storage
.write(key: 'fitbitAccessToken', value: accessToken);
await FitbitConnector.storage
.write(key: 'fitbitRefreshToken', value: refreshToken);
//GetIt.instance<SharedPreferences>()
// .setString('fitbitAccessToken', accessToken);
//GetIt.instance<SharedPreferences>()
Expand All @@ -192,15 +201,18 @@ class FitbitConnector {

//String userID;

//Get access token
final fitbitAccessToken = await FitbitConnector.storage.read(key: 'fitbitAccessToken');
if(fitbitAccessToken == null){
//Get access token
final fitbitAccessToken =
await FitbitConnector.storage.read(key: 'fitbitAccessToken');
if (fitbitAccessToken == null) {
return;
}//if
} //if

// Generate the fitbit url
final fitbitUrl = FitbitAuthAPIURL.unauthorize(
clientSecret: clientSecret, clientID: clientID, fitbitAccessToken: fitbitAccessToken);
clientSecret: clientSecret,
clientID: clientID,
fitbitAccessToken: fitbitAccessToken);

// Post revoke query to Fitbit API
try {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/managers/fitbitDataManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ abstract class FitbitDataManager {
Dio dio = Dio();
late Response response;

final fitbitAccessToken = await FitbitConnector.storage.read(key: 'fitbitAccessToken');
final fitbitAccessToken =
await FitbitConnector.storage.read(key: 'fitbitAccessToken');
try {
// get the fitbit profile data
response = await dio.get(
fitbitUrl.url!,
options: Options(
contentType: Headers.formUrlEncodedContentType,
headers: {
'Authorization':
'Bearer ${fitbitAccessToken}',
'Authorization': 'Bearer $fitbitAccessToken',
},
),
);
Expand Down
18 changes: 10 additions & 8 deletions lib/src/urls/fitbitAuthAPIURL.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class FitbitAuthAPIURL extends FitbitAPIURL {
/// Factory constructor that generates a [FitbitAuthAPIURL] to be used
/// to refresh the access token.
factory FitbitAuthAPIURL.refreshToken(
{String? userID, String? clientID, String? clientSecret, required String fitbitRefreshToken}) {
{String? userID,
String? clientID,
String? clientSecret,
required String fitbitRefreshToken}) {
// Generate the authorization header
Codec<String, String> stringToBase64 = utf8.fuse(base64);
final String authorizationHeader =
Expand Down Expand Up @@ -90,7 +93,9 @@ class FitbitAuthAPIURL extends FitbitAPIURL {
/// Factory constructor that generates a [FitbitAuthAPIURL] to be used
/// to revoke the access and refresh tokens.
factory FitbitAuthAPIURL.unauthorize(
{String? clientID, String? clientSecret, required String fitbitAccessToken}) {
{String? clientID,
String? clientSecret,
required String fitbitAccessToken}) {
// Generate the authorization header
Codec<String, String> stringToBase64 = utf8.fuse(base64);
final String authorizationHeader =
Expand All @@ -99,8 +104,7 @@ class FitbitAuthAPIURL extends FitbitAPIURL {
return FitbitAuthAPIURL(
userID: null,
url: '${_getBaseURL()}/revoke',
data:
'token=$fitbitAccessToken',
data: 'token=$fitbitAccessToken',
authorizationHeader: 'Basic $authorizationHeader',
);
} // FitbitAuthAPIURL.unauthorize
Expand All @@ -111,10 +115,8 @@ class FitbitAuthAPIURL extends FitbitAPIURL {
return FitbitAuthAPIURL(
userID: null,
url: 'https://api.fitbit.com/1.1/oauth2/introspect',
data:
'token=$fitbitAccessToken',
authorizationHeader:
'Bearer $fitbitAccessToken',
data: 'token=$fitbitAccessToken',
authorizationHeader: 'Bearer $fitbitAccessToken',
);
} // FitbitAuthAPIURL.isTokenValid

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fitbitter
description: A Flutter package to make your life easier when dealing with Fitbit APIs.
version: 1.1.0
version: 1.1.1
homepage: https://github.com/gcappon/fitbitter

environment:
Expand Down

0 comments on commit 24072ad

Please sign in to comment.