use std::fmt::Display;
use bytes::{Bytes, BytesMut};
use serde::Serialize;
use url::Url;
use crate::error::Error;
use crate::hooks::OperationInfo;
use crate::http::Method;
use crate::route::Route;
#[derive(Debug, Clone)]
pub struct Operation {
pub(crate) route: &'static Route,
pub(crate) info: OperationInfo,
pub(crate) method: Method,
pub(crate) path: String,
pub(crate) url: Option<Url>,
pub(crate) query: Vec<(String, String)>,
pub(crate) body: Option<Body>,
}
#[derive(Debug, Clone)]
pub(crate) struct Body {
pub(crate) content_type: String,
pub(crate) bytes: Bytes,
}
impl Operation {
pub(crate) fn info_for(route: &'static Route) -> OperationInfo {
OperationInfo {
service: route.service,
operation: route.id,
resource_type: route.resource_type,
is_mutation: route.method != Method::GET,
project_id: None,
resource_id: None,
}
}
pub(crate) fn for_route(route: &'static Route, params: &[&dyn Display]) -> Operation {
let path = route.fill(params);
let (project_id, resource_id) = ids_of(route, params);
Operation {
route,
info: OperationInfo {
project_id,
resource_id,
..Operation::info_for(route)
},
method: route.method.clone(),
path,
url: None,
query: Vec::new(),
body: None,
}
}
pub(crate) fn at(route: &'static Route, info: OperationInfo, url: Url) -> Operation {
Operation {
route,
info,
method: Method::GET,
path: url.path().to_string(),
url: Some(url),
query: Vec::new(),
body: None,
}
}
pub fn route(&self) -> &'static Route {
self.route
}
pub fn id(&self) -> &'static str {
self.route.id
}
pub fn info(&self) -> &OperationInfo {
&self.info
}
pub fn method(&self) -> &Method {
&self.method
}
pub fn path(&self) -> &str {
&self.path
}
pub fn query(&mut self, name: &str, value: impl Display) -> &mut Operation {
self.query.push((name.to_string(), value.to_string()));
self
}
pub fn query_optional<T: Display>(&mut self, name: &str, value: Option<&T>) -> &mut Operation {
if let Some(value) = value {
self.query(name, value);
}
self
}
pub fn query_list<T: Display>(&mut self, name: &str, values: &[T]) -> &mut Operation {
for value in values {
self.query(name, value);
}
self
}
pub fn query_list_optional<T: Display>(
&mut self,
name: &str,
values: Option<&[T]>,
) -> &mut Operation {
if let Some(values) = values {
self.query_list(name, values);
}
self
}
pub fn json<T: Serialize + ?Sized>(&mut self, body: &T) -> Result<&mut Operation, Error> {
let value = serde_json::to_value(body).map_err(|error| {
Error::usage(format!("request body could not be serialized: {error}"))
})?;
let compacted = compact(value);
let bytes = serde_json::to_vec(&compacted).map_err(|error| {
Error::usage(format!("request body could not be serialized: {error}"))
})?;
self.bytes("application/json", Bytes::from(bytes));
Ok(self)
}
pub fn bytes(&mut self, content_type: &str, bytes: Bytes) -> &mut Operation {
self.body = Some(Body {
content_type: content_type.to_string(),
bytes,
});
self
}
pub fn multipart(
&mut self,
field: &str,
filename: &str,
content_type: &str,
bytes: &[u8],
) -> &mut Operation {
let boundary = format!("basecamp-sdk-{:032x}", rand::random::<u128>());
let mut body = BytesMut::new();
body.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
body.extend_from_slice(
format!(
"Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"\r\n",
escape_quotes(field),
escape_quotes(filename)
)
.as_bytes(),
);
body.extend_from_slice(
format!(
"Content-Type: {}\r\n\r\n",
without_line_breaks(content_type)
)
.as_bytes(),
);
body.extend_from_slice(bytes);
body.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes());
self.body = Some(Body {
content_type: format!("multipart/form-data; boundary={boundary}"),
bytes: body.freeze(),
});
self
}
pub fn body_bytes(&self) -> Option<&Bytes> {
self.body.as_ref().map(|body| &body.bytes)
}
pub(crate) fn pins_a_page(&self) -> bool {
self.query
.iter()
.any(|(name, value)| name == "page" && value.parse::<i64>().is_ok_and(|page| page > 0))
}
}
fn escape_quotes(value: &str) -> String {
without_line_breaks(&value.replace('\\', "%5C").replace('"', "%22"))
}
fn without_line_breaks(value: &str) -> String {
value.replace(['\r', '\n'], "")
}
pub(crate) fn compact(value: serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Object(members) => serde_json::Value::Object(
members
.into_iter()
.filter(|(_, member)| !member.is_null())
.map(|(name, member)| (name, compact(member)))
.collect(),
),
serde_json::Value::Array(items) => {
serde_json::Value::Array(items.into_iter().map(compact).collect())
}
other => other,
}
}
fn ids_of(route: &Route, params: &[&dyn Display]) -> (Option<i64>, Option<i64>) {
let mut project_id = None;
let mut resource_id = None;
for (param, value) in route.params.iter().zip(params) {
let Ok(id) = value.to_string().parse::<i64>() else {
continue;
};
if param.name == "bucketId" || param.name == "projectId" {
project_id = Some(id);
}
resource_id = Some(id);
}
if route.params.len() == 1 && project_id.is_some() {
resource_id = None;
}
(project_id, resource_id)
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn ids_name_the_project_and_the_record_the_path_names() {
use crate::generated::routes;
assert_eq!(ids_of(&routes::GET_PROJECT, &[&12]), (Some(12), None));
assert_eq!(
ids_of(&routes::LIST_PROJECT_PEOPLE, &[&12]),
(Some(12), None)
);
assert_eq!(ids_of(&routes::GET_TODO, &[&34]), (None, Some(34)));
assert_eq!(
ids_of(&routes::CREATE_CLOUD_FILE, &[&12, &56]),
(Some(12), Some(56))
);
}
#[test]
fn multipart_part_headers_cannot_be_broken_by_their_values() {
let mut operation =
Operation::for_route(&crate::generated::routes::UPDATE_ACCOUNT_LOGO, &[]);
operation.multipart(
"logo",
"a\r\nb\"c\\d.png",
"image/png\r\n\r\nprefix",
b"pixels",
);
let body = String::from_utf8(operation.body_bytes().unwrap().to_vec()).unwrap();
assert!(body.contains("filename=\"ab%22c%5Cd.png\""), "{body}");
assert!(
body.contains("Content-Type: image/pngprefix\r\n\r\npixels"),
"{body}"
);
}
#[test]
fn compaction_strips_nulls_but_not_empty_strings() {
let compacted =
compact(json!({"a": null, "b": "", "c": {"d": null, "e": 1}, "f": [null, 1]}));
assert_eq!(compacted, json!({"b": "", "c": {"e": 1}, "f": [null, 1]}));
}
}