Skip to main content

azure_lite_rs/ops/
subscriptions.rs

1//! Operation contracts for the Azure Subscriptions API (v1).
2//!
3//! Auto-generated from the Azure ARM REST Specification.
4//! **Do not edit manually** — modify the manifest and re-run codegen.
5//!
6//! These are the raw HTTP operations with correct URLs, methods,
7//! and parameter ordering. The hand-written `api/subscriptions.rs` wraps
8//! these with ergonomic builders, operation polling, etc.
9
10use crate::types::subscriptions::*;
11use crate::{AzureHttpClient, Result};
12
13/// Raw HTTP operations for the Azure Subscriptions API.
14///
15/// These methods encode the correct URL paths, HTTP methods, and
16/// parameter ordering from the Azure ARM REST Specification.
17/// They are `pub(crate)` — use the ergonomic wrappers in
18/// [`super::subscriptions::SubscriptionsClient`] instead.
19pub struct SubscriptionsOps<'a> {
20    pub(crate) client: &'a AzureHttpClient,
21}
22
23impl<'a> SubscriptionsOps<'a> {
24    pub(crate) fn new(client: &'a AzureHttpClient) -> Self {
25        Self { client }
26    }
27
28    fn base_url(&self) -> &str {
29        #[cfg(any(test, feature = "test-support"))]
30        {
31            if let Some(ref base) = self.client.base_url {
32                return base.trim_end_matches('/');
33            }
34        }
35        "https://management.azure.com"
36    }
37
38    /// List all subscriptions accessible to the authenticated principal.
39    ///
40    /// **Azure API**: `GET /subscriptions`
41    ///
42    /// # Response
43    /// [`SubscriptionListResponse`]
44    #[allow(dead_code)]
45    pub(crate) async fn list_subscriptions(&self) -> Result<SubscriptionListResponse> {
46        let url = format!("{}/subscriptions", self.base_url(),);
47        let sep = if url.contains('?') { "&" } else { "?" };
48        let url = format!("{}{}api-version=2022-12-01", url, sep);
49        let response = self.client.get(&url).await?;
50        let response = response.error_for_status().await?;
51        let response_bytes =
52            response
53                .bytes()
54                .await
55                .map_err(|e| crate::AzureError::InvalidResponse {
56                    message: format!("Failed to read list_subscriptions response: {e}"),
57                    body: None,
58                })?;
59        serde_json::from_slice(&response_bytes).map_err(|e| crate::AzureError::InvalidResponse {
60            message: format!("Failed to parse list_subscriptions response: {e}"),
61            body: Some(String::from_utf8_lossy(&response_bytes).to_string()),
62        })
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[tokio::test]
71    async fn test_list_subscriptions() {
72        let mut mock = crate::MockClient::new();
73
74        mock.expect_get("/subscriptions")
75            .returning_json(serde_json::to_value(SubscriptionListResponse::fixture()).unwrap());
76
77        let client = crate::AzureHttpClient::from_mock(mock);
78        let ops = SubscriptionsOps::new(&client);
79
80        let result = ops.list_subscriptions().await;
81        assert!(result.is_ok());
82    }
83}