Skip to main content

stackless_integrations/providers/openrouter/
api.rs

1//! `openrouter/api` integration.
2
3use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12
13pub const RESOURCE_KIND: &str = "integration-openrouter";
14
15#[derive(Debug, Serialize)]
16pub struct OpenRouterApiConfig {}
17
18impl CatalogService for OpenRouterApiConfig {
19    const REFERENCE: &'static str = "openrouter/api";
20}
21
22#[derive(Debug)]
23pub struct OpenRouterApi;
24
25impl Hostable for OpenRouterApi {
26    const PROVIDER: &'static str = "openrouter";
27    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
28    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
29    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
30    const OUTPUTS: &'static [&'static str] = &["api_key", "type"];
31}
32
33impl FamilyResource for OpenRouterApi {
34    type Config = OpenRouterApiConfig;
35    const PROVIDER_PREFIX: &'static str = "OPENROUTER";
36    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
37        &[("API_KEY", "api_key", true), ("TYPE", "type", false)];
38
39    fn build_config(ctx: &ProvisionContext<'_>) -> Result<OpenRouterApiConfig, IntegrationError> {
40        let _ = super::integration_config(ctx)?;
41        Ok(OpenRouterApiConfig {})
42    }
43}
44
45pub fn validate_config(
46    name: &str,
47    config: &BTreeMap<String, toml::Value>,
48) -> Result<(), IntegrationError> {
49    let _ = (name, config);
50    Ok(())
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use crate::ProviderOps;
57    use crate::resource::ResourcePayload;
58    use stackless_core::def::StackDef;
59    use stackless_stripe_projects::stripe::StripeProjects;
60    use stackless_stripe_projects::test_support;
61
62    #[test]
63    fn config_matches_catalog() {
64        const FIXTURE: &str = include_str!(concat!(
65            env!("CARGO_MANIFEST_DIR"),
66            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
67        ));
68        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
69        let failures = stackless_stripe_projects::verify_service(&catalog, &OpenRouterApiConfig {});
70        assert!(
71            failures.is_empty(),
72            "openrouter/api catalog gaps:\n{}",
73            failures.join("\n")
74        );
75    }
76
77    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_api","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_openrouter","provider_name":"OpenRouter","service_id":"api","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"type":"object","required":[],"additionalProperties":false,"properties":{}}}]}}"##;
78
79    fn test_def() -> StackDef {
80        StackDef::parse(
81            r#"
82[stack]
83name = "atto"
84[stack.projects.stripe]
85project = "project_1"
86[integrations.res]
87provider = "openrouter"
88[services.api]
89source = { repo = "r", ref = "main" }
90env = { OUT = "${integrations.res.api_key}" }
91health = { path = "/health" }
92[services.api.local]
93run = "true"
94"#,
95        )
96        .unwrap()
97    }
98
99    #[tokio::test]
100    async fn provision_records_outputs() {
101        let runner = test_support::provision_script(
102            CATALOG_ENVELOPE,
103            serde_json::json!({"OPENROUTER_API_KEY": "val_api_key", "OPENROUTER_TYPE": "val_type"}),
104            0,
105        );
106        let dir = tempfile::tempdir().unwrap();
107        std::fs::write(
108            dir.path().join("stackless.toml"),
109            "[stack]\nname=\"atto\"\n",
110        )
111        .unwrap();
112        let stripe = StripeProjects::new(&runner, dir.path());
113
114        let resource = OpenRouterApi
115            .provision(
116                &stripe.as_dyn(),
117                &test_def(),
118                dir.path(),
119                "demo",
120                "res",
121                "local",
122                false,
123            )
124            .await
125            .unwrap();
126        assert_eq!(resource.resource_kind, "integration-openrouter");
127        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
128        assert_eq!(payload.outputs["api_key"], "val_api_key");
129    }
130}