hey-sdk 0.31.0

Rust client for the HEY API, generated from a Smithy model of the API
Documentation
// Generated by hey-sdk-generator from openapi.json and behavior-model.json. DO NOT EDIT.

#![allow(missing_docs, unreachable_pub, clippy::all, clippy::pedantic)]

use crate::client::Client;
use crate::error::Error;
use crate::generated::routes;
use crate::generated::types::*;
use crate::pagination::Page;

/// Optional query parameters for `ListTimeTracks`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ListTimeTracksParams {
    pub page: Option<String>,
    pub category_id: Option<i64>,
}

pub struct TimeTracks<'a> {
    client: &'a Client,
}

impl<'a> TimeTracks<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// The client this service sends through.
    pub fn client(&self) -> &'a Client {
        self.client
    }

    /// Record a finished stretch of time.
    ///
    /// JSON callers send the fields flat; Rails wraps them into calendar_time_track itself.
    pub async fn create(
        &self,
        body: &TimeTrackRequestContent,
    ) -> Result<CreateTimeTrackResponseContent, Error> {
        let mut operation = self.client.operation(&routes::CREATE_TIME_TRACK, &[]);
        operation.json(body)?;
        self.client.send(operation).await
    }

    /// Delete a time track. The id is the recording's.
    pub async fn delete(&self, time_track_id: i64) -> Result<(), Error> {
        let mut operation = self
            .client
            .operation(&routes::DELETE_TIME_TRACK, &[&time_track_id]);
        operation.resource_id(time_track_id);
        self.client.send_unit(operation).await
    }

    /// Get the ongoing time track (404 = no active track; see ADR-004)
    pub async fn get_ongoing(&self) -> Result<Option<GetOngoingTimeTrackResponseContent>, Error> {
        let operation = self.client.operation(&routes::GET_ONGOING_TIME_TRACK, &[]);
        self.client.send_optional(operation).await
    }

    /// List tracked time — completed tracks only, newest-ended first.
    ///
    /// A running track is not here; read that with GetOngoingTimeTrack. The next page, if
    /// any, is a Link header, and the last page carries none, so a nil Link is the end of
    /// the list rather than an error.
    ///
    /// category_id narrows the list to one category and 404s if the calendar has no
    /// category by that id.
    ///
    /// The calendar's categories come back alongside the tracks, so showing or applying the
    /// filter does not need ListTimeTrackCategories as well.
    pub async fn list(
        &self,
        params: &ListTimeTracksParams,
    ) -> Result<Page<ListTimeTracksResponseContent>, Error> {
        let mut operation = self.client.operation(&routes::LIST_TIME_TRACKS, &[]);
        operation.query_optional("page", params.page.as_ref());
        operation.query_optional("category_id", params.category_id.as_ref());
        self.client.send_page(operation).await
    }

    /// List the calendar's time track categories, alphabetically
    pub async fn list_categories(&self) -> Result<ListTimeTrackCategoriesResponseContent, Error> {
        let operation = self
            .client
            .operation(&routes::LIST_TIME_TRACK_CATEGORIES, &[]);
        self.client.send(operation).await
    }

    /// Start a new time track. Takes no body: haystack's
    /// Calendar::OngoingTimeTracksController#create ignores request parameters and
    /// starts a track with defaults; use UpdateTimeTrack to set notes and category_title,
    /// which also stops the track.
    pub async fn start(&self) -> Result<StartTimeTrackResponseContent, Error> {
        let operation = self.client.operation(&routes::START_TIME_TRACK, &[]);
        self.client.send(operation).await
    }

    /// Update a time track (stop by setting ends_at to current time).
    ///
    /// Every update completes the track, whether or not ends_at is sent, so this cannot
    /// be used to adjust a running track: it stops it.
    ///
    /// Only the fields sent are written, so a partial update leaves the rest of the track
    /// alone. A starts_at or ends_at the server cannot parse is a 400, not a 422.
    pub async fn update(
        &self,
        time_track_id: i64,
        body: &UpdateTimeTrackRequestContent,
    ) -> Result<UpdateTimeTrackResponseContent, Error> {
        let mut operation = self
            .client
            .operation(&routes::UPDATE_TIME_TRACK, &[&time_track_id]);
        operation.resource_id(time_track_id);
        operation.json(body)?;
        self.client.send(operation).await
    }
}