-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreport_news_command.dart
52 lines (45 loc) · 1.46 KB
/
report_news_command.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
// Dart imports:
import 'dart:convert';
// Package imports:
import 'package:http/http.dart' as http;
// Project imports:
import 'package:notredame/features/ets/events/api-client/hello_api_client.dart';
import 'package:notredame/features/ets/events/api-client/models/report.dart';
import 'package:notredame/utils/command.dart';
import 'package:notredame/utils/http_exception.dart';
/// Call the Hello API to report a news
/// [newsId] The news id
/// [report] The report
class ReportNewsCommand implements Command<bool> {
final HelloAPIClient client;
final http.Client _httpClient;
final String newsId;
final Report report;
ReportNewsCommand(this.client, this._httpClient, this.newsId, this.report);
@override
Future<bool> execute() async {
if (client.apiLink == null || client.apiLink!.isEmpty) {
throw ArgumentError("_apiLink is null or empty");
}
final uri = Uri.https(client.apiLink!, '/api/reports/$newsId');
final response = await _httpClient.post(
uri,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'reason': report.reason,
'category': report.category,
}),
);
// Log the http error and throw a exception
if (response.statusCode != 200) {
throw HttpException(
message: response.body,
prefix: HelloAPIClient.tagError,
code: response.statusCode,
);
}
return true;
}
}