veracode-platform 0.5.5

A comprehensive Rust client library for the Veracode platform (Applications, Identity, Pipeline Scan, Sandbox)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# Veracode Platform Client Library

[![Rust](https://img.shields.io/badge/rust-1.70%2B-brightgreen.svg)](https://www.rust-lang.org)
[![Crates.io](https://img.shields.io/crates/v/veracode-platform.svg)](https://crates.io/crates/veracode-platform)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](#license)
[![Documentation](https://docs.rs/veracode-platform/badge.svg)](https://docs.rs/veracode-platform)

A comprehensive Rust client library for the Veracode security platform, providing type-safe and ergonomic access to Applications, Identity, Pipeline Scan, Sandbox, Policy, and Build APIs.

## โœจ Features

- ๐Ÿ” **HMAC Authentication** - Built-in Veracode API credential support with automatic signature generation
- ๐Ÿ›ก๏ธ **Secure Credential Handling** - All API credentials are securely wrapped to prevent accidental exposure in logs
- ๐ŸŒ **Multi-Regional Support** - Automatic endpoint routing for Commercial, European, and Federal regions
- ๐Ÿ”„ **Smart API Routing** - Automatically uses REST or XML APIs based on operation requirements
- ๐Ÿ“ฑ **Applications API** - Complete application lifecycle management via REST API
- ๐Ÿ‘ฅ **Identity API** - User and team management via REST API
- ๐Ÿ” **Pipeline Scan API** - CI/CD security scanning via REST API
- ๐Ÿงช **Sandbox API** - Development sandbox management via REST API
- ๐Ÿ”จ **Build API** - Build management and SAST operations via XML API
- ๐Ÿ“Š **Scan API** - File upload and scan operations via XML API
- ๐Ÿ“‹ **Policy API** - Security policy management and compliance evaluation with intelligent REST/XML API fallback
- ๐Ÿš€ **Async/Await** - Built on tokio for high-performance concurrent operations
- โšก **Type-Safe** - Full Rust type safety with comprehensive serde serialization
- ๐Ÿ“Š **Rich Data Types** - Comprehensive data structures for all API responses
- ๐Ÿ”ง **Workflow Helpers** - High-level operations combining multiple API calls
- ๐Ÿ”„ **Intelligent Retry Logic** - Automatic retry with exponential backoff for transient failures and smart rate limit handling
- ๐Ÿ”€ **API Fallback System** - Automatic fallback from REST API to XML API on permission errors for maximum compatibility
- โฑ๏ธ **Configurable Timeouts** - Customizable connection and request timeouts for different use cases
- โšก **Performance Optimized** - Advanced memory allocation optimizations for high-throughput applications
- ๐Ÿ”’ **Debug Safety** - All sensitive credentials show `[REDACTED]` in debug output
- ๐Ÿงช **Comprehensive Testing** - Extensive test coverage including security measures

## ๐Ÿš€ Quick Start

Add to your `Cargo.toml`:

```toml
[dependencies]
veracode-platform = "0.5.3"
tokio = { version = "1.0", features = ["full"] }
```

### Basic Usage

```rust
use veracode_platform::{VeracodeConfig, VeracodeClient, RetryConfig, VeracodeRegion};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Configure client for your region with custom timeouts
    let config = VeracodeConfig::new(
        std::env::var("VERACODE_API_ID")?,
        std::env::var("VERACODE_API_KEY")?,
    )
    .with_region(VeracodeRegion::Commercial)  // Commercial (default), European, or Federal
    .with_timeouts(60, 600);                  // Optional: 60s connect, 10min request timeout
    
    // Create client
    let client = VeracodeClient::new(config)?;
    
    // Use Applications API (REST)
    let apps = client.get_all_applications().await?;
    println!("Found {} applications", apps.len());
    
    // Use Pipeline Scan API (REST)
    let pipeline = client.pipeline_api();
    // ... pipeline operations
    
    // Use Scan API for file uploads (XML)
    let scan_api = client.scan_api();
    // ... scan operations
    
    Ok(())
}
```

## ๐ŸŒ Regional Support

The library automatically handles regional endpoints for both REST and XML APIs:

```rust
use veracode_platform::{VeracodeConfig, VeracodeRegion};

// Commercial region (default)
let config = VeracodeConfig::new("api_id".to_string(), "api_key".to_string())
    .with_region(VeracodeRegion::Commercial);
// REST: api.veracode.com | XML: analysiscenter.veracode.com

// European region
let config = VeracodeConfig::new("api_id".to_string(), "api_key".to_string())
    .with_region(VeracodeRegion::European);
// REST: api.veracode.eu | XML: analysiscenter.veracode.eu

// US Federal region
let config = VeracodeConfig::new("api_id".to_string(), "api_key".to_string())
    .with_region(VeracodeRegion::Federal);
// REST: api.veracode.us | XML: analysiscenter.veracode.us
```

## ๐Ÿ“š API Modules

### Applications API (REST)
Complete application lifecycle management:

```rust
use veracode_platform::app::{BusinessCriticality, CreateApplicationRequest};
// List all applications
let apps = client.get_all_applications().await?;

// Get specific application
let app_query = ApplicationQuery {
    name: Some("MyApp".to_string()),
    ..Default::default()
};
let app = client.get_application(app_query).await?;

// Create new application
let create_request = CreateApplicationRequest {
    name: "New App".to_string(),
    description: Some("My new application".to_string()),
    business_unit_id: Some(12345),
    teams: vec![],
    tags: vec![],
};
let new_app = client.create_application(create_request).await?;

// Create application with team assignment (new in 0.5.1)
// Teams are automatically resolved from names to GUIDs
let app_with_teams = client.create_application_if_not_exists(
    "My Team App",
    BusinessCriticality::High,
    Some("App with team assignment".to_string()),
    Some(vec!["Security Team".to_string(), "Development Team".to_string()])
).await?;
```

### Pipeline Scan API (REST)
CI/CD security scanning:

```rust
use veracode_platform::pipeline::{CreateScanRequest, DevStage};

let pipeline = client.pipeline_api();

// Create a pipeline scan
let scan_request = CreateScanRequest {
    binary_name: "my-app.jar".to_string(),
    binary_size: file_data.len() as u64,
    binary_hash: calculate_sha256(&file_data),
    project_name: "My Project".to_string(),
    project_uri: Some("https://github.com/user/repo".to_string()),
    dev_stage: DevStage::Development,
    app_id: None,
    project_ref: Some("main".to_string()),
    scan_timeout: Some(30),
    plugin_version: None,
    emit_stack_dump: None,
    include_modules: None,
};

let scan_result = pipeline.create_scan(scan_request).await?;
println!("Created scan: {}", scan_result.scan_id);

// Monitor scan status
let scan_info = pipeline.get_scan(&scan_result.scan_id).await?;
println!("Scan status: {:?}", scan_info.status);

// Get findings when complete
if scan_info.status == ScanStatus::Complete {
    let findings = pipeline.get_findings(&scan_result.scan_id).await?;
    println!("Found {} security issues", findings.len());
}
```

### Identity API (REST)
User and team management:

```rust
let identity = client.identity_api();

// List users
let users = identity.get_users(None).await?;

// Create new user
let create_user = CreateUserRequest {
    email: "user@example.com".to_string(),
    first_name: "John".to_string(),
    last_name: "Doe".to_string(),
    user_type: UserType::User,
    roles: vec![],
    teams: vec![],
};
let new_user = identity.create_user(create_user).await?;

// Manage teams
let teams = identity.get_teams().await?;

// Find team by name (new in 0.5.1)
let security_team = identity.get_team_by_name("Security Team").await?;

// Get team GUID for application creation (new in 0.5.1)
let team_guid = identity.get_team_guid_by_name("Security Team").await?;
```

### Sandbox API (REST)
Development sandbox management:

```rust
let sandbox_api = client.sandbox_api();

// List sandboxes for an application
let sandboxes = sandbox_api.get_sandboxes("app-guid").await?;

// Create new sandbox
let create_request = CreateSandboxRequest {
    name: "Development Sandbox".to_string(),
    auto_recreate: Some(false),
    custom_fields: vec![],
};
let sandbox = sandbox_api.create_sandbox("app-guid", create_request).await?;
```

### Scan API (XML)
File upload and scan operations:

```rust
let scan_api = client.scan_api();

// Upload file for scanning
let upload_request = UploadFileRequest {
    app_id: "12345".to_string(),
    file_path: "/path/to/file.jar".to_string(),
    sandbox_id: Some("sandbox-guid".to_string()),
};

let uploaded_file = scan_api.upload_file(upload_request).await?;
println!("Uploaded: {}", uploaded_file.file_name);

// Start pre-scan
let pre_scan = scan_api.begin_pre_scan(BeginPreScanRequest {
    app_id: "12345".to_string(),
    sandbox_id: Some("sandbox-guid".to_string()),
    scan_all_nonfatal_top_level_modules: Some(true),
    auto_scan: Some(true),
});
```

### Policy API (REST + XML Fallback)
Security policy and compliance management with intelligent API fallback:

```rust
let policy_api = client.policy_api();

// Get organizational policies
let policies = policy_api.get_policies().await?;

// Evaluate policy compliance with automatic fallback
let (summary_report_opt, compliance_status, api_source) = policy_api
    .get_policy_status_with_fallback(
        &app_guid,          // Application GUID (for REST API)
        &app_id,           // Application ID (for XML API fallback)
        Some(&build_id),   // Build ID
        sandbox_guid.as_deref(), // Optional sandbox GUID
        sandbox_id.as_deref(),   // Optional sandbox ID (for fallback)
        30,                // Max retries
        10,                // Retry delay seconds
        true,              // Enable break build evaluation
        false,             // Don't force buildinfo API
    )
    .await?;

use veracode_platform::ApiSource;
match api_source {
    ApiSource::SummaryReport => {
        println!("โœ… Used summary report API successfully");
        if let Some(report) = summary_report_opt {
            println!("Full summary report available");
        }
    },
    ApiSource::BuildInfo => {
        println!("๐Ÿ”„ Used getbuildinfo.do API (fallback)");
        println!("Policy status only - no full summary report");
    }
}
println!("Policy compliance status: {}", compliance_status);

// Force buildinfo API usage (for restricted permissions)
let (_, status, _) = policy_api
    .get_policy_status_with_fallback(
        &app_guid, &app_id, Some(&build_id), 
        None, None, 30, 10, true, 
        true  // Force buildinfo API
    )
    .await?;

// Check if build should break
use veracode_platform::PolicyApi;
if PolicyApi::should_break_build(&compliance_status) {
    let exit_code = PolicyApi::get_exit_code_for_status(&compliance_status);
    println!("โŒ Build should break - exit code: {}", exit_code);
}
```

### Build API (XML)
Build management and SAST operations:

```rust
let build_api = client.build_api();

// Get build list for application
let builds = build_api.get_build_list(GetBuildListRequest {
    app_id: "12345".to_string(),
    sandbox_id: None,
}).await?;

// Create new build
let create_build = CreateBuildRequest {
    app_id: "12345".to_string(),
    version: "1.0.0".to_string(),
    sandbox_id: None,
    lifecycle_stage: None,
    launch_date: None,
};
let build = build_api.create_build(create_build).await?;
```

### Workflow Helpers
High-level operations combining multiple API calls:

```rust
let workflow = client.workflow();

// Complete application workflow
let workflow_config = WorkflowConfig {
    app_name: "My Application".to_string(),
    build_version: "1.0.0".to_string(),
    file_paths: vec!["/path/to/app.jar".to_string()],
    scan_timeout: Some(45),
    delete_incomplete_scans: true,
};

let result = workflow.run_complete_workflow(workflow_config).await?;
println!("Workflow completed: {:?}", result);
```

## ๐Ÿ” Authentication

### Environment Variables
Set your Veracode API credentials:

```bash
export VERACODE_API_ID="your-api-id"
export VERACODE_API_KEY="your-api-key"
```

### Direct Configuration
Or pass credentials directly:

```rust
let config = VeracodeConfig::new(
    "your-api-id".to_string(),
    "your-api-key".to_string()
);
```

### Development Mode
Disable certificate validation for development environments:

```bash
export VERASCAN_DISABLE_CERT_VALIDATION="true"
```

Or in code:
```rust
let config = VeracodeConfig::new("api_id".to_string(), "api_key".to_string())
    .with_certificate_validation_disabled(); // Only for development!
```

### HashiCorp Vault Integration
For enhanced security in production environments, you can retrieve Veracode credentials from HashiCorp Vault using JWT/OIDC authentication:

```bash
# Required Vault environment variables
export VAULT_CLI_ADDR="https://vault.example.com"
export VAULT_CLI_JWT="your-jwt-token"
export VAULT_CLI_ROLE="veracode-role"
export VAULT_CLI_SECRET_PATH="secret/veracode/api"

# Optional: Custom auth path (default: auth/jwt)
export VAULT_CLI_AUTH_PATH="auth/jwt"         # Default JWT auth
export VAULT_CLI_AUTH_PATH="auth/oidc"        # Custom OIDC auth
export VAULT_CLI_AUTH_PATH="auth/kubernetes"  # Kubernetes auth
export VAULT_CLI_AUTH_PATH="jwt"              # Direct mount point

# Optional: Vault namespace (Enterprise only)
export VAULT_CLI_NAMESPACE="my-namespace"
```

The Vault secret must contain:
```json
{
  "VERACODE_API_ID": "your-veracode-api-id",
  "VERACODE_API_KEY": "your-veracode-api-key"
}
```

**Note**: Vault integration is available in the `verascan` CLI application. See [VAULT_INTEGRATION.md](../VAULT_INTEGRATION.md) for detailed setup instructions.

## ๐Ÿ”„ Intelligent Retry Configuration

The library includes comprehensive retry functionality with exponential backoff for improved reliability and **smart rate limit handling** optimized for Veracode's 500 requests/minute limit:

### Default Behavior

All HTTP operations automatically retry transient failures with intelligent rate limit handling:

```rust
use veracode_platform::{VeracodeConfig, VeracodeClient};

// Default configuration enables 5 retry attempts with exponential backoff
// and smart rate limit handling for Veracode's 500/minute limit
let config = VeracodeConfig::new(
    std::env::var("VERACODE_API_ID")?,
    std::env::var("VERACODE_API_KEY")?,
);
let client = VeracodeClient::new(config)?;

// All API calls automatically include intelligent retry logic  
let apps = client.get_all_applications().await?; // Optimally handles rate limits and network failures
```

### Custom Retry Configuration

Fine-tune retry behavior for your specific needs:

```rust
use veracode_platform::{VeracodeConfig, RetryConfig};

let custom_retry = RetryConfig::new()
    .with_max_attempts(3)                    // 3 retry attempts (default: 5)
    .with_initial_delay(500)                 // Start with 500ms delay (default: 1000ms)
    .with_max_delay(60000)                   // Cap at 60 seconds (default: 30s)
    .with_backoff_multiplier(1.5)            // 1.5x growth factor (default: 2.0x)
    .with_max_total_delay(300000)            // 5 minutes total (default: 5 minutes)
    // New rate limiting options
    .with_rate_limit_buffer(10)              // 10s buffer for rate limit windows (default: 5s)
    .with_rate_limit_max_attempts(2);        // Max retries for 429 errors (default: 1)

let config = VeracodeConfig::new("api_id", "api_key")
    .with_retry_config(custom_retry);

let client = VeracodeClient::new(config)?;
```

### Disable Retries

For scenarios requiring immediate error responses:

```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_retries_disabled();  // No retries, immediate error on failure

let client = VeracodeClient::new(config)?;
```

### Retry Behavior

The retry system intelligently handles different error types with specialized logic for rate limiting:

**โœ… Automatically Retried:**
- Network timeouts and connection errors
- **HTTP 429 "Too Many Requests" responses** (with intelligent timing)
- HTTP 5xx server errors (500, 502, 503, 504)
- Temporary DNS resolution failures

**โŒ Not Retried (Immediate Failure):**
- HTTP 4xx client errors (except 429)
- Authentication and authorization failures
- Invalid request format errors
- Configuration errors

### Smart Rate Limit Handling

**๐Ÿšฆ HTTP 429 Rate Limiting** is handled with specialized logic optimized for Veracode's 500 requests/minute limit:

```rust
// When a 429 is encountered:
// 1. Parse Retry-After header if present
// 2. Calculate optimal wait time for Veracode's minute windows
// 3. Wait precisely until rate limit resets (no wasted attempts)
// 4. Only retry once by default (configurable)

// Example timing for 429 at different points in the minute:
// Hit 429 at second 15 โ†’ Wait ~50s (until next minute + 5s buffer)
// Hit 429 at second 45 โ†’ Wait ~20s (until next minute + 5s buffer)
// Hit 429 with Retry-After: 30 โ†’ Wait exactly 30s as instructed
```

### Standard Exponential Backoff

For non-rate-limit errors, retry timing follows exponential backoff:

```
Attempt 1: Immediate
Attempt 2: 1 second delay
Attempt 3: 2 second delay  
Attempt 4: 4 second delay
Attempt 5: 8 second delay
```

With jitter and maximum delay caps to prevent overwhelming servers.

### ๐Ÿš€ Rate Limiting Performance Benefits

The intelligent rate limit handling provides significant performance improvements over traditional exponential backoff:

| Scenario | **Traditional Approach** | **Smart Rate Limiting** | **Improvement** |
|----------|---------------------------|-------------------------|-----------------|
| 429 at second 45 | Wait 1s, 2s, 4s, 8s, 16s (~31s total) | Wait ~20s (until next minute) | **35% faster** |
| 429 at second 5 | Wait 1s, 2s, 4s, 8s, 16s (~31s total) | Wait ~60s (until next minute) | **Predictable timing** |
| 429 with Retry-After | Ignore header, use exponential backoff | Wait exactly as instructed | **Server-guided optimal** |
| Multiple 429s | 4-5 failed attempts per rate limit | 1 retry attempt per rate limit | **4x fewer API calls** |

**Key Benefits:**
- โšก **Faster Recovery**: Targeted waits vs repeated failed attempts
- ๐ŸŽฏ **Precise Timing**: Wait exactly until rate limit resets
- ๐Ÿ’พ **Resource Efficient**: No wasted API calls during rate limit windows
- ๐Ÿ“Š **Predictable**: Deterministic delays based on actual rate limit timing
- ๐Ÿ” **Clear Logging**: Distinct messages for rate limits vs other failures

**Example Log Output:**
```
๐Ÿšฆ GET /appsec/v1/applications rate limited on attempt 1, waiting 45s (until next minute window)
โœ… GET /appsec/v1/applications succeeded on attempt 2
```

## โฑ๏ธ HTTP Timeout Configuration

The library provides configurable HTTP timeouts to handle different network conditions and operation requirements:

### Default Timeouts

```rust
use veracode_platform::{VeracodeConfig, VeracodeClient};

// Default timeouts are applied automatically
let config = VeracodeConfig::new(
    std::env::var("VERACODE_API_ID")?,
    std::env::var("VERACODE_API_KEY")?,
);
// Default: 30 seconds connection timeout, 300 seconds (5 minutes) request timeout
let client = VeracodeClient::new(config)?;
```

### Custom Timeout Configuration

Configure timeouts based on your specific needs:

```rust
use veracode_platform::{VeracodeConfig, VeracodeClient};

// Individual timeout configuration
let config = VeracodeConfig::new("api_id", "api_key")
    .with_connect_timeout(60)      // 60 seconds to establish connection
    .with_request_timeout(900);    // 15 minutes total request timeout

// Convenience method for both timeouts
let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(120, 1800);     // 2 minutes connect, 30 minutes request

let client = VeracodeClient::new(config)?;
```

### Timeout Types

| Timeout Type | Default | Description |
|--------------|---------|-------------|
| **Connection Timeout** | 30 seconds | Maximum time to establish TCP connection |
| **Request Timeout** | 300 seconds (5 minutes) | Total time for complete request/response cycle |

### Use Case Examples

**Standard API Operations**:
```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(30, 300);  // Default values - good for most operations
```

**Large File Uploads**:
```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(120, 1800);  // Extended timeouts for large files (30 minutes)
```

**High-Performance/Low-Latency**:
```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(10, 60);  // Aggressive timeouts for fast operations
```

**Development/Testing**:
```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(5, 30);  // Short timeouts to catch issues quickly
```

### Combined with Retry Configuration

Timeouts work seamlessly with retry configuration:

```rust
use veracode_platform::{VeracodeConfig, RetryConfig};

let retry_config = RetryConfig::new()
    .with_max_attempts(3)
    .with_initial_delay(1000);

let config = VeracodeConfig::new("api_id", "api_key")
    .with_timeouts(60, 300)           // Custom timeouts
    .with_retry_config(retry_config); // Custom retry behavior

// Each retry attempt respects the timeout configuration
let client = VeracodeClient::new(config)?;
```

### Method Chaining

All timeout methods support fluent configuration:

```rust
let config = VeracodeConfig::new("api_id", "api_key")
    .with_region(VeracodeRegion::European)    // Set region
    .with_connect_timeout(45)                 // 45s connection timeout
    .with_request_timeout(600)                // 10 minute request timeout
    .with_retries_disabled();                 // Disable retries

let client = VeracodeClient::new(config)?;
```

## โšก Performance Optimizations

The library includes advanced performance optimizations for high-throughput applications:

### Memory Allocation Efficiency

**Copy-on-Write (Cow) Patterns**: Operation names and dynamic strings use `Cow<str>` to defer allocations until necessary, reducing memory pressure by ~60% in retry scenarios.

**String Pre-allocation**: URL building uses `String::with_capacity()` to eliminate heap reallocations, improving performance by ~40% for repeated requests.

**Request Body Optimization**: JSON serialization occurs once per retry sequence rather than per-attempt, significantly improving performance for large payloads.

### Smart Resource Management

**Authentication Constants**: Static error message strings prevent repeated allocations, reducing authentication error handling overhead by 4x.

**Smart Operation Naming**: Short endpoints use formatted strings while long endpoints use static references to avoid unnecessary allocations.

**Memory-Efficient Error Handling**: Streamlined error message creation with minimal string formatting in hot paths.

### Real-World Performance Impact

**Network Retry Scenarios** (most common use case):
- **60% fewer allocations** in retry hot paths
- **40% reduction** in memory pressure during network failures
- **3x less memory usage** for 5 retry attempts with network errors

**High-Throughput Operations**:
- **Pre-allocated URL building** eliminates repeated heap growth
- **Zero-cost abstractions** maintain API ergonomics
- **Efficient request body handling** for large payloads (>1MB)

All optimizations maintain **100% API compatibility** while delivering significant performance improvements under load.

## ๐ŸŽ›๏ธ Feature Flags

Enable only the APIs you need to reduce compile time and binary size:

```toml
[dependencies]
veracode-platform = { version = "0.1.0", features = ["pipeline", "applications"] }
```

Available features:
- `applications` - Applications API support
- `identity` - Identity API support
- `pipeline` - Pipeline Scan API support
- `sandbox` - Sandbox API support
- `policy` - Policy API support
- `default` - All APIs enabled

## ๐Ÿงช Examples

The library includes comprehensive examples for each API:

```bash
# Set up credentials first
export VERACODE_API_ID="your-api-id"
export VERACODE_API_KEY="your-api-key"

# Applications API example
cargo run --example application_lifecycle

# Identity API example
cargo run --example identity_lifecycle

# Pipeline Scan API example
cargo run --example pipeline_scan_lifecycle

# Sandbox API example
cargo run --example sandbox_lifecycle

# Policy API example
cargo run --example policy_lifecycle

# Basic usage example
cargo run --example basic_usage

# Build lifecycle example
cargo run --example build_lifecycle_example

# Large file upload example
cargo run --example large_file_upload_example

# XML API workflow validation
cargo run --example xml_api_workflow_validation
```

## ๐Ÿ—๏ธ Architecture

### API Type Routing
The library automatically routes operations to the correct API type:

- **REST API (`api.veracode.*`)**: Applications, Identity, Pipeline, Policy, Sandbox management
- **XML API (`analysiscenter.veracode.*`)**: Build management, Scan operations, Legacy workflows

### Smart Client Management
The client automatically creates specialized instances for different API types:

```rust
let client = VeracodeClient::new(config)?;

// REST API modules use the main client
let apps = client.get_all_applications().await?;  // Uses REST
let pipeline = client.pipeline_api();             // Uses REST
let identity = client.identity_api();             // Uses REST

// XML API modules use a specialized XML client internally
let scan_api = client.scan_api();                 // Uses XML
let build_api = client.build_api();               // Uses XML
```

### Regional Endpoint Management
All regional variants are supported with automatic endpoint resolution:

| Region | REST Endpoint | XML Endpoint |
|--------|---------------|--------------|
| Commercial | `api.veracode.com` | `analysiscenter.veracode.com` |
| European | `api.veracode.eu` | `analysiscenter.veracode.eu` |
| Federal | `api.veracode.us` | `analysiscenter.veracode.us` |

## ๐Ÿ” Security Features

### Secure Credential Handling

All API credentials are automatically secured to prevent accidental exposure:

```rust
use veracode_platform::{VeracodeConfig, VeracodeClient};

// Credentials are automatically wrapped in secure containers
let config = VeracodeConfig::new(
    std::env::var("VERACODE_API_ID")?,
    std::env::var("VERACODE_API_KEY")?,
);

// Debug output shows [REDACTED] instead of actual credentials
println!("{:?}", config);
// Output: VeracodeConfig { api_id: [REDACTED], api_key: [REDACTED], ... }
```

### Debug Safety

All sensitive information is automatically redacted in debug output:

- **API Credentials**: `VERACODE_API_ID` and `VERACODE_API_KEY` show as `[REDACTED]`
- **Configuration Structures**: `VeracodeConfig` safely displays structure without exposing credentials
- **Identity API**: `ApiCredential` structures redact sensitive `api_key` fields
- **Comprehensive Coverage**: All credential-containing structures are protected

### Backward Compatibility

All improvements are transparent to existing code:

- **All existing examples continue to work unchanged**
- **No breaking changes to public APIs**
- **Rate limiting improvements are automatically applied** to all requests
- New `VeracodeError::RateLimited` variant added (non-breaking addition)
- New rate limit configuration options available but not required
- Secure wrappers are internal implementation details
- Access to credentials through standard methods (`as_str()`, `into_string()`)

## ๐Ÿ”ง Error Handling

The library provides comprehensive error types for robust error handling:

```rust
use veracode_platform::{VeracodeError, pipeline::PipelineError, sandbox::SandboxError};

// Pipeline API error handling
match pipeline.get_findings(&scan_id).await {
    Ok(findings) => println!("Found {} issues", findings.len()),
    Err(PipelineError::FindingsNotReady) => {
        println!("Scan still processing, try again later");
    },
    Err(PipelineError::ApiError(VeracodeError::Unauthorized)) => {
        println!("Check your API credentials");
    },
    Err(PipelineError::ApiError(VeracodeError::NotFound(msg))) => {
        println!("Scan not found: {}", msg);
    },
    Err(e) => println!("Error: {}", e),
}

// Sandbox API error handling
match sandbox_api.get_sandboxes("app-guid").await {
    Ok(sandboxes) => println!("Found {} sandboxes", sandboxes.len()),
    Err(SandboxError::InvalidApplicationGuid(guid)) => {
        println!("Invalid application GUID: {}", guid);
    },
    Err(SandboxError::ApiError(VeracodeError::Authentication(msg))) => {
        println!("Authentication failed: {}", msg);
    },
    Err(e) => println!("Error: {}", e),
}
```

### Common Error Types

| Error Type | Description |
|------------|-------------|
| `VeracodeError::Authentication` | Invalid API credentials or signature issues |
| `VeracodeError::NotFound` | Requested resource doesn't exist |
| `VeracodeError::InvalidResponse` | API returned unexpected response format |
| `VeracodeError::Http` | Network or HTTP-level errors |
| `VeracodeError::Serialization` | JSON parsing or serialization errors |
| `VeracodeError::RateLimited` | HTTP 429 rate limit exceeded - includes server's suggested retry timing |
| `VeracodeError::RetryExhausted` | All retry attempts failed - includes detailed timing and error information |

### Retry Error Handling

The retry system provides detailed error information when all attempts are exhausted:

```rust
use veracode_platform::{VeracodeError};

match client.get_all_applications().await {
    Ok(apps) => println!("Found {} applications", apps.len()),
    Err(VeracodeError::RetryExhausted(msg)) => {
        // Detailed error with attempt count and timing
        println!("Request failed after all retries: {}", msg);
        // Example: "GET /appsec/v1/applications failed after 5 attempts over 15234ms: Connection timeout"
    },
    Err(VeracodeError::RateLimited { retry_after_seconds, message }) => {
        // Rate limit errors with timing information
        match retry_after_seconds {
            Some(seconds) => println!("Rate limited: {} (retry after {}s)", message, seconds),
            None => println!("Rate limited: {} (window-based)", message),
        }
    },
    Err(VeracodeError::Authentication(msg)) => {
        // Authentication errors are not retried
        println!("Authentication failed immediately: {}", msg);
    },
    Err(e) => println!("Other error: {}", e),
}
```

## ๐Ÿ“Š Data Types

### Core Types
```rust
// Application management
pub struct Application {
    pub guid: String,
    pub name: String,
    pub description: Option<String>,
    // ... more fields
}

// Pipeline scanning
pub struct Finding {
    pub issue_id: u32,
    pub issue_type: String,
    pub issue_type_id: String,
    pub cwe_id: String,
    pub severity: Severity,
    // ... more fields
}

// Identity management
pub struct User {
    pub user_id: String,
    pub email: String,
    pub first_name: String,
    pub last_name: String,
    pub user_type: UserType,
    // ... more fields
}
```

### Enums and Status Types
```rust
// Scan status tracking
#[derive(Debug, Clone, PartialEq)]
pub enum ScanStatus {
    Pending,
    Running,
    Complete,
    Failed,
    Cancelled,
}

// Security severity levels
#[derive(Debug, Clone, PartialEq)]
pub enum Severity {
    VeryHigh,
    High,
    Medium,
    Low,
    VeryLow,
    Informational,
}

// Development stages
#[derive(Debug, Clone, PartialEq)]
pub enum DevStage {
    Development,
    Testing,
    Release,
}
```

## ๐Ÿ”ฌ Testing

Run the test suite:

```bash
# Run all tests
cargo test

# Run tests with output
cargo test -- --nocapture

# Run specific test module
cargo test --test applications_api
```

Note: Integration tests require valid Veracode API credentials and may create/modify resources in your Veracode account.

## ๐Ÿ“– Documentation

Generate and view the documentation:

```bash
# Build and open documentation
cargo doc --open

# Build documentation for all features
cargo doc --all-features --open
```

## ๐Ÿท๏ธ Versioning

This library follows [Semantic Versioning](https://semver.org/):

- **Major version** changes indicate breaking API changes
- **Minor version** changes add functionality in a backward-compatible manner
- **Patch version** changes include backward-compatible bug fixes

## ๐Ÿค Contributing

Contributions are welcome! Please read our contributing guidelines:

1. **Fork** the repository
2. **Create** a feature branch (`git checkout -b feature/amazing-feature`)
3. **Commit** your changes (`git commit -m 'Add amazing feature'`)
4. **Push** to the branch (`git push origin feature/amazing-feature`)
5. **Open** a Pull Request

### Development Setup

```bash
# Clone the repository
git clone <repository-url>
cd veracode-workspace/veracode-api

# Run tests
cargo test

# Check formatting
cargo fmt -- --check

# Run lints
cargo clippy -- -D warnings

# Build documentation
cargo doc --all-features
```

## ๐Ÿ“œ License

This project is licensed under the Apache License, Version 2.0 - see the [LICENSE](LICENSE) file for details.

```
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```

## ๐Ÿ†˜ Support

- ๐Ÿ“š **Documentation**: [docs.rs/veracode-platform]https://docs.rs/veracode-platform
- ๐Ÿ› **Issues**: [GitHub Issues]https://github.com/tastyfrankfurt/veracode-platform/issues
- ๐Ÿ’ฌ **Discussions**: [GitHub Discussions]https://github.com/tastyfrankfurt/veracode-platform/discussions
- ๐Ÿ“ **Changelog**: [CHANGELOG.md]CHANGELOG.md

---

*Built with โค๏ธ in Rust for the security community*