Skip to content

Commit

Permalink
Add auto retry gql
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Dec 6, 2020
1 parent 9bdc120 commit b910e92
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 33 deletions.
6 changes: 2 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:flutter/material.dart';
import 'package:strollplanner_tracker/config.dart';
import 'package:strollplanner_tracker/pages/root.dart';

void main() {
runApp(AppConfigWidget(
runRoot(
config: AppConfig(
flavorName: 'prod',
apiBaseApiUrl: 'https://api.stroll.pl',
appBaseApiUrl: 'https://app.stroll.pl',
),
child: root(),
));
);
}
6 changes: 2 additions & 4 deletions lib/main_staging.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:flutter/material.dart';
import 'package:strollplanner_tracker/config.dart';
import 'package:strollplanner_tracker/pages/root.dart';

void main() {
runApp(AppConfigWidget(
runRoot(
config: AppConfig(
flavorName: 'staging',
apiBaseApiUrl: 'https://api.stagingstrollpl.ovh',
appBaseApiUrl: 'https://app.stagingstrollpl.ovh',
),
child: root(),
));
);
}
26 changes: 22 additions & 4 deletions lib/pages/root.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:strollplanner_tracker/config.dart';
import 'package:strollplanner_tracker/pages/organizations.dart';
import 'package:strollplanner_tracker/pages/track.dart';
import 'package:strollplanner_tracker/pages/update.dart';
import 'package:strollplanner_tracker/services/auth.dart';
import 'package:flutter/material.dart';

Widget root() {
return ChangeNotifierProvider<AuthService>(
child: RootPage(),
create: (context) => AuthService(context),
Future runRoot({config: AppConfig}) async {
var app = AppConfigWidget(
config: config,
child: ChangeNotifierProvider<AuthService>(
child: RootPage(),
create: (context) => AuthService(context),
),
);

if (kReleaseMode) {
return SentryFlutter.init(
(options) => options
..dsn =
'https://[email protected]/5546076'
..environment = config.flavorName,
appRunner: () => runApp(app),
);
}

return runApp(app);
}

class RootPage extends StatelessWidget {
Expand Down
28 changes: 21 additions & 7 deletions lib/services/gql.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:retry/retry.dart';
import 'package:strollplanner_tracker/config.dart';
import 'package:strollplanner_tracker/services/auth.dart';

Expand Down Expand Up @@ -49,13 +52,24 @@ Future<Response<D>> request<D>(
headers['Authorization'] = 'Bearer $token';
}

final res = await http.post(
'${config.apiBaseApiUrl}/graphql',
headers: headers,
body: jsonEncode(<String, dynamic>{
'query': query,
'variables': variables,
}),
final res = await retry(
() => http
.post(
'${config.apiBaseApiUrl}/graphql',
headers: headers,
body: jsonEncode(<String, dynamic>{
'query': query,
'variables': variables,
}),
)
.timeout(Duration(seconds: 5)),
maxDelay: Duration(minutes: 1),
maxAttempts: 15,
onRetry: (e) {
print("Retry: ${e.toString()}");
},
// Retry on SocketException or TimeoutException
retryIf: (e) => e is SocketException || e is TimeoutException,
);

if (res.statusCode == 200 || res.statusCode == 422) {
Expand Down
43 changes: 29 additions & 14 deletions lib/services/tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:isolate';
import 'dart:ui';
import 'package:background_locator/background_locator.dart';
import 'package:background_locator/location_dto.dart';
import 'package:retry/retry.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:strollplanner_tracker/config.dart';
import 'package:strollplanner_tracker/services/gql.dart';
Expand Down Expand Up @@ -82,7 +84,7 @@ class LocationServiceRepository {
}

Future<void> callback(LocationDto location) async {
print('$_count location in dart: ${location.toString()}');
print('$_count location: ${location.toString()}');
final SendPort send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(LocationUpdate(location, _count));

Expand All @@ -91,26 +93,39 @@ class LocationServiceRepository {
if (_postBackend) {
print("Posting to backend");

await request(
_config,
"""
mutation (\$orgId: ID!, \$id: ID!, \$lat: Float!, \$lng: Float!, \$acc: Float!) {
var variables = {
"orgId": _orgId,
"id": _routeId,
"lat": location.latitude,
"lng": location.longitude,
"acc": location.accuracy,
"time": DateTime.fromMillisecondsSinceEpoch(location.time.round())
.toUtc()
.toIso8601String(),
};

try {
await request(
_config,
"""
mutation (\$orgId: ID!, \$id: ID!, \$lat: Float!, \$lng: Float!, \$acc: Float!, \$time: Time) {
tracker(organizationId: \$orgId, id: \$id, input: {
lat: \$lat,
lng: \$lng,
acc: \$acc,
time: \$time,
})
}
""",
(_) => null,
variables: {
"orgId": _orgId,
"id": _routeId,
"lat": location.latitude,
"lng": location.longitude,
"acc": location.accuracy,
},
token: _token);
(_) => null,
variables: variables,
token: _token);
} catch (exception, stacktrace) {
await Sentry.captureException(
exception,
stackTrace: stacktrace,
);
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.3"
package_info:
dependency: transitive
description:
name: package_info
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3+2"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -289,6 +296,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.2+2"
retry:
dependency: "direct main"
description:
name: retry
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
sentry:
dependency: transitive
description:
name: sentry
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0-beta.1"
sentry_flutter:
dependency: "direct main"
description:
name: sentry_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0-beta.1"
shared_preferences:
dependency: "direct main"
description:
Expand Down Expand Up @@ -434,6 +462,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+3"
uuid:
dependency: transitive
description:
name: uuid
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.2"
vector_math:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ dependencies:
path_provider: ^1.6.24
flutter_secure_storage: ^3.3.5
shared_preferences: ^0.5.12+4
retry: ^3.0.1
sentry_flutter: ^4.0.0-beta.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit b910e92

Please sign in to comment.