refactor: Clean up ApiClient code structure and improve readability
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
@@ -31,11 +31,7 @@ class ApiClient {
|
||||
return _decodeOrNull(_guardResponse(response));
|
||||
}
|
||||
|
||||
Future<dynamic> postJson(
|
||||
String path, {
|
||||
Object? body,
|
||||
String? token,
|
||||
}) async {
|
||||
Future<dynamic> postJson(String path, {Object? body, String? token}) async {
|
||||
final response = await _client.post(
|
||||
Uri.parse('$baseUrl$path'),
|
||||
headers: _headers(token: token),
|
||||
@@ -44,11 +40,7 @@ class ApiClient {
|
||||
return _decodeOrNull(_guardResponse(response));
|
||||
}
|
||||
|
||||
Future<dynamic> putJson(
|
||||
String path, {
|
||||
Object? body,
|
||||
String? token,
|
||||
}) async {
|
||||
Future<dynamic> putJson(String path, {Object? body, String? token}) async {
|
||||
final response = await _client.put(
|
||||
Uri.parse('$baseUrl$path'),
|
||||
headers: _headers(token: token),
|
||||
@@ -57,11 +49,7 @@ class ApiClient {
|
||||
return _decodeOrNull(_guardResponse(response));
|
||||
}
|
||||
|
||||
Future<dynamic> patchJson(
|
||||
String path, {
|
||||
Object? body,
|
||||
String? token,
|
||||
}) async {
|
||||
Future<dynamic> patchJson(String path, {Object? body, String? token}) async {
|
||||
final response = await _client.patch(
|
||||
Uri.parse('$baseUrl$path'),
|
||||
headers: _headers(token: token),
|
||||
@@ -69,6 +57,18 @@ class ApiClient {
|
||||
);
|
||||
return _decodeOrNull(_guardResponse(response));
|
||||
}
|
||||
|
||||
Future<dynamic> deleteJson(String path, {String? token}) async {
|
||||
final response = await _client.delete(
|
||||
Uri.parse('$baseUrl$path'),
|
||||
headers: _headers(token: token),
|
||||
);
|
||||
return _decodeOrNull(_guardResponse(response));
|
||||
}
|
||||
|
||||
http.Response _guardResponse(http.Response response) {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
return response;
|
||||
}
|
||||
|
||||
final parsedBody = _decodeOrNull(response);
|
||||
@@ -107,10 +107,7 @@ class ApiClient {
|
||||
|
||||
dynamic _decodeOrNull(http.Response response) {
|
||||
final body = response.body.trim();
|
||||
if (body.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (body.isEmpty) return null;
|
||||
try {
|
||||
return jsonDecode(body);
|
||||
} catch (_) {
|
||||
@@ -121,20 +118,14 @@ class ApiClient {
|
||||
String? _extractMessage(dynamic parsedBody) {
|
||||
if (parsedBody is Map<String, dynamic>) {
|
||||
final message = parsedBody['message'];
|
||||
if (message is String && message.trim().isNotEmpty) {
|
||||
return message;
|
||||
}
|
||||
if (message is String && message.trim().isNotEmpty) return message;
|
||||
if (message is List && message.isNotEmpty) {
|
||||
return message.first.toString();
|
||||
}
|
||||
final error = parsedBody['error'];
|
||||
if (error is String && error.trim().isNotEmpty) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
if (parsedBody is String && parsedBody.trim().isNotEmpty) {
|
||||
return parsedBody;
|
||||
if (error is String && error.trim().isNotEmpty) return error;
|
||||
}
|
||||
if (parsedBody is String && parsedBody.trim().isNotEmpty) return parsedBody;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user