processHttpEvent method

Future<void> processHttpEvent(
  1. ObslyEventBase event
)

Execute rules for HTTP events with fresh appContext

Implementation

Future<void> processHttpEvent(ObslyEventBase event) async {
  if (!_isAvailable || !_isExecutionEnabled) return;

  final url = 'Unknown URL';
  final method = 'Unknown Method';

  try {
    final httpContext = _extractHttpContext(event);
    if (httpContext != null) {
      final contextUrl = httpContext['url'] ?? url;
      final contextMethod = httpContext['method'] ?? method;

      // Log HTTP rule processing
      ObslyLogger.verbose('🌐 HTTP Request: $contextMethod $contextUrl');
      ObslyLogger.verbose('🎯 Starting HTTP rule evaluation: $contextUrl ($contextMethod)');

      // Build fresh appContext for this evaluation
      final currentAppContext = _buildCurrentAppContext();

      // Log context access for debugging
      ObslyLogger.verbose('πŸ—‚οΈ HTTP Rule context access: app_context');

      // Merge HTTP context with app context
      final fullContext = {
        ...currentAppContext,
        'request': {
          'url': contextUrl,
          'verb': contextMethod,
          'statusCode': httpContext['statusCode'] ?? 200,
          'timeout': httpContext['timeout'] ?? false,
          'duration': httpContext['duration'] ?? 0,
          'requestBody': httpContext['requestBody'],
          'responseBody': httpContext['responseBody'],
        },
      };

      // Log the merged context
      ObslyLogger.verbose('πŸ—‚οΈ HTTP Rule merged context prepared');

      // Use unified execution method
      final results = await RulesManager.instance.executeRulesForEvent(
        eventType: 'http',
        context: fullContext,
      );

      // Log results with new system
      final allTags = results
          .expand((r) => RulesResultFormatter.formatForUI(r.rawResult, includeTimestamp: false)['tags']
              as List<Map<String, dynamic>>)
          .toList();
      final allVariables = results
          .expand((r) => RulesResultFormatter.formatForUI(r.rawResult, includeTimestamp: false)['variables']
              as List<Map<String, dynamic>>)
          .toList();

      final resultStatus = results.isNotEmpty ? 'βœ… SUCCESS' : '❌ NO RESULTS';
      ObslyLogger.verbose(
          '🏁 HTTP Rules result: $resultStatus | Rules: ${results.length} | Tags: ${allTags.length} | Variables: ${allVariables.length}');

      ObslyLogger.log(
          '🌐 HTTP Rules executed for: $contextUrl ($contextMethod) - Status: ${httpContext['statusCode']}');
    }
  } catch (e, stackTrace) {
    ObslyLogger.errorWithContext(
      'RulesIntegration',
      'processHttpEvent',
      e.toString(),
      stackTrace,
      context: {'url': url, 'method': method},
    );
  }
}