oc-front/lib/models/response.dart

89 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:oc_front/models/abstract.dart';
2024-08-08 08:42:32 +02:00
import 'package:oc_front/models/logs.dart';
import 'package:oc_front/models/search.dart';
2024-08-26 17:37:23 +02:00
import 'package:oc_front/models/shared.dart';
2024-08-08 08:42:32 +02:00
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(),
2024-11-08 13:59:22 +01:00
ComputeItem: ComputeItem(),
StorageItem: StorageItem(),
2024-08-08 08:42:32 +02:00
ProcessingItem: ProcessingItem(),
Workflow: Workflow(),
Resource: Resource(),
WorkflowExecutions: WorkflowExecutions(),
2024-08-22 15:46:16 +02:00
LogsResult: LogsResult(),
Check: Check(),
2024-11-08 13:59:22 +01:00
CollaborativeArea: CollaborativeArea(),
SimpleData: SimpleData(),
};
class APIResponse<T extends SerializerDeserializer> {
APIResponse({
this.data,
2024-08-08 08:42:32 +02:00
this.code = 200,
this.error = "",
this.offset = 0,
});
2024-08-08 08:42:32 +02:00
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) {
2024-08-08 08:42:32 +02:00
dynamic data;
2024-11-08 13:59:22 +01:00
try {
if (j["data"] == null) { data = j; }
else {
data = j["data"];
}
2024-08-08 08:42:32 +02:00
} catch (e) { data = j; }
try {
return APIResponse<T>(
2024-08-08 08:42:32 +02:00
data: refs[T]!.deserialize(data),
code: j.containsKey("code") && j["code"] != null ? j["code"] : 200,
error: j.containsKey("error") && j["error"] != null ? j["error"] : "",
);
2024-08-08 08:42:32 +02:00
} catch (e) { return APIResponse<T>( data: refs[T]!.deserialize(data), ); }
}
}
2024-08-08 08:42:32 +02:00
2024-11-08 13:59:22 +01:00
class SimpleData extends SerializerDeserializer<SimpleData> {
SimpleData({ this.value });
dynamic value;
@override deserialize(dynamic json) {
return SimpleData(value: json);
}
@override Map<String, dynamic> serialize() => { };
}
class RawData extends SerializerDeserializer<RawData> {
RawData({ this.values = const []});
2024-08-08 08:42:32 +02:00
List<dynamic> values = [];
2024-11-13 08:12:37 +01:00
@override deserialize(dynamic json) {
return RawData(values: json ?? []); }
@override Map<String, dynamic> serialize() => { };
}
2024-08-30 12:52:32 +02:00
abstract class ShallowData {
String getID();
String getName();
}
class Shallow {
String id;
String name;
Shallow({
this.id = "",
this.name = "",
});
}