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
194
195
196
197
use std::option::Option;
use api::{self, Paginated, AccessToken, ResourceState};
use error::Result;
use http;
use gear::Gear;
use clubs::Club;
use segmentefforts::SegmentEffort;
#[derive(Debug, RustcDecodable)]
pub struct Athlete {
pub id: i32,
pub resource_state: ResourceState,
pub firstname: Option<String>,
pub lastname: Option<String>,
pub profile_medium: Option<String>,
pub profile: Option<String>,
pub city: Option<String>,
pub state: Option<String>,
pub country: Option<String>,
pub sex: Option<String>,
pub friend: Option<String>,
pub follower: Option<String>,
pub premium: Option<bool>,
pub created_at: Option<String>,
pub updated_at: Option<String>,
pub approve_followers: Option<bool>,
pub follower_count: Option<i32>,
pub friend_count: Option<i32>,
pub mutual_friend_count: Option<i32>,
pub date_preference: Option<String>,
pub measurement_preference: Option<String>,
pub email: Option<String>,
pub ftp: Option<i32>,
pub weight: Option<f32>,
pub athlete_type: Option<i32>,
pub clubs: Option<Vec<Club>>,
pub shoes: Option<Vec<Gear>>,
pub bikes: Option<Vec<Gear>>,
}
#[derive(Debug, RustcDecodable)]
pub enum AthleteType {
Cycling,
Running,
}
#[derive(Debug, RustcDecodable)]
pub struct Stats {
pub biggest_ride_distance: f32,
pub biggest_climb_elevation_gain: f32,
pub recent_ride_totals: RecentTotals,
pub recent_run_totals: RecentTotals,
pub ytd_ride_totals: Totals,
pub ytd_run_totals: Totals,
pub all_ride_totals: Totals,
pub all_run_totals: Totals,
}
#[derive(Debug, RustcDecodable)]
pub struct RecentTotals {
pub count: i32,
pub distance: f32,
pub moving_time: i32,
pub elapsed_time: i32,
pub elevation_gain: f32,
pub achievement_count: i32
}
#[derive(Debug, RustcDecodable)]
pub struct Totals {
pub count: i32,
pub distance: i32,
pub moving_time: i32,
pub elapsed_time: i32,
pub elevation_gain: i32
}
impl Athlete {
pub fn get_current(token: &AccessToken) -> Result<Athlete> {
let url = api::v3(token, "athlete".to_string());
http::get::<Athlete>(&url[..])
}
pub fn get(token: &AccessToken, id: i32) -> Result<Athlete> {
let url = api::v3(token, format!("athletes/{}", id));
http::get::<Athlete>(&url[..])
}
pub fn stats(&self, token: &AccessToken) -> Result<Stats> {
let url = api::v3(token, format!("athletes/{}/stats", self.id));
http::get::<Stats>(&url[..])
}
pub fn koms(&self, token: &AccessToken) -> Result<Paginated<SegmentEffort>> {
let url = api::v3(token, format!("athletes/{}/koms", self.id));
let efforts = try!(http::get::<Vec<SegmentEffort>>(&url[..]));
Ok(Paginated::new(url, efforts))
}
}
#[cfg(feature = "api_test")]
#[cfg(test)]
mod api_tests {
use api::{AccessToken, ResourceState};
use super::Athlete;
use error::ApiError;
#[test]
fn get_current_athlete() {
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get_current(&token).unwrap();
assert_eq!(athlete.resource_state, ResourceState::Detailed);
}
#[test]
fn get_athlete_by_id() {
let id = 1712082;
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get(&token, id).unwrap();
assert_eq!(athlete.id, id);
}
#[test]
fn get_athlete_stats() {
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get_current(&token).unwrap();
let stats = athlete.stats(&token).unwrap();
println!("{:?}", stats);
}
#[test]
fn get_other_athlete_stats() {
let id = 1712082;
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get(&token, id).unwrap();
match athlete.stats(&token) {
Ok(_) => panic!("somehow got stats for other athlete"),
Err(ApiError::InvalidAccessToken) => (),
Err(e) => panic!("unexpected error type {:?}", e)
}
}
#[test]
fn get_koms() {
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get_current(&token).unwrap();
let koms = athlete.koms(&token).unwrap();
println!("{:?}", koms);
}
#[test]
fn print_response() {
let token = AccessToken::new_from_env().unwrap();
let athlete = Athlete::get_current(&token).unwrap();
println!("{:?}",athlete);
}
}