decodeControlPlaneResponse function
解析控制面响应。非 2xx 或业务码非 0 时抛 SyImControlPlaneException。
Implementation
Map<String, dynamic> decodeControlPlaneResponse(int httpStatus, String body) {
Object? parsed;
try {
parsed = jsonDecode(body.isEmpty ? '{}' : body);
} on FormatException {
parsed = null;
}
final ok = httpStatus >= 200 && httpStatus < 300;
if (parsed is! Map) {
if (ok) {
throw SyImControlPlaneException(-1, httpStatus, 'unexpected response');
}
throw SyImControlPlaneException(httpStatus, httpStatus, 'HTTP $httpStatus');
}
final m = Map<String, dynamic>.from(parsed);
final rawCode = m['code'];
final code = rawCode is num ? rawCode.toInt() : (ok ? 0 : httpStatus);
if (!ok || code != 0) {
final msg = m['msg']?.toString();
throw SyImControlPlaneException(code, httpStatus,
(msg == null || msg.isEmpty) ? 'HTTP $httpStatus' : msg);
}
final data = m['data'];
if (data is Map) return Map<String, dynamic>.from(data);
if (data == null) return <String, dynamic>{};
return <String, dynamic>{'value': data};
}