oc-front/lib/models/response.dart

46 lines
1.3 KiB
Dart

import 'package:oc_front/models/abstract.dart';
import 'package:oc_front/models/search.dart';
import 'package:oc_front/models/workspace.dart';
Map<Type, SerializerDeserializer> refs = <Type, SerializerDeserializer> {
RawData: RawData(),
Search: Search(),
Workspace: Workspace(),
DataItem: DataItem(),
DataCenterItem: DataCenterItem(),
StorageItem: StorageItem(),
ComputingItem: ComputingItem(),
};
class APIResponse<T extends SerializerDeserializer> {
APIResponse({
this.data,
this.error = "",
this.offset = 0,
});
int offset = 0;
T? data ;
String? error = "";
SerializerDeserializer? getTypeString() {
try { return refs[refs.keys.firstWhere((ref) => '$ref' == '$T')];
} catch (e) { return null; }
}
APIResponse<T> deserialize(dynamic j) {
try {
return APIResponse<T>(
data: refs[T]!.deserialize(j),
error: j.containsKey("error") && j["error"] != null ? j["error"] : "",
);
} catch (e) { return APIResponse<T>( data: refs[T]!.deserialize(j), ); }
}
}
class RawData extends SerializerDeserializer<RawData> {
RawData({ this.values = const []});
dynamic values;
@override deserialize(dynamic json) { return RawData(values: json); }
@override Map<String, dynamic> serialize() => { };
}