1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! # Oura API Client
//!
//! This crate provides a client for the [Oura V2 REST API](https://cloud.ouraring.com/v2/docs).
//!
//! Note that this client does not support the Oura V1 REST API.
//!
//! ## Usage
//! ```no_run
//! use oura_api::{OuraClient, DateQuery};
//!
//! // token is the personal access token for the Oura API
//! let token = std::env::var("OURA_PERSONAL_ACCESS_TOKEN").unwrap();
//! let client = OuraClient::new(&token);
//!
//! let august_date_query = DateQuery::builder().start_date("2023-08-01").end_date("2023-08-31").build();
//! let august_daily_sleep = client.list_daily_sleep(august_date_query).unwrap();
//! ```

pub mod models;

use paste::paste;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::models::*;

const API_BASE_URL: &str = "https://api.ouraring.com/v2/usercollection";

/// Query parameters for endpoints that accept a date range.
#[derive(Serialize, TypedBuilder)]
pub struct DateQuery<'a> {
    #[builder(default = None, setter(strip_option))]
    start_date: Option<&'a str>,
    #[builder(default = None, setter(strip_option))]
    end_date: Option<&'a str>,
    #[builder(default = None, setter(strip_option))]
    next_token: Option<&'a str>,
}

/// Query parameters for endpoints that accept a datetime range.
#[derive(Serialize, TypedBuilder)]
pub struct DatetimeQuery<'a> {
    #[builder(default = None, setter(strip_option))]
    start_datetime: Option<&'a str>,
    #[builder(default = None, setter(strip_option))]
    end_datetime: Option<&'a str>,
    #[builder(default = None, setter(strip_option))]
    next_token: Option<&'a str>,
}

/// Response from endpoints that return a list of items.
#[derive(Deserialize, Debug, PartialEq)]
pub struct ListResponse<T> {
    /// The list of items returned by the endpoint.
    pub data: Vec<T>,
    /// The optional token to use to retrieve the next page of results.
    pub next_token: Option<String>,
}

macro_rules! generic_endpoint {
    ($(#[$m:meta])*, $name: ident, $type: ty, $path: literal) => {
        $(#[$m])*
        pub fn $name(&self) -> Result<$type, reqwest::Error> {
            let url = format!("{}/{}", &self.base_url, $path);
            let response = self
                .client
                .get(&url)
                .bearer_auth(&self.token)
                .send()?
                .json::<$type>()?;
            Ok(response)
        }
    };
}

macro_rules! list_endpoint {
    ($(#[$m:meta])*, $name: ident, $type: ty, $path: literal, $query: ty) => {
        $(#[$m])*
        pub fn $name(&self, query: $query) -> Result<ListResponse<$type>, reqwest::Error> {
            let url = format!("{}/{}", &self.base_url, $path);
            let response = self
                .client
                .get(&url)
                .bearer_auth(&self.token)
                .query(&query)
                .send()?
                .json::<ListResponse<$type>>()?;
            Ok(response)
        }
    };
}

macro_rules! get_endpoint {
    ($(#[$m:meta])*, $name: ident, $type: ty, $path: literal) => {
        $(#[$m])*
        pub fn $name(&self, id: &str) -> Result<$type, reqwest::Error> {
            let url = format!("{}/{}/{}", &self.base_url, $path, id);
            let response = self
                .client
                .get(&url)
                .bearer_auth(&self.token)
                .send()?
                .json::<$type>()?;
            Ok(response)
        }
    };
}

macro_rules! endpoint_set {
    ($name: ident, $type: ty, $path: literal, $params: ty) => {
        paste! {
            get_endpoint!(#[doc = "Gets a single [" $type "] item by id."], [<get_ $name>], $type, $path);
            list_endpoint!(#[doc = "Returns a [ListResponse] of [" $type "] items based on the supplied query."], [<list_ $name>], $type, $path, $params);
        }
    };
}

/// The Oura API client.
///
/// This client is used to make requests to the Oura API.
pub struct OuraClient<'a> {
    token: &'a str,
    base_url: &'a str,
    client: Client,
}

impl<'a> OuraClient<'a> {
    /// Creates a new OuraClient from a personal access token.
    pub fn new(token: &'a str) -> Self {
        let client = Client::new();
        Self {
            token,
            client,
            base_url: API_BASE_URL,
        }
    }

    /// Creates a new OuraClient from a personal access token and a base URL.
    ///
    /// *Note:* This is only useful for testing against a mock server.
    pub fn build_with_base_url(token: &'a str, base_url: &'a str) -> Self {
        let client = Client::new();
        Self {
            token,
            client,
            base_url,
        }
    }

    endpoint_set!(daily_activity, DailyActivity, "daily_activity", DateQuery);

    endpoint_set!(
        daily_readiness,
        DailyReadiness,
        "daily_readiness",
        DateQuery
    );

    endpoint_set!(daily_sleep, DailySleep, "daily_sleep", DateQuery);

    endpoint_set!(daily_spo2, DailySpO2, "daily_spo2", DateQuery);

    list_endpoint!(#[doc = "Returns a [ListResponse] of [HeartRate] items based on the supplied query."], list_heart_rate, HeartRate, "heartrate", DatetimeQuery);

    generic_endpoint!(#[doc = "Returns a [PersonalInfo] based on the caller."], get_personal_info, PersonalInfo, "personal_info");

    endpoint_set!(
        rest_mode_period,
        RestModePeriod,
        "rest_mode_period",
        DateQuery
    );

    endpoint_set!(
        ring_configuration,
        RingConfiguration,
        "ring_configuration",
        DateQuery
    );

    endpoint_set!(session, Session, "session", DateQuery);

    endpoint_set!(sleep, Sleep, "sleep", DateQuery);

    endpoint_set!(sleep_time, SleepTime, "sleep_time", DateQuery);

    endpoint_set!(tag, Tag, "tag", DateQuery);

    endpoint_set!(workout, Workout, "workout", DateQuery);

    endpoint_set!(tag_v2, TagV2, "tag/v2", DateQuery);
}