makeApiCall method
Future<ApiCallResponse>
makeApiCall(
{ - required String callName,
- required String apiUrl,
- required ApiCallType callType,
- Map<String, dynamic> params = const {},
- String? body,
- BodyType? bodyType,
- bool returnBody = true,
- bool encodeBodyUtf8 = false,
- bool decodeUtf8 = false,
- bool cache = false,
})
Implementation
Future<ApiCallResponse> makeApiCall({
required String callName,
required String apiUrl,
required ApiCallType callType,
Map<String, dynamic> headers = const {},
Map<String, dynamic> params = const {},
String? body,
BodyType? bodyType,
bool returnBody = true,
bool encodeBodyUtf8 = false,
bool decodeUtf8 = false,
bool cache = false,
}) async {
final callRecord =
ApiCallRecord(callName, apiUrl, headers, params, body, bodyType);
// Modify for your specific needs if this differs from your API.
if (_accessToken != null) {
headers[HttpHeaders.authorizationHeader] = 'Token $_accessToken';
}
if (!apiUrl.startsWith('http')) {
apiUrl = 'https://$apiUrl';
}
// If we've already made this exact call before and caching is on,
// return the cached result.
if (cache && _apiCache.containsKey(callRecord)) {
return _apiCache[callRecord]!;
}
ApiCallResponse result;
switch (callType) {
case ApiCallType.GET:
case ApiCallType.DELETE:
result = await urlRequest(
callType,
apiUrl,
headers,
params,
returnBody,
decodeUtf8,
);
break;
case ApiCallType.POST:
case ApiCallType.PUT:
case ApiCallType.PATCH:
result = await requestWithBody(
callType,
apiUrl,
headers,
params,
body,
bodyType,
returnBody,
encodeBodyUtf8,
decodeUtf8,
);
break;
}
// If caching is on, cache the result (if present).
if (cache) {
_apiCache[callRecord] = result;
}
return result;
}