-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.dart
54 lines (44 loc) · 1.52 KB
/
api.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 'dart:io';
import 'opensource_platform.dart';
import 'successful_status.dart';
import '../core/errors/api_exceptions.dart';
import '../core/http_client/http_client_interface.dart';
import '../parser/todo.dart';
import '../utils/configuration.dart';
class API {
IOpenSourcePlatform _openSourcePlatform;
API(this._openSourcePlatform);
set openSourcePlatform(IOpenSourcePlatform openSourcePlatform) {
_openSourcePlatform = openSourcePlatform;
}
Future<SuccessfulStatus> createIssues(
List<Todo> todos, Configuration configuration) async {
int issueCounter = 0;
for (Todo todo in todos) {
if (!todo.wasPosted) {
HttpResponse response =
await _openSourcePlatform.createIssue(todo, configuration);
if (response.statusCode != 201) handleErrorStatusCode(response);
issueCounter++;
sleep(Duration(seconds: 2));
}
}
return SuccessfulStatus(issueCounter);
}
void handleErrorStatusCode(HttpResponse response) {
switch (response.statusCode) {
case 401:
case 404:
throw InvalidCredentials(
"Make sure your credentials from 'todo.json' are correct and valid");
case 503:
throw ServiceUnavaiable("Service unavaiable. Please try again later.");
case 422:
throw SpammedCommand(
"Validation failed or this command was probably spammed");
default:
// TODO(hicaro): improving error reporting for this case
throw UnexpectedError(response.body.toString());
}
}
}