api_claude 0.7.1

Claude API for accessing Anthropic's large language models (LLMs).
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Enterprise Configuration Module
//!
//! Provides unified configuration builder for all enterprise reliability features:
//! - Retry logic
//! - Circuit breaker
//! - Rate limiting
//! - Failover
//! - Health checks

mod private
{
  use serde::{ Serialize, Deserialize };
  use std::time::Duration;

/// Enterprise configuration combining all reliability features
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct EnterpriseConfig
{
  #[ cfg( feature = "retry-logic" ) ]
  retry : Option< crate::retry_logic::RetryConfig >,
  #[ cfg( feature = "circuit-breaker" ) ]
  circuit_breaker : Option< crate::circuit_breaker::CircuitBreakerConfig >,
  #[ cfg( feature = "rate-limiting" ) ]
  rate_limiting : Option< crate::rate_limiting::RateLimiterConfig >,
  #[ cfg( feature = "failover" ) ]
  failover : Option< crate::failover::FailoverConfig >,
  #[ cfg( feature = "health-checks" ) ]
  health_checks : Option< crate::health_checks::HealthCheckConfig >,
}

impl EnterpriseConfig
{
  /// Check if configuration is valid
  pub fn is_valid( &self ) -> bool
  {
    self.validate().is_ok()
  }

  /// Validate the enterprise configuration
  ///
  /// # Errors
  ///
  /// Returns an error if any of the enabled feature configurations are invalid
  pub fn validate( &self ) -> Result< (), String >
  {
    // Validate retry config
    #[ cfg( feature = "retry-logic" ) ]
    if let Some( ref retry ) = self.retry
    {
      if !retry.is_valid()
      {
        return Err( "Invalid retry configuration".to_string() );
      }
    }

    // Validate circuit breaker
    #[ cfg( feature = "circuit-breaker" ) ]
    if let Some( ref cb ) = self.circuit_breaker
    {
      if !cb.is_valid()
      {
        return Err( "Invalid circuit breaker configuration".to_string() );
      }
    }

    // Validate rate limiting
    #[ cfg( feature = "rate-limiting" ) ]
    if let Some( ref rl ) = self.rate_limiting
    {
      if !rl.is_valid()
      {
        return Err( "Invalid rate limiting configuration".to_string() );
      }
    }

    Ok(())
  }

  /// Check if retry is enabled
  #[ cfg( feature = "retry-logic" ) ]
  pub fn retry_enabled( &self ) -> bool
  {
    self.retry.is_some()
  }

  /// Check if retry is enabled (stub when feature disabled)
  #[ cfg( not( feature = "retry-logic" ) ) ]
  pub fn retry_enabled( &self ) -> bool
  {
    false
  }

  /// Get retry configuration
  #[ cfg( feature = "retry-logic" ) ]
  pub fn retry_config( &self ) -> Option< &crate::retry_logic::RetryConfig >
  {
    self.retry.as_ref()
  }

  /// Check if circuit breaker is enabled
  #[ cfg( feature = "circuit-breaker" ) ]
  pub fn circuit_breaker_enabled( &self ) -> bool
  {
    self.circuit_breaker.is_some()
  }

  /// Check if circuit breaker is enabled (stub for non-feature builds)
  #[ cfg( not( feature = "circuit-breaker" ) ) ]
  pub fn circuit_breaker_enabled( &self ) -> bool
  {
    false
  }

  /// Get circuit breaker configuration
  #[ cfg( feature = "circuit-breaker" ) ]
  pub fn circuit_breaker_config( &self ) -> Option< &crate::circuit_breaker::CircuitBreakerConfig >
  {
    self.circuit_breaker.as_ref()
  }

  /// Check if rate limiting is enabled
  #[ cfg( feature = "rate-limiting" ) ]
  pub fn rate_limiting_enabled( &self ) -> bool
  {
    self.rate_limiting.is_some()
  }

  /// Check if rate limiting is enabled (stub for non-feature builds)
  #[ cfg( not( feature = "rate-limiting" ) ) ]
  pub fn rate_limiting_enabled( &self ) -> bool
  {
    false
  }

