SimpleCloudNotifier/flutter/lib/state/request_log.dart

182 lines
5.8 KiB
Dart
Raw Normal View History

2024-05-26 00:20:25 +02:00
import 'package:hive_flutter/hive_flutter.dart';
2024-05-25 18:09:39 +02:00
import 'package:simplecloudnotifier/models/api_error.dart';
2024-05-26 00:20:25 +02:00
import 'package:simplecloudnotifier/state/interfaces.dart';
import 'package:xid/xid.dart';
2024-05-23 20:05:55 +02:00
2024-05-25 18:09:39 +02:00
part 'request_log.g.dart';
class RequestLog {
2024-06-15 16:33:30 +02:00
//TODO max size, auto clear old
2024-05-25 18:09:39 +02:00
static void addRequestException(String name, DateTime tStart, String method, Uri uri, String reqbody, Map<String, String> reqheaders, dynamic e, StackTrace trace) {
Hive.box<SCNRequest>('scn-requests').add(SCNRequest(
2024-05-26 00:20:25 +02:00
id: Xid().toString(),
2024-05-25 18:09:39 +02:00
timestampStart: tStart,
timestampEnd: DateTime.now(),
name: name,
method: method,
url: uri.toString(),
requestHeaders: reqheaders,
requestBody: reqbody,
responseStatusCode: 0,
responseHeaders: {},
responseBody: '',
type: 'EXCEPTION',
error: (e is Exception) ? e.toString() : '$e',
stackTrace: trace.toString(),
));
}
static void addRequestAPIError(String name, DateTime t0, String method, Uri uri, String reqbody, Map<String, String> reqheaders, int responseStatusCode, String responseBody, Map<String, String> responseHeaders, APIError apierr) {
Hive.box<SCNRequest>('scn-requests').add(SCNRequest(
2024-05-26 00:20:25 +02:00
id: Xid().toString(),
2024-05-25 18:09:39 +02:00
timestampStart: t0,
timestampEnd: DateTime.now(),
name: name,
method: method,
url: uri.toString(),
requestHeaders: reqheaders,
requestBody: reqbody,
responseStatusCode: responseStatusCode,
responseHeaders: responseHeaders,
responseBody: responseBody,
type: 'API_ERROR',
error: apierr.message,
stackTrace: '',
));
}
static void addRequestErrorStatuscode(String name, DateTime t0, String method, Uri uri, String reqbody, Map<String, String> reqheaders, int responseStatusCode, String responseBody, Map<String, String> responseHeaders) {
Hive.box<SCNRequest>('scn-requests').add(SCNRequest(
2024-05-26 00:20:25 +02:00
id: Xid().toString(),
2024-05-25 18:09:39 +02:00
timestampStart: t0,
timestampEnd: DateTime.now(),
name: name,
method: method,
url: uri.toString(),
requestHeaders: reqheaders,
requestBody: reqbody,
responseStatusCode: responseStatusCode,
responseHeaders: responseHeaders,
responseBody: responseBody,
type: 'ERROR_STATUSCODE',
error: 'API request failed with status code $responseStatusCode',
stackTrace: '',
));
}
static void addRequestSuccess(String name, DateTime t0, String method, Uri uri, String reqbody, Map<String, String> reqheaders, int responseStatusCode, String responseBody, Map<String, String> responseHeaders) {
Hive.box<SCNRequest>('scn-requests').add(SCNRequest(
2024-05-26 00:20:25 +02:00
id: Xid().toString(),
2024-05-25 18:09:39 +02:00
timestampStart: t0,
timestampEnd: DateTime.now(),
name: name,
method: method,
url: uri.toString(),
requestHeaders: reqheaders,
requestBody: reqbody,
responseStatusCode: responseStatusCode,
responseHeaders: responseHeaders,
responseBody: responseBody,
type: 'SUCCESS',
error: '',
stackTrace: '',
));
}
static void addRequestDecodeError(String name, DateTime t0, String method, Uri uri, String reqbody, Map<String, String> reqheaders, int responseStatusCode, String responseBody, Map<String, String> responseHeaders, Object exc, StackTrace trace) {
Hive.box<SCNRequest>('scn-requests').add(SCNRequest(
2024-05-26 00:20:25 +02:00
id: Xid().toString(),
2024-05-25 18:09:39 +02:00
timestampStart: t0,
timestampEnd: DateTime.now(),
name: name,
method: method,
url: uri.toString(),
requestHeaders: reqheaders,
requestBody: reqbody,
responseStatusCode: responseStatusCode,
responseHeaders: responseHeaders,
responseBody: responseBody,
type: 'DECODE_ERROR',
error: (exc is Exception) ? exc.toString() : '$exc',
stackTrace: trace.toString(),
));
}
}
@HiveType(typeId: 100)
2024-05-26 00:20:25 +02:00
class SCNRequest extends HiveObject implements FieldDebuggable {
2024-05-25 18:09:39 +02:00
@HiveField(0)
2024-05-26 00:20:25 +02:00
final String id;
@HiveField(10)
2024-05-25 18:09:39 +02:00
final DateTime timestampStart;
2024-05-26 00:20:25 +02:00
@HiveField(11)
2024-05-25 18:09:39 +02:00
final DateTime timestampEnd;
2024-05-26 00:20:25 +02:00
@HiveField(12)
2024-05-25 18:09:39 +02:00
final String name;
2024-05-26 00:20:25 +02:00
@HiveField(13)
final String type; // SUCCESS | EXCEPTION | API_ERROR | ERROR_STATUSCODE | DECODE_ERROR
@HiveField(14)
2024-05-25 18:09:39 +02:00
final String error;
2024-05-26 00:20:25 +02:00
@HiveField(15)
2024-05-25 18:09:39 +02:00
final String stackTrace;
2024-05-26 00:20:25 +02:00
@HiveField(21)
2024-05-25 18:09:39 +02:00
final String method;
2024-05-26 00:20:25 +02:00
@HiveField(22)
2024-05-25 18:09:39 +02:00
final String url;
2024-05-26 00:20:25 +02:00
@HiveField(23)
2024-05-25 18:09:39 +02:00
final Map<String, String> requestHeaders;
2024-05-26 00:20:25 +02:00
@HiveField(24)
2024-05-25 18:09:39 +02:00
final String requestBody;
2024-05-26 00:20:25 +02:00
@HiveField(31)
2024-05-25 18:09:39 +02:00
final int responseStatusCode;
2024-05-26 00:20:25 +02:00
@HiveField(32)
2024-05-25 18:09:39 +02:00
final Map<String, String> responseHeaders;
2024-05-26 00:20:25 +02:00
@HiveField(33)
2024-05-25 18:09:39 +02:00
final String responseBody;
SCNRequest({
2024-05-26 00:20:25 +02:00
required this.id,
2024-05-25 18:09:39 +02:00
required this.timestampStart,
required this.timestampEnd,
required this.name,
required this.method,
required this.url,
required this.requestHeaders,
required this.requestBody,
required this.responseStatusCode,
required this.responseHeaders,
required this.responseBody,
required this.type,
required this.error,
required this.stackTrace,
});
2024-05-26 00:20:25 +02:00
@override
String toString() {
return 'SCNRequest[${this.id}]';
}
List<(String, String)> debugFieldList() {
return [
('id', this.id),
('timestampStart', this.timestampStart.toIso8601String()),
('timestampEnd', this.timestampEnd.toIso8601String()),
('name', this.name),
('method', this.method),
('url', this.url),
for (var (idx, item) in this.requestHeaders.entries.indexed) ('requestHeaders[$idx]', '${item.key}=${item.value}'),
('requestBody', this.requestBody),
('responseStatusCode', this.responseStatusCode.toString()),
for (var (idx, item) in this.responseHeaders.entries.indexed) ('responseHeaders[$idx]', '${item.key}=${item.value}'),
('responseBody', this.responseBody),
('type', this.type),
('error', this.error),
('stackTrace', this.stackTrace),
];
}
2024-05-25 18:09:39 +02:00
}