Skip to content

Commit 04fc109

Browse files
authored
Merge pull request #75 from appwrite/dev
chore: add setDevKey and upsertDocument methods
2 parents 642ae39 + a19ee5d commit 04fc109

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:
3131

3232
```swift
3333
dependencies: [
34-
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.0.0"),
34+
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.1.0"),
3535
],
3636
```
3737

Sources/Appwrite/Client.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class Client {
2323
"x-sdk-name": "Apple",
2424
"x-sdk-platform": "client",
2525
"x-sdk-language": "apple",
26-
"x-sdk-version": "10.0.0",
26+
"x-sdk-version": "10.1.0",
2727
"x-appwrite-response-format": "1.7.0"
2828
]
2929

@@ -147,6 +147,21 @@ open class Client {
147147
return self
148148
}
149149

150+
///
151+
/// Set DevKey
152+
///
153+
/// Your secret dev API key
154+
///
155+
/// @param String value
156+
///
157+
/// @return Client
158+
///
159+
open func setDevKey(_ value: String) -> Client {
160+
config["devkey"] = value
161+
_ = addHeader(key: "X-Appwrite-Dev-Key", value: value)
162+
return self
163+
}
164+
150165

151166
///
152167
/// Set self signed

Sources/Appwrite/Services/Databases.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,86 @@ open class Databases: Service {
218218
)
219219
}
220220

221+
///
222+
/// Create or update a Document. Before using this route, you should create a
223+
/// new collection resource using either a [server
224+
/// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
225+
/// API or directly from your database console.
226+
///
227+
/// @param String databaseId
228+
/// @param String collectionId
229+
/// @param String documentId
230+
/// @param Any data
231+
/// @param [String] permissions
232+
/// @throws Exception
233+
/// @return array
234+
///
235+
open func upsertDocument<T>(
236+
databaseId: String,
237+
collectionId: String,
238+
documentId: String,
239+
data: Any,
240+
permissions: [String]? = nil,
241+
nestedType: T.Type
242+
) async throws -> AppwriteModels.Document<T> {
243+
let apiPath: String = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
244+
.replacingOccurrences(of: "{databaseId}", with: databaseId)
245+
.replacingOccurrences(of: "{collectionId}", with: collectionId)
246+
.replacingOccurrences(of: "{documentId}", with: documentId)
247+
248+
let apiParams: [String: Any?] = [
249+
"data": data,
250+
"permissions": permissions
251+
]
252+
253+
let apiHeaders: [String: String] = [
254+
"content-type": "application/json"
255+
]
256+
257+
let converter: (Any) -> AppwriteModels.Document<T> = { response in
258+
return AppwriteModels.Document.from(map: response as! [String: Any])
259+
}
260+
261+
return try await client.call(
262+
method: "PUT",
263+
path: apiPath,
264+
headers: apiHeaders,
265+
params: apiParams,
266+
converter: converter
267+
)
268+
}
269+
270+
///
271+
/// Create or update a Document. Before using this route, you should create a
272+
/// new collection resource using either a [server
273+
/// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
274+
/// API or directly from your database console.
275+
///
276+
/// @param String databaseId
277+
/// @param String collectionId
278+
/// @param String documentId
279+
/// @param Any data
280+
/// @param [String] permissions
281+
/// @throws Exception
282+
/// @return array
283+
///
284+
open func upsertDocument(
285+
databaseId: String,
286+
collectionId: String,
287+
documentId: String,
288+
data: Any,
289+
permissions: [String]? = nil
290+
) async throws -> AppwriteModels.Document<[String: AnyCodable]> {
291+
return try await upsertDocument(
292+
databaseId: databaseId,
293+
collectionId: collectionId,
294+
documentId: documentId,
295+
data: data,
296+
permissions: permissions,
297+
nestedType: [String: AnyCodable].self
298+
)
299+
}
300+
221301
///
222302
/// Update a document by its unique ID. Using the patch method you can pass
223303
/// only specific fields that will get updated.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Appwrite
2+
3+
let client = Client()
4+
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
5+
.setProject("<YOUR_PROJECT_ID>") // Your project ID
6+
7+
let databases = Databases(client)
8+
9+
let document = try await databases.upsertDocument(
10+
databaseId: "<DATABASE_ID>",
11+
collectionId: "<COLLECTION_ID>",
12+
documentId: "<DOCUMENT_ID>",
13+
data: [:],
14+
permissions: ["read("any")"] // optional
15+
)
16+

0 commit comments

Comments
 (0)