azure-lite-rs 0.1.1

Lightweight HTTP client for Azure APIs
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
//! Core HTTP client for Azure API access.

use crate::auth::AzureCredential;
use crate::error::AzureError;
use cloud_lite_core::rate_limit::{RateLimitConfig, RateLimiter};
use cloud_lite_core::retry::RetryConfig;

/// HTTP client for Azure API operations.
///
/// Provides automatic Bearer token injection, retry, and rate limiting.
pub struct AzureHttpClient {
    http: reqwest::Client,
    credential: AzureCredential,
    subscription_id: String,
    retry_config: RetryConfig,
    rate_limiter: RateLimiter,
    /// Override base URL for testing (e.g. mock server).
    #[cfg(any(test, feature = "test-support"))]
    pub(crate) base_url: Option<String>,
    /// Mock client for testing.
    #[cfg(any(test, feature = "test-support"))]
    pub(crate) mock: Option<std::sync::Arc<crate::mock_client::MockClient>>,
}

/// Response wrapper that abstracts over real and mock responses.
pub struct AzureResponse {
    data: ResponseData,
}

enum ResponseData {
    Real(reqwest::Response),
    #[cfg(any(test, feature = "test-support"))]
    Mock(Vec<u8>),
}

impl AzureResponse {
    /// Get the HTTP status code of the response.
    pub fn status(&self) -> u16 {
        match &self.data {
            ResponseData::Real(response) => response.status().as_u16(),
            #[cfg(any(test, feature = "test-support"))]
            ResponseData::Mock(_) => 200,
        }
    }

    /// Check for HTTP error status and parse the error body.
    pub async fn error_for_status(self) -> Result<Self, AzureError> {
        let status = self.status();
        if status < 400 {
            return Ok(self);
        }

        let body_bytes = self
            .bytes()
            .await
            .unwrap_or_else(|_| bytes::Bytes::from_static(b""));
        let body_text = std::str::from_utf8(&body_bytes).unwrap_or("");

        Err(crate::error::parse_json_error(status, body_text))
    }

    /// Get the `Location` response header value, if present.
    pub fn location(&self) -> Option<String> {
        match &self.data {
            ResponseData::Real(response) => response
                .headers()
                .get("location")
                .and_then(|v| v.to_str().ok())
                .map(|s| s.to_string()),
            #[cfg(any(test, feature = "test-support"))]
            ResponseData::Mock(_) => None,
        }
    }

    /// Read the response body as bytes.
    pub async fn bytes(self) -> Result<bytes::Bytes, AzureError> {
        match self.data {
            ResponseData::Real(response) => response
                .bytes()
                .await
                .map_err(|e| AzureError::Network(e.to_string())),
            #[cfg(any(test, feature = "test-support"))]
            ResponseData::Mock(data) => Ok(bytes::Bytes::from(data)),
        }
    }
}

/// Builder for [`AzureHttpClient`].
pub struct AzureHttpClientBuilder {
    pub(crate) subscription_id: Option<String>,
    pub(crate) retry_config: RetryConfig,
    pub(crate) rate_limit: RateLimitConfig,
    credential: Option<AzureCredential>,
}

impl Default for AzureHttpClientBuilder {
    fn default() -> Self {
        Self {
            subscription_id: None,
            retry_config: RetryConfig::default(),
            rate_limit: RateLimitConfig::new(20),
            credential: None,
        }
    }
}

impl AzureHttpClientBuilder {
    /// Set the Azure subscription ID.
    pub fn subscription_id(mut self, id: impl Into<String>) -> Self {
        self.subscription_id = Some(id.into());
        self
    }

    /// Set an explicit credential (overrides default chain).
    pub fn credential(mut self, cred: AzureCredential) -> Self {
        self.credential = Some(cred);
        self
    }

    /// Set retry configuration.
    pub fn retry_config(mut self, config: RetryConfig) -> Self {
        self.retry_config = config;
        self
    }

    /// Set rate limiting configuration.
    pub fn rate_limit(mut self, config: RateLimitConfig) -> Self {
        self.rate_limit = config;
        self
    }

