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