Skip to content

Commit

Permalink
feat: add ping to apple and wsift sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzNotABug committed Dec 12, 2024
1 parent a852aba commit dc47e63
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 13 deletions.
28 changes: 25 additions & 3 deletions templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -309,7 +332,6 @@ open class Client {
}
default:
var message = ""
var data = try await response.body.collect(upTo: Int.max)
var type = ""

do {
Expand Down
28 changes: 25 additions & 3 deletions templates/swift/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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
}
Expand All @@ -345,7 +368,6 @@ open class Client {
}
default:
var message = ""
var data = try await response.body.collect(upTo: Int.max)
var type = ""

do {
Expand Down
16 changes: 16 additions & 0 deletions templates/swift/Sources/Models/Model.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
1 change: 1 addition & 0 deletions tests/AppleSwift56Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/Swift56Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions tests/languages/apple/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions tests/languages/swift/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dc47e63

Please sign in to comment.