Skip to main content

claude_api/admin/
organization.rs

1//! `GET /v1/organizations/me`.
2
3use serde::{Deserialize, Serialize};
4
5use crate::client::Client;
6use crate::error::Result;
7
8/// The authenticated organization.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[non_exhaustive]
11pub struct OrganizationInfo {
12    /// Stable organization ID.
13    pub id: String,
14    /// Display name.
15    pub name: String,
16    /// Wire type tag (always `"organization"`).
17    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
18    pub ty: Option<String>,
19}
20
21/// Namespace handle for the organization endpoint.
22pub struct Organization<'a> {
23    client: &'a Client,
24}
25
26impl<'a> Organization<'a> {
27    pub(crate) fn new(client: &'a Client) -> Self {
28        Self { client }
29    }
30
31    /// `GET /v1/organizations/me`.
32    pub async fn me(&self) -> Result<OrganizationInfo> {
33        self.client
34            .execute_with_retry(
35                || {
36                    self.client
37                        .request_builder(reqwest::Method::GET, "/v1/organizations/me")
38                },
39                &[],
40            )
41            .await
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use serde_json::json;
49    use wiremock::matchers::{method, path};
50    use wiremock::{Mock, MockServer, ResponseTemplate};
51
52    fn client_for(mock: &MockServer) -> Client {
53        Client::builder()
54            .api_key("sk-ant-admin-test")
55            .base_url(mock.uri())
56            .build()
57            .unwrap()
58    }
59
60    #[tokio::test]
61    async fn me_returns_typed_organization() {
62        let mock = MockServer::start().await;
63        Mock::given(method("GET"))
64            .and(path("/v1/organizations/me"))
65            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
66                "id": "org_01",
67                "type": "organization",
68                "name": "Acme"
69            })))
70            .mount(&mock)
71            .await;
72        let client = client_for(&mock);
73        let org = client.admin().organization().me().await.unwrap();
74        assert_eq!(org.id, "org_01");
75        assert_eq!(org.name, "Acme");
76    }
77}