composeUri static method
Composes URI from config parameters. The result URI will be in the following form: protocol://username@password@host1:port1,host2:port2,...?param1=abc¶m2=xyz&...
options
configuration parametersdefaultProtocol
a default protocoldefaultPort
a default port
Implementation
static String composeUri(
ConfigParams options, String? defaultProtocol, int? defaultPort) {
var builder = '';
var protocol =
options.getAsStringWithDefault('protocol', defaultProtocol ?? '');
if (protocol != '') {
builder = protocol + '://' + builder;
}
var username = options.getAsNullableString('username');
if (username != null) {
builder += username;
var password = options.getAsNullableString('password');
if (password != null) {
builder += ':' + password;
}
builder += '@';
}
var servers = '';
var defaultPortStr =
defaultPort != null && defaultPort > 0 ? defaultPort.toString() : '';
var hosts = options.getAsStringWithDefault('host', '???').split(',');
var ports =
options.getAsStringWithDefault('port', defaultPortStr).split(',');
for (var index = 0; index < hosts.length; index++) {
if (servers.isNotEmpty) {
servers += ',';
}
var host = hosts[index];
servers += host;
var port = ports.length > index ? ports[index] : defaultPortStr;
port = port != '' ? port : defaultPortStr;
if (port != '') {
servers += ':' + port;
}
}
builder += servers;
var path = options.getAsNullableString('path');
if (path != null) {
builder += '/' + path;
}
var params = '';
var reservedKeys = [
'protocol',
'host',
'port',
'username',
'password',
'servers',
'path'
];
for (var key in options.getKeys()) {
if (reservedKeys.contains(key)) {
continue;
}
if (params.isNotEmpty) {
params += '&';
}
params += Uri.encodeComponent(key);
var value = options.getAsNullableString(key);
if (value != null && value != '') {
params += '=' + Uri.encodeComponent(value);
}
}
if (params.isNotEmpty) {
builder += '?' + params;
}
return builder.toString();
}