oc-front/lib/models/response.dart
2024-08-08 08:42:32 +02:00

58 lines
1.7 KiB
Dart

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/workflow.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(),
ProcessingItem: ProcessingItem(),
Workflow: Workflow(),
Resource: Resource(),
WorkflowExecutions: WorkflowExecutions(),
LogResults: LogResults(),
};
class APIResponse<T extends SerializerDeserializer> {
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<T> deserialize(dynamic j) {
dynamic data;
try { data = j["data"];
} catch (e) { data = j; }
try {
return APIResponse<T>(
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<T>( data: refs[T]!.deserialize(data), ); }
}
}
class RawData extends SerializerDeserializer<RawData> {
RawData({ this.values = const []});
List<dynamic> values = [];
@override deserialize(dynamic json) { return RawData(values: json ?? []); }
@override Map<String, dynamic> serialize() => { };
}