invokeAPI method
Future<Response>
invokeAPI(
- String path,
- String method,
- List<
QueryParam> queryParams, - Object? body,
- Map<
String, String> headerParams, - Map<
String, String> formParams, - String? contentType,
override
Implementation
Future<Response> invokeAPI(
String path,
String method,
List<QueryParam> queryParams,
Object? body,
Map<String, String> headerParams,
Map<String, String> formParams,
String? contentType,
) async {
headerParams.addAll(_defaultHeaderMap);
if (contentType != null) {
headerParams['Content-Type'] = contentType;
}
final urlEncodedQueryParams = queryParams.map((param) => '$param');
final queryString = urlEncodedQueryParams.isNotEmpty
? '?${urlEncodedQueryParams.join('&')}'
: '';
/// Logbot additional procedure for local connection
String? deviceId =
LogbotDiscovery.utils.getDeviceIdFromCookies(headerParams['Cookie']);
List<Device> devices = await LogbotDiscovery.discovery.discoverLocal();
String? deviceLocalIp =
devices.firstWhere((Device device) => device.deviceId == deviceId).ip;
/// TODO: cannot use getLocalIp because it crosschecks with online devices. In this
/// API client you are not connected to internet so it doesn't work. Should refactor
/// getLocalIp from LogbotDiscovery to let choose to make it work in "offline mode2
/// by looking only at local wifi devices with no user crosscheck.
// String? deviceLocalIp = await LogbotDiscovery.utils.getLocalIp(deviceId);
LogbotLogger().simple("Device is available locally @$deviceLocalIp.");
String _basePath =
'http://$deviceLocalIp:${LbSetupEnvironment.servicePort}/api/v1';
Uri uri = Uri.parse('$_basePath$path$queryString');
try {
// Special case for uploading a single file which isn't a 'multipart/form-data'.
if (body is MultipartFile &&
(contentType == null ||
!contentType.toLowerCase().startsWith('multipart/form-data'))) {
final request = StreamedRequest(method, uri);
request.headers.addAll(headerParams);
request.contentLength = body.length;
body.finalize().listen(
request.sink.add,
onDone: request.sink.close,
// ignore: avoid_types_on_closure_parameters
onError: (Object error, StackTrace trace) => request.sink.close(),
cancelOnError: true,
);
final response = await _client.send(request);
return Response.fromStream(response);
}
if (body is MultipartRequest) {
final request = MultipartRequest(method, uri);
request.fields.addAll(body.fields);
request.files.addAll(body.files);
request.headers.addAll(body.headers);
request.headers.addAll(headerParams);
final response = await _client.send(request);
return Response.fromStream(response);
}
final msgBody = contentType == 'application/x-www-form-urlencoded'
? formParams
: await serializeAsync(body);
final nullableHeaderParams = headerParams.isEmpty ? null : headerParams;
switch (method) {
case 'POST':
return await _client.post(
uri,
headers: nullableHeaderParams,
body: msgBody,
);
case 'PUT':
return await _client.put(
uri,
headers: nullableHeaderParams,
body: msgBody,
);
case 'DELETE':
return await _client.delete(
uri,
headers: nullableHeaderParams,
body: msgBody,
);
case 'PATCH':
return await _client.patch(
uri,
headers: nullableHeaderParams,
body: msgBody,
);
case 'HEAD':
return await _client.head(
uri,
headers: nullableHeaderParams,
);
case 'GET':
return await _client.get(
uri,
headers: nullableHeaderParams,
);
}
} on SocketException catch (error, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'Socket operation failed: $method $path',
error,
trace,
);
} on TlsException catch (error, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'TLS/SSL communication failed: $method $path',
error,
trace,
);
} on IOException catch (error, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'I/O operation failed: $method $path',
error,
trace,
);
} on ClientException catch (error, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'HTTP connection failed: $method $path',
error,
trace,
);
} on Exception catch (error, trace) {
throw ApiException.withInner(
HttpStatus.badRequest,
'Exception occurred: $method $path',
error,
trace,
);
}
throw ApiException(
HttpStatus.badRequest,
'Invalid HTTP operation: $method $path',
);
}