import 'package:oc_front/models/abstract.dart'; import 'package:oc_front/models/logs.dart'; import 'package:oc_front/models/search.dart'; import 'package:oc_front/models/shared.dart'; import 'package:oc_front/models/workflow.dart'; import 'package:oc_front/models/workspace.dart'; Map refs = { RawData: RawData(), Search: Search(), Workspace: Workspace(), DataItem: DataItem(), ComputeItem: ComputeItem(), StorageItem: StorageItem(), ProcessingItem: ProcessingItem(), Workflow: Workflow(), Resource: Resource(), WorkflowExecutions: WorkflowExecutions(), LogsResult: LogsResult(), Check: Check(), CollaborativeArea: CollaborativeArea(), SimpleData: SimpleData(), }; class APIResponse { APIResponse({ this.data, this.code = 200, this.error = "", this.offset = 0, }); int code = 200; int offset = 0; T? data ; String? error = ""; SerializerDeserializer? getTypeString() { try { return refs[refs.keys.firstWhere((ref) => '$ref' == '$T')]; } catch (e) { return null; } } APIResponse deserialize(dynamic j) { dynamic data; try { if (j["data"] == null) { data = j; } else { data = j["data"]; } } catch (e) { data = j; } try { return APIResponse( data: refs[T]!.deserialize(data), code: j.containsKey("code") && j["code"] != null ? j["code"] : 200, error: j.containsKey("error") && j["error"] != null ? j["error"] : "", ); } catch (e) { return APIResponse( data: refs[T]!.deserialize(data), ); } } } class SimpleData extends SerializerDeserializer { SimpleData({ this.value }); dynamic value; @override deserialize(dynamic json) { return SimpleData(value: json); } @override Map serialize() => { }; } class RawData extends SerializerDeserializer { RawData({ this.values = const []}); List values = []; @override deserialize(dynamic json) { return RawData(values: json ?? []); } @override Map serialize() => { }; } abstract class ShallowData { String getID(); String getName(); } class Shallow { String id; String name; Shallow({ this.id = "", this.name = "", }); }