75 lines
1.9 KiB
Dart
75 lines
1.9 KiB
Dart
/// Central configuration of the backend Rest API (.Net Web API).
|
|
///
|
|
/// The API is reached over plain http because it only ever gets called
|
|
/// inside of a trusted network.
|
|
class ApiConfig {
|
|
const ApiConfig({
|
|
this.scheme = 'http',
|
|
this.host = '10.10.50.30',
|
|
this.port = 12781,
|
|
this.basePath = '',
|
|
this.timeout = const Duration(seconds: 15),
|
|
this.defaultHeaders = const {},
|
|
});
|
|
|
|
final String scheme;
|
|
final String host;
|
|
final int? port;
|
|
|
|
/// Optional prefix in front of every controller route, e.g. `api`.
|
|
final String basePath;
|
|
|
|
final Duration timeout;
|
|
final Map<String, String> defaultHeaders;
|
|
|
|
/// Default configuration used by [AcApi] when nothing else is provided.
|
|
static const ApiConfig development = ApiConfig();
|
|
|
|
ApiConfig copyWith({
|
|
String? scheme,
|
|
String? host,
|
|
int? port,
|
|
String? basePath,
|
|
Duration? timeout,
|
|
Map<String, String>? defaultHeaders,
|
|
}) {
|
|
return ApiConfig(
|
|
scheme: scheme ?? this.scheme,
|
|
host: host ?? this.host,
|
|
port: port ?? this.port,
|
|
basePath: basePath ?? this.basePath,
|
|
timeout: timeout ?? this.timeout,
|
|
defaultHeaders: defaultHeaders ?? this.defaultHeaders,
|
|
);
|
|
}
|
|
|
|
/// Builds an absolute [Uri] for a controller route like `agents`.
|
|
Uri buildUri(String path, [Map<String, dynamic>? queryParameters]) {
|
|
final segments = <String>[
|
|
...basePath.split('/'),
|
|
...path.split('/'),
|
|
].where((segment) => segment.isNotEmpty).toList();
|
|
|
|
Map<String, String>? query;
|
|
if (queryParameters != null && queryParameters.isNotEmpty) {
|
|
query = <String, String>{};
|
|
queryParameters.forEach((key, value) {
|
|
if (value != null) {
|
|
query![key] = value.toString();
|
|
}
|
|
});
|
|
if (query.isEmpty) {
|
|
query = null;
|
|
}
|
|
}
|
|
|
|
return Uri(
|
|
scheme: scheme,
|
|
host: host,
|
|
port: port,
|
|
pathSegments: segments,
|
|
queryParameters: query,
|
|
);
|
|
}
|
|
}
|