    /// Build the client (sync — requires credential to be supplied explicitly).
    ///
    /// For default credential chain resolution, use [`AzureHttpClient::from_env()`] which is async.
    pub fn build(self) -> Result<AzureHttpClient, AzureError> {
        let subscription_id = self
            .subscription_id
            .or_else(|| std::env::var("AZURE_SUBSCRIPTION_ID").ok())
            .ok_or_else(|| AzureError::Auth {
                message:
                    "subscription_id required (set AZURE_SUBSCRIPTION_ID or call .subscription_id())"
                        .into(),
            })?;

        let credential = self.credential.ok_or_else(|| AzureError::Auth {
            message:
                "credential required — use AzureHttpClient::from_env().await for the default chain"
                    .into(),
        })?;

        let http = reqwest::Client::builder()
            .build()
            .map_err(|e| AzureError::Network(e.to_string()))?;

        Ok(AzureHttpClient {
            http,
            credential,
            subscription_id,
            retry_config: self.retry_config,
            rate_limiter: RateLimiter::new(self.rate_limit),
            #[cfg(any(test, feature = "test-support"))]
            base_url: None,
            #[cfg(any(test, feature = "test-support"))]
            mock: None,
        })
    }
}

impl AzureHttpClient {
    /// Create a new builder.
    pub fn builder() -> AzureHttpClientBuilder {
        AzureHttpClientBuilder::default()
    }

    /// Create a client using the default credential chain.
    ///
    /// Resolves credentials in order:
    /// 1. Service principal env vars
    /// 2. Managed identity IMDS
    /// 3. Azure CLI
    pub async fn from_env() -> Result<Self, AzureError> {
        let credential = crate::auth::default_credential().await?;
        let subscription_id =
            std::env::var("AZURE_SUBSCRIPTION_ID").map_err(|_| AzureError::Auth {
                message: "AZURE_SUBSCRIPTION_ID environment variable not set".into(),
            })?;

        let http = reqwest::Client::builder()
            .build()
            .map_err(|e| AzureError::Network(e.to_string()))?;

        Ok(Self {
            http,
            credential,
            subscription_id,
            retry_config: RetryConfig::default(),
            rate_limiter: RateLimiter::new(RateLimitConfig::new(20)),
            #[cfg(any(test, feature = "test-support"))]
            base_url: None,
            #[cfg(any(test, feature = "test-support"))]
            mock: None,
        })
    }

    /// Create a client from a mock for testing.
    #[cfg(any(test, feature = "test-support"))]
    pub fn from_mock(mock: crate::mock_client::MockClient) -> Self {
        use crate::auth::cli::AzureCliCredential;
        Self {
            http: reqwest::Client::new(),
            credential: AzureCredential::AzureCli(AzureCliCredential::new()),
            subscription_id: "test-subscription-id".into(),
            retry_config: RetryConfig::default(),
            rate_limiter: RateLimiter::new(RateLimitConfig::disabled()),
            base_url: None,
            mock: Some(std::sync::Arc::new(mock)),
        }
    }

    /// Get the configured subscription ID.
    pub fn subscription_id(&self) -> &str {
        &self.subscription_id
    }

    /// Acquire a token from the credential chain.
    pub async fn token(&self) -> Result<String, AzureError> {
        Ok(self.credential.get_token().await?.token)
    }

    // === Generated API Accessors (do not edit) ===

