API SDK REST

REST needs a generic function for CRUD operations so these functions are placed in the api_base_helper.dart file under the ApiBaseHelper class. The exceptions for this API go into the api_exception.dart file.

rest_api_handler_data.dart contains all the generic functions that the app needs to access:

class RestApiHandlerData {
static ApiBaseHelper _apiBaseHelper = ApiBaseHelper();
static getData(String path) async {
final response = await _apiBaseHelper.get('$path');
return response;
}
static postData(String path, dynamic body) async {
final response = await _apiBaseHelper.post('$path', body);
return response;
}
}

For post, we need to send the path which is in api_constants.dart and a body. Whereas for get, we need to only send the path.

main.dart for API SDK contains all the functions that the app needs to access. For example if the app wants to access data for a particular user, it will call this function inside the ApiSdk class:

static getUserData(int id) async {
final response =
await RestApiHandlerData.getData('${apiConstants["auth"]}/users/$id');
return response;
}

To call this function anywhere, do the following:

ApiSdk.getUserData(id);