From dc47e63388b4a74fcdcd3eafd57067cb6855bac3 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 12 Dec 2024 18:34:14 +0530 Subject: [PATCH 1/9] feat: add ping to apple and wsift sdk. --- templates/apple/Sources/Client.swift.twig | 28 +++++++++++++++++-- templates/swift/Sources/Client.swift.twig | 28 +++++++++++++++++-- .../swift/Sources/Models/Model.swift.twig | 16 +++++++++++ tests/AppleSwift56Test.php | 1 + tests/Swift56Test.php | 1 + tests/languages/apple/Tests.swift | 16 ++++++++--- tests/languages/swift/Tests.swift | 14 ++++++++-- 7 files changed, 91 insertions(+), 13 deletions(-) diff --git a/templates/apple/Sources/Client.swift.twig b/templates/apple/Sources/Client.swift.twig index baa7adbcb..48b023f02 100644 --- a/templates/apple/Sources/Client.swift.twig +++ b/templates/apple/Sources/Client.swift.twig @@ -214,6 +214,26 @@ open class Client { ) ?? "" } + /// + /// Sends a "ping" request to Appwrite to verify connectivity. + /// + /// @return String + /// @throws Exception + /// + open func ping() async throws -> String { + let apiPath: String = "/ping" + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + return try await call( + method: "GET", + path: apiPath, + headers: apiHeaders + ) + } + /// /// Make an API call /// @@ -284,6 +304,8 @@ open class Client { } } + var data = try await response.body.collect(upTo: Int.max) + switch response.status.code { case 0..<400: if response.headers["Set-Cookie"].count > 0 { @@ -296,10 +318,11 @@ open class Client { switch T.self { case is Bool.Type: return true as! T + case is String.Type: + return (data.readString(length: data.readableBytes) ?? "") as! T case is ByteBuffer.Type: - return try await response.body.collect(upTo: Int.max) as! T + return data as! T default: - let data = try await response.body.collect(upTo: Int.max) if data.readableBytes == 0 { return true as! T } @@ -309,7 +332,6 @@ open class Client { } default: var message = "" - var data = try await response.body.collect(upTo: Int.max) var type = "" do { diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index 6cd23d78b..3e1c63d56 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -192,6 +192,26 @@ open class Client { ) ?? "" } + /// + /// Sends a "ping" request to Appwrite to verify connectivity. + /// + /// @return String + /// @throws Exception + /// + open func ping() async throws -> String { + let apiPath: String = "/ping" + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + return try await call( + method: "GET", + path: apiPath, + headers: apiHeaders + ) + } + /// /// Make an API call /// @@ -327,15 +347,18 @@ open class Client { } } + var data = try await response.body.collect(upTo: Int.max) + switch response.status.code { case 0..<400: switch T.self { case is Bool.Type: return true as! T + case is String.Type: + return (data.readString(length: data.readableBytes) ?? "") as! T case is ByteBuffer.Type: - return try await response.body.collect(upTo: Int.max) as! T + return data as! T default: - let data = try await response.body.collect(upTo: Int.max) if data.readableBytes == 0 { return true as! T } @@ -345,7 +368,6 @@ open class Client { } default: var message = "" - var data = try await response.body.collect(upTo: Int.max) var type = "" do { diff --git a/templates/swift/Sources/Models/Model.swift.twig b/templates/swift/Sources/Models/Model.swift.twig index cc3fbe6be..3be9caadb 100644 --- a/templates/swift/Sources/Models/Model.swift.twig +++ b/templates/swift/Sources/Models/Model.swift.twig @@ -58,5 +58,21 @@ public class {{ definition | modelType(spec) | raw }} { {%~ endif %} ) } + + public static func from(json: String) -> {{ definition.name | caseUcfirst }}? { + guard let data = json.data(using: .utf8) else { + return nil + } + + do { + if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { + return {{ definition.name | caseUcfirst }}.from(map: jsonObject) + } else { + return nil + } + } catch { + return nil + } + } } {% endif %} \ No newline at end of file diff --git a/tests/AppleSwift56Test.php b/tests/AppleSwift56Test.php index cfca2c18e..ccf79f6cc 100644 --- a/tests/AppleSwift56Test.php +++ b/tests/AppleSwift56Test.php @@ -19,6 +19,7 @@ class AppleSwift56Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/apple swift:5.6-focal swift test'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/Swift56Test.php b/tests/Swift56Test.php index 6ef89c41b..15fa954e2 100644 --- a/tests/Swift56Test.php +++ b/tests/Swift56Test.php @@ -19,6 +19,7 @@ class Swift56Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/swift swift:5.6-focal swift test'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/languages/apple/Tests.swift b/tests/languages/apple/Tests.swift index 05059d158..d8aae3d63 100644 --- a/tests/languages/apple/Tests.swift +++ b/tests/languages/apple/Tests.swift @@ -21,11 +21,21 @@ class Tests: XCTestCase { func test() async throws { let client = Client() - .setEndpointRealtime("ws://cloud.appwrite.io/v1") - .setProject("console") + .setProject("123456") .addHeader(key: "Origin", value: "http://localhost") .setSelfSigned() + var mock: Mock + + // Ping pong test + let ping = try await client.ping() + mock = Mock.from(json: ping)! + print(mock.result) + + // reset configs + client.setProject("console") + .setEndpointRealtime("ws://cloud.appwrite.io/v1") + let foo = Foo(client) let bar = Bar(client) let general = General(client) @@ -39,8 +49,6 @@ class Tests: XCTestCase { expectation.fulfill() } - var mock: Mock - // Foo Tests mock = try await foo.get(x: "string", y: 123, z: ["string in array"]) print(mock.result) diff --git a/tests/languages/swift/Tests.swift b/tests/languages/swift/Tests.swift index 3c89e6b89..d8054a6c6 100644 --- a/tests/languages/swift/Tests.swift +++ b/tests/languages/swift/Tests.swift @@ -21,16 +21,24 @@ class Tests: XCTestCase { func test() async throws { do { let client = Client() - .setProject("console") + .setProject("123456") .addHeader(key: "Origin", value: "http://localhost") .setSelfSigned() + var mock: Mock + + // Ping pong test + let ping = try await client.ping() + mock = Mock.from(json: ping)! + print(mock.result) + + // reset project + client.setProject("console") + let foo = Foo(client) let bar = Bar(client) let general = General(client) - var mock: Mock - // Foo Tests mock = try await foo.get(x: "string", y: 123, z: ["string in array"]) print(mock.result) From 9f2a0c817e63955389dc0fb05131e18f56bb98c9 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 12 Dec 2024 19:17:37 +0530 Subject: [PATCH 2/9] fix: use local level parsing. --- tests/languages/apple/Tests.swift | 19 ++++++++++++++----- tests/languages/swift/Tests.swift | 17 +++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/languages/apple/Tests.swift b/tests/languages/apple/Tests.swift index d8aae3d63..9ac970e3b 100644 --- a/tests/languages/apple/Tests.swift +++ b/tests/languages/apple/Tests.swift @@ -25,12 +25,10 @@ class Tests: XCTestCase { .addHeader(key: "Origin", value: "http://localhost") .setSelfSigned() - var mock: Mock - // Ping pong test let ping = try await client.ping() - mock = Mock.from(json: ping)! - print(mock.result) + let pingResult = parse(from: ping)! + print(pingResult) // reset configs client.setProject("console") @@ -48,7 +46,9 @@ class Tests: XCTestCase { realtimeResponse = message.payload!["response"] as! String expectation.fulfill() } - + + var mock: Mock + // Foo Tests mock = try await foo.get(x: "string", y: 123, z: ["string in array"]) print(mock.result) @@ -206,4 +206,13 @@ class Tests: XCTestCase { mock = try await general.headers() print(mock.result) } + + func parse(from json: String) -> String? { + if let data = json.data(using: .utf8), + let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], + let result = jsonObject["result"] as? String { + return result + } + return nil + } } diff --git a/tests/languages/swift/Tests.swift b/tests/languages/swift/Tests.swift index d8054a6c6..7269011bd 100644 --- a/tests/languages/swift/Tests.swift +++ b/tests/languages/swift/Tests.swift @@ -25,12 +25,10 @@ class Tests: XCTestCase { .addHeader(key: "Origin", value: "http://localhost") .setSelfSigned() - var mock: Mock - // Ping pong test let ping = try await client.ping() - mock = Mock.from(json: ping)! - print(mock.result) + let pingResult = parse(from: ping)! + print(pingResult) // reset project client.setProject("console") @@ -39,6 +37,8 @@ class Tests: XCTestCase { let bar = Bar(client) let general = General(client) + var mock: Mock + // Foo Tests mock = try await foo.get(x: "string", y: 123, z: ["string in array"]) print(mock.result) @@ -197,4 +197,13 @@ class Tests: XCTestCase { print(error.localizedDescription) } } + + func parse(from json: String) -> String? { + if let data = json.data(using: .utf8), + let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], + let result = jsonObject["result"] as? String { + return result + } + return nil + } } From 1d644daacb761527de708bcfd761f047a86dfe21 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 12 Dec 2024 19:18:08 +0530 Subject: [PATCH 3/9] fix: remove unnecessary method. --- templates/swift/Sources/Models/Model.swift.twig | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/templates/swift/Sources/Models/Model.swift.twig b/templates/swift/Sources/Models/Model.swift.twig index 3be9caadb..cc3fbe6be 100644 --- a/templates/swift/Sources/Models/Model.swift.twig +++ b/templates/swift/Sources/Models/Model.swift.twig @@ -58,21 +58,5 @@ public class {{ definition | modelType(spec) | raw }} { {%~ endif %} ) } - - public static func from(json: String) -> {{ definition.name | caseUcfirst }}? { - guard let data = json.data(using: .utf8) else { - return nil - } - - do { - if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { - return {{ definition.name | caseUcfirst }}.from(map: jsonObject) - } else { - return nil - } - } catch { - return nil - } - } } {% endif %} \ No newline at end of file From 715e01dc92537fe699aecda1aac674c733b04dd9 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 10:59:00 +0530 Subject: [PATCH 4/9] add: ping to android kotlin. --- .../src/main/java/io/package/Client.kt.twig | 27 +++++++++++++++++++ tests/Android14Java11Test.php | 1 + tests/Android14Java17Test.php | 1 + tests/Android14Java8Test.php | 1 + tests/Android5Java17Test.php | 1 + tests/languages/android/Tests.kt | 22 +++++++++++++-- 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/templates/android/library/src/main/java/io/package/Client.kt.twig b/templates/android/library/src/main/java/io/package/Client.kt.twig index 7ddeac404..ddf40dbc4 100644 --- a/templates/android/library/src/main/java/io/package/Client.kt.twig +++ b/templates/android/library/src/main/java/io/package/Client.kt.twig @@ -211,6 +211,25 @@ class Client @JvmOverloads constructor( return this } + /** + * Sends a "ping" request to Appwrite to verify connectivity. + * + * @return String + */ + suspend fun ping(): String { + val apiPath = "/ping" + val apiParams = mutableMapOf() + val apiHeaders = mutableMapOf("content-type" to "application/json") + + return call( + "GET", + apiPath, + apiHeaders, + apiParams, + responseType = String::class.java + ) + } + /** * Send the HTTP request * @@ -489,6 +508,14 @@ class Client @JvmOverloads constructor( it.resume(true as T) return } + responseType == String::class.java -> { + val body = response.body!! + .charStream() + .buffered() + .use(BufferedReader::readText) + it.resume(body as T) + return + } responseType == ByteArray::class.java -> { it.resume(response.body!! .byteStream() diff --git a/tests/Android14Java11Test.php b/tests/Android14Java11Test.php index 4d3860788..23ca00f73 100644 --- a/tests/Android14Java11Test.php +++ b/tests/Android14Java11Test.php @@ -20,6 +20,7 @@ class Android14Java11Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk11 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/Android14Java17Test.php b/tests/Android14Java17Test.php index 0696252ce..78e17f3ce 100644 --- a/tests/Android14Java17Test.php +++ b/tests/Android14Java17Test.php @@ -20,6 +20,7 @@ class Android14Java17Test extends Base 'docker run --rm --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk17 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/Android14Java8Test.php b/tests/Android14Java8Test.php index ddbb53c94..280fabf7a 100644 --- a/tests/Android14Java8Test.php +++ b/tests/Android14Java8Test.php @@ -20,6 +20,7 @@ class Android14Java8Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-34-jdk8 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/Android5Java17Test.php b/tests/Android5Java17Test.php index e2d5b1bb6..1bf4ad99e 100644 --- a/tests/Android5Java17Test.php +++ b/tests/Android5Java17Test.php @@ -20,6 +20,7 @@ class Android5Java17Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/android alvrme/alpine-android:android-21-jdk17 sh -c "./gradlew :library:testReleaseUnitTest --stacktrace -q && cat library/result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/languages/android/Tests.kt b/tests/languages/android/Tests.kt index 1b6c42232..cb1139d08 100644 --- a/tests/languages/android/Tests.kt +++ b/tests/languages/android/Tests.kt @@ -29,6 +29,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.json.JSONObject import org.robolectric.annotation.Config import java.io.File import java.io.IOException @@ -61,10 +62,20 @@ class ServiceTest { @Throws(IOException::class) fun test() { val client = Client(ApplicationProvider.getApplicationContext()) - .setEndpointRealtime("wss://cloud.appwrite.io/v1") - .setProject("console") + .setProject("123456") .addHeader("Origin", "http://localhost") .setSelfSigned(true) + + runBlocking { + val ping = client.ping() + val pingResponse = parse(ping) + writeToFile(pingResponse) + } + + // reset configs + client.setProject("console") + .setEndpointRealtime("wss://cloud.appwrite.io/v1") + val foo = Foo(client) val bar = Bar(client) val general = General(client) @@ -219,4 +230,11 @@ class ServiceTest { File("result.txt").appendText(text) } + private fun parse(json: String): String? { + return try { + JSONObject(json).getString("result") + } catch (exception: Exception) { + null + } + } } \ No newline at end of file From 1b9793575b5fa98caac23b4253e74a785f34f0b6 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 11:14:31 +0530 Subject: [PATCH 5/9] feat: add ping to kotlin. --- .../main/kotlin/io/appwrite/Client.kt.twig | 27 +++++++++++++++++++ tests/KotlinJava11Test.php | 1 + tests/KotlinJava17Test.php | 1 + tests/KotlinJava8Test.php | 1 + tests/languages/kotlin/Tests.kt | 19 +++++++++++++ 5 files changed, 49 insertions(+) diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index d970c5101..2eceab281 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -170,6 +170,25 @@ class Client @JvmOverloads constructor( return this } + /** + * Sends a "ping" request to Appwrite to verify connectivity. + * + * @return String + */ + suspend fun ping(): String { + val apiPath = "/ping" + val apiParams = mutableMapOf() + val apiHeaders = mutableMapOf("content-type" to "application/json") + + return call( + "GET", + apiPath, + apiHeaders, + apiParams, + responseType = String::class.java + ) + } + /** * Prepare the HTTP request * @@ -536,6 +555,14 @@ class Client @JvmOverloads constructor( it.resume(true as T) return } + responseType == String::class.java -> { + val body = response.body!! + .charStream() + .buffered() + .use(BufferedReader::readText) + it.resume(body as T) + return + } responseType == ByteArray::class.java -> { it.resume(response.body!! .byteStream() diff --git a/tests/KotlinJava11Test.php b/tests/KotlinJava11Test.php index aa18a40ad..f97a93083 100644 --- a/tests/KotlinJava11Test.php +++ b/tests/KotlinJava11Test.php @@ -20,6 +20,7 @@ class KotlinJava11Test extends Base 'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:11-jdk-slim sh -c "./gradlew test -q && cat result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/KotlinJava17Test.php b/tests/KotlinJava17Test.php index e09d8afc6..41dfac724 100644 --- a/tests/KotlinJava17Test.php +++ b/tests/KotlinJava17Test.php @@ -20,6 +20,7 @@ class KotlinJava17Test extends Base 'docker run --network="mockapi" -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:17-jdk-slim sh -c "./gradlew test -q && cat result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/KotlinJava8Test.php b/tests/KotlinJava8Test.php index 1d569971e..bb030a4b4 100644 --- a/tests/KotlinJava8Test.php +++ b/tests/KotlinJava8Test.php @@ -20,6 +20,7 @@ class KotlinJava8Test extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/kotlin openjdk:8-jdk-slim sh -c "./gradlew test -q && cat result.txt"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/languages/kotlin/Tests.kt b/tests/languages/kotlin/Tests.kt index 13044b210..2358155e2 100644 --- a/tests/languages/kotlin/Tests.kt +++ b/tests/languages/kotlin/Tests.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.runBlocking import okhttp3.Response import org.junit.Before import org.junit.Test +import org.json.JSONObject import java.io.File import java.io.IOException import java.nio.file.Files @@ -38,8 +39,19 @@ class ServiceTest { @Throws(IOException::class) fun test() { val client = Client() + .setProject("123456") .addHeader("Origin", "http://localhost") .setSelfSigned(true) + + runBlocking { + val ping = client.ping() + val pingResponse = parse(ping) + writeToFile(pingResponse) + } + + // reset project + client.setProject("123456") + val foo = Foo(client) val bar = Bar(client) val general = General(client) @@ -185,4 +197,11 @@ class ServiceTest { File("result.txt").appendText(text) } + private fun parse(json: String): String? { + return try { + JSONObject(json).getString("result") + } catch (exception: Exception) { + null + } + } } \ No newline at end of file From cc1da05046c2aec5c90b80891e931e6f7969022d Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 11:30:47 +0530 Subject: [PATCH 6/9] fix: android and kotlin tests. --- tests/languages/android/Tests.kt | 3 +-- tests/languages/kotlin/Tests.kt | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/languages/android/Tests.kt b/tests/languages/android/Tests.kt index cb1139d08..3d9d7e985 100644 --- a/tests/languages/android/Tests.kt +++ b/tests/languages/android/Tests.kt @@ -29,7 +29,6 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.json.JSONObject import org.robolectric.annotation.Config import java.io.File import java.io.IOException @@ -232,7 +231,7 @@ class ServiceTest { private fun parse(json: String): String? { return try { - JSONObject(json).getString("result") + json.fromJson>()["result"] as? String } catch (exception: Exception) { null } diff --git a/tests/languages/kotlin/Tests.kt b/tests/languages/kotlin/Tests.kt index 2358155e2..42145de72 100644 --- a/tests/languages/kotlin/Tests.kt +++ b/tests/languages/kotlin/Tests.kt @@ -19,7 +19,6 @@ import kotlinx.coroutines.runBlocking import okhttp3.Response import org.junit.Before import org.junit.Test -import org.json.JSONObject import java.io.File import java.io.IOException import java.nio.file.Files @@ -199,7 +198,7 @@ class ServiceTest { private fun parse(json: String): String? { return try { - JSONObject(json).getString("result") + json.fromJson>()["result"] as? String } catch (exception: Exception) { null } From 9e9c7f3bfc8b652780f6d7302a85f3ed389b4453 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 12:19:17 +0530 Subject: [PATCH 7/9] add: ping to flutter sdk. --- templates/flutter/lib/src/client.dart.twig | 3 +++ .../flutter/lib/src/client_base.dart.twig | 3 +++ .../flutter/lib/src/client_browser.dart.twig | 8 +++++++ templates/flutter/lib/src/client_io.dart.twig | 8 +++++++ tests/FlutterBetaTest.php | 1 + tests/FlutterStableTest.php | 1 + tests/languages/flutter/tests.dart | 23 +++++++++++++++---- 7 files changed, 43 insertions(+), 4 deletions(-) diff --git a/templates/flutter/lib/src/client.dart.twig b/templates/flutter/lib/src/client.dart.twig index 5d139afd7..a9c71d8ba 100644 --- a/templates/flutter/lib/src/client.dart.twig +++ b/templates/flutter/lib/src/client.dart.twig @@ -65,6 +65,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(); + /// Send the API request. Future call(HttpMethod method, { String path = '', diff --git a/templates/flutter/lib/src/client_base.dart.twig b/templates/flutter/lib/src/client_base.dart.twig index be02c477a..3493c5e66 100644 --- a/templates/flutter/lib/src/client_base.dart.twig +++ b/templates/flutter/lib/src/client_base.dart.twig @@ -23,6 +23,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/flutter/lib/src/client_browser.dart.twig b/templates/flutter/lib/src/client_browser.dart.twig index e3bffef21..3bb749220 100644 --- a/templates/flutter/lib/src/client_browser.dart.twig +++ b/templates/flutter/lib/src/client_browser.dart.twig @@ -98,6 +98,14 @@ class ClientBrowser extends ClientBase with ClientMixin { return this; } + @override + Future ping() async { + final String apiPath = '/ping'; + final response = await call(HttpMethod.get, responseType: ResponseType.plain); + + return response.data; + } + Future init() async { final cookieFallback = web.window.localStorage['cookieFallback']; if (cookieFallback != null) { diff --git a/templates/flutter/lib/src/client_io.dart.twig b/templates/flutter/lib/src/client_io.dart.twig index 371dd51ee..b926d3031 100644 --- a/templates/flutter/lib/src/client_io.dart.twig +++ b/templates/flutter/lib/src/client_io.dart.twig @@ -130,6 +130,14 @@ class ClientIO extends ClientBase with ClientMixin { return this; } + @override + Future ping() async { + final String apiPath = '/ping'; + final response = await call(HttpMethod.get, responseType: ResponseType.plain); + + return response.data; + } + Future init() async { if(_initProgress) return; _initProgress = true; diff --git a/tests/FlutterBetaTest.php b/tests/FlutterBetaTest.php index 4a1057900..4ff381684 100644 --- a/tests/FlutterBetaTest.php +++ b/tests/FlutterBetaTest.php @@ -19,6 +19,7 @@ class FlutterBetaTest extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app -w /app/tests/sdks/flutter fischerscode/flutter-sudo:beta sh -c "sudo chown -R flutter:flutter . && flutter pub get && flutter test test/appwrite_test.dart"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/FlutterStableTest.php b/tests/FlutterStableTest.php index 183e3943c..6020390e9 100644 --- a/tests/FlutterStableTest.php +++ b/tests/FlutterStableTest.php @@ -19,6 +19,7 @@ class FlutterStableTest extends Base 'docker run --network="mockapi" --rm -v $(pwd):/app:rw -w /app/tests/sdks/flutter fischerscode/flutter-sudo:stable sh -c "sudo chown -R flutter:flutter . && flutter pub get && flutter test test/appwrite_test.dart"'; protected array $expectedOutput = [ + ...Base::PING_RESPONSE, ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, diff --git a/tests/languages/flutter/tests.dart b/tests/languages/flutter/tests.dart index 7a65d0896..042156ba4 100644 --- a/tests/languages/flutter/tests.dart +++ b/tests/languages/flutter/tests.dart @@ -7,6 +7,7 @@ import '../lib/models.dart'; import '../lib/enums.dart'; import '../lib/src/input_file.dart'; import 'dart:io'; +import 'dart:convert'; class FakePathProvider extends PathProviderPlatform { @override @@ -20,17 +21,23 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); PathProviderPlatform.instance = FakePathProvider(); Client client = Client() + .setProject('123456') .addHeader("Origin", "http://localhost") .setSelfSigned(); - Foo foo = Foo(client); - Bar bar = Bar(client); - General general = General(client); - client.setSelfSigned(); + final ping = await client.ping(); + final pingResponse = parse(ping)!; + print(pingResponse); + + // reset configs client.setProject('console'); client.setEndPointRealtime( "wss://cloud.appwrite.io/v1"); + Foo foo = Foo(client); + Bar bar = Bar(client); + General general = General(client); + Realtime realtime = Realtime(client); final rtsub = realtime.subscribe(["tests"]); @@ -189,3 +196,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; + } +} \ No newline at end of file From 475a5df311c0e76f9316b6cc07f83e6b252b7376 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 12:50:09 +0530 Subject: [PATCH 8/9] fix: tests. --- .../flutter/lib/src/client_browser.dart.twig | 2 +- templates/flutter/lib/src/client_io.dart.twig | 2 +- tests/languages/flutter/tests.dart | 25 +++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/templates/flutter/lib/src/client_browser.dart.twig b/templates/flutter/lib/src/client_browser.dart.twig index 3bb749220..22b0d6bc6 100644 --- a/templates/flutter/lib/src/client_browser.dart.twig +++ b/templates/flutter/lib/src/client_browser.dart.twig @@ -101,7 +101,7 @@ class ClientBrowser extends ClientBase with ClientMixin { @override Future ping() async { final String apiPath = '/ping'; - final response = await call(HttpMethod.get, responseType: ResponseType.plain); + final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain); return response.data; } diff --git a/templates/flutter/lib/src/client_io.dart.twig b/templates/flutter/lib/src/client_io.dart.twig index b926d3031..f93ab9ac1 100644 --- a/templates/flutter/lib/src/client_io.dart.twig +++ b/templates/flutter/lib/src/client_io.dart.twig @@ -133,7 +133,7 @@ class ClientIO extends ClientBase with ClientMixin { @override Future ping() async { final String apiPath = '/ping'; - final response = await call(HttpMethod.get, responseType: ResponseType.plain); + final response = await call(HttpMethod.get, path: apiPath, responseType: ResponseType.plain); return response.data; } diff --git a/tests/languages/flutter/tests.dart b/tests/languages/flutter/tests.dart index 042156ba4..f7b27a4c8 100644 --- a/tests/languages/flutter/tests.dart +++ b/tests/languages/flutter/tests.dart @@ -25,27 +25,32 @@ void main() async { .addHeader("Origin", "http://localhost") .setSelfSigned(); - final ping = await client.ping(); - final pingResponse = parse(ping)!; - print(pingResponse); + Foo foo = Foo(client); + Bar bar = Bar(client); + General general = General(client); - // reset configs + client.setSelfSigned(); client.setProject('console'); client.setEndPointRealtime( "wss://cloud.appwrite.io/v1"); - Foo foo = Foo(client); - Bar bar = Bar(client); - General general = General(client); - Realtime realtime = Realtime(client); - final rtsub = realtime.subscribe(["tests"]); + final rtsub = realtime.subscribe(["tests"]); await Future.delayed(Duration(seconds: 5)); client.addHeader('Origin', 'http://localhost'); - // Foo Tests print('\nTest Started'); + // Ping pong tests + 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']); print(response.result); From b30d003262b5599d1ebe388b1dff0e1c8d546701 Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 13 Dec 2024 12:54:03 +0530 Subject: [PATCH 9/9] 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; + } +}