  /// Get rate limiting configuration
  #[ cfg( feature = "rate-limiting" ) ]
  pub fn rate_limiting_config( &self ) -> Option< &crate::rate_limiting::RateLimiterConfig >
  {
    self.rate_limiting.as_ref()
  }

  /// Check if failover is enabled
  #[ cfg( feature = "failover" ) ]
  pub fn failover_enabled( &self ) -> bool
  {
    self.failover.is_some()
  }

  /// Check if failover is enabled (stub for non-feature builds)
  #[ cfg( not( feature = "failover" ) ) ]
  pub fn failover_enabled( &self ) -> bool
  {
    false
  }

  /// Check if health checks are enabled
  #[ cfg( feature = "health-checks" ) ]
  pub fn health_checks_enabled( &self ) -> bool
  {
    self.health_checks.is_some()
  }

  /// Check if health checks are enabled (stub for non-feature builds)
  #[ cfg( not( feature = "health-checks" ) ) ]
  pub fn health_checks_enabled( &self ) -> bool
  {
    false
  }
}

/// Builder for enterprise configuration
#[ derive( Debug, Clone ) ]
pub struct EnterpriseConfigBuilder
{
  #[ cfg( feature = "retry-logic" ) ]
  retry : Option< crate::retry_logic::RetryConfig >,
  #[ cfg( feature = "circuit-breaker" ) ]
  circuit_breaker : Option< crate::circuit_breaker::CircuitBreakerConfig >,
  #[ cfg( feature = "rate-limiting" ) ]
  rate_limiting : Option< crate::rate_limiting::RateLimiterConfig >,
  #[ cfg( feature = "failover" ) ]
  failover : Option< crate::failover::FailoverConfig >,
  #[ cfg( feature = "health-checks" ) ]
  health_checks : Option< crate::health_checks::HealthCheckConfig >,
}

impl EnterpriseConfigBuilder
{
  /// Create a new enterprise configuration builder
  pub fn new() -> Self
  {
    Self
    {
      #[ cfg( feature = "retry-logic" ) ]
      retry : None,
      #[ cfg( feature = "circuit-breaker" ) ]
      circuit_breaker : None,
      #[ cfg( feature = "rate-limiting" ) ]
      rate_limiting : None,
      #[ cfg( feature = "failover" ) ]
      failover : None,
      #[ cfg( feature = "health-checks" ) ]
      health_checks : None,
    }
  }

  /// Configure retry logic
  #[ cfg( feature = "retry-logic" ) ]
  #[ must_use ]
  pub fn with_retry( mut self, config : crate::retry_logic::RetryConfig ) -> Self
  {
    self.retry = Some( config );
    self
  }

  /// Configure circuit breaker
  #[ cfg( feature = "circuit-breaker" ) ]
  #[ must_use ]
  pub fn with_circuit_breaker( mut self, config : crate::circuit_breaker::CircuitBreakerConfig ) -> Self
  {
    self.circuit_breaker = Some( config );
    self
  }

  /// Configure rate limiting
  #[ cfg( feature = "rate-limiting" ) ]
  #[ must_use ]
  pub fn with_rate_limiting( mut self, config : crate::rate_limiting::RateLimiterConfig ) -> Self
  {
    self.rate_limiting = Some( config );
    self
  }

  /// Configure failover
  #[ cfg( feature = "failover" ) ]
  #[ must_use ]
  pub fn with_failover( mut self, config : crate::failover::FailoverConfig ) -> Self
  {
    self.failover = Some( config );
    self
  }

  /// Configure health checks
  #[ cfg( feature = "health-checks" ) ]
  #[ must_use ]
  pub fn with_health_checks( mut self, config : crate::health_checks::HealthCheckConfig ) -> Self
  {
    self.health_checks = Some( config );
    self
  }

  /// Build the enterprise configuration
  pub fn build( self ) -> EnterpriseConfig
  {
    EnterpriseConfig
    {
      #[ cfg( feature = "retry-logic" ) ]
      retry : self.retry,
      #[ cfg( feature = "circuit-breaker" ) ]
      circuit_breaker : self.circuit_breaker,
      #[ cfg( feature = "rate-limiting" ) ]
      rate_limiting : self.rate_limiting,
      #[ cfg( feature = "failover" ) ]
      failover : self.failover,
      #[ cfg( feature = "health-checks" ) ]
      health_checks : self.health_checks,
    }
  }

