Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pong response & chunked upload #27

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2024 Appwrite (https://appwrite.io) and individual contributors.
Copyright (c) 2025 Appwrite (https://appwrite.io) and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-swift.git", from: "6.2.0"),
.package(url: "[email protected]:appwrite/sdk-for-swift.git", from: "7.0.0"),
],
```

Expand Down
32 changes: 27 additions & 5 deletions Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class Client {
"x-sdk-name": "Swift",
"x-sdk-platform": "server",
"x-sdk-language": "swift",
"x-sdk-version": "6.2.0",
"x-sdk-version": "7.0.0",
"x-appwrite-response-format": "1.6.0"
]

Expand Down Expand Up @@ -257,6 +257,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 @@ -392,15 +412,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 @@ -410,7 +433,6 @@ open class Client {
}
default:
var message = ""
var data = try await response.body.collect(upTo: Int.max)
var type = ""

do {
Expand Down Expand Up @@ -466,7 +488,7 @@ open class Client {
var offset = 0
var result = [String:Any]()

if idParamName != nil && params[idParamName!] as! String != "unique()" {
if idParamName != nil {
// Make a request to check if a file already exists
do {
let map = try await call(
Expand Down
10 changes: 8 additions & 2 deletions Sources/Appwrite/Services/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ open class Account: Service {
open func updateMfaChallenge(
challengeId: String,
otp: String
) async throws -> Any {
) async throws -> AppwriteModels.Session {
let apiPath: String = "/account/mfa/challenge"

let apiParams: [String: Any?] = [
Expand All @@ -596,11 +596,17 @@ open class Account: Service {
"content-type": "application/json"
]

let converter: (Any) -> AppwriteModels.Session = { response in
return AppwriteModels.Session.from(map: response as! [String: Any])
}

return try await client.call(
method: "PUT",
path: apiPath,
headers: apiHeaders,
params: apiParams )
params: apiParams,
converter: converter
)
}

///
Expand Down
12 changes: 12 additions & 0 deletions Sources/Appwrite/Services/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ open class Functions: Service {
///
/// Rebuild deployment
///
/// Create a new build for an existing function deployment. This endpoint
/// allows you to rebuild a deployment with the updated function configuration,
/// including its entrypoint and build commands if they have been modified The
/// build process will be queued and executed asynchronously. The original
/// deployment's code will be preserved and used for the new build.
///
/// @param String functionId
/// @param String deploymentId
/// @param String buildId
Expand Down Expand Up @@ -605,6 +611,12 @@ open class Functions: Service {
///
/// Cancel deployment
///
/// Cancel an ongoing function deployment build. If the build is already in
/// progress, it will be stopped and marked as canceled. If the build hasn't
/// started yet, it will be marked as canceled without executing. You cannot
/// cancel builds that have already completed (status 'ready') or failed. The
/// response includes the final build status and details.
///
/// @param String functionId
/// @param String deploymentId
/// @throws Exception
Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/Services/Messaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ open class Messaging: Service {
///
/// Update SMS
///
/// Update an email message by its unique ID.
/// Update an SMS message by its unique ID.
///
///
/// @param String messageId
Expand Down
36 changes: 4 additions & 32 deletions Sources/Appwrite/Services/Users.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1217,11 +1217,10 @@ open class Users: Service {
/// @throws Exception
/// @return array
///
open func deleteMfaAuthenticator<T>(
open func deleteMfaAuthenticator(
userId: String,
type: AppwriteEnums.AuthenticatorType,
nestedType: T.Type
) async throws -> AppwriteModels.User<T> {
type: AppwriteEnums.AuthenticatorType
) async throws -> Any {
let apiPath: String = "/users/{userId}/mfa/authenticators/{type}"
.replacingOccurrences(of: "{userId}", with: userId)
.replacingOccurrences(of: "{type}", with: type.rawValue)
Expand All @@ -1232,38 +1231,11 @@ open class Users: Service {
"content-type": "application/json"
]

let converter: (Any) -> AppwriteModels.User<T> = { response in
return AppwriteModels.User.from(map: response as! [String: Any])
}

return try await client.call(
method: "DELETE",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: converter
)
}

///
/// Delete authenticator
///
/// Delete an authenticator app.
///
/// @param String userId
/// @param AppwriteEnums.AuthenticatorType type
/// @throws Exception
/// @return array
///
open func deleteMfaAuthenticator(
userId: String,
type: AppwriteEnums.AuthenticatorType
) async throws -> AppwriteModels.User<[String: AnyCodable]> {
return try await deleteMfaAuthenticator(
userId: userId,
type: type,
nestedType: [String: AnyCodable].self
)
params: apiParams )
}

///
Expand Down
1 change: 1 addition & 0 deletions Sources/AppwriteEnums/ImageFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum ImageFormat: String, CustomStringConvertible {
case gif = "gif"
case png = "png"
case webp = "webp"
case heic = "heic"
case avif = "avif"

public var description: String {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/account/update-mfa-challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let client = Client()

let account = Account(client)

let result = try await account.updateMfaChallenge(
let session = try await account.updateMfaChallenge(
challengeId: "<CHALLENGE_ID>",
otp: "<OTP>"
)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/users/delete-mfa-authenticator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let client = Client()

let users = Users(client)

let user = try await users.deleteMfaAuthenticator(
let result = try await users.deleteMfaAuthenticator(
userId: "<USER_ID>",
type: .totp
)
Expand Down