    /// Access the Azure Container Registry API
    pub fn acr(&self) -> crate::api::AcrClient<'_> {
        crate::api::AcrClient::new(self)
    }

    /// Access the Azure Kubernetes Service API
    pub fn aks(&self) -> crate::api::AksClient<'_> {
        crate::api::AksClient::new(self)
    }

    /// Access the Azure Compute API
    pub fn compute(&self) -> crate::api::ComputeClient<'_> {
        crate::api::ComputeClient::new(self)
    }

    /// Access the Azure CosmosDB API
    pub fn cosmosdb(&self) -> crate::api::CosmosDbClient<'_> {
        crate::api::CosmosDbClient::new(self)
    }

    /// Access the Azure Cost Management API
    pub fn cost(&self) -> crate::api::CostClient<'_> {
        crate::api::CostClient::new(self)
    }

    /// Access the Azure DNS API
    pub fn dns(&self) -> crate::api::DnsClient<'_> {
        crate::api::DnsClient::new(self)
    }

    /// Access the Azure Functions API
    pub fn functions(&self) -> crate::api::FunctionsClient<'_> {
        crate::api::FunctionsClient::new(self)
    }

    /// Access the Microsoft Graph API
    pub fn graph(&self) -> crate::api::GraphClient<'_> {
        crate::api::GraphClient::new(self)
    }

    /// Access the Azure Managed Identities API
    pub fn identity(&self) -> crate::api::IdentityClient<'_> {
        crate::api::IdentityClient::new(self)
    }

    /// Access the Azure Key Vault API
    pub fn keyvault(&self) -> crate::api::KeyVaultClient<'_> {
        crate::api::KeyVaultClient::new(self)
    }

    /// Access the Azure Log Analytics API
    pub fn log_analytics(&self) -> crate::api::LogAnalyticsClient<'_> {
        crate::api::LogAnalyticsClient::new(self)
    }

    /// Access the Azure Monitor API
    pub fn monitor(&self) -> crate::api::MonitorClient<'_> {
        crate::api::MonitorClient::new(self)
    }

    /// Access the Azure Networking API
    pub fn networking(&self) -> crate::api::NetworkingClient<'_> {
        crate::api::NetworkingClient::new(self)
    }

    /// Access the Azure RBAC API
    pub fn rbac(&self) -> crate::api::RbacClient<'_> {
        crate::api::RbacClient::new(self)
    }

    /// Access the Azure Redis Cache API
    pub fn redis(&self) -> crate::api::RedisClient<'_> {
        crate::api::RedisClient::new(self)
    }

    /// Access the Azure Resource Graph API
    pub fn resource_graph(&self) -> crate::api::ResourceGraphClient<'_> {
        crate::api::ResourceGraphClient::new(self)
    }

    /// Access the Azure Defender for Cloud API
    pub fn security(&self) -> crate::api::SecurityClient<'_> {
        crate::api::SecurityClient::new(self)
    }

    /// Access the Azure SQL API
    pub fn sql(&self) -> crate::api::SqlClient<'_> {
        crate::api::SqlClient::new(self)
    }

    /// Access the Azure Storage API
    pub fn storage(&self) -> crate::api::StorageClient<'_> {
        crate::api::StorageClient::new(self)
    }

    /// Access the Azure Subscriptions API
    pub fn subscriptions(&self) -> crate::api::SubscriptionsClient<'_> {
        crate::api::SubscriptionsClient::new(self)
    }
    // === End Generated API Accessors ===

    // =========================================================================
    // Microsoft Graph API — uses a separate OAuth2 scope
    // =========================================================================

    const GRAPH_SCOPE: &str = "https://graph.microsoft.com/.default";

    /// Make a GET request to the Microsoft Graph API.
    ///
    /// Acquires a Graph-scoped token (`https://graph.microsoft.com/.default`)
    /// rather than the ARM scope used by the standard `get()` method.
    pub(crate) async fn graph_get(&self, url: &str) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("GET", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self
            .credential
            .get_token_for_scope(Self::GRAPH_SCOPE)
            .await?
            .token;
        let response = self.bearer_request("GET", url, &token, b"", None).await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a POST request to the Microsoft Graph API with a JSON body.
    ///
    /// Acquires a Graph-scoped token (`https://graph.microsoft.com/.default`)
    /// rather than the ARM scope used by the standard `post()` method.
    pub(crate) async fn graph_post(
        &self,
        url: &str,
        body: &[u8],
    ) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("POST", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self
            .credential
            .get_token_for_scope(Self::GRAPH_SCOPE)
            .await?
            .token;
        let response = self
            .bearer_request("POST", url, &token, body, Some("application/json"))
            .await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a GET request with automatic Bearer token injection and retry.
    pub async fn get(&self, url: &str) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("GET", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self.credential.get_token().await?.token;
        let response = self.bearer_request("GET", url, &token, b"", None).await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a PUT request with a JSON body.
    pub async fn put(&self, url: &str, body: &[u8]) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("PUT", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self.credential.get_token().await?.token;
        let response = self
            .bearer_request("PUT", url, &token, body, Some("application/json"))
            .await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a POST request with a JSON body.
    pub async fn post(&self, url: &str, body: &[u8]) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("POST", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self.credential.get_token().await?.token;
        let response = self
            .bearer_request("POST", url, &token, body, Some("application/json"))
            .await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a DELETE request.
    pub async fn delete(&self, url: &str) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("DELETE", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self.credential.get_token().await?.token;
        let response = self
            .bearer_request("DELETE", url, &token, b"", None)
            .await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Make a PATCH request with a JSON body.
    pub async fn patch(&self, url: &str, body: &[u8]) -> Result<AzureResponse, AzureError> {
        #[cfg(any(test, feature = "test-support"))]
        if let Some(ref mock) = self.mock {
            let result = mock.execute("PATCH", url, None).await?;
            return Ok(AzureResponse {
                data: ResponseData::Mock(result),
            });
        }

        let token = self.credential.get_token().await?.token;
        let response = self
            .bearer_request("PATCH", url, &token, body, Some("application/json"))
            .await?;
        Ok(AzureResponse {
            data: ResponseData::Real(response),
        })
    }

    /// Internal: request with Bearer auth, content-type, and retry.
    async fn bearer_request(
        &self,
        method: &str,
        url: &str,
        token: &str,
        body: &[u8],
        content_type: Option<&str>,
    ) -> Result<reqwest::Response, AzureError> {
        let _permit = self.rate_limiter.acquire(url).await;

        let mut attempt = 0u32;
        let mut backoff = self.retry_config.initial_backoff;
        let body_bytes = if body.is_empty() {
            None
        } else {
            Some(bytes::Bytes::copy_from_slice(body))
        };

        loop {
            let mut request = self
                .http
                .request(method.parse().expect("invalid HTTP method"), url)
                .header("Authorization", format!("Bearer {token}"));

            if let Some(ct) = content_type {
                request = request.header("Content-Type", ct);
            }
            if let Some(ref b) = body_bytes {
                request = request.body(b.clone());
            } else {
                // Azure ARM requires Content-Length: 0 for POST with no body (returns 411 otherwise)
                request = request.header("Content-Length", "0");
            }

            let result = match request.send().await {
                Ok(response) => Self::classify_response(response).await,
                Err(e) => Err(AzureError::from(e)),
            };

            match result {
                Ok(response) => return Ok(response),
                Err(azure_err) => {
                    if azure_err.is_retryable() && attempt < self.retry_config.max_retries {
                        let delay = self
                            .retry_config
                            .compute_backoff(backoff, azure_err.retry_after());
                        tokio::time::sleep(delay).await;
                        backoff = std::time::Duration::from_secs_f64(
                            backoff.as_secs_f64() * self.retry_config.backoff_multiplier,
                        );
                        attempt += 1;
                        continue;
                    }
                    return Err(azure_err);
                }
            }
        }
    }

    /// Classify an HTTP response: return Ok for 2xx, parse and return Err for 4xx/5xx.
    async fn classify_response(
        response: reqwest::Response,
    ) -> Result<reqwest::Response, AzureError> {
        let status = response.status().as_u16();
        if status < 400 {
            return Ok(response);
        }

        // Extract Retry-After header before consuming body
        let retry_after_secs: Option<u64> = response
            .headers()
            .get("retry-after")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse().ok());

        let body_text = response.text().await.unwrap_or_default();

        let mut err = crate::error::parse_json_error(status, &body_text);

        if let Some(secs) = retry_after_secs
            && let AzureError::Throttled { retry_after, .. } = &mut err
        {
            *retry_after = Some(std::time::Duration::from_secs(secs));
        }

        Err(err)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builder_succeeds_with_explicit_subscription_id_and_credential() {
        use crate::auth::cli::AzureCliCredential;
        let cred = AzureCredential::AzureCli(AzureCliCredential::new());
        let client = AzureHttpClient::builder()
            .subscription_id("test-sub-123")
            .credential(cred)
            .build();
        assert!(client.is_ok());
        assert_eq!(client.unwrap().subscription_id(), "test-sub-123");
    }

    #[test]
    fn builder_requires_subscription_id_without_env() {
        use crate::auth::cli::AzureCliCredential;
        let cred = AzureCredential::AzureCli(AzureCliCredential::new());
        let result = AzureHttpClientBuilder {
            subscription_id: None,
            retry_config: RetryConfig::default(),
            rate_limit: RateLimitConfig::disabled(),
            credential: Some(cred),
        }
        .build();
        if std::env::var("AZURE_SUBSCRIPTION_ID").is_err() {
            assert!(
                matches!(result, Err(AzureError::Auth { .. })),
                "expected Auth error, got: {:?}",
                result.err()
            );
        }
    }
}