Adapt app client to JSON API payloads

This commit is contained in:
Artem Kokos
2026-05-16 10:29:54 +07:00
parent 8ffaa14b60
commit 70fedb6134
2 changed files with 93 additions and 5 deletions

86
test/api_client_test.dart Normal file
View File

@@ -0,0 +1,86 @@
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ignis_app/services/api_client.dart';
class RecordingAdapter implements HttpClientAdapter {
RequestOptions? lastRequest;
@override
Future<ResponseBody> fetch(
RequestOptions options,
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) async {
lastRequest = options;
return ResponseBody.fromString(
'{}',
200,
headers: {
Headers.contentTypeHeader: ['application/json'],
},
);
}
@override
void close({bool force = false}) {}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('controlGroup sends command payload in request body', () async {
final adapter = RecordingAdapter();
final dio = Dio()..httpClientAdapter = adapter;
final api = IgnisApi(dio: dio)..init('http://localhost:8000', 'secret');
await api.controlGroup('kitchen', {'state': true, 'brightness': 42});
expect(adapter.lastRequest, isNotNull);
expect(adapter.lastRequest?.path, '/control/group/kitchen');
expect(adapter.lastRequest?.queryParameters, isEmpty);
expect(adapter.lastRequest?.data, {'state': true, 'brightness': 42});
});
test('scheduleOnce sends schedule payload in request body', () async {
final adapter = RecordingAdapter();
final dio = Dio()..httpClientAdapter = adapter;
final api = IgnisApi(dio: dio)..init('http://localhost:8000', 'secret');
await api.scheduleOnce({
'target_id': 'hall',
'hours_from_now': 4,
'state': false,
'is_group': true,
});
expect(adapter.lastRequest, isNotNull);
expect(adapter.lastRequest?.path, '/schedules/once');
expect(adapter.lastRequest?.queryParameters, isEmpty);
expect(adapter.lastRequest?.data, {
'target_id': 'hall',
'hours_from_now': 4,
'state': false,
'is_group': true,
});
});
test(
'createApiKey keeps query-based contract until backend is changed',
() async {
final adapter = RecordingAdapter();
final dio = Dio()..httpClientAdapter = adapter;
final api = IgnisApi(dio: dio)..init('http://localhost:8000', 'secret');
await api.createApiKey('Guest', isAdmin: true);
expect(adapter.lastRequest, isNotNull);
expect(adapter.lastRequest?.path, '/api-keys');
expect(adapter.lastRequest?.queryParameters, {
'name': 'Guest',
'is_admin': true,
});
},
);
}