replicate_rs/
lib.rs

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