sendWebhook function
Equivalent of send_webhook
Implementation
Future<void> sendWebhook(
http.Client client, {
required String url,
required String event,
required Map<String, dynamic> data,
String? secret,
Map<String, String>? headers,
}) async {
final payloadBody = <String, dynamic>{'event': event, 'data': data};
final payload = jsonEncode(payloadBody);
final hash = crypto.sha256.convert(utf8.encode(payload));
final mergedHeaders = <String, String>{
'Content-Type': 'application/json',
if (headers != null) ...headers,
};
if (secret != null) {
// If you REALLY want JWT here, plug in a JWT lib.
// The Python code uses:
// jwt.encode({"sha256": hash.hexdigest()}, key=secret, algorithm="HS256")
//
// For now, imagine a helper:
final jwt = signSha256Jwt(hash.toString(), secret);
mergedHeaders['Meshagent-Signature'] = 'Bearer $jwt';
}
final resp = await client.post(
Uri.parse(url),
headers: mergedHeaders,
body: payload,
);
if (resp.statusCode < 200 || resp.statusCode >= 300) {
logger.warning(
'webhook call failed $event $url (status: ${resp.statusCode})',
);
throw RoomServerException(
'error status returned from webhook call $url, '
'http status code: ${resp.statusCode}',
);
}
}