createRequest method
Implementation
Future<Request> createRequest(SendWebContext info) async {
var url = info.url ?? info.uri?.toString();
var method = info.method;
var request = info.request;
var body = info.body ?? request;
var args = info.args;
if (url == null || url == '') {
var bodyNotRequestDto = info.request != null && info.body != null;
if (bodyNotRequestDto) {
url = combinePaths([this.replyBaseUrl, nameOf(request)]);
url = appendQueryString(url, toMap(request));
} else {
url = createUrlFromDto(method, request);
}
}
if (url == null) throw ArgumentError.notNull('url');
if (args != null) url = appendQueryString(url, args);
String? bodyStr = null;
if (hasRequestBody(method)) {
if (body is String) {
bodyStr = body;
} else {
bodyStr = json.encode(body);
}
}
Request? req;
Exception? firstEx;
var uri = info.uri ?? createUri(url);
for (var attempt = 0; attempt < maxRetries; attempt++) {
try {
req = Request(method, uri!);
break;
} on Exception catch (e, trace) {
Log.debug("createRequest(): $e\n$trace");
if (firstEx == null) {
firstEx = e;
}
}
}
if (req == null) throw firstEx!;
if (bearerToken != null)
req.headers["Authorization"] = 'Bearer ' + bearerToken!;
else if (userName != null)
req.headers["Authorization"] =
'Basic ' + base64.encode(utf8.encode('${userName}:${password}'));
headers.forEach((key, val) {
req!.headers[key] = val;
});
if (bodyStr != null) {
req.headers["Content-Type"] = "application/json";
}
if (info.requestFilter != null) {
info.requestFilter!(req);
}
if (requestFilter != null) {
requestFilter!(req);
}
if (globalRequestFilter != null) {
globalRequestFilter!(req);
}
if (bodyStr != null) {
req.body = bodyStr;
}
return req;
}