ขนาดวิดีโอ: 1280 X 720853 X 480640 X 360
แสดงแผงควบคุมโปรแกรมเล่น
เล่นอัตโนมัติ
เล่นใหม่
import 'dart:convert';import 'package:http/http.dart' as http;class ApiResponse { final bool done; final String? errorMsg; final String? responseString; ApiResponse({required this.done, this.errorMsg, this.responseString});}class ApiService { static final instance = ApiService._(); ApiService._(); final String _baseUrl = 'jsonplaceholder.typicode.com'; // Private method to check response and return ApiResponse static ApiResponse _checkResponse(http.Response response) { switch (response.statusCode) { case 200: return ApiResponse( done: true, errorMsg: null, responseString: response.body); case 201: return ApiResponse( done: true, errorMsg: null, responseString: response.body); case 400: return ApiResponse( done: false, errorMsg: "Bad Request", responseString: response.body); case 401: return ApiResponse( done: false, errorMsg: "Unauthorized", responseString: null); case 403: return ApiResponse( done: false, errorMsg: "Forbidden", responseString: null); case 404: return ApiResponse( done: false, errorMsg: "Not Found", responseString: response.body); case 500: return ApiResponse( done: false, errorMsg: "Server Error", responseString: null); default: return ApiResponse( done: false, errorMsg: "Unknown Error", responseString: null); } } Future getRequest() async { try { final response = await http.get(Uri.parse('$_baseUrl/users')); return _checkResponse(response); } catch (error) { return ApiResponse( done: false, errorMsg: 'Error fetching users: $error', responseString: null); } } Future postRequest({required Map body}) async { try { final response = await http.post( Uri.parse('$_baseUrl/users'), headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: json.encode(body), ); return _checkResponse(response); } catch (error) { return ApiResponse( done: false, errorMsg: 'Error creating user: $error', responseString: null); } } Future putRequest( String userId, Map updatedUser) async { final url = Uri.parse('$_baseUrl/users/$userId'); try { final response = await http.put( url, headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: json.encode(updatedUser), ); return _checkResponse(response); } catch (error) { return ApiResponse( done: false, errorMsg: 'Error updating user: $error', responseString: null); } } Future deleteRequest(int userId) async { try { final response = await http.delete(Uri.parse('$_baseUrl/users/$userId')); return _checkResponse(response); } catch (error) { return ApiResponse( done: false, errorMsg: 'Error deleting user: $error', responseString: null); } }}
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiResponse {
final bool done;
final String? errorMsg;
final String? responseString;
ApiResponse({required this.done, this.errorMsg, this.responseString});
}
class ApiService {
static final instance = ApiService._();
ApiService._();
final String _baseUrl = 'jsonplaceholder.typicode.com';
// Private method to check response and return ApiResponse
static ApiResponse _checkResponse(http.Response response) {
switch (response.statusCode) {
case 200:
return ApiResponse(
done: true, errorMsg: null, responseString: response.body);
case 201:
return ApiResponse(
done: true, errorMsg: null, responseString: response.body);
case 400:
return ApiResponse(
done: false,
errorMsg: "Bad Request",
responseString: response.body);
case 401:
return ApiResponse(
done: false, errorMsg: "Unauthorized", responseString: null);
case 403:
return ApiResponse(
done: false, errorMsg: "Forbidden", responseString: null);
case 404:
return ApiResponse(
done: false, errorMsg: "Not Found", responseString: response.body);
case 500:
return ApiResponse(
done: false, errorMsg: "Server Error", responseString: null);
default:
return ApiResponse(
done: false, errorMsg: "Unknown Error", responseString: null);
}
}
Future getRequest() async {
try {
final response = await http.get(Uri.parse('$_baseUrl/users'));
return _checkResponse(response);
} catch (error) {
return ApiResponse(
done: false,
errorMsg: 'Error fetching users: $error',
responseString: null);
}
}
Future postRequest({required Map body}) async {
try {
final response = await http.post(
Uri.parse('$_baseUrl/users'),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: json.encode(body),
);
return _checkResponse(response);
} catch (error) {
return ApiResponse(
done: false,
errorMsg: 'Error creating user: $error',
responseString: null);
}
}
Future putRequest(
String userId, Map updatedUser) async {
final url = Uri.parse('$_baseUrl/users/$userId');
try {
final response = await http.put(
url,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: json.encode(updatedUser),
);
return _checkResponse(response);
} catch (error) {
return ApiResponse(
done: false,
errorMsg: 'Error updating user: $error',
responseString: null);
}
}
Future deleteRequest(int userId) async {
try {
final response = await http.delete(Uri.parse('$_baseUrl/users/$userId'));
return _checkResponse(response);
} catch (error) {
return ApiResponse(
done: false,
errorMsg: 'Error deleting user: $error',
responseString: null);
}
}
}