From 17e51b77b9942a0c3ad4cbde6d851566925b81ff Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 May 2025 05:14:22 +0000 Subject: [PATCH 1/3] chore: add setDevKey and upsertDocument methods --- README.md | 2 +- Sources/Appwrite/Client.swift | 2 +- Sources/Appwrite/Services/Databases.swift | 68 +++++++++++++++++++++ docs/examples/databases/create-documents.md | 14 +++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 docs/examples/databases/create-documents.md diff --git a/README.md b/README.md index 93433a6..87a6cbc 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies: ```swift dependencies: [ - .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "10.0.0"), + .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "10.0.1"), ], ``` diff --git a/Sources/Appwrite/Client.swift b/Sources/Appwrite/Client.swift index 8e8ad56..8780957 100644 --- a/Sources/Appwrite/Client.swift +++ b/Sources/Appwrite/Client.swift @@ -23,7 +23,7 @@ open class Client { "x-sdk-name": "Apple", "x-sdk-platform": "client", "x-sdk-language": "apple", - "x-sdk-version": "10.0.0", + "x-sdk-version": "10.0.1", "x-appwrite-response-format": "1.7.0" ] diff --git a/Sources/Appwrite/Services/Databases.swift b/Sources/Appwrite/Services/Databases.swift index d370f73..6b8b55f 100644 --- a/Sources/Appwrite/Services/Databases.swift +++ b/Sources/Appwrite/Services/Databases.swift @@ -150,6 +150,74 @@ open class Databases: Service { ) } + /// + /// Create new Documents. Before using this route, you should create a new + /// collection resource using either a [server + /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) + /// API or directly from your database console. + /// + /// @param String databaseId + /// @param String collectionId + /// @param [Any] documents + /// @throws Exception + /// @return array + /// + open func createDocuments( + databaseId: String, + collectionId: String, + documents: [Any], + nestedType: T.Type + ) async throws -> AppwriteModels.DocumentList { + let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents" + .replacingOccurrences(of: "{databaseId}", with: databaseId) + .replacingOccurrences(of: "{collectionId}", with: collectionId) + + let apiParams: [String: Any?] = [ + "documents": documents + ] + + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] + + let converter: (Any) -> AppwriteModels.DocumentList = { response in + return AppwriteModels.DocumentList.from(map: response as! [String: Any]) + } + + return try await client.call( + method: "POST", + path: apiPath, + headers: apiHeaders, + params: apiParams, + converter: converter + ) + } + + /// + /// Create new Documents. Before using this route, you should create a new + /// collection resource using either a [server + /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) + /// API or directly from your database console. + /// + /// @param String databaseId + /// @param String collectionId + /// @param [Any] documents + /// @throws Exception + /// @return array + /// + open func createDocuments( + databaseId: String, + collectionId: String, + documents: [Any] + ) async throws -> AppwriteModels.DocumentList<[String: AnyCodable]> { + return try await createDocuments( + databaseId: databaseId, + collectionId: collectionId, + documents: documents, + nestedType: [String: AnyCodable].self + ) + } + /// /// Get a document by its unique ID. This endpoint response returns a JSON /// object with the document data. diff --git a/docs/examples/databases/create-documents.md b/docs/examples/databases/create-documents.md new file mode 100644 index 0000000..b47247a --- /dev/null +++ b/docs/examples/databases/create-documents.md @@ -0,0 +1,14 @@ +import Appwrite + +let client = Client() + .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint + .setKey("") // + +let databases = Databases(client) + +let documentList = try await databases.createDocuments( + databaseId: "", + collectionId: "", + documents: [] +) + From f3bd5de3d0ad1ad7fdc627733987bdbdecabdba7 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 May 2025 05:16:17 +0000 Subject: [PATCH 2/3] chore: add setDevkey and upsertDocument methods --- Sources/Appwrite/Client.swift | 15 +++ Sources/Appwrite/Services/Databases.swift | 94 +++++++++++-------- ...create-documents.md => upsert-document.md} | 8 +- 3 files changed, 73 insertions(+), 44 deletions(-) rename docs/examples/databases/{create-documents.md => upsert-document.md} (53%) diff --git a/Sources/Appwrite/Client.swift b/Sources/Appwrite/Client.swift index 8780957..a8a1ca0 100644 --- a/Sources/Appwrite/Client.swift +++ b/Sources/Appwrite/Client.swift @@ -147,6 +147,21 @@ open class Client { return self } + /// + /// Set DevKey + /// + /// Your secret dev API key + /// + /// @param String value + /// + /// @return Client + /// + open func setDevKey(_ value: String) -> Client { + config["devkey"] = value + _ = addHeader(key: "X-Appwrite-Dev-Key", value: value) + return self + } + /// /// Set self signed diff --git a/Sources/Appwrite/Services/Databases.swift b/Sources/Appwrite/Services/Databases.swift index 6b8b55f..239f4bc 100644 --- a/Sources/Appwrite/Services/Databases.swift +++ b/Sources/Appwrite/Services/Databases.swift @@ -151,41 +151,40 @@ open class Databases: Service { } /// - /// Create new Documents. Before using this route, you should create a new - /// collection resource using either a [server - /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) - /// API or directly from your database console. + /// Get a document by its unique ID. This endpoint response returns a JSON + /// object with the document data. /// /// @param String databaseId /// @param String collectionId - /// @param [Any] documents + /// @param String documentId + /// @param [String] queries /// @throws Exception /// @return array /// - open func createDocuments( + open func getDocument( databaseId: String, collectionId: String, - documents: [Any], + documentId: String, + queries: [String]? = nil, nestedType: T.Type - ) async throws -> AppwriteModels.DocumentList { - let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents" + ) async throws -> AppwriteModels.Document { + let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" .replacingOccurrences(of: "{databaseId}", with: databaseId) .replacingOccurrences(of: "{collectionId}", with: collectionId) + .replacingOccurrences(of: "{documentId}", with: documentId) let apiParams: [String: Any?] = [ - "documents": documents + "queries": queries ] - let apiHeaders: [String: String] = [ - "content-type": "application/json" - ] + let apiHeaders: [String: String] = [:] - let converter: (Any) -> AppwriteModels.DocumentList = { response in - return AppwriteModels.DocumentList.from(map: response as! [String: Any]) + let converter: (Any) -> AppwriteModels.Document = { response in + return AppwriteModels.Document.from(map: response as! [String: Any]) } return try await client.call( - method: "POST", + method: "GET", path: apiPath, headers: apiHeaders, params: apiParams, @@ -194,46 +193,51 @@ open class Databases: Service { } /// - /// Create new Documents. Before using this route, you should create a new - /// collection resource using either a [server - /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) - /// API or directly from your database console. + /// Get a document by its unique ID. This endpoint response returns a JSON + /// object with the document data. /// /// @param String databaseId /// @param String collectionId - /// @param [Any] documents + /// @param String documentId + /// @param [String] queries /// @throws Exception /// @return array /// - open func createDocuments( + open func getDocument( databaseId: String, collectionId: String, - documents: [Any] - ) async throws -> AppwriteModels.DocumentList<[String: AnyCodable]> { - return try await createDocuments( + documentId: String, + queries: [String]? = nil + ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { + return try await getDocument( databaseId: databaseId, collectionId: collectionId, - documents: documents, + documentId: documentId, + queries: queries, nestedType: [String: AnyCodable].self ) } /// - /// Get a document by its unique ID. This endpoint response returns a JSON - /// object with the document data. + /// Create or update a Document. Before using this route, you should create a + /// new collection resource using either a [server + /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) + /// API or directly from your database console. /// /// @param String databaseId /// @param String collectionId /// @param String documentId - /// @param [String] queries + /// @param Any data + /// @param [String] permissions /// @throws Exception /// @return array /// - open func getDocument( + open func upsertDocument( databaseId: String, collectionId: String, documentId: String, - queries: [String]? = nil, + data: Any, + permissions: [String]? = nil, nestedType: T.Type ) async throws -> AppwriteModels.Document { let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}" @@ -242,17 +246,20 @@ open class Databases: Service { .replacingOccurrences(of: "{documentId}", with: documentId) let apiParams: [String: Any?] = [ - "queries": queries + "data": data, + "permissions": permissions ] - let apiHeaders: [String: String] = [:] + let apiHeaders: [String: String] = [ + "content-type": "application/json" + ] let converter: (Any) -> AppwriteModels.Document = { response in return AppwriteModels.Document.from(map: response as! [String: Any]) } return try await client.call( - method: "GET", + method: "PUT", path: apiPath, headers: apiHeaders, params: apiParams, @@ -261,27 +268,32 @@ open class Databases: Service { } /// - /// Get a document by its unique ID. This endpoint response returns a JSON - /// object with the document data. + /// Create or update a Document. Before using this route, you should create a + /// new collection resource using either a [server + /// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) + /// API or directly from your database console. /// /// @param String databaseId /// @param String collectionId /// @param String documentId - /// @param [String] queries + /// @param Any data + /// @param [String] permissions /// @throws Exception /// @return array /// - open func getDocument( + open func upsertDocument( databaseId: String, collectionId: String, documentId: String, - queries: [String]? = nil + data: Any, + permissions: [String]? = nil ) async throws -> AppwriteModels.Document<[String: AnyCodable]> { - return try await getDocument( + return try await upsertDocument( databaseId: databaseId, collectionId: collectionId, documentId: documentId, - queries: queries, + data: data, + permissions: permissions, nestedType: [String: AnyCodable].self ) } diff --git a/docs/examples/databases/create-documents.md b/docs/examples/databases/upsert-document.md similarity index 53% rename from docs/examples/databases/create-documents.md rename to docs/examples/databases/upsert-document.md index b47247a..3e1bf83 100644 --- a/docs/examples/databases/create-documents.md +++ b/docs/examples/databases/upsert-document.md @@ -2,13 +2,15 @@ import Appwrite let client = Client() .setEndpoint("https://.cloud.appwrite.io/v1") // Your API Endpoint - .setKey("") // + .setProject("") // Your project ID let databases = Databases(client) -let documentList = try await databases.createDocuments( +let document = try await databases.upsertDocument( databaseId: "", collectionId: "", - documents: [] + documentId: "", + data: [:], + permissions: ["read("any")"] // optional ) From a19ee5d2c518672685e147a123790f62dc6530fb Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 May 2025 06:27:17 +0000 Subject: [PATCH 3/3] chore: bump to minor version --- README.md | 2 +- Sources/Appwrite/Client.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 87a6cbc..172967b 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies: ```swift dependencies: [ - .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "10.0.1"), + .package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "10.1.0"), ], ``` diff --git a/Sources/Appwrite/Client.swift b/Sources/Appwrite/Client.swift index a8a1ca0..55ebf0e 100644 --- a/Sources/Appwrite/Client.swift +++ b/Sources/Appwrite/Client.swift @@ -23,7 +23,7 @@ open class Client { "x-sdk-name": "Apple", "x-sdk-platform": "client", "x-sdk-language": "apple", - "x-sdk-version": "10.0.1", + "x-sdk-version": "10.1.0", "x-appwrite-response-format": "1.7.0" ]