Skip to content

chore: add setDevKey and upsertDocument methods #77

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

Merged
merged 3 commits into from
May 21, 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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# Change Log
# Change Log

## 8.0.0

* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage
* Update default `quality` for `getFilePreview` from 0 to -1
* Remove `Gif` from ImageFormat enum
* Remove `search` param from `listExecutions` method
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repositories {
Next, add the dependency to your project's `build.gradle(.kts)` file:

```groovy
implementation("io.appwrite:sdk-for-android:8.0.0")
implementation("io.appwrite:sdk-for-android:8.1.0")
```

### Maven
Expand All @@ -49,7 +49,7 @@ Add this to your project's `pom.xml` file:
<dependency>
<groupId>io.appwrite</groupId>
<artifactId>sdk-for-android</artifactId>
<version>8.0.0</version>
<version>8.1.0</version>
</dependency>
</dependencies>
```
Expand Down
26 changes: 26 additions & 0 deletions docs/examples/java/databases/upsert-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import io.appwrite.Client;
import io.appwrite.coroutines.CoroutineCallback;
import io.appwrite.services.Databases;

Client client = new Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>"); // Your project ID

Databases databases = new Databases(client);

databases.upsertDocument(
"<DATABASE_ID>", // databaseId
"<COLLECTION_ID>", // collectionId
"<DOCUMENT_ID>", // documentId
mapOf( "a" to "b" ), // data
listOf("read("any")"), // permissions (optional)
new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}

Log.d("Appwrite", result.toString());
})
);

17 changes: 17 additions & 0 deletions docs/examples/kotlin/databases/upsert-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import io.appwrite.Client
import io.appwrite.coroutines.CoroutineCallback
import io.appwrite.services.Databases

val client = Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>") // Your project ID

val databases = Databases(client)

val result = databases.upsertDocument(
databaseId = "<DATABASE_ID>",
collectionId = "<COLLECTION_ID>",
documentId = "<DOCUMENT_ID>",
data = mapOf( "a" to "b" ),
permissions = listOf("read("any")"), // (optional)
)
17 changes: 16 additions & 1 deletion library/src/main/java/io/appwrite/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Client @JvmOverloads constructor(
"x-sdk-name" to "Android",
"x-sdk-platform" to "client",
"x-sdk-language" to "android",
"x-sdk-version" to "8.0.0",
"x-sdk-version" to "8.1.0",
"x-appwrite-response-format" to "1.7.0"
)
config = mutableMapOf()
Expand Down Expand Up @@ -153,6 +153,21 @@ class Client @JvmOverloads constructor(
return this
}

/**
* Set DevKey
*
* Your secret dev API key
*
* @param {string} devkey
*
* @return this
*/
fun setDevKey(value: String): Client {
config["devKey"] = value
addHeader("x-appwrite-dev-key", value)
return this
}

/**
* Set self Signed
*
Expand Down
72 changes: 72 additions & 0 deletions library/src/main/java/io/appwrite/services/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,78 @@ class Databases(client: Client) : Service(client) {
nestedType = classOf(),
)

/**
* 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 databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
suspend fun <T> upsertDocument(
databaseId: String,
collectionId: String,
documentId: String,
data: Any,
permissions: List<String>? = null,
nestedType: Class<T>,
): io.appwrite.models.Document<T> {
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}"
.replace("{databaseId}", databaseId)
.replace("{collectionId}", collectionId)
.replace("{documentId}", documentId)

val apiParams = mutableMapOf<String, Any?>(
"data" to data,
"permissions" to permissions,
)
val apiHeaders = mutableMapOf<String, String>(
"content-type" to "application/json",
)
val converter: (Any) -> io.appwrite.models.Document<T> = {
@Suppress("UNCHECKED_CAST")
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
}
return client.call(
"PUT",
apiPath,
apiHeaders,
apiParams,
responseType = classOf(),
converter,
)
}

/**
* 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 databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param data Document data as JSON object. Include all required attributes of the document to be created or updated.
* @param permissions An array of permissions strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
@Throws(AppwriteException::class)
suspend fun upsertDocument(
databaseId: String,
collectionId: String,
documentId: String,
data: Any,
permissions: List<String>? = null,
): io.appwrite.models.Document<Map<String, Any>> = upsertDocument(
databaseId,
collectionId,
documentId,
data,
permissions,
nestedType = classOf(),
)

/**
* Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated.
*
Expand Down