request static method
Future<Response>
request(
- String requestType,
- String url, {
- String? contentType,
- Map<String, dynamic>? queryParameters,
- dynamic body,
- Options? requestOption,
- bool isToCache = true,
- bool forceRefresh = true,
- int daysToCache = 30,
- int hoursToCache = 0,
})
Implementation
static Future<Response> request(
String requestType,
String url, {
String? contentType,
Map<String, dynamic>? queryParameters,
Map<String, dynamic>? headers,
dynamic body,
Options? requestOption,
bool isToCache = true,
bool forceRefresh = true,
int daysToCache = 30,
int hoursToCache = 0,
}) async {
initDio();
if (Apis.tokenValue.isEmpty) {
authOptions?.headers = {};
}
authOptions?.headers?["X-App-Version"] = Q.VERSION_NAME;
authOptions?.headers?["X-Os-Version"] = Platform.operatingSystemVersion;
authOptions?.headers?["X-Platform"] = Platform.operatingSystem;
if (headers != null && headers.isNotEmpty) {
headers.keys.forEach((key) {
authOptions?.headers?[key] = headers[key];
});
}
if (contentType != null)
authOptions?.headers?[Headers.contentTypeHeader] = contentType;
print("Apis.TOKEN_VALUE ${Apis.tokenValue}");
print("$url : headers : ${jsonEncode(authOptions?.headers)}");
if (body != null) Utils.printLongLine("$url : body : ${body.toString()}");
var options = isToCache
? cacheOptions.copyWith(
policy: forceRefresh
? CachePolicy.refreshForceCache
: CachePolicy.forceCache,
maxStale: Duration(days: daysToCache, hours: hoursToCache))
.toOptions()
: authOptions;
if(isToCache) {
options?.headers ??= {};
options?.headers?.addAll(authOptions!.headers!);
}
Response? response;
try {
switch (requestType) {
case REQUEST_GET:
response = await dio!.get(
url,
queryParameters: queryParameters,
options: options,
);
break;
case REQUEST_POST:
response = await dio!.post(
url,
options: requestOption ?? options,
queryParameters: queryParameters,
data: body,
onSendProgress: (int sent, int total) =>
print("$url : sent : $sent/$total"),
onReceiveProgress: (recieved, total) =>
print("$url : recieved : $recieved/$total"),
);
break;
case REQUEST_PUT:
response = await dio!.put(
url,
options: options,
queryParameters: queryParameters,
data: body,
onSendProgress: (int sent, int total) =>
print("$url : sent : $sent/$total"),
onReceiveProgress: (recieved, total) =>
print("$url : recieved : $recieved/$total"),
);
break;
case REQUEST_DELETE:
response = await dio!.delete(
url,
options: options,
queryParameters: queryParameters,
data: body,
);
break;
}
} catch (e) {
var error = e as DioError;
response = error.response;
}
printResponse(url, response);
return response!;
}