use crate::batbelt::miro::MiroConfig;
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde_json::json;
use super::MiroApiResult;
#[derive(Debug)]
pub struct ConnectorOptions {
pub start_x_position: String,
pub start_y_position: String,
pub end_x_position: String,
pub end_y_position: String,
}
pub async fn create_connector(
start_item_id: &str,
end_item_id: &str,
connect_options: Option<ConnectorOptions>,
) -> MiroApiResult {
create_connector_with_color(start_item_id, end_item_id, connect_options, None).await
}
pub async fn create_connector_with_color(
start_item_id: &str,
end_item_id: &str,
connect_options: Option<ConnectorOptions>,
stroke_color: Option<&str>,
) -> MiroApiResult {
let MiroConfig {
access_token,
board_id,
..
} = MiroConfig::new()?;
let stroke_color_value = stroke_color.unwrap_or("#000000");
let body = if let Some(options) = connect_options {
let ConnectorOptions {
start_x_position,
start_y_position,
end_x_position,
end_y_position,
} = options;
json!({
"startItem": {
"id": start_item_id,
"position": {
"x": start_x_position,
"y": start_y_position,
},
},
"endItem": {
"id": end_item_id,
"position": {
"x": end_x_position,
"y": end_y_position,
},
},
"style": {
"strokeWidth": "3",
"strokeColor": stroke_color_value
},
"shape": "elbowed"
})
.to_string()
} else {
json!({
"startItem": {
"id": start_item_id
},
"endItem": {
"id": end_item_id
},
"style": {
"strokeWidth": "3",
"strokeColor": stroke_color_value
},
"shape": "elbowed"
})
.to_string()
};
let client = reqwest::Client::new();
let response = client
.post(format!(
"https://api.miro.com/v2/boards/{board_id}/connectors",
))
.body(body)
.header(CONTENT_TYPE, "application/json")
.header(AUTHORIZATION, format!("Bearer {access_token}"))
.send()
.await;
MiroConfig::parse_response_from_miro(response)
}