Skip to content

Commit ca71a47

Browse files
committed
Add a timeout to the API calls
1 parent 9c1dc55 commit ca71a47

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/api/schedule/api_schedule.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:convert';
23
import 'dart:core';
34

@@ -16,22 +17,26 @@ Future<Schedule> postSchedule(Schedule schedule) async {
1617
final uri = Uri.http(apiUrl, '$scheduleEndpoint');
1718
final response = await httpClient.put(uri,
1819
headers: applicationJsonHeader,
19-
body: jsonEncode(serialize<Schedule>(schedule)));
20+
body: jsonEncode(serialize<Schedule>(schedule))).timeout(timeout);
2021
_expectResponseCode(201, response);
2122
return deserialize<Schedule>(jsonDecode(response.body));
2223
}
2324

2425
Future<List<Schedule>> getSchedules() async {
2526
final uri = Uri.http(apiUrl, scheduleEndpoint);
26-
final response = await httpClient.get(uri);
27-
_expectResponseCode(200, response);
28-
// Use the compute function to run parseSchedules in a separate isolate.
29-
return compute(_parseSchedules, response.body);
27+
try {
28+
final response = await httpClient.get(uri).timeout(timeout);
29+
_expectResponseCode(200, response);
30+
// Use the compute function to run parseSchedules in a separate isolate.
31+
return compute(_parseSchedules, response.body);
32+
} on TimeoutException catch (te, stacktrace) {
33+
return Future.error(te, stacktrace);
34+
}
3035
}
3136

3237
Future<Schedule> getSchedule(String id) async {
3338
final uri = Uri.http(apiUrl, '$scheduleEndpoint/$id');
34-
final response = await httpClient.get(uri);
39+
final response = await httpClient.get(uri).timeout(timeout);
3540
_expectResponseCode(200, response);
3641
return deserialize<Schedule>(jsonDecode(response.body));
3742
}
@@ -40,14 +45,14 @@ Future<Schedule> putSchedule(Schedule schedule, String id) async {
4045
final uri = Uri.http(apiUrl, '$scheduleEndpoint/$id');
4146
final response = await httpClient.put(uri,
4247
headers: applicationJsonHeader,
43-
body: jsonEncode(serialize<Schedule>(schedule)));
48+
body: jsonEncode(serialize<Schedule>(schedule))).timeout(timeout);
4449
_expectResponseCode(200, response);
4550
return deserialize<Schedule>(jsonDecode(response.body));
4651
}
4752

4853
Future<Schedule> deleteSchedule(Schedule schedule, String id) async {
4954
final uri = Uri.http(apiUrl, '$scheduleEndpoint/$id');
50-
final response = await httpClient.delete(uri);
55+
final response = await httpClient.delete(uri).timeout(timeout);
5156
_expectResponseCode(200, response);
5257
return deserialize<Schedule>(jsonDecode(response.body));
5358
}

lib/http_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:http/http.dart' as http;
33

44

55
final http.Client httpClient = http.Client();
6+
final Duration timeout = Duration(seconds: 30);
67

78
/// The API endpoint we want to hit.
89
final String apiUrl = '10.0.2.2:8080';

0 commit comments

Comments
 (0)