Skip to main content

stackless_integrations/providers/heygen/
api.rs

1//! `heygen/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-heygen";
14
15#[derive(Debug, Serialize)]
16pub struct HeyGenApiConfig {
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub auto_reload_amount: Option<i64>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub auto_reload_enabled: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub auto_reload_threshold: Option<i64>,
23}
24
25impl CatalogService for HeyGenApiConfig {
26    const REFERENCE: &'static str = "heygen/api";
27}
28
29#[derive(Debug)]
30pub struct HeyGenApi;
31
32impl Hostable for HeyGenApi {
33    const PROVIDER: &'static str = "heygen";
34    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
35    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
36    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
37    const OUTPUTS: &'static [&'static str] = &["api_key"];
38}
39
40impl FamilyResource for HeyGenApi {
41    type Config = HeyGenApiConfig;
42    const PROVIDER_PREFIX: &'static str = "HEYGEN";
43    // Provisional until pinned by `mise run discover heygen/api`.
44    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
45        &[("API_KEY", "api_key", true)];
46
47    fn build_config(ctx: &ProvisionContext<'_>) -> Result<HeyGenApiConfig, IntegrationError> {
48        let config = super::integration_config(ctx)?;
49        Ok(HeyGenApiConfig {
50            auto_reload_amount: super::int_optional(ctx, &config, "auto_reload_amount")?,
51            auto_reload_enabled: super::bool_optional(ctx, &config, "auto_reload_enabled")?,
52            auto_reload_threshold: super::int_optional(ctx, &config, "auto_reload_threshold")?,
53        })
54    }
55}
56
57pub fn validate_config(
58    name: &str,
59    config: &BTreeMap<String, toml::Value>,
60) -> Result<(), IntegrationError> {
61    let _ = (name, config);
62    Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::ProviderOps;
69    use crate::resource::ResourcePayload;
70    use stackless_core::def::StackDef;
71    use stackless_stripe_projects::stripe::StripeProjects;
72    use stackless_stripe_projects::test_support;
73
74    #[test]
75    fn config_matches_catalog() {
76        const FIXTURE: &str = include_str!(concat!(
77            env!("CARGO_MANIFEST_DIR"),
78            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
79        ));
80        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
81        let failures = stackless_stripe_projects::verify_service(
82            &catalog,
83            &HeyGenApiConfig {
84                auto_reload_amount: None,
85                auto_reload_enabled: None,
86                auto_reload_threshold: None,
87            },
88        );
89        assert!(
90            failures.is_empty(),
91            "heygen/api catalog gaps:\n{}",
92            failures.join("\n")
93        );
94    }
95
96    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_heygen","provider_name":"HeyGen","service_id":"api","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"paid"},"configuration_schema":{"properties":{"auto_reload_amount":{"default":10,"description":"USD of API credit to add each time the balance runs low. Range $5-$1000, default $10.","maximum":1000,"minimum":5,"type":"integer"},"auto_reload_enabled":{"description":"Turn auto-reload on to top up API credit automatically when the balance runs low. Off by default; when off, amount/threshold are ignored.","type":"boolean"},"auto_reload_threshold":{"default":5,"description":"Auto-reload triggers when the remaining API credit balance falls below this USD amount. Range $5-$1000, default $5.","maximum":1000,"minimum":5,"type":"integer"}},"type":"object"}}]}}"##;
97
98    fn test_def() -> StackDef {
99        StackDef::parse(
100            r#"
101[stack]
102name = "atto"
103[stack.projects.stripe]
104project = "project_1"
105[integrations.res]
106provider = "heygen"
107[services.api]
108source = { repo = "r", ref = "main" }
109env = { OUT = "${integrations.res.api_key}" }
110health = { path = "/health" }
111[services.api.local]
112run = "true"
113"#,
114        )
115        .unwrap()
116    }
117
118    #[tokio::test]
119    async fn provision_records_outputs() {
120        let runner = test_support::provision_script(
121            CATALOG_ENVELOPE,
122            serde_json::json!({"HEYGEN_API_KEY": "val_api_key"}),
123            0,
124        );
125        let dir = tempfile::tempdir().unwrap();
126        std::fs::write(
127            dir.path().join("stackless.toml"),
128            "[stack]\nname=\"atto\"\n",
129        )
130        .unwrap();
131        let stripe = StripeProjects::new(&runner, dir.path());
132
133        let resource = HeyGenApi
134            .provision(
135                &stripe.as_dyn(),
136                &test_def(),
137                dir.path(),
138                "demo",
139                "res",
140                "local",
141                false,
142            )
143            .await
144            .unwrap();
145        assert_eq!(resource.resource_kind, "integration-heygen");
146        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
147        assert_eq!(payload.outputs["api_key"], "val_api_key");
148    }
149}