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
//! 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.2.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;
//!
//! let config = ReplicateConfig::new().unwrap();
//! let prediction_client = PredictionClient::from(config);
//!
//! #[derive(Serialize)]
//! struct HelloWorldInput {
//!     text: String
//! }
//!
//! // The library is async agnostic, so you should be able to use any async runtime you please
//! tokio_test::block_on(async move {
//!
//!     // Create the prediction
//!     let prediction_input = Box::new(HelloWorldInput{ text: "kyle".to_string() });
//!     let mut prediction = prediction_client
//!         .create(
//!             "replicate",
//!             "hello-world",
//!             prediction_input
//!         )
//!         .await
//!         .unwrap();
//!
//!     // Refresh the data
//!     prediction.reload().await;
//! })
//! ```

#![warn(missing_docs)]

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

use std::env::var;
use std::sync::OnceLock;

fn api_key() -> anyhow::Result<&'static str> {
    let api_key = var("REPLICATE_API_KEY")?;
    static REPLICATE_API_KEY: OnceLock<String> = OnceLock::new();
    anyhow::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")
}