classApiRepository{
staticfinalApiRepository _instance = ApiRepository._internal(); /// singleton api repositoryApiManager _apiManager;
factoryApiRepository() {
return _instance;
}
/// base configuration for api managerApiRepository._internal() {
_apiManager = ApiManager();
_apiManager.options.baseUrl = BASE_URL; /// EX: BASE_URL = https://google.com/api/v1
_apiManager.options.connectTimeout = 100000;
_apiManager.options.receiveTimeout = 100000;
_apiManager.responseBodyWrapper('data'); /// This is used to parse the response without data attribute, some use case will shown below
_apiManager.enableLogging(responseBody: true, requestBody: false); /// enable api logging EX: response, request, headers etc
_apiManager.enableAuthTokenCheck(() => "access_token"); /// EX: JWT/PASSPORT auth token store in cache
}
}
Future<ApiResponse<SampleResponse>> getRequestSample() async =>
await _apiManager.request<SampleResponse>(
requestType: RequestType.GET,
route: 'api_route',
requestParams: {"userId": 12}, /// add params if required
isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
responseBodySerializer: (jsonMap) {
returnSampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
},
);
#Now we Performing a POST request :
Future<ApiResponse<SampleResponse>> postRequestSample() async =>
await _apiManager.request<SampleResponse>(
requestType: RequestType.POST,
route: 'api_route',
requestBody: {"userId": 12}, /// add POST request body
isAuthRequired: true, /// by set it to true, this request add a header authorization from this method enableAuthTokenCheck();
responseBodySerializer: (jsonMap) {
returnSampleResponse.fromJson(jsonMap); /// parse the json response into dart model class
},
);
#Now er performing a multipart file upload request :