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 crate::server::{APPLICATION, OrdinaryApiServerState};
use axum::extract::{Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::IntoResponse;
use bytes::Bytes;
use serde::Deserialize;
use std::sync::Arc;
use tracing::Instrument;
use utoipa::IntoParams;

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

#[utoipa::path(
    get,
    path = "/models/items",
    tag = APPLICATION,
    params(ListParams),
    responses(
        (status = 401, description = "unauthorized for operation"),
        (status = 200, description = "items list as flexbuffer bytes", body = [u8]),
    ),
    security(
        ("access" = []),
    ),
)]
pub async fn items_list(
    State(state): State<Arc<OrdinaryApiServerState>>,
    Query(ListParams { d, i }): Query<ListParams>,
    headers: HeaderMap,
) -> impl IntoResponse {
    let domain = d;
    let idx = i;

    let span = tracing::info_span!("app", %domain);
    let span = span.in_scope(|| tracing::info_span!("items", idx = &idx));
    let span = span.in_scope(|| tracing::info_span!("list"));

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

        tracing::info!(account, "listing");

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

        if let Some(wrapped_app) = apps.get(&domain) {
            match wrapped_app.app.list_items(idx, None) {
                Ok(items) => return (StatusCode::OK, items),
                Err(err) => tracing::error!(%err, "failed to list items"),
            }
        }

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