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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Collection-related types and methods
use crate::{PaginatedResponse, Result, RideWithGpsClient, Route, Trip};
use serde::{Deserialize, Serialize};
/// A collection of routes and trips
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Collection {
/// Collection ID
pub id: u64,
/// Collection name
pub name: Option<String>,
/// Collection description
pub description: Option<String>,
/// User ID of the collection owner
pub user_id: Option<u64>,
/// Visibility
pub visibility: Option<crate::Visibility>,
/// API URL
pub url: Option<String>,
/// HTML/web URL
pub html_url: Option<String>,
/// Cover photo/image
pub cover: Option<String>,
/// Created timestamp
pub created_at: Option<String>,
/// Updated timestamp
pub updated_at: Option<String>,
/// Number of routes in the collection
pub route_count: Option<u32>,
/// Collection cover photo URL
pub cover_photo_url: Option<String>,
/// Routes in the collection (included when fetching a specific collection)
pub routes: Option<Vec<Route>>,
/// Trips in the collection (included when fetching a specific collection)
pub trips: Option<Vec<Trip>>,
}
/// Parameters for listing collections
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListCollectionsParams {
/// Filter by collection name
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// Page number
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<u32>,
/// Page size
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<u32>,
}
impl RideWithGpsClient {
/// List collections
///
/// # Arguments
///
/// * `params` - Optional parameters for filtering and pagination
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// let collections = client.list_collections(None).unwrap();
/// println!("Found {} collections", collections.results.len());
/// ```
pub fn list_collections(
&self,
params: Option<&ListCollectionsParams>,
) -> Result<PaginatedResponse<Collection>> {
let mut url = "/api/v1/collections.json".to_string();
if let Some(params) = params {
let query = serde_json::to_value(params)?;
if let Some(obj) = query.as_object() {
if !obj.is_empty() {
let query_str = serde_urlencoded::to_string(obj).map_err(|e| {
crate::Error::ApiError(format!("Failed to encode query: {}", e))
})?;
url.push('?');
url.push_str(&query_str);
}
}
}
self.get(&url)
}
/// Get a specific collection by ID
///
/// This returns the full collection including its routes and trips.
///
/// # Arguments
///
/// * `id` - The collection ID
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// None
/// );
///
/// let collection = client.get_collection(12345).unwrap();
/// println!("Collection: {:?}", collection);
///
/// // Access routes within the collection
/// if let Some(routes) = &collection.routes {
/// for route in routes {
/// println!("Route: {} - {:?}", route.id, route.name);
/// }
/// }
///
/// // Access trips within the collection
/// if let Some(trips) = &collection.trips {
/// for trip in trips {
/// println!("Trip: {} - {:?}", trip.id, trip.name);
/// }
/// }
/// ```
pub fn get_collection(&self, id: u64) -> Result<Collection> {
#[derive(Deserialize)]
struct CollectionWrapper {
collection: Collection,
}
let wrapper: CollectionWrapper = self.get(&format!("/api/v1/collections/{}.json", id))?;
Ok(wrapper.collection)
}
/// Get the pinned collection
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// let collection = client.get_pinned_collection().unwrap();
/// println!("Pinned collection: {:?}", collection);
/// ```
pub fn get_pinned_collection(&self) -> Result<Collection> {
#[derive(Deserialize)]
struct CollectionWrapper {
collection: Collection,
}
let wrapper: CollectionWrapper = self.get("/api/v1/collections/pinned.json")?;
Ok(wrapper.collection)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collection_deserialization() {
let json = r#"{
"id": 999,
"name": "My Favorite Routes",
"description": "Best rides in the area",
"user_id": 123,
"route_count": 15
}"#;
let collection: Collection = serde_json::from_str(json).unwrap();
assert_eq!(collection.id, 999);
assert_eq!(collection.name.as_deref(), Some("My Favorite Routes"));
assert_eq!(
collection.description.as_deref(),
Some("Best rides in the area")
);
assert_eq!(collection.route_count, Some(15));
}
#[test]
fn test_list_collections_params() {
let params = ListCollectionsParams {
name: Some("favorites".to_string()),
page: Some(1),
..Default::default()
};
let json = serde_json::to_value(¶ms).unwrap();
assert!(json.get("name").is_some());
assert!(json.get("page").is_some());
}
#[test]
fn test_collection_wrapper_deserialization() {
let json = r#"{
"collection": {
"id": 888,
"name": "Wrapped Collection",
"visibility": "public"
}
}"#;
#[derive(Deserialize)]
struct CollectionWrapper {
collection: Collection,
}
let wrapper: CollectionWrapper = serde_json::from_str(json).unwrap();
assert_eq!(wrapper.collection.id, 888);
assert_eq!(
wrapper.collection.name.as_deref(),
Some("Wrapped Collection")
);
assert_eq!(
wrapper.collection.visibility,
Some(crate::Visibility::Public)
);
}
#[test]
fn test_collection_with_nested_routes() {
let json = r#"{
"id": 777,
"name": "Collection with Routes",
"routes": [
{
"id": 1,
"name": "Route 1",
"distance": 10000.0
},
{
"id": 2,
"name": "Route 2",
"distance": 20000.0
}
]
}"#;
let collection: Collection = serde_json::from_str(json).unwrap();
assert_eq!(collection.id, 777);
assert!(collection.routes.is_some());
let routes = collection.routes.unwrap();
assert_eq!(routes.len(), 2);
assert_eq!(routes[0].id, 1);
assert_eq!(routes[0].name.as_deref(), Some("Route 1"));
assert_eq!(routes[1].id, 2);
}
#[test]
fn test_collection_with_nested_trips() {
let json = r#"{
"id": 666,
"name": "Collection with Trips",
"trips": [
{
"id": 10,
"name": "Trip 1",
"distance": 15000.0
}
]
}"#;
let collection: Collection = serde_json::from_str(json).unwrap();
assert_eq!(collection.id, 666);
assert!(collection.trips.is_some());
let trips = collection.trips.unwrap();
assert_eq!(trips.len(), 1);
assert_eq!(trips[0].id, 10);
assert_eq!(trips[0].name.as_deref(), Some("Trip 1"));
}
}