Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from NainooG/main
Browse files Browse the repository at this point in the history
added delete_record and delete_records + tests
  • Loading branch information
anish-dfg authored Nov 18, 2024
2 parents c6cf843 + b8f1d59 commit d4f6b9e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.env.*
tmp/
!.env.example
.vscode/
62 changes: 56 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 39 additions & 2 deletions scipio-airtable/src/base_data/records.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fmt::Display;

use anyhow::Result;
use anyhow::{Result, bail};
use derive_builder::Builder;
use derive_more::derive::Display;
use scipio_macros::ToQueryString;
use serde::{Deserialize, Serialize};

use super::responses::{GetRecordResponse, ListRecordsResponse};
use super::responses::{DeleteRecordResponse, GetRecordResponse, ListRecordsResponse, DeleteRecordsResponse};
use crate::Airtable;

/// A struct representing a sort query parameter.
Expand Down Expand Up @@ -133,4 +133,41 @@ impl Airtable {
{
Ok(())
}

pub async fn delete_record(
&self,
base_id: &str,
table_id: &str,
record_id: &str,
) -> Result<()> {
let url = format!(
"https://api.airtable.com/v0/{base_id}/{table_id}/{record_id}"
);

self.http.delete(&url).send().await?;

Ok(())
}

pub async fn delete_records(
&self,
base_id: &str,
table_id: &str,
records: Vec<String>,
) -> Result<()> {
if records.len() > 10 {
bail!("Maximum limit for deleting records is 10");
}

let query = records.into_iter().map(|rec| format!("records[]={rec}")).collect::<Vec<_>>().join("&");

let url = format!(
"https://api.airtable.com/v0/{base_id}/{table_id}/?{query}",
);

self.http.delete(&url).send().await?;

Ok(())
}

}
11 changes: 11 additions & 0 deletions scipio-airtable/src/base_data/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ pub struct ListRecordsResponse<T> {
}

pub type GetRecordResponse<T> = Record<T>;

#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteRecordResponse {
record_id: String,
deleted: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteRecordsResponse {
records: Vec<DeleteRecordResponse>
}
58 changes: 58 additions & 0 deletions scipio-airtable/src/tests/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,61 @@ pub async fn test_update_record(context: AsyncTestContext) -> Result<()> {

Ok(())
}

#[cfg(feature = "integration")]
#[rstest]
#[traced_test]
#[tokio::test]
pub async fn delete_record(context: AsyncTestContext) -> Result<()> {
let mut cleanup = context.cleanup.lock().await;
cleanup.push(Box::new(|| {
Box::pin(async move {
tracing::info!("Cleaning up test_get_record");
// bail!("test_get_record failed");
Ok(())
})
}));

let base = env::var("TEST_AIRTABLE_API_BASE").expect("missing TEST_AIRTABLE_BASE variable");
let table = env::var("TEST_AIRTABLE_API_TABLE").expect("missing TEST_AIRTABLE_TABLE variable");
let record_id =
env::var("TEST_AIRTABLE_API_DELETE_RECORD_ID").expect("missing TEST_AIRTABLE_API_DELETE_RECORD_ID variable");

let query = GetRecordQueryBuilder::default().build()?;

let res =
context.airtable.delete_record(&base, &table, &record_id).await?;

dbg!(&res);

Ok(())
}

#[cfg(feature = "integration")]
#[rstest]
#[traced_test]
#[tokio::test]
pub async fn delete_records(context: AsyncTestContext) -> Result<()> {
let mut cleanup = context.cleanup.lock().await;
cleanup.push(Box::new(|| {
Box::pin(async move {
tracing::info!("Cleaning up test_get_record");
// bail!("test_get_record failed");
Ok(())
})
}));

let base = env::var("TEST_AIRTABLE_API_BASE").expect("missing TEST_AIRTABLE_BASE variable");
let table = env::var("TEST_AIRTABLE_API_TABLE").expect("missing TEST_AIRTABLE_TABLE variable");
let delete_records_ids =
env::var("TEST_AIRTABLE_API_DELETE_RECORDS_IDS").expect("missing TEST_AIRTABLE_API_DELETE_RECORDS_IDS variable").split(',').map(String::from).collect();

let query = GetRecordQueryBuilder::default().build()?;

let res =
context.airtable.delete_records(&base, &table, delete_records_ids).await?;

dbg!(&res);

Ok(())
}

0 comments on commit d4f6b9e

Please sign in to comment.