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 of invokeAPI method, representing an adapter that transforms the request from the classic HTTP request to a BLE request
Implementation
@override
Future<Response> invokeAPI(
String path,
String method,
List<QueryParam> queryParams,
Object? body,
Map<String, String> headerParams,
Map<String, String> formParams,
String? contentType,
) async {
/// Gets the device ID from the global variable
String? deviceId = LogbotBleManager().connectedDeviceId;
/// If no device is connected, throws an error
if (deviceId == null) {
throw ApiException.withInner(
HttpStatus.notFound,
'No device connected: $method $path',
null,
null,
);
}
List<QualifiedCharacteristic> characteristics =
LogbotBleUtils.getDeviceCharacteristics(deviceId);
/// Does not support MultipartRequest and MultipartFile
final String msgBody = contentType == 'application/x-www-form-urlencoded'
? formParams.toString()
: await serializeAsync(body);
/// If present, removes uuidToken header to save some
/// bytes, as the token is useless in ble connection
headerParams.remove("uuidToken");
Response response = await _invokeAPI(
path,
HpsMethod.values.firstWhere((HpsMethod e) => e.name == method),
queryParams,
msgBody,
headerParams,
contentType,
characteristics,
).timeout(
const Duration(
minutes: 1,
),
onTimeout: () {
throw ApiException.withInner(
HttpStatus.requestTimeout,
'Request timeout: $method $path',
null,
null,
);
},
);
return response;
}