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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! Main client implementation for the Replicate API.
use crate::api::{FilesApi, PredictionsApi, predictions::PredictionBuilder};
use crate::error::{Error, Result};
use crate::http::{HttpClient, HttpConfig, TimeoutConfig};
use std::{env, time::Duration};
/// Main client for interacting with the Replicate API.
#[derive(Debug, Clone)]
pub struct Client {
http: HttpClient,
predictions_api: PredictionsApi,
files_api: FilesApi,
}
impl Client {
/// Create a new client with the given API token.
pub fn new(api_token: impl Into<String>) -> Result<Self> {
let http = HttpClient::new(api_token)?;
let predictions_api = PredictionsApi::new(http.clone());
let files_api = FilesApi::new(http.clone());
Ok(Self {
http,
predictions_api,
files_api,
})
}
/// Create a new client using the API token from the environment.
///
/// Looks for the token in the `REPLICATE_API_TOKEN` environment variable.
pub fn from_env() -> Result<Self> {
let api_token = env::var("REPLICATE_API_TOKEN")
.map_err(|_| Error::auth_error("REPLICATE_API_TOKEN environment variable not found"))?;
Self::new(api_token)
}
/// Create a new client with custom base URL.
pub fn with_base_url(
api_token: impl Into<String>,
base_url: impl Into<String>,
) -> Result<Self> {
let http = HttpClient::with_base_url(api_token, base_url)?;
let predictions_api = PredictionsApi::new(http.clone());
let files_api = FilesApi::new(http.clone());
Ok(Self {
http,
predictions_api,
files_api,
})
}
/// Get access to the predictions API.
pub fn predictions(&self) -> &PredictionsApi {
&self.predictions_api
}
/// Get access to the files API.
pub fn files(&self) -> &FilesApi {
&self.files_api
}
/// Create a new prediction with a fluent builder API.
///
/// # Examples
///
/// ```no_run
/// # use replicate_client::Client;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("your-api-token")?;
///
/// let prediction = client
/// .create_prediction("stability-ai/sdxl:version-id")
/// .input("prompt", "A futuristic city skyline")
/// .input("width", 1024)
/// .input("height", 1024)
/// .send()
/// .await?;
///
/// println!("Prediction ID: {}", prediction.id);
/// # Ok(())
/// # }
/// ```
pub fn create_prediction(&self, version: impl Into<String>) -> PredictionBuilder {
PredictionBuilder::new(self.predictions_api.clone(), version)
}
/// Run a model and wait for completion (convenience method).
///
/// This is equivalent to creating a prediction and waiting for it to complete.
///
/// # Examples
///
/// ```no_run
/// # use replicate_client::Client;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("your-api-token")?;
///
/// let result = client
/// .run("stability-ai/sdxl:version-id")
/// .input("prompt", "A futuristic city skyline")
/// .send_and_wait()
/// .await?;
///
/// println!("Result: {:?}", result.output);
/// # Ok(())
/// # }
/// ```
pub fn run(&self, version: impl Into<String>) -> PredictionBuilder {
self.create_prediction(version)
}
/// Get the underlying HTTP client.
pub fn http_client(&self) -> &HttpClient {
&self.http
}
/// Get mutable access to the underlying HTTP client.
///
/// This allows configuring retry settings after client creation.
pub fn http_client_mut(&mut self) -> &mut HttpClient {
&mut self.http
}
/// Configure retry settings for this client.
///
/// This is a convenience method that delegates to the HTTP client.
///
/// # Examples
///
/// ```no_run
/// # use replicate_client::Client;
/// # use std::time::Duration;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = Client::new("your-api-token")?;
///
/// // Configure more aggressive retry settings
/// client.configure_retries(
/// 5, // max_retries
/// Duration::from_millis(100), // min_delay
/// Duration::from_secs(60), // max_delay
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn configure_retries(
&mut self,
max_retries: u32,
min_delay: Duration,
max_delay: Duration,
) -> Result<()> {
self.http
.configure_retries(max_retries, min_delay, max_delay)
}
/// Configure timeout settings for this client.
///
/// This is a convenience method that delegates to the HTTP client.
///
/// # Examples
///
/// ```no_run
/// # use replicate_client::Client;
/// # use std::time::Duration;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = Client::new("your-api-token")?;
///
/// // Configure custom timeouts
/// client.configure_timeouts(
/// Some(Duration::from_secs(10)), // connect_timeout
/// Some(Duration::from_secs(120)), // request_timeout
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn configure_timeouts(
&mut self,
connect_timeout: Option<Duration>,
request_timeout: Option<Duration>,
) -> Result<()> {
self.http
.configure_timeouts(connect_timeout, request_timeout)
}
/// Create a new client with custom HTTP configuration.
pub fn with_http_config(api_token: impl Into<String>, http_config: HttpConfig) -> Result<Self> {
let http = HttpClient::with_http_config(api_token, http_config)?;
let predictions_api = PredictionsApi::new(http.clone());
let files_api = FilesApi::new(http.clone());
Ok(Self {
http,
predictions_api,
files_api,
})
}
/// Get the current timeout configuration.
pub fn timeout_config(&self) -> &TimeoutConfig {
self.http.timeout_config()
}
/// Get the current HTTP configuration.
pub fn http_config(&self) -> &HttpConfig {
self.http.http_config()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_creation() {
let client = Client::new("test-token");
assert!(client.is_ok());
}
#[test]
fn test_client_empty_token() {
let client = Client::new("");
assert!(client.is_err());
assert!(matches!(client.unwrap_err(), Error::Auth(_)));
}
#[test]
fn test_client_from_env_missing() {
// Save current value and remove it for test
let original = env::var("REPLICATE_API_TOKEN").ok();
unsafe {
env::remove_var("REPLICATE_API_TOKEN");
}
let client = Client::from_env();
assert!(client.is_err());
assert!(matches!(client.unwrap_err(), Error::Auth(_)));
// Restore original value if it existed
if let Some(value) = original {
unsafe {
env::set_var("REPLICATE_API_TOKEN", value);
}
}
}
#[test]
fn test_client_from_env_present() {
// Save current value
let original = env::var("REPLICATE_API_TOKEN").ok();
unsafe {
env::set_var("REPLICATE_API_TOKEN", "test-token");
}
let client = Client::from_env();
assert!(client.is_ok());
// Restore original value or remove if it didn't exist
unsafe {
match original {
Some(value) => env::set_var("REPLICATE_API_TOKEN", value),
None => env::remove_var("REPLICATE_API_TOKEN"),
}
}
}
}