mirror of
https://github.com/appwrite/sdk-for-flutter.git
synced 2026-04-07 19:27:41 +00:00
chore: add setDevkey and upsertDocument methods
This commit is contained in:
+5
-3
@@ -2,12 +2,14 @@ import 'package:appwrite/appwrite.dart';
|
||||
|
||||
Client client = Client()
|
||||
.setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint
|
||||
.setKey(''); //
|
||||
.setProject('<YOUR_PROJECT_ID>'); // Your project ID
|
||||
|
||||
Databases databases = Databases(client);
|
||||
|
||||
DocumentList result = await databases.createDocuments(
|
||||
Document result = await databases.upsertDocument(
|
||||
databaseId: '<DATABASE_ID>',
|
||||
collectionId: '<COLLECTION_ID>',
|
||||
documents: [],
|
||||
documentId: '<DOCUMENT_ID>',
|
||||
data: {},
|
||||
permissions: ["read("any")"], // optional
|
||||
);
|
||||
+22
-21
@@ -48,27 +48,6 @@ class Databases extends 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.
|
||||
Future<models.DocumentList> createDocuments({required String databaseId, required String collectionId, required List<Map> documents}) async {
|
||||
final String apiPath = '/databases/{databaseId}/collections/{collectionId}/documents'.replaceAll('{databaseId}', databaseId).replaceAll('{collectionId}', collectionId);
|
||||
|
||||
final Map<String, dynamic> apiParams = {
|
||||
'documents': documents,
|
||||
};
|
||||
|
||||
final Map<String, String> apiHeaders = {
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
|
||||
final res = await client.call(HttpMethod.post, path: apiPath, params: apiParams, headers: apiHeaders);
|
||||
|
||||
return models.DocumentList.fromMap(res.data);
|
||||
|
||||
}
|
||||
|
||||
/// Get a document by its unique ID. This endpoint response returns a JSON
|
||||
/// object with the document data.
|
||||
Future<models.Document> getDocument({required String databaseId, required String collectionId, required String documentId, List<String>? queries}) async {
|
||||
@@ -88,6 +67,28 @@ class Databases extends Service {
|
||||
|
||||
}
|
||||
|
||||
/// 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.
|
||||
Future<models.Document> upsertDocument({required String databaseId, required String collectionId, required String documentId, required Map data, List<String>? permissions}) async {
|
||||
final String apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replaceAll('{databaseId}', databaseId).replaceAll('{collectionId}', collectionId).replaceAll('{documentId}', documentId);
|
||||
|
||||
final Map<String, dynamic> apiParams = {
|
||||
'data': data,
|
||||
'permissions': permissions,
|
||||
};
|
||||
|
||||
final Map<String, String> apiHeaders = {
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
|
||||
final res = await client.call(HttpMethod.put, path: apiPath, params: apiParams, headers: apiHeaders);
|
||||
|
||||
return models.Document.fromMap(res.data);
|
||||
|
||||
}
|
||||
|
||||
/// Update a document by its unique ID. Using the patch method you can pass
|
||||
/// only specific fields that will get updated.
|
||||
Future<models.Document> updateDocument({required String databaseId, required String collectionId, required String documentId, Map? data, List<String>? permissions}) async {
|
||||
|
||||
@@ -71,6 +71,11 @@ abstract class Client {
|
||||
/// The user session to authenticate with.
|
||||
Client setSession(value);
|
||||
|
||||
/// Set DevKey.
|
||||
///
|
||||
/// Your secret dev API key.
|
||||
Client setDevKey(value);
|
||||
|
||||
/// Add headers that should be sent with all API calls.
|
||||
Client addHeader(String key, String value);
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ abstract class ClientBase implements Client {
|
||||
/// The user session to authenticate with
|
||||
@override
|
||||
ClientBase setSession(value);
|
||||
/// Your secret dev API key
|
||||
@override
|
||||
ClientBase setDevKey(value);
|
||||
|
||||
@override
|
||||
ClientBase setSelfSigned({bool status = true});
|
||||
|
||||
@@ -84,6 +84,13 @@ class ClientBrowser extends ClientBase with ClientMixin {
|
||||
addHeader('X-Appwrite-Session', value);
|
||||
return this;
|
||||
}
|
||||
/// Your secret dev API key
|
||||
@override
|
||||
ClientBrowser setDevKey(value) {
|
||||
config['devKey'] = value;
|
||||
addHeader('X-Appwrite-Dev-Key', value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@override
|
||||
ClientBrowser setSelfSigned({bool status = true}) {
|
||||
|
||||
@@ -113,6 +113,13 @@ class ClientIO extends ClientBase with ClientMixin {
|
||||
addHeader('X-Appwrite-Session', value);
|
||||
return this;
|
||||
}
|
||||
/// Your secret dev API key
|
||||
@override
|
||||
ClientIO setDevKey(value) {
|
||||
config['devKey'] = value;
|
||||
addHeader('X-Appwrite-Dev-Key', value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@override
|
||||
ClientIO setSelfSigned({bool status = true}) {
|
||||
|
||||
@@ -98,26 +98,6 @@ void main() {
|
||||
|
||||
});
|
||||
|
||||
test('test method createDocuments()', () async {
|
||||
final Map<String, dynamic> data = {
|
||||
'total': 5,
|
||||
'documents': [],};
|
||||
|
||||
|
||||
when(client.call(
|
||||
HttpMethod.post,
|
||||
)).thenAnswer((_) async => Response(data: data));
|
||||
|
||||
|
||||
final response = await databases.createDocuments(
|
||||
databaseId: '<DATABASE_ID>',
|
||||
collectionId: '<COLLECTION_ID>',
|
||||
documents: [],
|
||||
);
|
||||
expect(response, isA<models.DocumentList>());
|
||||
|
||||
});
|
||||
|
||||
test('test method getDocument()', () async {
|
||||
final Map<String, dynamic> data = {
|
||||
'\$id': '5e5ea5c16897e',
|
||||
@@ -142,6 +122,31 @@ void main() {
|
||||
|
||||
});
|
||||
|
||||
test('test method upsertDocument()', () async {
|
||||
final Map<String, dynamic> data = {
|
||||
'\$id': '5e5ea5c16897e',
|
||||
'\$collectionId': '5e5ea5c15117e',
|
||||
'\$databaseId': '5e5ea5c15117e',
|
||||
'\$createdAt': '2020-10-15T06:38:00.000+00:00',
|
||||
'\$updatedAt': '2020-10-15T06:38:00.000+00:00',
|
||||
'\$permissions': [],};
|
||||
|
||||
|
||||
when(client.call(
|
||||
HttpMethod.put,
|
||||
)).thenAnswer((_) async => Response(data: data));
|
||||
|
||||
|
||||
final response = await databases.upsertDocument(
|
||||
databaseId: '<DATABASE_ID>',
|
||||
collectionId: '<COLLECTION_ID>',
|
||||
documentId: '<DOCUMENT_ID>',
|
||||
data: {},
|
||||
);
|
||||
expect(response, isA<models.Document>());
|
||||
|
||||
});
|
||||
|
||||
test('test method updateDocument()', () async {
|
||||
final Map<String, dynamic> data = {
|
||||
'\$id': '5e5ea5c16897e',
|
||||
|
||||
Reference in New Issue
Block a user