Skip to main content

strava_wrapper/filters/
streams.rs

1use crate::models::StreamSet;
2use crate::query::{
3    get_with_query_and_path, Endpoint, ErrorWrapper, PathQuery, Query, Sendable, ID,
4};
5use async_trait::async_trait;
6use std::collections::HashMap;
7use strava_wrapper_macros::{Endpoint, PathQuery, Query, ID};
8
9/// Identifiers for the `keys` query parameter on Strava stream endpoints.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum StreamKey {
13    Time,
14    Distance,
15    Latlng,
16    Altitude,
17    VelocitySmooth,
18    Heartrate,
19    Cadence,
20    Watts,
21    Temp,
22    Moving,
23    GradeSmooth,
24}
25
26impl StreamKey {
27    pub fn as_str(self) -> &'static str {
28        match self {
29            Self::Time => "time",
30            Self::Distance => "distance",
31            Self::Latlng => "latlng",
32            Self::Altitude => "altitude",
33            Self::VelocitySmooth => "velocity_smooth",
34            Self::Heartrate => "heartrate",
35            Self::Cadence => "cadence",
36            Self::Watts => "watts",
37            Self::Temp => "temp",
38            Self::Moving => "moving",
39            Self::GradeSmooth => "grade_smooth",
40        }
41    }
42}
43
44fn push_keys(query: &mut Vec<(String, String)>, keys: &[StreamKey]) {
45    let joined = keys
46        .iter()
47        .map(|k| k.as_str())
48        .collect::<Vec<_>>()
49        .join(",");
50    query.push(("keys".to_string(), joined));
51    query.push(("key_by_type".to_string(), "true".to_string()));
52}
53
54#[derive(Debug, Clone, Endpoint, Query, PathQuery, ID)]
55#[must_use = "this request is not executed until you call .send().await"]
56pub struct GetActivityStreams {
57    url: String,
58    token: String,
59    path: String,
60    query: Vec<(String, String)>,
61    path_params: Vec<(String, String)>,
62}
63
64impl GetActivityStreams {
65    /// Required. Set the stream types to fetch.
66    pub fn keys(mut self, keys: &[StreamKey]) -> Self {
67        push_keys(&mut self.query, keys);
68        self
69    }
70}
71
72#[async_trait]
73impl Sendable<StreamSet> for GetActivityStreams {
74    async fn send(self) -> Result<StreamSet, ErrorWrapper> {
75        let token = self.token.clone();
76        get_with_query_and_path(self, &token).await
77    }
78}
79
80#[derive(Debug, Clone, Endpoint, Query, PathQuery, ID)]
81#[must_use = "this request is not executed until you call .send().await"]
82pub struct GetRouteStreams {
83    url: String,
84    token: String,
85    path: String,
86    query: Vec<(String, String)>,
87    path_params: Vec<(String, String)>,
88}
89
90#[async_trait]
91impl Sendable<StreamSet> for GetRouteStreams {
92    async fn send(self) -> Result<StreamSet, ErrorWrapper> {
93        let token = self.token.clone();
94        get_with_query_and_path(self, &token).await
95    }
96}
97
98#[derive(Debug, Clone, Endpoint, Query, PathQuery, ID)]
99#[must_use = "this request is not executed until you call .send().await"]
100pub struct GetSegmentEffortStreams {
101    url: String,
102    token: String,
103    path: String,
104    query: Vec<(String, String)>,
105    path_params: Vec<(String, String)>,
106}
107
108impl GetSegmentEffortStreams {
109    pub fn keys(mut self, keys: &[StreamKey]) -> Self {
110        push_keys(&mut self.query, keys);
111        self
112    }
113}
114
115#[async_trait]
116impl Sendable<StreamSet> for GetSegmentEffortStreams {
117    async fn send(self) -> Result<StreamSet, ErrorWrapper> {
118        let token = self.token.clone();
119        get_with_query_and_path(self, &token).await
120    }
121}
122
123#[derive(Debug, Clone, Endpoint, Query, PathQuery, ID)]
124#[must_use = "this request is not executed until you call .send().await"]
125pub struct GetSegmentStreams {
126    url: String,
127    token: String,
128    path: String,
129    query: Vec<(String, String)>,
130    path_params: Vec<(String, String)>,
131}
132
133impl GetSegmentStreams {
134    pub fn keys(mut self, keys: &[StreamKey]) -> Self {
135        push_keys(&mut self.query, keys);
136        self
137    }
138}
139
140#[async_trait]
141impl Sendable<StreamSet> for GetSegmentStreams {
142    async fn send(self) -> Result<StreamSet, ErrorWrapper> {
143        let token = self.token.clone();
144        get_with_query_and_path(self, &token).await
145    }
146}