use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct DocumentsDB {
client: Client,
}
impl DocumentsDB {
pub fn new(client: &Client) -> Self {
Self {
client: client.clone(),
}
}
pub fn client(&self) -> &Client {
&self.client
}
pub async fn list(
&self,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::DatabaseList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb".to_string();
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn create(
&self,
database_id: impl Into<String>,
name: impl Into<String>,
enabled: Option<bool>,
specification: Option<&str>,
replicas: Option<i64>,
sync_mode: Option<&str>,
) -> crate::error::Result<crate::models::Database> {
let mut params = HashMap::new();
params.insert("databaseId".to_string(), json!(database_id.into()));
params.insert("name".to_string(), json!(name.into()));
if let Some(value) = enabled {
params.insert("enabled".to_string(), json!(value));
}
if let Some(value) = specification {
params.insert("specification".to_string(), json!(value));
}
if let Some(value) = replicas {
params.insert("replicas".to_string(), json!(value));
}
if let Some(value) = sync_mode {
params.insert("syncMode".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb".to_string();
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_specifications(
&self,
) -> crate::error::Result<crate::models::DedicatedDatabaseSpecificationList> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/specifications".to_string();
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_transactions(
&self,
queries: Option<Vec<String>>,
) -> crate::error::Result<crate::models::TransactionList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions".to_string();
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn create_transaction(
&self,
ttl: Option<i64>,
) -> crate::error::Result<crate::models::Transaction> {
let mut params = HashMap::new();
if let Some(value) = ttl {
params.insert("ttl".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions".to_string();
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_transaction(
&self,
transaction_id: impl Into<String>,
) -> crate::error::Result<crate::models::Transaction> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions/{transactionId}"
.to_string()
.replace("{transactionId}", &transaction_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn update_transaction(
&self,
transaction_id: impl Into<String>,
commit: Option<bool>,
rollback: Option<bool>,
) -> crate::error::Result<crate::models::Transaction> {
let mut params = HashMap::new();
if let Some(value) = commit {
params.insert("commit".to_string(), json!(value));
}
if let Some(value) = rollback {
params.insert("rollback".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions/{transactionId}"
.to_string()
.replace("{transactionId}", &transaction_id.into().to_string());
self.client
.call(Method::PATCH, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete_transaction(
&self,
transaction_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions/{transactionId}"
.to_string()
.replace("{transactionId}", &transaction_id.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
pub async fn create_operations(
&self,
transaction_id: impl Into<String>,
operations: Option<Vec<serde_json::Value>>,
) -> crate::error::Result<crate::models::Transaction> {
let mut params = HashMap::new();
if let Some(value) = operations {
params.insert("operations".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/transactions/{transactionId}/operations"
.to_string()
.replace("{transactionId}", &transaction_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn get(
&self,
database_id: impl Into<String>,
) -> crate::error::Result<crate::models::Database> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn update(
&self,
database_id: impl Into<String>,
name: impl Into<String>,
enabled: Option<bool>,
specification: Option<&str>,
replicas: Option<i64>,
sync_mode: Option<&str>,
) -> crate::error::Result<crate::models::Database> {
let mut params = HashMap::new();
params.insert("name".to_string(), json!(name.into()));
if let Some(value) = enabled {
params.insert("enabled".to_string(), json!(value));
}
if let Some(value) = specification {
params.insert("specification".to_string(), json!(value));
}
if let Some(value) = replicas {
params.insert("replicas".to_string(), json!(value));
}
if let Some(value) = sync_mode {
params.insert("syncMode".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::PUT, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete(&self, database_id: impl Into<String>) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_collections(
&self,
database_id: impl Into<String>,
queries: Option<Vec<String>>,
search: Option<&str>,
total: Option<bool>,
) -> crate::error::Result<crate::models::CollectionList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = search {
params.insert("search".to_string(), json!(value));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn create_collection(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
name: impl Into<String>,
permissions: Option<Vec<String>>,
document_security: Option<bool>,
enabled: Option<bool>,
attributes: Option<Vec<serde_json::Value>>,
indexes: Option<Vec<serde_json::Value>>,
) -> crate::error::Result<crate::models::Collection> {
let mut params = HashMap::new();
params.insert("collectionId".to_string(), json!(collection_id.into()));
params.insert("name".to_string(), json!(name.into()));
if let Some(value) = permissions {
params.insert(
"permissions".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = document_security {
params.insert("documentSecurity".to_string(), json!(value));
}
if let Some(value) = enabled {
params.insert("enabled".to_string(), json!(value));
}
if let Some(value) = attributes {
params.insert("attributes".to_string(), json!(value));
}
if let Some(value) = indexes {
params.insert("indexes".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_collection(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
) -> crate::error::Result<crate::models::Collection> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn update_collection(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
name: impl Into<String>,
permissions: Option<Vec<String>>,
document_security: Option<bool>,
enabled: Option<bool>,
purge: Option<bool>,
) -> crate::error::Result<crate::models::Collection> {
let mut params = HashMap::new();
params.insert("name".to_string(), json!(name.into()));
if let Some(value) = permissions {
params.insert(
"permissions".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = document_security {
params.insert("documentSecurity".to_string(), json!(value));
}
if let Some(value) = enabled {
params.insert("enabled".to_string(), json!(value));
}
if let Some(value) = purge {
params.insert("purge".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::PUT, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete_collection(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_documents(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
queries: Option<Vec<String>>,
transaction_id: Option<&str>,
total: Option<bool>,
ttl: Option<i64>,
) -> crate::error::Result<crate::models::DocumentList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
if let Some(value) = ttl {
params.insert("ttl".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn create_document(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
data: serde_json::Value,
permissions: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
params.insert("documentId".to_string(), json!(document_id.into()));
params.insert("data".to_string(), json!(data));
if let Some(value) = permissions {
params.insert(
"permissions".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn create_documents(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
documents: Vec<serde_json::Value>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::DocumentList> {
let mut params = HashMap::new();
params.insert("documents".to_string(), json!(documents));
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn upsert_documents(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
documents: Vec<serde_json::Value>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::DocumentList> {
let mut params = HashMap::new();
params.insert("documents".to_string(), json!(documents));
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::PUT, &path, Some(api_headers), Some(params))
.await
}
pub async fn update_documents(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
data: Option<serde_json::Value>,
queries: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::DocumentList> {
let mut params = HashMap::new();
if let Some(value) = data {
params.insert("data".to_string(), json!(value));
}
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::PATCH, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete_documents(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
queries: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::DocumentList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_document(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
queries: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn upsert_document(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
data: Option<serde_json::Value>,
permissions: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
if let Some(value) = data {
params.insert("data".to_string(), json!(value));
}
if let Some(value) = permissions {
params.insert(
"permissions".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string());
self.client
.call(Method::PUT, &path, Some(api_headers), Some(params))
.await
}
pub async fn update_document(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
data: Option<serde_json::Value>,
permissions: Option<Vec<String>>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
if let Some(value) = data {
params.insert("data".to_string(), json!(value));
}
if let Some(value) = permissions {
params.insert(
"permissions".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string());
self.client
.call(Method::PATCH, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete_document(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
transaction_id: Option<&str>,
) -> crate::error::Result<()> {
let mut params = HashMap::new();
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn decrement_document_attribute(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
attribute: impl Into<String>,
value: Option<f64>,
min: Option<f64>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
if let Some(value) = value {
params.insert("value".to_string(), json!(value));
}
if let Some(value) = min {
params.insert("min".to_string(), json!(value));
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path =
"/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/decrement"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string())
.replace("{attribute}", &attribute.into().to_string());
self.client
.call(Method::PATCH, &path, Some(api_headers), Some(params))
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn increment_document_attribute(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
document_id: impl Into<String>,
attribute: impl Into<String>,
value: Option<f64>,
max: Option<f64>,
transaction_id: Option<&str>,
) -> crate::error::Result<crate::models::Document> {
let mut params = HashMap::new();
if let Some(value) = value {
params.insert("value".to_string(), json!(value));
}
if let Some(value) = max {
params.insert("max".to_string(), json!(value));
}
if let Some(value) = transaction_id {
params.insert("transactionId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path =
"/documentsdb/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/increment"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{documentId}", &document_id.into().to_string())
.replace("{attribute}", &attribute.into().to_string());
self.client
.call(Method::PATCH, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_indexes(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::IndexList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert(
"queries".to_string(),
json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
);
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/indexes"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn create_index(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
key: impl Into<String>,
r#type: crate::enums::DocumentsDBIndexType,
attributes: impl IntoIterator<Item = impl Into<String>>,
orders: Option<Vec<crate::enums::OrderBy>>,
lengths: Option<Vec<i64>>,
) -> crate::error::Result<crate::models::Index> {
let mut params = HashMap::new();
params.insert("key".to_string(), json!(key.into()));
params.insert("type".to_string(), json!(r#type));
params.insert(
"attributes".to_string(),
json!(attributes
.into_iter()
.map(|s| s.into())
.collect::<Vec<String>>()),
);
if let Some(value) = orders {
params.insert("orders".to_string(), json!(value));
}
if let Some(value) = lengths {
params.insert("lengths".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/indexes"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_index(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
key: impl Into<String>,
) -> crate::error::Result<crate::models::Index> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/indexes/{key}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{key}", &key.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn delete_index(
&self,
database_id: impl Into<String>,
collection_id: impl Into<String>,
key: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/collections/{collectionId}/indexes/{key}"
.to_string()
.replace("{databaseId}", &database_id.into().to_string())
.replace("{collectionId}", &collection_id.into().to_string())
.replace("{key}", &key.into().to_string());
self.client
.call(Method::DELETE, &path, Some(api_headers), Some(params))
.await
}
pub async fn create_failover(
&self,
database_id: impl Into<String>,
target_replica_id: Option<&str>,
) -> crate::error::Result<crate::models::DedicatedDatabase> {
let mut params = HashMap::new();
if let Some(value) = target_replica_id {
params.insert("targetReplicaId".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/failovers"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::POST, &path, Some(api_headers), Some(params))
.await
}
pub async fn list_operations(
&self,
database_id: impl Into<String>,
status: Option<&str>,
limit: Option<i64>,
offset: Option<i64>,
) -> crate::error::Result<crate::models::DedicatedDatabaseOperationList> {
let mut params = HashMap::new();
if let Some(value) = status {
params.insert("status".to_string(), json!(value));
}
if let Some(value) = limit {
params.insert("limit".to_string(), json!(value));
}
if let Some(value) = offset {
params.insert("offset".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/operations"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_replicas(
&self,
database_id: impl Into<String>,
) -> crate::error::Result<crate::models::DedicatedDatabaseReplicas> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/replicas"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
pub async fn get_status(
&self,
database_id: impl Into<String>,
) -> crate::error::Result<crate::models::DatabaseStatus> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/documentsdb/{databaseId}/status"
.to_string()
.replace("{databaseId}", &database_id.into().to_string());
self.client
.call(Method::GET, &path, Some(api_headers), Some(params))
.await
}
}
impl crate::services::Service for DocumentsDB {
fn client(&self) -> &Client {
&self.client
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_documents_db_creation() {
let client = Client::new();
let service = DocumentsDB::new(&client);
assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
}
}