use crate::openapi::{Parameter, ResponseKind};
#[derive(Debug, Clone)]
pub struct RequestBody {
pub body_type: String,
pub required: bool,
}
#[derive(Debug, Clone)]
pub struct Endpoint {
pub method: String,
pub path: String,
pub operation_id: Option<String>,
pub summary: Option<String>,
pub description: Option<String>,
pub path_params: Vec<Parameter>,
pub query_params: Vec<Parameter>,
pub request_body: Option<RequestBody>,
pub response_kind: ResponseKind,
pub deprecated: bool,
pub supports_csv: bool,
}
impl Endpoint {
pub fn should_generate(&self) -> bool {
!self.deprecated && (self.method == "GET" || self.method == "POST")
}
pub fn returns_json(&self) -> bool {
matches!(self.response_kind, ResponseKind::Json(_))
}
pub fn returns_binary(&self) -> bool {
matches!(self.response_kind, ResponseKind::Binary)
}
pub fn returns_text(&self) -> bool {
matches!(self.response_kind, ResponseKind::Text(_))
}
pub fn schema_name(&self) -> Option<&str> {
self.response_kind.schema_name()
}
pub fn operation_name(&self) -> String {
if let Some(op_id) = &self.operation_id {
return op_id.clone();
}
let mut parts: Vec<String> = Vec::new();
let mut prev_segment = "";
for segment in self.path.split('/').filter(|s| !s.is_empty()) {
if segment == "api" {
continue;
}
if let Some(param) = segment.strip_prefix('{').and_then(|s| s.strip_suffix('}')) {
let prev_normalized = prev_segment.replace('-', "_");
if !prev_normalized.ends_with(param) {
parts.push(format!("by_{}", param));
}
} else {
let normalized = segment.replace('-', "_");
parts.push(normalized);
prev_segment = segment;
}
}
format!("get_{}", parts.join("_"))
}
}