2019-04-22 17:16:26 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
/// API response
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class APIResponse {
|
2022-03-10 18:39:57 +00:00
|
|
|
final int code;
|
|
|
|
final String? content;
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
const APIResponse(this.code, this.content);
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
List<dynamic>? getArray() => jsonDecode(this.content!);
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
Map<String, dynamic> getObject() => jsonDecode(this.content!);
|
2019-05-18 16:48:12 +00:00
|
|
|
|
|
|
|
/// Check if the request is successful or not
|
|
|
|
bool get isOK => code == 200;
|
2020-04-15 10:04:19 +00:00
|
|
|
|
|
|
|
/// Make sure the request succeed, or throw an exception else
|
|
|
|
APIResponse assertOk() {
|
|
|
|
|
|
|
|
if(!this.isOK)
|
2020-04-30 11:32:22 +00:00
|
|
|
throw Exception("Request failed with status $code -> $content");
|
2020-04-15 10:04:19 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
}
|