mirror of
https://github.com/appwrite/sdk-for-android.git
synced 2026-04-07 19:17:49 +00:00
chore: add setDevkey and upsertDocument methods
This commit is contained in:
+5
-3
@@ -4,14 +4,16 @@ import io.appwrite.services.Databases;
|
||||
|
||||
Client client = new Client(context)
|
||||
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
|
||||
.setKey(""); //
|
||||
.setProject("<YOUR_PROJECT_ID>"); // Your project ID
|
||||
|
||||
Databases databases = new Databases(client);
|
||||
|
||||
databases.createDocuments(
|
||||
databases.upsertDocument(
|
||||
"<DATABASE_ID>", // databaseId
|
||||
"<COLLECTION_ID>", // collectionId
|
||||
listOf(), // documents
|
||||
"<DOCUMENT_ID>", // documentId
|
||||
mapOf( "a" to "b" ), // data
|
||||
listOf("read("any")"), // permissions (optional)
|
||||
new CoroutineCallback<>((result, error) -> {
|
||||
if (error != null) {
|
||||
error.printStackTrace();
|
||||
+5
-3
@@ -4,12 +4,14 @@ import io.appwrite.services.Databases
|
||||
|
||||
val client = Client(context)
|
||||
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
|
||||
.setKey("") //
|
||||
.setProject("<YOUR_PROJECT_ID>") // Your project ID
|
||||
|
||||
val databases = Databases(client)
|
||||
|
||||
val result = databases.createDocuments(
|
||||
val result = databases.upsertDocument(
|
||||
databaseId = "<DATABASE_ID>",
|
||||
collectionId = "<COLLECTION_ID>",
|
||||
documents = listOf(),
|
||||
documentId = "<DOCUMENT_ID>",
|
||||
data = mapOf( "a" to "b" ),
|
||||
permissions = listOf("read("any")"), // (optional)
|
||||
)
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -146,64 +146,6 @@ class Databases(client: Client) : Service(client) {
|
||||
nestedType = classOf(),
|
||||
)
|
||||
|
||||
/**
|
||||
* 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 databaseId Database ID.
|
||||
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection). Make sure to define attributes before creating documents.
|
||||
* @param documents Array of documents data as JSON objects.
|
||||
* @return [io.appwrite.models.DocumentList<T>]
|
||||
*/
|
||||
suspend fun <T> createDocuments(
|
||||
databaseId: String,
|
||||
collectionId: String,
|
||||
documents: List<Any>,
|
||||
nestedType: Class<T>,
|
||||
): io.appwrite.models.DocumentList<T> {
|
||||
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents"
|
||||
.replace("{databaseId}", databaseId)
|
||||
.replace("{collectionId}", collectionId)
|
||||
|
||||
val apiParams = mutableMapOf<String, Any?>(
|
||||
"documents" to documents,
|
||||
)
|
||||
val apiHeaders = mutableMapOf<String, String>(
|
||||
"content-type" to "application/json",
|
||||
)
|
||||
val converter: (Any) -> io.appwrite.models.DocumentList<T> = {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
io.appwrite.models.DocumentList.from(map = it as Map<String, Any>, nestedType)
|
||||
}
|
||||
return client.call(
|
||||
"POST",
|
||||
apiPath,
|
||||
apiHeaders,
|
||||
apiParams,
|
||||
responseType = classOf(),
|
||||
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 databaseId Database ID.
|
||||
* @param collectionId Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection). Make sure to define attributes before creating documents.
|
||||
* @param documents Array of documents data as JSON objects.
|
||||
* @return [io.appwrite.models.DocumentList<T>]
|
||||
*/
|
||||
@Throws(AppwriteException::class)
|
||||
suspend fun createDocuments(
|
||||
databaseId: String,
|
||||
collectionId: String,
|
||||
documents: List<Any>,
|
||||
): io.appwrite.models.DocumentList<Map<String, Any>> = createDocuments(
|
||||
databaseId,
|
||||
collectionId,
|
||||
documents,
|
||||
nestedType = classOf(),
|
||||
)
|
||||
|
||||
/**
|
||||
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
|
||||
*
|
||||
@@ -269,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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user