1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
//! Airtable integration for [Flows.network](https://test.flows.network)
//!
//! # Quick Start
//!
//! To get started, let's write a very tiny flow function.
//!
//! ```rust
//! use airtable_flows::create_record;
//! use slack_flows::{listen_to_channel};
//!
//! #[no_mangle]
//! pub fn run() {
//! listen_to_channel("myworkspace", "mychannel", |sm| {
//! let record = serde_json::json!({
//! "Name": sm.text,
//! });
//! create_record("accountName", "mybaseId", "mytable", record);
//! });
//! }
//! ```
//!
//! When the Slack message is received, create a new record in Airtable using [create_record].
use http_req::{
request::{Method, Request},
uri::Uri,
};
use lazy_static::lazy_static;
use serde_json::Value;
use urlencoding::encode;
lazy_static! {
static ref AIRTABLE_API_PREFIX: String = String::from(
std::option_env!("AIRTABLE_API_PREFIX")
.unwrap_or("https://airtable-flows-extension.vercel.app/api")
);
}
extern "C" {
fn get_flows_user(p: *mut u8) -> i32;
fn set_error_log(p: *const u8, len: i32);
}
/// Create a new record in the specified table.
///
/// `account` is the account name when you connect
/// [Flows.network](https://test.flows.network) platform with your Airtable account.
///
/// `base_id` is the id of the base which the table belongs to.
///
/// `table_name` is the name of the table.
///
/// `text` is a serde_json::Value::Object whose key is the field name
/// of the table.
///
/// If you have not connected your Airtable account with [Flows.network platform](https://test.flows.network),
/// you will receive an error in the flow's building log or running log.
///
pub fn create_record(account: &str, base_id: &str, table_name: &str, text: Value) {
unsafe {
let mut flows_user = Vec::<u8>::with_capacity(100);
let c = get_flows_user(flows_user.as_mut_ptr());
flows_user.set_len(c as usize);
let flows_user = String::from_utf8(flows_user).unwrap();
let mut writer = Vec::new();
let uri = format!(
"{}/{}/create_record?account={}&base={}&table={}",
AIRTABLE_API_PREFIX.as_str(),
flows_user,
encode(account),
encode(base_id),
encode(table_name)
);
let uri = Uri::try_from(uri.as_str()).unwrap();
let body = serde_json::to_vec(&text).unwrap_or_default();
if let Ok(res) = Request::new(&uri)
.method(Method::POST)
.header("Content-Type", "application/json")
.header("Content-Length", &body.len())
.body(&body)
.send(&mut writer)
{
if !res.status_code().is_success() {
set_error_log(writer.as_ptr(), writer.len() as i32);
}
}
}
}