From f20fa6a98ac16182c887035a159c4563e97cb959 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 14:20:44 +0100 Subject: [PATCH 1/2] refactor(ui): take the dashboard's API paths from the generated client The dashboard wrote its manifest download link and its two event streams' paths by hand, and the client its TOML, local preview and exec requests' paths, so a route could change under them unnoticed. generate_openapi now also writes piqueld_client::paths, one function per operation named like its client method, from the OpenAPI document, and they take their paths from it. API_PREFIX, which nothing uses any more, goes. Fixes PQD-162 --- .../src/browser/management/pages.rs | 3 +- .../src/browser/management/timeline.rs | 11 +- .../piqueld/examples/generate_openapi/main.rs | 115 +- crates/piqueld-client/src/applications.rs | 5 +- crates/piqueld-client/src/exec/mod.rs | 7 +- crates/piqueld-client/src/lib.rs | 7 +- crates/piqueld-client/src/paths.rs | 1148 +++++++++++++++++ crates/piqueld-client/src/previews.rs | 7 +- crates/piqueld-core/src/api/mod.rs | 2 - docs/dev/architecture/dependency-flow.md | 5 +- tools/client-codegen/README.md | 11 +- 11 files changed, 1286 insertions(+), 35 deletions(-) create mode 100644 crates/piqueld-client/src/paths.rs diff --git a/apps/piqueld-ui/src/browser/management/pages.rs b/apps/piqueld-ui/src/browser/management/pages.rs index d68e7ce26..4957ac032 100644 --- a/apps/piqueld-ui/src/browser/management/pages.rs +++ b/apps/piqueld-ui/src/browser/management/pages.rs @@ -19,6 +19,7 @@ use leptos_router::NavigateOptions; use leptos_router::components::A; use leptos_router::hooks::use_navigate; use piqueld_client::access::AppPermission; +use piqueld_client::paths; /// The application page: shared configuration, its environments, and history /// across all of them. @@ -50,7 +51,7 @@ pub(super) fn ApplicationSections() -> impl IntoView {
diff --git a/apps/piqueld-ui/src/browser/management/timeline.rs b/apps/piqueld-ui/src/browser/management/timeline.rs index f900ddb80..d35c625d3 100644 --- a/apps/piqueld-ui/src/browser/management/timeline.rs +++ b/apps/piqueld-ui/src/browser/management/timeline.rs @@ -23,9 +23,7 @@ use leptos::task::spawn_local; use leptos_router::components::A; use piqueld_client::observability::Elapsed; use piqueld_client::timeline::{Progress, Step, StepKey, StepState, Timeline}; -use piqueld_client::{ - API_PREFIX, BuildRecord, Client, Event, Operation, Phase, access::AppPermission, -}; +use piqueld_client::{BuildRecord, Client, Event, Operation, Phase, access::AppPermission, paths}; use std::collections::VecDeque; use std::time::Duration; @@ -130,10 +128,7 @@ impl LiveTimeline { /// Opens the stream, which sends the whole timeline at once, then again /// on every change. fn connect(self) { - let url = format!( - "{API_PREFIX}/operations/{}/timeline/stream", - self.operation.get_value() - ); + let url = paths::stream_operation_timeline(&self.operation.get_value()); let stream = EventStream::open( &url, &["timeline", "access_ended"], @@ -1228,7 +1223,7 @@ fn EventFeed(operation: String, live: Memo, attempt: Memo) -> impl In let _ = settled.try_set_value(true); }); }; - let url = format!("{API_PREFIX}/events/stream?operation_id={operation}"); + let url = format!("{}?operation_id={operation}", paths::stream_events()); let history = operation_events(&operation); // The newest event received, which a reopened stream resumes after. let last = StoredValue::new(0_i64); diff --git a/apps/piqueld/examples/generate_openapi/main.rs b/apps/piqueld/examples/generate_openapi/main.rs index e44aad683..17c81a8ce 100644 --- a/apps/piqueld/examples/generate_openapi/main.rs +++ b/apps/piqueld/examples/generate_openapi/main.rs @@ -1,13 +1,13 @@ -//! Generates or checks the `OpenAPI` specification, its Rust client bindings, -//! the standalone application manifest and daemon configuration JSON -//! Schemas, and the documentation's settings references rendered from those -//! schemas. `piquelctl`'s `cli_reference` example renders the command -//! reference; it depends on the client this generates, so it runs as a -//! separate step. +//! Generates or checks the `OpenAPI` specification, its Rust client bindings +//! and operation paths, the standalone application manifest and daemon +//! configuration JSON Schemas, and the documentation's settings references +//! rendered from those schemas. `piquelctl`'s `cli_reference` example renders +//! the command reference; it depends on the client this generates, so it runs +//! as a separate step. mod settings_reference; -use anyhow::{Context, Result, anyhow, ensure}; +use anyhow::{Context, Result, anyhow, bail, ensure}; use piqueld::api::http::openapi_document; use piqueld_config::DaemonConfig; use piqueld_core::manifest::ApplicationManifest; @@ -16,7 +16,8 @@ use serde::Deserialize; use serde_json::Value; use settings_reference::SettingsPage; use std::{ - collections::BTreeSet, + collections::{BTreeMap, BTreeSet}, + fmt::Write as _, fs, io::Write, process::{Command, Stdio}, @@ -76,6 +77,7 @@ impl Generator { .context("read Progenitor configuration")?, ) .context("parse Progenitor configuration")?; + let operation_paths = Self::generate_paths(&document)?; let client = self.generate_client(document.clone(), &config)?; let document = format!("{}\n", serde_json::to_string_pretty(&document)?); let manifest_schema = ApplicationManifest::json_schema(); @@ -90,6 +92,10 @@ impl Generator { ("docs/application-manifest.schema.json", manifest_schema), ("docs/piqueld-config.schema.json", config_schema), ("crates/piqueld-client/src/generated.rs", client), + ( + "crates/piqueld-client/src/paths.rs", + self.rustfmt(&operation_paths)?, + ), (SettingsPage::MANIFEST.path, manifest_settings), (SettingsPage::CONFIG.path, config_settings), ])?; @@ -158,6 +164,99 @@ impl Generator { )) } + /// Generates the source of `piqueld_client::paths`: one function per + /// operation, named like its client method, that builds the operation's + /// path with its path parameters percent-encoded as the client encodes + /// them. It reads the complete document, so it also covers the operations + /// Progenitor's copy drops, such as the exec WebSocket. + fn generate_paths(document: &Value) -> Result { + let paths = document + .get("paths") + .and_then(Value::as_object) + .context("OpenAPI document has no paths object")?; + let mut functions = BTreeMap::new(); + for (path, item) in paths { + let operations = item + .as_object() + .with_context(|| format!("OpenAPI path {path} is not an object"))?; + for (method, operation) in operations { + let Some(operation) = operation.as_object() else { + continue; + }; + let id = operation + .get("operationId") + .and_then(Value::as_str) + .with_context(|| format!("{method} {path} has no operation ID"))?; + // Progenitor's method names: the operation ID in snake case. + let name: String = id + .chars() + .flat_map(|c| { + c.is_ascii_uppercase() + .then_some('_') + .into_iter() + .chain([c.to_ascii_lowercase()]) + }) + .collect(); + let mut template = String::new(); + let mut parameters = Vec::new(); + let mut arguments = String::new(); + // Placeholders are the odd parts between braces. + for (index, part) in path.split(['{', '}']).enumerate() { + if index % 2 == 0 { + template.push_str(part); + continue; + } + let format = operation + .get("parameters") + .and_then(Value::as_array) + .into_iter() + .flatten() + .find(|parameter| { + parameter.get("in").and_then(Value::as_str) == Some("path") + && parameter.get("name").and_then(Value::as_str) == Some(part) + }) + .and_then(|parameter| parameter.get("schema")) + .map(|schema| { + ( + schema.get("type").and_then(Value::as_str), + schema.get("format").and_then(Value::as_str), + ) + }); + // Strings are percent-encoded; integers need no encoding. + let kind = match format { + Some((Some("string"), _)) => { + write!(arguments, ", {part} = encode_path({part})")?; + "&str" + } + Some((Some("integer"), Some("int64"))) => "i64", + _ => bail!("{id}'s path parameter {part} has an unsupported type"), + }; + write!(template, "{{{part}}}")?; + parameters.push(format!("{part}: {kind}")); + } + let body = if parameters.is_empty() { + format!("String::from({template:?})") + } else { + format!("format!({template:?}{arguments})") + }; + let function = format!( + "/// `{} {path}`\n#[must_use]\npub fn {name}({}) -> String {{ {body} }}\n\n", + method.to_uppercase(), + parameters.join(", ") + ); + ensure!( + functions.insert(name, function).is_none(), + "two operations are named {id}" + ); + } + } + Ok(format!( + "// Generated from the OpenAPI document. Run `just generate`; do not edit.\n\n\ + use progenitor_client::encode_path;\n\n{}", + functions.into_values().collect::() + )) + } + /// Formats Rust source through `rustfmt` over stdin and stdout. fn rustfmt(&self, source: &str) -> Result { let mut child = Command::new("rustfmt") diff --git a/crates/piqueld-client/src/applications.rs b/crates/piqueld-client/src/applications.rs index 312c68b7f..f54cf1727 100644 --- a/crates/piqueld-client/src/applications.rs +++ b/crates/piqueld-client/src/applications.rs @@ -8,6 +8,7 @@ pub use piqueld_core::api::{ use crate::{ Client, ClientError, Page, client::{EnvelopedCall, GeneratedCall, check_page_limit, collect_byte_stream}, + paths, }; #[derive(Clone, Debug, Default, Eq, PartialEq)] @@ -202,7 +203,7 @@ impl Client { headers.push(("X-Expected-Application-Id", expected_id.to_owned())); } self.send( - "/api/v1/applications/apply", + &paths::apply_application(), &query, &headers, ("application/toml", manifest.to_owned().into()), @@ -228,7 +229,7 @@ impl Client { let query = environment.map_or_default(|environment| vec![("environment", environment.to_owned())]); self.send( - "/api/v1/applications/plan", + &paths::plan_application(), &query, &headers, ("application/toml", manifest.to_owned().into()), diff --git a/crates/piqueld-client/src/exec/mod.rs b/crates/piqueld-client/src/exec/mod.rs index 3e35bbf9f..0ab5d5c83 100644 --- a/crates/piqueld-client/src/exec/mod.rs +++ b/crates/piqueld-client/src/exec/mod.rs @@ -5,8 +5,7 @@ //! cookie. Dropping both the reader and the writer closes the connection, //! which stops streaming without stopping the command. -use crate::{Client, ClientError}; -use piqueld_core::api::API_PREFIX; +use crate::{Client, ClientError, paths}; use piqueld_core::exec::MAX_STDIN_BYTES; pub use piqueld_core::exec::{ ExecCommand, ExecCommandError, ExecFrame, ExecInput, ExecOutput, ExecRequest, TerminalSize, @@ -37,9 +36,9 @@ impl Client { request: &ExecRequest, ) -> Result<(ExecReader, ExecWriter), ClientError> { let url = format!( - "{}{API_PREFIX}/environments/{}/exec", + "{}{}", self.generated.baseurl, - progenitor_client::encode_path(id) + paths::exec_environment_command(id) ); let request = serde_json::to_string(request).map_err(ClientError::invalid_request)?; let (reader, writer) = socket::open(self, url, request).await?; diff --git a/crates/piqueld-client/src/lib.rs b/crates/piqueld-client/src/lib.rs index e17cb9283..021993545 100644 --- a/crates/piqueld-client/src/lib.rs +++ b/crates/piqueld-client/src/lib.rs @@ -39,6 +39,11 @@ pub mod observability; pub mod openapi; /// Operation inspection contracts, and their timelines of steps. pub mod operations; +/// Each operation's path, for what the client does not send itself: the +/// dashboard's links and event streams, and the client's hand-written +/// requests. Functions are named like the client's methods and percent-encode +/// path parameters as they do. +pub mod paths; /// Preview lifecycle and branch state contracts. pub mod previews; /// Deploying on push: environment opt-out and GitHub webhooks. @@ -118,7 +123,7 @@ pub use system::{ }; pub use piqueld_core::api::{ - API_PREFIX, ApplicationLogs, BuildLogChunk, BuildLogPage, BuildRecord, BuildState, Envelope, + ApplicationLogs, BuildLogChunk, BuildLogPage, BuildRecord, BuildState, Envelope, EnvironmentAccess, ErrorBody, LogRecord, LogStream, ManifestChange, MountedSecret, Page, SecretAccess, SecretAccessChange, SecretKeyRecovery, SecretMetadata, StoredSecret, }; diff --git a/crates/piqueld-client/src/paths.rs b/crates/piqueld-client/src/paths.rs new file mode 100644 index 000000000..89d25a48a --- /dev/null +++ b/crates/piqueld-client/src/paths.rs @@ -0,0 +1,1148 @@ +// Generated from the OpenAPI document. Run `just generate`; do not edit. + +use progenitor_client::encode_path; + +/// `POST /api/v1/applications/{id}/services` +#[must_use] +pub fn add_application_service(id: &str) -> String { + format!("/api/v1/applications/{id}/services", id = encode_path(id)) +} + +/// `POST /api/v1/applications/{id}/volumes` +#[must_use] +pub fn add_application_volume(id: &str) -> String { + format!("/api/v1/applications/{id}/volumes", id = encode_path(id)) +} + +/// `GET /api/v1/applications/{id}/secrets` +#[must_use] +pub fn application_secrets(id: &str) -> String { + format!("/api/v1/applications/{id}/secrets", id = encode_path(id)) +} + +/// `POST /api/v1/applications/apply` +#[must_use] +pub fn apply_application() -> String { + String::from("/api/v1/applications/apply") +} + +/// `POST /api/v1/auth/device/approve` +#[must_use] +pub fn auth_device_approve() -> String { + String::from("/api/v1/auth/device/approve") +} + +/// `POST /api/v1/auth/device/confirm` +#[must_use] +pub fn auth_device_confirm() -> String { + String::from("/api/v1/auth/device/confirm") +} + +/// `POST /api/v1/auth/device/inspect` +#[must_use] +pub fn auth_device_inspect() -> String { + String::from("/api/v1/auth/device/inspect") +} + +/// `POST /api/v1/auth/device/poll` +#[must_use] +pub fn auth_device_poll() -> String { + String::from("/api/v1/auth/device/poll") +} + +/// `POST /api/v1/auth/device/start` +#[must_use] +pub fn auth_device_start() -> String { + String::from("/api/v1/auth/device/start") +} + +/// `GET /api/v1/auth/directory` +#[must_use] +pub fn auth_directory() -> String { + String::from("/api/v1/auth/directory") +} + +/// `POST /api/v1/auth/login/finish` +#[must_use] +pub fn auth_login_finish() -> String { + String::from("/api/v1/auth/login/finish") +} + +/// `POST /api/v1/auth/login/start` +#[must_use] +pub fn auth_login_start() -> String { + String::from("/api/v1/auth/login/start") +} + +/// `POST /api/v1/auth/logout` +#[must_use] +pub fn auth_logout() -> String { + String::from("/api/v1/auth/logout") +} + +/// `POST /api/v1/auth/manage` +#[must_use] +pub fn auth_manage() -> String { + String::from("/api/v1/auth/manage") +} + +/// `GET /api/v1/auth/me` +#[must_use] +pub fn auth_me() -> String { + String::from("/api/v1/auth/me") +} + +/// `POST /api/v1/auth/login/operator` +#[must_use] +pub fn auth_operator_sign_in() -> String { + String::from("/api/v1/auth/login/operator") +} + +/// `POST /api/v1/auth/reauthenticate/finish` +#[must_use] +pub fn auth_reauthenticate_finish() -> String { + String::from("/api/v1/auth/reauthenticate/finish") +} + +/// `POST /api/v1/auth/reauthenticate/start` +#[must_use] +pub fn auth_reauthenticate_start() -> String { + String::from("/api/v1/auth/reauthenticate/start") +} + +/// `POST /api/v1/auth/recovery` +#[must_use] +pub fn auth_recover_admin() -> String { + String::from("/api/v1/auth/recovery") +} + +/// `POST /api/v1/auth/register/finish` +#[must_use] +pub fn auth_registration_finish() -> String { + String::from("/api/v1/auth/register/finish") +} + +/// `POST /api/v1/auth/register/start` +#[must_use] +pub fn auth_registration_start() -> String { + String::from("/api/v1/auth/register/start") +} + +/// `GET /api/v1/auth/setup-link` +#[must_use] +pub fn auth_setup_link() -> String { + String::from("/api/v1/auth/setup-link") +} + +/// `POST /api/v1/auth/sign-in-link` +#[must_use] +pub fn auth_sign_in_link() -> String { + String::from("/api/v1/auth/sign-in-link") +} + +/// `GET /api/v1/auth/status` +#[must_use] +pub fn auth_status() -> String { + String::from("/api/v1/auth/status") +} + +/// `GET /api/v1/builds/{id}/logs` +#[must_use] +pub fn build_logs(id: i64) -> String { + format!("/api/v1/builds/{id}/logs") +} + +/// `POST /api/v1/system/builder/check` +#[must_use] +pub fn check_builder() -> String { + String::from("/api/v1/system/builder/check") +} + +/// `POST /api/v1/applications` +#[must_use] +pub fn create_application() -> String { + String::from("/api/v1/applications") +} + +/// `POST /api/v1/applications/{id}/environments` +#[must_use] +pub fn create_environment(id: &str) -> String { + format!( + "/api/v1/applications/{id}/environments", + id = encode_path(id) + ) +} + +/// `POST /api/v1/applications/{id}/previews` +#[must_use] +pub fn create_preview(id: &str) -> String { + format!("/api/v1/applications/{id}/previews", id = encode_path(id)) +} + +/// `DELETE /api/v1/applications/{id}` +#[must_use] +pub fn delete_application(id: &str) -> String { + format!("/api/v1/applications/{id}", id = encode_path(id)) +} + +/// `DELETE /api/v1/applications/{id}/secrets/{name}` +#[must_use] +pub fn delete_application_secret(id: &str, name: &str) -> String { + format!( + "/api/v1/applications/{id}/secrets/{name}", + id = encode_path(id), + name = encode_path(name) + ) +} + +/// `DELETE /api/v1/environments/{id}` +#[must_use] +pub fn delete_environment(id: &str) -> String { + format!("/api/v1/environments/{id}", id = encode_path(id)) +} + +/// `DELETE /api/v1/environments/{id}/secrets/{name}` +#[must_use] +pub fn delete_environment_secret(id: &str, name: &str) -> String { + format!( + "/api/v1/environments/{id}/secrets/{name}", + id = encode_path(id), + name = encode_path(name) + ) +} + +/// `DELETE /api/v1/notifications/destinations/{name}` +#[must_use] +pub fn delete_notification_destination(name: &str) -> String { + format!( + "/api/v1/notifications/destinations/{name}", + name = encode_path(name) + ) +} + +/// `DELETE /api/v1/previews/{id}` +#[must_use] +pub fn delete_preview(id: &str) -> String { + format!("/api/v1/previews/{id}", id = encode_path(id)) +} + +/// `POST /api/v1/environments/{id}/deploy` +#[must_use] +pub fn deploy_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/deploy", id = encode_path(id)) +} + +/// `POST /api/v1/applications/{id}/previews/local` +#[must_use] +pub fn deploy_local_preview(id: &str) -> String { + format!( + "/api/v1/applications/{id}/previews/local", + id = encode_path(id) + ) +} + +/// `POST /api/v1/previews/{id}/deploy` +#[must_use] +pub fn deploy_preview(id: &str) -> String { + format!("/api/v1/previews/{id}/deploy", id = encode_path(id)) +} + +/// `GET /api/v1/analytics/deployments` +#[must_use] +pub fn deployment_analytics() -> String { + String::from("/api/v1/analytics/deployments") +} + +/// `DELETE /api/v1/applications/{id}/repository` +#[must_use] +pub fn disconnect_manifest_repository(id: &str) -> String { + format!("/api/v1/applications/{id}/repository", id = encode_path(id)) +} + +/// `GET /api/v1/applications/{id}/manifest` +#[must_use] +pub fn download_application_manifest(id: &str) -> String { + format!("/api/v1/applications/{id}/manifest", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/logs` +#[must_use] +pub fn environment_logs(id: &str) -> String { + format!("/api/v1/environments/{id}/logs", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/secrets` +#[must_use] +pub fn environment_secrets(id: &str) -> String { + format!("/api/v1/environments/{id}/secrets", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/status` +#[must_use] +pub fn environment_status(id: &str) -> String { + format!("/api/v1/environments/{id}/status", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/traffic` +#[must_use] +pub fn environment_traffic(id: &str) -> String { + format!("/api/v1/environments/{id}/traffic", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/exec` +#[must_use] +pub fn exec_environment_command(id: &str) -> String { + format!("/api/v1/environments/{id}/exec", id = encode_path(id)) +} + +/// `POST /api/v1/applications/{id}/webhook/secret` +#[must_use] +pub fn generate_webhook_secret(id: &str) -> String { + format!( + "/api/v1/applications/{id}/webhook/secret", + id = encode_path(id) + ) +} + +/// `GET /api/v1/applications/{id}` +#[must_use] +pub fn get_application(id: &str) -> String { + format!("/api/v1/applications/{id}", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/deployments/{deployment}` +#[must_use] +pub fn get_deployment(id: &str, deployment: &str) -> String { + format!( + "/api/v1/environments/{id}/deployments/{deployment}", + id = encode_path(id), + deployment = encode_path(deployment) + ) +} + +/// `GET /api/v1/diagnostics/{id}` +#[must_use] +pub fn get_diagnostic(id: &str) -> String { + format!("/api/v1/diagnostics/{id}", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}` +#[must_use] +pub fn get_environment(id: &str) -> String { + format!("/api/v1/environments/{id}", id = encode_path(id)) +} + +/// `GET /api/v1/environments/{id}/detail` +#[must_use] +pub fn get_environment_detail(id: &str) -> String { + format!("/api/v1/environments/{id}/detail", id = encode_path(id)) +} + +/// `GET /api/v1/operations/{id}` +#[must_use] +pub fn get_operation(id: &str) -> String { + format!("/api/v1/operations/{id}", id = encode_path(id)) +} + +/// `GET /api/v1/operations/{id}/timeline` +#[must_use] +pub fn get_operation_timeline(id: &str) -> String { + format!("/api/v1/operations/{id}/timeline", id = encode_path(id)) +} + +/// `GET /api/v1/previews/{id}` +#[must_use] +pub fn get_preview(id: &str) -> String { + format!("/api/v1/previews/{id}", id = encode_path(id)) +} + +/// `GET /api/v1/applications/{id}/webhook` +#[must_use] +pub fn get_webhook(id: &str) -> String { + format!("/api/v1/applications/{id}/webhook", id = encode_path(id)) +} + +/// `GET /api/v1/applications` +#[must_use] +pub fn list_applications() -> String { + String::from("/api/v1/applications") +} + +/// `GET /api/v1/audit` +#[must_use] +pub fn list_audit() -> String { + String::from("/api/v1/audit") +} + +/// `GET /api/v1/builds` +#[must_use] +pub fn list_builds() -> String { + String::from("/api/v1/builds") +} + +/// `GET /api/v1/environments/{id}/deployments/{deployment}/attempts` +#[must_use] +pub fn list_deployment_attempts(id: &str, deployment: &str) -> String { + format!( + "/api/v1/environments/{id}/deployments/{deployment}/attempts", + id = encode_path(id), + deployment = encode_path(deployment) + ) +} + +/// `GET /api/v1/environments/{id}/deployments` +#[must_use] +pub fn list_deployments(id: &str) -> String { + format!( + "/api/v1/environments/{id}/deployments", + id = encode_path(id) + ) +} + +/// `GET /api/v1/events/kinds` +#[must_use] +pub fn list_event_kinds() -> String { + String::from("/api/v1/events/kinds") +} + +/// `GET /api/v1/events` +#[must_use] +pub fn list_events() -> String { + String::from("/api/v1/events") +} + +/// `GET /api/v1/applications/{id}/previews` +#[must_use] +pub fn list_previews(id: &str) -> String { + format!("/api/v1/applications/{id}/previews", id = encode_path(id)) +} + +/// `GET /api/v1/applications/{id}/releases` +#[must_use] +pub fn list_releases(id: &str) -> String { + format!("/api/v1/applications/{id}/releases", id = encode_path(id)) +} + +/// `GET /api/v1/notifications/deliveries` +#[must_use] +pub fn notification_deliveries() -> String { + String::from("/api/v1/notifications/deliveries") +} + +/// `GET /api/v1/notifications/destinations` +#[must_use] +pub fn notification_destinations() -> String { + String::from("/api/v1/notifications/destinations") +} + +/// `GET /api/v1/openapi.json` +#[must_use] +pub fn open_api_document() -> String { + String::from("/api/v1/openapi.json") +} + +/// `POST /api/v1/applications/plan` +#[must_use] +pub fn plan_application() -> String { + String::from("/api/v1/applications/plan") +} + +/// `POST /api/v1/environments/{id}/promote/plan` +#[must_use] +pub fn plan_promotion(id: &str) -> String { + format!( + "/api/v1/environments/{id}/promote/plan", + id = encode_path(id) + ) +} + +/// `POST /api/v1/environments/{id}/promote` +#[must_use] +pub fn promote_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/promote", id = encode_path(id)) +} + +/// `POST /api/v1/applications/{id}/previews/prune` +#[must_use] +pub fn prune_previews(id: &str) -> String { + format!( + "/api/v1/applications/{id}/previews/prune", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/secrets/{name}` +#[must_use] +pub fn put_application_secret(id: &str, name: &str) -> String { + format!( + "/api/v1/applications/{id}/secrets/{name}", + id = encode_path(id), + name = encode_path(name) + ) +} + +/// `PUT /api/v1/notifications/destinations/{name}` +#[must_use] +pub fn put_notification_destination(name: &str) -> String { + format!( + "/api/v1/notifications/destinations/{name}", + name = encode_path(name) + ) +} + +/// `POST /api/v1/environments/{id}/reconcile` +#[must_use] +pub fn reconcile_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/reconcile", id = encode_path(id)) +} + +/// `POST /api/v1/system/secrets/recover-key` +#[must_use] +pub fn recover_secret_key() -> String { + String::from("/api/v1/system/secrets/recover-key") +} + +/// `POST /api/v1/system/dns/refresh` +#[must_use] +pub fn refresh_dns() -> String { + String::from("/api/v1/system/dns/refresh") +} + +/// `POST /api/v1/environments/{id}/secrets/{name}/regenerate` +#[must_use] +pub fn regenerate_environment_secret(id: &str, name: &str) -> String { + format!( + "/api/v1/environments/{id}/secrets/{name}/regenerate", + id = encode_path(id), + name = encode_path(name) + ) +} + +/// `DELETE /api/v1/applications/{id}/services/{service}` +#[must_use] +pub fn remove_application_service(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `DELETE /api/v1/applications/{id}/volumes/{volume}` +#[must_use] +pub fn remove_application_volume(id: &str, volume: &str) -> String { + format!( + "/api/v1/applications/{id}/volumes/{volume}", + id = encode_path(id), + volume = encode_path(volume) + ) +} + +/// `DELETE /api/v1/applications/{id}/services/{service}/environment/{key}` +#[must_use] +pub fn remove_service_environment_entry(id: &str, service: &str, key: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/environment/{key}", + id = encode_path(id), + service = encode_path(service), + key = encode_path(key) + ) +} + +/// `DELETE /api/v1/applications/{id}/services/{service}/file` +#[must_use] +pub fn remove_service_file(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/file", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `DELETE /api/v1/applications/{id}/services/{service}/mount` +#[must_use] +pub fn remove_service_mount(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/mount", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `POST /api/v1/environments/{id}/rename` +#[must_use] +pub fn rename_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/rename", id = encode_path(id)) +} + +/// `POST /api/v1/environments/{id}/restore` +#[must_use] +pub fn restore_deployment(id: &str) -> String { + format!("/api/v1/environments/{id}/restore", id = encode_path(id)) +} + +/// `POST /api/v1/previews/{id}/restore` +#[must_use] +pub fn restore_preview_deployment(id: &str) -> String { + format!("/api/v1/previews/{id}/restore", id = encode_path(id)) +} + +/// `POST /api/v1/notifications/deliveries/{id}/retry` +#[must_use] +pub fn retry_notification_delivery(id: &str) -> String { + format!( + "/api/v1/notifications/deliveries/{id}/retry", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/exports` +#[must_use] +pub fn set_application_exports(id: &str) -> String { + format!("/api/v1/applications/{id}/exports", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/generated-secrets` +#[must_use] +pub fn set_application_generated_secrets(id: &str) -> String { + format!( + "/api/v1/applications/{id}/generated-secrets", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/imports` +#[must_use] +pub fn set_application_imports(id: &str) -> String { + format!("/api/v1/applications/{id}/imports", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/jobs` +#[must_use] +pub fn set_application_jobs(id: &str) -> String { + format!("/api/v1/applications/{id}/jobs", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/name` +#[must_use] +pub fn set_application_name(id: &str) -> String { + format!("/api/v1/applications/{id}/name", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/routes` +#[must_use] +pub fn set_application_routes(id: &str) -> String { + format!("/api/v1/applications/{id}/routes", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/variables` +#[must_use] +pub fn set_application_variables(id: &str) -> String { + format!("/api/v1/applications/{id}/variables", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/volumes` +#[must_use] +pub fn set_application_volumes(id: &str) -> String { + format!("/api/v1/applications/{id}/volumes", id = encode_path(id)) +} + +/// `PUT /api/v1/environments/{id}/branch` +#[must_use] +pub fn set_environment_branch(id: &str) -> String { + format!("/api/v1/environments/{id}/branch", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/environments/{environment}/rollback` +#[must_use] +pub fn set_environment_rollback(id: &str, environment: &str) -> String { + format!( + "/api/v1/applications/{id}/environments/{environment}/rollback", + id = encode_path(id), + environment = encode_path(environment) + ) +} + +/// `PUT /api/v1/environments/{id}/source` +#[must_use] +pub fn set_environment_source(id: &str) -> String { + format!("/api/v1/environments/{id}/source", id = encode_path(id)) +} + +/// `PUT /api/v1/environments/{id}/sync` +#[must_use] +pub fn set_environment_sync(id: &str) -> String { + format!("/api/v1/environments/{id}/sync", id = encode_path(id)) +} + +/// `PUT /api/v1/environments/{id}/telemetry` +#[must_use] +pub fn set_environment_telemetry(id: &str) -> String { + format!("/api/v1/environments/{id}/telemetry", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/environments/{environment}/visibility` +#[must_use] +pub fn set_environment_visibility(id: &str, environment: &str) -> String { + format!( + "/api/v1/applications/{id}/environments/{environment}/visibility", + id = encode_path(id), + environment = encode_path(environment) + ) +} + +/// `PUT /api/v1/applications/{id}/repository` +#[must_use] +pub fn set_manifest_repository(id: &str) -> String { + format!("/api/v1/applications/{id}/repository", id = encode_path(id)) +} + +/// `PUT /api/v1/applications/{id}/repository/path` +#[must_use] +pub fn set_manifest_repository_path(id: &str) -> String { + format!( + "/api/v1/applications/{id}/repository/path", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/repository/sync` +#[must_use] +pub fn set_manifest_repository_sync(id: &str) -> String { + format!( + "/api/v1/applications/{id}/repository/sync", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/repository/url` +#[must_use] +pub fn set_manifest_repository_url(id: &str) -> String { + format!( + "/api/v1/applications/{id}/repository/url", + id = encode_path(id) + ) +} + +/// `PUT /api/v1/applications/{id}/secrets/{name}/access` +#[must_use] +pub fn set_secret_access(id: &str, name: &str) -> String { + format!( + "/api/v1/applications/{id}/secrets/{name}/access", + id = encode_path(id), + name = encode_path(name) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/arguments` +#[must_use] +pub fn set_service_arguments(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/arguments", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/command` +#[must_use] +pub fn set_service_command(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/command", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/git/context` +#[must_use] +pub fn set_service_context(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/git/context", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/resources/cpu` +#[must_use] +pub fn set_service_cpu(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/resources/cpu", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/depends-on` +#[must_use] +pub fn set_service_depends_on(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/depends-on", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/git/dockerfile` +#[must_use] +pub fn set_service_dockerfile(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/git/dockerfile", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/environment` +#[must_use] +pub fn set_service_environment(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/environment", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/environment/{key}` +#[must_use] +pub fn set_service_environment_entry(id: &str, service: &str, key: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/environment/{key}", + id = encode_path(id), + service = encode_path(service), + key = encode_path(key) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/file` +#[must_use] +pub fn set_service_file(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/file", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/files` +#[must_use] +pub fn set_service_files(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/files", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/general` +#[must_use] +pub fn set_service_general(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/general", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/git/branch` +#[must_use] +pub fn set_service_git_branch(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/git/branch", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/git/commit` +#[must_use] +pub fn set_service_git_commit(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/git/commit", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/git/url` +#[must_use] +pub fn set_service_git_url(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/git/url", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/command` +#[must_use] +pub fn set_service_health_command(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/command", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/interval` +#[must_use] +pub fn set_service_health_interval(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/interval", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/path` +#[must_use] +pub fn set_service_health_path(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/path", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/port` +#[must_use] +pub fn set_service_health_port(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/port", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/retries` +#[must_use] +pub fn set_service_health_retries(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/retries", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/start-period` +#[must_use] +pub fn set_service_health_start_period(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/start-period", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck/timeout` +#[must_use] +pub fn set_service_health_timeout(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck/timeout", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/healthcheck` +#[must_use] +pub fn set_service_healthcheck(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/healthcheck", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source/image` +#[must_use] +pub fn set_service_image(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source/image", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/resources/memory` +#[must_use] +pub fn set_service_memory(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/resources/memory", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/mount` +#[must_use] +pub fn set_service_mount(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/mount", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/mounts` +#[must_use] +pub fn set_service_mounts(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/mounts", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/name` +#[must_use] +pub fn set_service_name(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/name", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/process` +#[must_use] +pub fn set_service_process(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/process", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/receives-telemetry` +#[must_use] +pub fn set_service_receives_telemetry(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/receives-telemetry", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/replicas` +#[must_use] +pub fn set_service_replicas(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/replicas", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/resources` +#[must_use] +pub fn set_service_resources(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/resources", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/rollout` +#[must_use] +pub fn set_service_rollout(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/rollout", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/secrets` +#[must_use] +pub fn set_service_secrets(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/secrets", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `PUT /api/v1/applications/{id}/services/{service}/source` +#[must_use] +pub fn set_service_source(id: &str, service: &str) -> String { + format!( + "/api/v1/applications/{id}/services/{service}/source", + id = encode_path(id), + service = encode_path(service) + ) +} + +/// `POST /api/v1/environments/{id}/start` +#[must_use] +pub fn start_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/start", id = encode_path(id)) +} + +/// `POST /api/v1/environments/{id}/stop` +#[must_use] +pub fn stop_environment(id: &str) -> String { + format!("/api/v1/environments/{id}/stop", id = encode_path(id)) +} + +/// `GET /api/v1/events/stream` +#[must_use] +pub fn stream_events() -> String { + String::from("/api/v1/events/stream") +} + +/// `GET /api/v1/operations/{id}/timeline/stream` +#[must_use] +pub fn stream_operation_timeline(id: &str) -> String { + format!( + "/api/v1/operations/{id}/timeline/stream", + id = encode_path(id) + ) +} + +/// `GET /api/v1/system/checks` +#[must_use] +pub fn system_checks() -> String { + String::from("/api/v1/system/checks") +} + +/// `GET /api/v1/system/configuration` +#[must_use] +pub fn system_configuration() -> String { + String::from("/api/v1/system/configuration") +} + +/// `GET /api/v1/system/readiness` +#[must_use] +pub fn system_readiness() -> String { + String::from("/api/v1/system/readiness") +} + +/// `GET /api/v1/system/resources` +#[must_use] +pub fn system_resources() -> String { + String::from("/api/v1/system/resources") +} + +/// `GET /api/v1/system/status` +#[must_use] +pub fn system_status() -> String { + String::from("/api/v1/system/status") +} + +/// `POST /api/v1/notifications/destinations/{name}/test` +#[must_use] +pub fn test_notification_destination(name: &str) -> String { + format!( + "/api/v1/notifications/destinations/{name}/test", + name = encode_path(name) + ) +} + +/// `GET /api/v1/audit/verify` +#[must_use] +pub fn verify_audit() -> String { + String::from("/api/v1/audit/verify") +} diff --git a/crates/piqueld-client/src/previews.rs b/crates/piqueld-client/src/previews.rs index 97731c223..64b9a7f4e 100644 --- a/crates/piqueld-client/src/previews.rs +++ b/crates/piqueld-client/src/previews.rs @@ -8,7 +8,7 @@ pub use piqueld_core::api::{ pub use piqueld_core::manifest::PreviewLimits; pub use piqueld_core::{EnvironmentKind, Preview, PreviewSlot, PreviewSlug}; -use crate::{AcceptedOperation, Client, ClientError, client::EnvelopedCall}; +use crate::{AcceptedOperation, Client, ClientError, client::EnvelopedCall, paths}; impl Client { /// Lists an application's previews with their branch states. @@ -70,10 +70,7 @@ impl Client { let mut query = vec![("branch", branch.to_owned())]; query.extend(slot.map(|slot| ("slot", slot.to_owned()))); self.send( - &format!( - "/api/v1/applications/{}/previews/local", - progenitor_client::encode_path(application) - ), + &paths::deploy_local_preview(application), &query, &[("Content-Length", length.to_string())], ("application/octet-stream", bundle.into()), diff --git a/crates/piqueld-core/src/api/mod.rs b/crates/piqueld-core/src/api/mod.rs index 97fab0f4f..e7174465a 100644 --- a/crates/piqueld-core/src/api/mod.rs +++ b/crates/piqueld-core/src/api/mod.rs @@ -34,8 +34,6 @@ use utoipa::ToSchema; pub use crate::urls::{RouteUrl, UrlCondition, UrlState}; -/// Versioned prefix used by all API endpoints. -pub const API_PREFIX: &str = "/api/v1"; /// Page size of a paginated listing that requests none, except the /// application list, which defaults to [`MAX_PAGE_SIZE`]. pub const DEFAULT_PAGE_SIZE: u16 = 50; diff --git a/docs/dev/architecture/dependency-flow.md b/docs/dev/architecture/dependency-flow.md index 675741734..c6c25e53f 100644 --- a/docs/dev/architecture/dependency-flow.md +++ b/docs/dev/architecture/dependency-flow.md @@ -21,7 +21,10 @@ Progenitor and checked into `piqueld-client`. They reuse the core wire contracts and share bounded TCP/Unix/browser response handling. Handwritten convenience methods unwrap envelopes, provide local validation, and cover the requests Progenitor cannot generate: the two TOML operations, the local snapshot upload, -and the exec WebSocket. The generator is a +and the exec WebSocket. `piqueld_client::paths`, generated from the same +document, builds each operation's path, named like its client method: those +requests take their paths from it, as do the dashboard's manifest download +link and its event streams, which the browser opens itself. The generator is a daemon example that only development builds compile; the client uses Progenitor's small runtime support crate. See [client generation](https://github.com/sourceorigin-dev/piqueld/blob/main/tools/client-codegen/README.md). diff --git a/tools/client-codegen/README.md b/tools/client-codegen/README.md index 9cdfa394f..b737ca11f 100644 --- a/tools/client-codegen/README.md +++ b/tools/client-codegen/README.md @@ -4,11 +4,16 @@ The daemon's `generate_openapi` example produces the OpenAPI 3.0 document at `docs/openapi-v1.json`, then passes a client-specific copy directly to Progenitor 0.11. It writes the resulting operations to `crates/piqueld-client/src/generated.rs`, and the application manifest's JSON -Schema to `docs/application-manifest.schema.json`. All three artifacts are -prepared before any checked-in file is updated. +Schema to `docs/application-manifest.schema.json`. From the complete document +it also writes `crates/piqueld-client/src/paths.rs`: one function per +operation, named like its client method, that builds the operation's path with +its path parameters percent-encoded. The client's hand-written requests and +the dashboard's links and event streams, which the browser opens itself, take +their paths from it. All the artifacts are prepared before any checked-in file +is updated. Run `just generate` after changing API metadata, shared wire contracts, or the -manifest. Run `just openapi-check` to compare all three artifacts without +manifest. Run `just openapi-check` to compare all the artifacts without modifying them. The generator is an example, and its dependencies are development dependencies, so daemon builds do not compile it and the published crate does not depend on it. Generation needs only the Rust toolchain; it does From 8a3df824fc4c929b648d1c24ce86513319d81b79 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 15:57:40 +0100 Subject: [PATCH 2/2] refactor(client): name paths with heck, and say what their names follow The path functions are named after the API's operation IDs, as the generated client's methods are, not like the public Client methods; the docs now say so. The generator names them with heck's snake case, as Progenitor does, so the two can't disagree, and takes the document's paths from run() instead of reading them again. Part of PQD-162 --- Cargo.lock | 1 + Cargo.toml | 1 + apps/piqueld/Cargo.toml | 2 ++ .../piqueld/examples/generate_openapi/main.rs | 32 +++++++------------ crates/piqueld-client/src/lib.rs | 5 +-- docs/dev/architecture/dependency-flow.md | 7 ++-- tools/client-codegen/README.md | 4 +-- 7 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fd944a01..a3966a316 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2686,6 +2686,7 @@ dependencies = [ "clap", "futures-util", "getrandom 0.3.4", + "heck", "hickory-resolver", "http", "http-body-util", diff --git a/Cargo.toml b/Cargo.toml index f4d9c76ec..5ee8c70cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -135,6 +135,7 @@ web-sys = "=0.3.103" shlex = "2.0.1" vt100 = "0.16.2" +heck = "0.5.0" proptest = "1.6.0" prettyplease = "0.2.37" progenitor = { version = "0.11.0", default-features = false } diff --git a/apps/piqueld/Cargo.toml b/apps/piqueld/Cargo.toml index 265479296..01568d9ec 100644 --- a/apps/piqueld/Cargo.toml +++ b/apps/piqueld/Cargo.toml @@ -100,6 +100,8 @@ piqueld-store = { workspace = true, features = ["testing"] } tokio-tungstenite.workspace = true # The `generate_openapi` example, which `just generate` runs. piqueld-generated.workspace = true +# Names paths like Progenitor names methods. +heck.workspace = true prettyplease.workspace = true progenitor.workspace = true syn.workspace = true diff --git a/apps/piqueld/examples/generate_openapi/main.rs b/apps/piqueld/examples/generate_openapi/main.rs index 17c81a8ce..ad85bd201 100644 --- a/apps/piqueld/examples/generate_openapi/main.rs +++ b/apps/piqueld/examples/generate_openapi/main.rs @@ -8,12 +8,13 @@ mod settings_reference; use anyhow::{Context, Result, anyhow, bail, ensure}; +use heck::ToSnakeCase; use piqueld::api::http::openapi_document; use piqueld_config::DaemonConfig; use piqueld_core::manifest::ApplicationManifest; use piqueld_generated::Artifacts; use serde::Deserialize; -use serde_json::Value; +use serde_json::{Map, Value}; use settings_reference::SettingsPage; use std::{ collections::{BTreeMap, BTreeSet}, @@ -77,7 +78,7 @@ impl Generator { .context("read Progenitor configuration")?, ) .context("parse Progenitor configuration")?; - let operation_paths = Self::generate_paths(&document)?; + let operation_paths = Self::generate_paths(paths)?; let client = self.generate_client(document.clone(), &config)?; let document = format!("{}\n", serde_json::to_string_pretty(&document)?); let manifest_schema = ApplicationManifest::json_schema(); @@ -164,16 +165,13 @@ impl Generator { )) } - /// Generates the source of `piqueld_client::paths`: one function per - /// operation, named like its client method, that builds the operation's - /// path with its path parameters percent-encoded as the client encodes - /// them. It reads the complete document, so it also covers the operations + /// Generates the source of `piqueld_client::paths` from the document's + /// `paths`: one function per operation, named after its operation ID as + /// the generated client's methods are, that builds the operation's path + /// with its path parameters percent-encoded as the client encodes them. + /// It reads the complete document, so it also covers the operations /// Progenitor's copy drops, such as the exec WebSocket. - fn generate_paths(document: &Value) -> Result { - let paths = document - .get("paths") - .and_then(Value::as_object) - .context("OpenAPI document has no paths object")?; + fn generate_paths(paths: &Map) -> Result { let mut functions = BTreeMap::new(); for (path, item) in paths { let operations = item @@ -187,16 +185,8 @@ impl Generator { .get("operationId") .and_then(Value::as_str) .with_context(|| format!("{method} {path} has no operation ID"))?; - // Progenitor's method names: the operation ID in snake case. - let name: String = id - .chars() - .flat_map(|c| { - c.is_ascii_uppercase() - .then_some('_') - .into_iter() - .chain([c.to_ascii_lowercase()]) - }) - .collect(); + // Progenitor names its methods with heck's snake case too. + let name = id.to_snake_case(); let mut template = String::new(); let mut parameters = Vec::new(); let mut arguments = String::new(); diff --git a/crates/piqueld-client/src/lib.rs b/crates/piqueld-client/src/lib.rs index 021993545..cf96670ab 100644 --- a/crates/piqueld-client/src/lib.rs +++ b/crates/piqueld-client/src/lib.rs @@ -41,8 +41,9 @@ pub mod openapi; pub mod operations; /// Each operation's path, for what the client does not send itself: the /// dashboard's links and event streams, and the client's hand-written -/// requests. Functions are named like the client's methods and percent-encode -/// path parameters as they do. +/// requests. Functions are named after the API's operation IDs, as the +/// generated client's methods are, and percent-encode path parameters as +/// those methods do. pub mod paths; /// Preview lifecycle and branch state contracts. pub mod previews; diff --git a/docs/dev/architecture/dependency-flow.md b/docs/dev/architecture/dependency-flow.md index c6c25e53f..425bc0e75 100644 --- a/docs/dev/architecture/dependency-flow.md +++ b/docs/dev/architecture/dependency-flow.md @@ -22,9 +22,10 @@ and share bounded TCP/Unix/browser response handling. Handwritten convenience methods unwrap envelopes, provide local validation, and cover the requests Progenitor cannot generate: the two TOML operations, the local snapshot upload, and the exec WebSocket. `piqueld_client::paths`, generated from the same -document, builds each operation's path, named like its client method: those -requests take their paths from it, as do the dashboard's manifest download -link and its event streams, which the browser opens itself. The generator is a +document, builds each operation's path, in a function named after its +operation ID as the generated client's methods are. Those requests take their +paths from it, as do the dashboard's manifest download link and its event +streams, which the browser opens itself. The generator is a daemon example that only development builds compile; the client uses Progenitor's small runtime support crate. See [client generation](https://github.com/sourceorigin-dev/piqueld/blob/main/tools/client-codegen/README.md). diff --git a/tools/client-codegen/README.md b/tools/client-codegen/README.md index b737ca11f..da46610a5 100644 --- a/tools/client-codegen/README.md +++ b/tools/client-codegen/README.md @@ -6,8 +6,8 @@ Progenitor 0.11. It writes the resulting operations to `crates/piqueld-client/src/generated.rs`, and the application manifest's JSON Schema to `docs/application-manifest.schema.json`. From the complete document it also writes `crates/piqueld-client/src/paths.rs`: one function per -operation, named like its client method, that builds the operation's path with -its path parameters percent-encoded. The client's hand-written requests and +operation, named after its operation ID as the generated client's methods are, +that builds the operation's path with its path parameters percent-encoded. The client's hand-written requests and the dashboard's links and event streams, which the browser opens itself, take their paths from it. All the artifacts are prepared before any checked-in file is updated.