Setting up basic API Structure
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/// 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 = 'localhost',
|
||||
this.port = 5000,
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user