linear-api 0.1.0

Unofficial async Rust client for the Linear GraphQL API (API-key auth)
Documentation
//! The authenticated viewer and its organization — the crate's working
//! example module (the smallest full query pipeline).

use serde::Deserialize;

use crate::client::LinearClient;
use crate::error::Result;
use crate::ids::{OrganizationId, UserId};

/// The user the API key authenticates as.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Viewer {
    /// User ID.
    pub id: UserId,
    /// Full name.
    pub name: String,
    /// Display (handle) name.
    pub display_name: String,
    /// Email address.
    pub email: String,
    /// Whether the account is active.
    pub active: bool,
    /// Whether the user is a workspace admin.
    pub admin: bool,
}

/// The workspace (organization) the API key belongs to.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Organization {
    /// Organization ID.
    pub id: OrganizationId,
    /// Organization name.
    pub name: String,
    /// URL key, as in `https://linear.app/<urlKey>`.
    pub url_key: String,
}

const VIEWER: &str = "query Viewer { viewer { id name displayName email active admin } }";
const ORGANIZATION: &str = "query Organization { organization { id name urlKey } }";

pub(crate) const DOCUMENTS: &[(&str, &str)] = &[("Viewer", VIEWER), ("Organization", ORGANIZATION)];

impl LinearClient {
    /// Fetches the user the API key authenticates as.
    pub async fn viewer(&self) -> Result<Viewer> {
        #[derive(Deserialize)]
        struct Data {
            viewer: Viewer,
        }
        Ok(self
            .query::<_, Data>("Viewer", VIEWER, serde_json::json!({}))
            .await?
            .viewer)
    }

    /// Fetches the workspace the API key belongs to.
    pub async fn organization(&self) -> Result<Organization> {
        #[derive(Deserialize)]
        struct Data {
            organization: Organization,
        }
        Ok(self
            .query::<_, Data>("Organization", ORGANIZATION, serde_json::json!({}))
            .await?
            .organization)
    }
}