invokeAPI method

  1. @override
Future<Response> invokeAPI(
  1. String path,
  2. String method,
  3. List<QueryParam> queryParams,
  4. Object? body,
  5. Map<String, String> headerParams,
  6. Map<String, String> formParams,
  7. 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;
}