1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! A simple http client for interacting with [Replicate](https://replicate.com/).  
//! Provides simple async functionality for interacting with Replicate via
//! [serde](https://serde.rs) and [isahc](https://docs.rs/isahc/latest/isahc/).
//!
//! # Getting Started
//!
//! Add the following to your cargo toml
//! ```toml
//! replicate-rs = "0.7.0"
//! ```
//!
//! # Examples
//!
//! #### Create a Prediction
//!
//! Create a prediction, and get refreshed prediction data.
//!
//! ```rust
//! use replicate_rs::config::ReplicateConfig;
//! use replicate_rs::predictions::PredictionClient;
//! use serde::Serialize;
//! use serde_json::json;
//!
//! // The library is runtime agnostic, so you should be able to use any async runtime you please
//! #[tokio::main]
//! async fn main() {
//!     tokio::spawn(async move {
//!
//!         let config = ReplicateConfig::new().unwrap();
//!         let prediction_client = PredictionClient::from(config);
//!
//!         // Create the prediction
//!         let mut prediction = prediction_client
//!             .create(
//!                 "replicate",
//!                 "hello-world",
//!                 json!({"text": "kyle"}),
//!                 false
//!             )
//!             .await
//!             .unwrap();
//!
//!         // Refresh the data
//!         prediction.reload().await;
//!     });
//! }
//! ```

#![warn(missing_docs)]

pub mod config;
pub mod errors;
pub mod models;
pub mod predictions;

use crate::errors::{ReplicateError, ReplicateResult};
use std::env::var;
use std::sync::OnceLock;

fn api_key() -> ReplicateResult<&'static str> {
    let api_key = var("REPLICATE_API_KEY").map_err(|_| {
        ReplicateError::MissingCredentials(
            "REPLICATE_API_KEY not available in environment variables.".to_string(),
        )
    })?;

    static REPLICATE_API_KEY: OnceLock<String> = OnceLock::new();
    Ok(REPLICATE_API_KEY.get_or_init(|| api_key))
}

fn base_url() -> &'static str {
    static REPLICATE_BASE_URL: OnceLock<&'static str> = OnceLock::new();
    REPLICATE_BASE_URL.get_or_init(|| "https://api.replicate.com/v1")
}