Skip to content

Commit 7b1cb56

Browse files
authored
Merge pull request #77 from appwrite/dev
chore: add setDevKey and upsertDocument methods
2 parents 66557e5 + 151e7a0 commit 7b1cb56

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
# Change Log
1+
# Change Log
2+
3+
## 8.0.0
4+
5+
* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage
6+
* Update default `quality` for `getFilePreview` from 0 to -1
7+
* Remove `Gif` from ImageFormat enum
8+
* Remove `search` param from `listExecutions` method

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ repositories {
3838
Next, add the dependency to your project's `build.gradle(.kts)` file:
3939

4040
```groovy
41-
implementation("io.appwrite:sdk-for-android:8.0.0")
41+
implementation("io.appwrite:sdk-for-android:8.1.0")
4242
```
4343

4444
### Maven
@@ -49,7 +49,7 @@ Add this to your project's `pom.xml` file:
4949
<dependency>
5050
<groupId>io.appwrite</groupId>
5151
<artifactId>sdk-for-android</artifactId>
52-
<version>8.0.0</version>
52+
<version>8.1.0</version>
5353
</dependency>
5454
</dependencies>
5555
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import io.appwrite.Client;
2+
import io.appwrite.coroutines.CoroutineCallback;
3+
import io.appwrite.services.Databases;
4+
5+
Client client = new Client(context)
6+
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
7+
.setProject("<YOUR_PROJECT_ID>"); // Your project ID
8+
9+
Databases databases = new Databases(client);
10+
11+
databases.upsertDocument(
12+
"<DATABASE_ID>", // databaseId
13+
"<COLLECTION_ID>", // collectionId
14+
"<DOCUMENT_ID>", // documentId
15+
mapOf( "a" to "b" ), // data
16+
listOf("read("any")"), // permissions (optional)
17+
new CoroutineCallback<>((result, error) -> {
18+
if (error != null) {
19+
error.printStackTrace();
20+
return;
21+
}
22+
23+
Log.d("Appwrite", result.toString());
24+
})
25+
);
26+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import io.appwrite.Client
2+
import io.appwrite.coroutines.CoroutineCallback
3+
import io.appwrite.services.Databases
4+
5+
val client = Client(context)
6+
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
7+
.setProject("<YOUR_PROJECT_ID>") // Your project ID
8+
9+
val databases = Databases(client)
10+
11+
val result = databases.upsertDocument(
12+
databaseId = "<DATABASE_ID>",
13+
collectionId = "<COLLECTION_ID>",
14+
documentId = "<DOCUMENT_ID>",
15+
data = mapOf( "a" to "b" ),
16+
permissions = listOf("read("any")"), // (optional)
17+
)

library/src/main/java/io/appwrite/Client.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Client @JvmOverloads constructor(
8787
"x-sdk-name" to "Android",
8888
"x-sdk-platform" to "client",
8989
"x-sdk-language" to "android",
90-
"x-sdk-version" to "8.0.0",
90+
"x-sdk-version" to "8.1.0",
9191
"x-appwrite-response-format" to "1.7.0"
9292
)
9393
config = mutableMapOf()
@@ -153,6 +153,21 @@ class Client @JvmOverloads constructor(
153153
return this
154154
}
155155

156+
/**
157+
* Set DevKey
158+
*
159+
* Your secret dev API key
160+
*
161+
* @param {string} devkey
162+
*
163+
* @return this
164+
*/
165+
fun setDevKey(value: String): Client {
166+
config["devKey"] = value
167+
addHeader("x-appwrite-dev-key", value)
168+
return this
169+
}
170+
156171
/**
157172
* Set self Signed
158173
*

library/src/main/java/io/appwrite/services/Databases.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,78 @@ class Databases(client: Client) : Service(client) {
211211
nestedType = classOf(),
212212
)
213213

214+
/**
215+
* 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.
216+
*
217+
* @param databaseId Database ID.
218+
* @param collectionId Collection ID.
219+
* @param documentId Document ID.
220+
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
221+
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
222+
* @return [io.appwrite.models.Document<T>]
223+
*/
224+
@JvmOverloads
225+
suspend fun <T> upsertDocument(
226+
databaseId: String,
227+
collectionId: String,
228+
documentId: String,
229+
data: Any,
230+
permissions: List<String>? = null,
231+
nestedType: Class<T>,
232+
): io.appwrite.models.Document<T> {
233+
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
234+
.replace("{databaseId}", databaseId)
235+
.replace("{collectionId}", collectionId)
236+
.replace("{documentId}", documentId)
237+
238+
val apiParams = mutableMapOf<String, Any?>(
239+
"data" to data,
240+
"permissions" to permissions,
241+
)
242+
val apiHeaders = mutableMapOf<String, String>(
243+
"content-type" to "application/json",
244+
)
245+
val converter: (Any) -> io.appwrite.models.Document<T> = {
246+
@Suppress("UNCHECKED_CAST")
247+
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
248+
}
249+
return client.call(
250+
"PUT",
251+
apiPath,
252+
apiHeaders,
253+
apiParams,
254+
responseType = classOf(),
255+
converter,
256+
)
257+
}
258+
259+
/**
260+
* 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.
261+
*
262+
* @param databaseId Database ID.
263+
* @param collectionId Collection ID.
264+
* @param documentId Document ID.
265+
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
266+
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
267+
* @return [io.appwrite.models.Document<T>]
268+
*/
269+
@JvmOverloads
270+
@Throws(AppwriteException::class)
271+
suspend fun upsertDocument(
272+
databaseId: String,
273+
collectionId: String,
274+
documentId: String,
275+
data: Any,
276+
permissions: List<String>? = null,
277+
): io.appwrite.models.Document<Map<String, Any>> = upsertDocument(
278+
databaseId,
279+
collectionId,
280+
documentId,
281+
data,
282+
permissions,
283+
nestedType = classOf(),
284+
)
285+
214286
/**
215287
* Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated.
216288
*

0 commit comments

Comments
 (0)