Skip to main content

linear_api/
viewer.rs

1//! The authenticated viewer and its organization — the crate's working
2//! example module (the smallest full query pipeline).
3
4use serde::Deserialize;
5
6use crate::client::LinearClient;
7use crate::error::Result;
8use crate::ids::{OrganizationId, UserId};
9
10/// The user the API key authenticates as.
11#[derive(Debug, Clone, Deserialize)]
12#[serde(rename_all = "camelCase")]
13#[non_exhaustive]
14pub struct Viewer {
15    /// User ID.
16    pub id: UserId,
17    /// Full name.
18    pub name: String,
19    /// Display (handle) name.
20    pub display_name: String,
21    /// Email address.
22    pub email: String,
23    /// Whether the account is active.
24    pub active: bool,
25    /// Whether the user is a workspace admin.
26    pub admin: bool,
27}
28
29/// The workspace (organization) the API key belongs to.
30#[derive(Debug, Clone, Deserialize)]
31#[serde(rename_all = "camelCase")]
32#[non_exhaustive]
33pub struct Organization {
34    /// Organization ID.
35    pub id: OrganizationId,
36    /// Organization name.
37    pub name: String,
38    /// URL key, as in `https://linear.app/<urlKey>`.
39    pub url_key: String,
40}
41
42const VIEWER: &str = "query Viewer { viewer { id name displayName email active admin } }";
43const ORGANIZATION: &str = "query Organization { organization { id name urlKey } }";
44
45pub(crate) const DOCUMENTS: &[(&str, &str)] = &[("Viewer", VIEWER), ("Organization", ORGANIZATION)];
46
47impl LinearClient {
48    /// Fetches the user the API key authenticates as.
49    pub async fn viewer(&self) -> Result<Viewer> {
50        #[derive(Deserialize)]
51        struct Data {
52            viewer: Viewer,
53        }
54        Ok(self
55            .query::<_, Data>("Viewer", VIEWER, serde_json::json!({}))
56            .await?
57            .viewer)
58    }
59
60    /// Fetches the workspace the API key belongs to.
61    pub async fn organization(&self) -> Result<Organization> {
62        #[derive(Deserialize)]
63        struct Data {
64            organization: Organization,
65        }
66        Ok(self
67            .query::<_, Data>("Organization", ORGANIZATION, serde_json::json!({}))
68            .await?
69            .organization)
70    }
71}