From 27299ed266e12754cfe7157ad54868b40def804a Mon Sep 17 00:00:00 2001 From: Tibor Bossanyi Date: Sun, 19 Mar 2023 19:14:48 +0100 Subject: [PATCH] v1.0.16 utf8 decoding --- .gitignore | 4 ++-- README.md | 3 +++ lib/service/webapi.dart | 21 ++++++++++----------- pubspec.yaml | 2 +- test/api_test.dart | 20 ++++++++++++++++++-- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 5b6cfad..649b9f9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,5 @@ migrate_working_dir/ .packages build/ .dart_tool/ -.flutter-plugins -.flutter-plugins-dependencies \ No newline at end of file +/.flutter-plugins +/.flutter-plugins-dependencies \ No newline at end of file diff --git a/README.md b/README.md index 81a2c0c..84103f9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ Workout Test and Diet 4 You Common Util Functions +Version 1.0.16 +Webapi utf8 decode + Version 1.0.15 AppText and translations diff --git a/lib/service/webapi.dart b/lib/service/webapi.dart index 443a3ee..8ef4a35 100644 --- a/lib/service/webapi.dart +++ b/lib/service/webapi.dart @@ -23,11 +23,8 @@ class APIWebClient with Common, Logging { log("Base URL: $baseUrl"); var url = "${baseUrl}authenticate"; - try { - final body = jsonEncode({ - 'username': email, - 'password': password - }); + try { + final body = jsonEncode({'username': email, 'password': password}); var uri = Uri.parse(url); var result = await http.post(uri, body: body, headers: { "Content-Type": "application/json", @@ -38,7 +35,7 @@ class APIWebClient with Common, Logging { log("authentication response: ${result.statusCode} with URL: $url"); throw Exception("Authentication error: ${result.statusCode}"); } - + String response = result.body; log("Authentication status: ${result.statusCode}, response: $response"); final data = jsonDecode(response); @@ -78,7 +75,7 @@ class APIWebClient with Common, Logging { } else { throw Exception("Network Error, please try again later"); } - } on NotFoundException catch(e) { + } on NotFoundException catch (e) { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); @@ -100,18 +97,21 @@ class APIWebClient with Common, Logging { var uri = Uri.parse(url); var result = await http.get(uri, headers: { "Content-Type": "application/json", + //"Accept-Charset": 'utf-8', "Authorization": 'Bearer $authToken', }); log(" ------------get response code: ${result.statusCode}"); if (result.statusCode == 200) { - return result.body; + List bytes = result.body.codeUnits; + String decodedString = utf8.decode(bytes); + return decodedString; } else if (result.statusCode == 404) { throw const NotFoundException(message: "Not Found"); } else { throw Exception("Network Error, please try again later"); } - } on NotFoundException catch(e) { + } on NotFoundException catch (e) { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); @@ -119,5 +119,4 @@ class APIWebClient with Common, Logging { throw Exception("Network Error, please try again later"); } } - -} \ No newline at end of file +} diff --git a/pubspec.yaml b/pubspec.yaml index c990d51..48fb12b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: workouttest_util description: Workout Test app and web functions. -version: 1.0.15 +version: 1.0.16 environment: sdk: ">=2.18.6 <3.0.0" diff --git a/test/api_test.dart b/test/api_test.dart index 1c3c9a8..252aa5c 100644 --- a/test/api_test.dart +++ b/test/api_test.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter_test/flutter_test.dart'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/service/api.dart'; @@ -9,7 +11,6 @@ main() { }); group('api', () { - test('connect api auth successfully', () async { var api = APIClient(); var responseJson = await api.authenticateUser("bosi", "andio2009"); @@ -27,5 +28,20 @@ main() { final body = await api.get("app_package", ""); print(body); }); + + test('test customer conversation in utf8', () async { + var api = APIWebClient(); + var responseJson = await api.authenticateUser("bosi", "andio2009"); + print(responseJson); + Cache().authToken = responseJson['token']; + final body = await api.get("customer_conversation/72", ""); + + List bytes3 = base64.decode(body); + String decodedString = utf8.decode(bytes3); + print(decodedString); + + final body2 = await api.get("meal/21", ""); + print(body2); + }); }); -} \ No newline at end of file +}