  /// Try to build with validation
  ///
  /// # Errors
  ///
  /// Returns an error if the enterprise configuration is invalid
  pub fn try_build( self ) -> Result< EnterpriseConfig, String >
  {
    let config = self.build();
    config.validate()?;
    Ok( config )
  }

  /// Create a conservative enterprise profile (high safety, low performance impact)
  pub fn conservative() -> EnterpriseConfig
  {
    let mut builder = Self::new();

    #[ cfg( feature = "retry-logic" ) ]
    {
      builder = builder.with_retry( crate::retry_logic::RetryConfig::new()
        .with_max_attempts( 3 )
        .with_initial_delay( Duration::from_millis( 100 ) )
        .with_max_delay( Duration::from_secs( 5 ) )
        .with_backoff_multiplier( 2.0 ) );
    }

    #[ cfg( feature = "circuit-breaker" ) ]
    {
      builder = builder.with_circuit_breaker( crate::circuit_breaker::CircuitBreakerConfig::new()
        .with_failure_threshold( 5 )
        .with_success_threshold( 2 ) );
    }

    builder.build()
  }

  /// Create a balanced enterprise profile (moderate safety and performance)
  pub fn balanced() -> EnterpriseConfig
  {
    let mut builder = Self::new();

    #[ cfg( feature = "retry-logic" ) ]
    {
      builder = builder.with_retry( crate::retry_logic::RetryConfig::new()
        .with_max_attempts( 5 )
        .with_initial_delay( Duration::from_millis( 50 ) )
        .with_max_delay( Duration::from_secs( 10 ) )
        .with_backoff_multiplier( 2.0 ) );
    }

    #[ cfg( feature = "circuit-breaker" ) ]
    {
      builder = builder.with_circuit_breaker( crate::circuit_breaker::CircuitBreakerConfig::new()
        .with_failure_threshold( 3 )
        .with_success_threshold( 2 ) );
    }

    #[ cfg( feature = "rate-limiting" ) ]
    {
      builder = builder.with_rate_limiting( crate::rate_limiting::RateLimiterConfig::new()
        .with_tokens_per_second( 10.0 )
        .with_bucket_capacity( 100 ) );
    }

    builder.build()
  }

  /// Create an aggressive enterprise profile (maximum reliability features)
  pub fn aggressive() -> EnterpriseConfig
  {
    let mut builder = Self::new();

    #[ cfg( feature = "retry-logic" ) ]
    {
      builder = builder.with_retry( crate::retry_logic::RetryConfig::new()
        .with_max_attempts( 10 )
        .with_initial_delay( Duration::from_millis( 25 ) )
        .with_max_delay( Duration::from_secs( 30 ) )
        .with_backoff_multiplier( 2.5 ) );
    }

    #[ cfg( feature = "circuit-breaker" ) ]
    {
      builder = builder.with_circuit_breaker( crate::circuit_breaker::CircuitBreakerConfig::new()
        .with_failure_threshold( 2 )
        .with_success_threshold( 3 ) );
    }

    #[ cfg( feature = "rate-limiting" ) ]
    {
      builder = builder.with_rate_limiting( crate::rate_limiting::RateLimiterConfig::new()
        .with_tokens_per_second( 5.0 )
        .with_bucket_capacity( 50 ) );
    }

    #[ cfg( feature = "failover" ) ]
    {
      builder = builder.with_failover( crate::failover::FailoverConfig::new()
        .with_strategy( crate::failover::FailoverStrategy::Priority ) );
    }

    #[ cfg( feature = "health-checks" ) ]
    {
      builder = builder.with_health_checks( crate::health_checks::HealthCheckConfig::new()
        .with_interval( Duration::from_secs( 30 ) )
        .with_timeout( Duration::from_secs( 5 ) ) );
    }

    builder.build()
  }

  /// Reset the builder to empty state
  #[ must_use ]
  pub fn reset( self ) -> Self
  {
    Self::new()
  }
}

impl Default for EnterpriseConfigBuilder
{
  fn default() -> Self
  {
    Self::new()
  }
}

} // end mod private

crate::mod_interface!
{
  exposed use EnterpriseConfig;
  exposed use EnterpriseConfigBuilder;
}