ordinary-api 0.6.0-pre.13

API server for Ordinary
Documentation
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

use axum::body::Bytes;
use axum::extract::{Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::IntoResponse;

use crate::server::APPLICATION;
use serde::Deserialize;
use std::sync::Arc;
use tracing::Instrument;
use utoipa::IntoParams;

#[derive(Deserialize, IntoParams)]
pub struct Params {
    /// project domain
    d: String,
    /// action index in the ordinary.json
    i: u8,
}

#[utoipa::path(
    put,
    path = "/actions",
    tag = APPLICATION,
    request_body(content = [u8], content_type = "application/octet-stream"),
    params(Params),
    responses(
        (status = 401, description = "unauthorized for operation"),
        (status = 200, description = "install an action for an application"),
    ),
    security(
        ("access" = []),
    ),
)]
pub async fn install(
    State(state): State<Arc<crate::server::OrdinaryApiServerState>>,
    Query(Params { d, i }): Query<Params>,
    headers: HeaderMap,
    body: Bytes,
) -> impl IntoResponse {
    let domain = d;
    let idx = i;

    let span = tracing::info_span!("app", %domain);
    let span = span.in_scope(|| tracing::info_span!("action", idx));
    let span = span.in_scope(|| tracing::info_span!("install"));

    async {
        match crate::server::check_ordinary_auth(&state, &headers, 5, &domain) {
            Ok(account) => account,
            Err(code) => return (code, Bytes::new()),
        };

        tracing::info!("installing");

        let apps = state.apps.read().await;

        if let Some(wrapped_app) = apps.get(&domain) {
            match wrapped_app.app.set_action(idx, &body[..]).await {
                Ok(name) => {
                    tracing::info!(name, "installed");
                    return (StatusCode::OK, Bytes::new());
                }
                Err(err) => {
                    tracing::error!(%err, "failed");
                }
            }
        }

        (StatusCode::INTERNAL_SERVER_ERROR, Bytes::new())
    }
    .instrument(span)
    .await
}