From d45def7cbe2c1705d06aeb88e34a195274ceac18 Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 15 Aug 2023 12:09:33 +0800 Subject: [PATCH] opt ab json decode Signed-off-by: 21pages --- flutter/lib/models/ab_model.dart | 51 ++++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/flutter/lib/models/ab_model.dart b/flutter/lib/models/ab_model.dart index 7fdf940d3..b126715b0 100644 --- a/flutter/lib/models/ab_model.dart +++ b/flutter/lib/models/ab_model.dart @@ -78,15 +78,8 @@ class AbModel { tags.clear(); peers.clear(); } else if (resp.body.isNotEmpty) { - Map json; - try { - json = jsonDecode(utf8.decode(resp.bodyBytes)); - } catch (e) { - if (resp.statusCode != 200) { - throw 'HTTP ${resp.statusCode}, $e'; - } - rethrow; - } + Map json = + _jsonDecode(utf8.decode(resp.bodyBytes), resp.statusCode); if (json.containsKey('error')) { throw json['error']; } else if (json.containsKey('data')) { @@ -221,7 +214,7 @@ class AbModel { }); http.Response resp; // support compression - if (licensedDevices > 0 && body.length > 1024) { + if (licensedDevices > 0 && body.length > 102400) { authHeaders['Content-Encoding'] = "gzip"; resp = await http.post(Uri.parse(api), headers: authHeaders, body: GZipCodec().encode(utf8.encode(body))); @@ -229,25 +222,18 @@ class AbModel { resp = await http.post(Uri.parse(api), headers: authHeaders, body: body); } - try { - if (resp.statusCode == 200 && - (resp.body.isEmpty || resp.body.toLowerCase() == 'null')) { + if (resp.statusCode == 200 && + (resp.body.isEmpty || resp.body.toLowerCase() == 'null')) { + _saveCache(); + } else { + Map json = _jsonDecode(resp.body, resp.statusCode); + if (json.containsKey('error')) { + throw json['error']; + } else if (resp.statusCode == 200) { _saveCache(); } else { - final json = jsonDecode(resp.body); - if (json.containsKey('error')) { - throw json['error']; - } else if (resp.statusCode == 200) { - _saveCache(); - } else { - throw 'HTTP ${resp.statusCode}'; - } + throw 'HTTP ${resp.statusCode}'; } - } catch (e) { - if (resp.statusCode != 200) { - throw 'HTTP ${resp.statusCode}, $e'; - } - rethrow; } } catch (e) { pushError.value = @@ -469,4 +455,17 @@ class AbModel { debugPrint("load ab cache: $e"); } } + + Map _jsonDecode(String body, int statusCode) { + try { + Map json = jsonDecode(body); + return json; + } catch (e) { + final err = body.isNotEmpty && body.length < 128 ? body : e.toString(); + if (statusCode != 200) { + throw 'HTTP $statusCode, $err'; + } + throw err; + } + } }