From b30d003262b5599d1ebe388b1dff0e1c8d546701 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 12:54:03 +0530 Subject: [PATCH] add: ping to dart sdk. --- templates/dart/lib/src/client.dart.twig | 3 +++ templates/dart/lib/src/client_base.dart.twig | 3 +++ .../dart/lib/src/client_browser.dart.twig | 8 ++++++++ templates/dart/lib/src/client_io.dart.twig | 8 ++++++++ tests/DartBetaTest.php | 1 + tests/DartStableTest.php | 1 + tests/languages/dart/tests.dart | 20 ++++++++++++++++++- 7 files changed, 43 insertions(+), 1 deletion(-) diff --git a/templates/dart/lib/src/client.dart.twig b/templates/dart/lib/src/client.dart.twig index 5e386223f..09986d05c 100644 --- a/templates/dart/lib/src/client.dart.twig +++ b/templates/dart/lib/src/client.dart.twig @@ -48,6 +48,9 @@ abstract class Client { /// Add headers that should be sent with all API calls. Client addHeader(String key, String value); + /// Sends a "ping" request to Appwrite to verify connectivity. + Future ping(); + /// Upload a file in chunks. Future chunkedUpload({ required String path, diff --git a/templates/dart/lib/src/client_base.dart.twig b/templates/dart/lib/src/client_base.dart.twig index b0c41ffeb..f3137281d 100644 --- a/templates/dart/lib/src/client_base.dart.twig +++ b/templates/dart/lib/src/client_base.dart.twig @@ -20,6 +20,9 @@ abstract class ClientBase implements Client { @override ClientBase addHeader(String key, String value); + @override + Future ping(); + @override Future call( HttpMethod method, { diff --git a/templates/dart/lib/src/client_browser.dart.twig b/templates/dart/lib/src/client_browser.dart.twig index 6a0f046fd..41d9860c9 100644 --- a/templates/dart/lib/src/client_browser.dart.twig +++ b/templates/dart/lib/src/client_browser.dart.twig @@ -77,6 +77,14 @@ class ClientBrowser extends ClientBase with ClientMixin { return this; } + @override + Future ping() async { + final String apiPath = '/ping'; + final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain); + + return response.data; + } + @override Future webAuth(Uri url) async { final request = http.Request('GET', url); diff --git a/templates/dart/lib/src/client_io.dart.twig b/templates/dart/lib/src/client_io.dart.twig index 8a51e5979..09a3ca6a3 100644 --- a/templates/dart/lib/src/client_io.dart.twig +++ b/templates/dart/lib/src/client_io.dart.twig @@ -89,6 +89,14 @@ class ClientIO extends ClientBase with ClientMixin { return this; } + @override + Future ping() async { + final String apiPath = '/ping'; + final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain); + + return response.data; + } + @override Future chunkedUpload({ required String path, diff --git a/tests/DartBetaTest.php b/tests/DartBetaTest.php index 816620260..af0165ab9 100644 --- a/tests/DartBetaTest.php +++ b/tests/DartBetaTest.php @@ -19,6 +19,7 @@ class DartBetaTest extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:beta sh -c "dart pub get && dart pub run tests/tests.dart"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/DartStableTest.php b/tests/DartStableTest.php index f55acae99..1aaafd33a 100644 --- a/tests/DartStableTest.php +++ b/tests/DartStableTest.php @@ -19,6 +19,7 @@ class DartStableTest extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:stable sh -c "dart pub get && dart pub run tests/tests.dart"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/languages/dart/tests.dart b/tests/languages/dart/tests.dart index 5682307ae..32ab2cd91 100644 --- a/tests/languages/dart/tests.dart +++ b/tests/languages/dart/tests.dart @@ -4,6 +4,7 @@ import '../lib/enums.dart'; import '../lib/src/input_file.dart'; import 'dart:io'; +import 'dart:convert'; void main() async { Client client = Client().setSelfSigned(); @@ -15,7 +16,16 @@ void main() async { client.setSelfSigned(); print('\nTest Started'); - + + // Ping pong test + client.setProject('123456'); + final ping = await client.ping(); + final pingResponse = parse(ping)!; + print(pingResponse); + + // reset project. + client.setProject('console'); + // Foo Tests Mock response; response = await foo.get(x: 'string', y: 123, z: ['string in array']); @@ -164,3 +174,11 @@ void main() async { response = await general.headers(); print(response.result); } + +String? parse(String json) { + try { + return jsonDecode(json)['result'] as String?; + } catch (_) { + return null; + } +}