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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Points of Interest related types and methods
//!
//! Note: These endpoints are only available to organization accounts.
use crate::{PaginatedResponse, Result, RideWithGpsClient};
use serde::{Deserialize, Serialize};
/// A point of interest
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PointOfInterest {
/// POI ID
pub id: u64,
/// POI name
pub name: Option<String>,
/// POI description
pub description: Option<String>,
/// Latitude
#[serde(alias = "latitude")]
pub lat: Option<f64>,
/// Longitude
#[serde(alias = "longitude")]
pub lng: Option<f64>,
/// POI type/category
#[serde(alias = "poi_type")]
pub r#type: Option<String>,
/// Type ID
pub type_id: Option<u64>,
/// Type name
pub type_name: Option<String>,
/// Icon identifier
pub icon: Option<String>,
/// User ID of the POI owner
pub user_id: Option<u64>,
/// Organization ID
pub organization_id: Option<u64>,
/// API URL
pub url: Option<String>,
/// Created timestamp
pub created_at: Option<String>,
/// Updated timestamp
pub updated_at: Option<String>,
/// Address
pub address: Option<String>,
/// Phone number
pub phone: Option<String>,
/// Website URL
pub website: Option<String>,
/// Tag names
pub tag_names: Option<Vec<String>>,
}
/// Parameters for listing POIs
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListPointsOfInterestParams {
/// Filter by POI name
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// Filter by POI type
#[serde(skip_serializing_if = "Option::is_none")]
pub poi_type: 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>,
}
/// Request to create or update a POI
#[derive(Debug, Clone, Serialize)]
pub struct PointOfInterestRequest {
/// POI name
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// POI description
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Latitude
#[serde(skip_serializing_if = "Option::is_none")]
pub latitude: Option<f64>,
/// Longitude
#[serde(skip_serializing_if = "Option::is_none")]
pub longitude: Option<f64>,
/// POI type/category
#[serde(skip_serializing_if = "Option::is_none")]
pub poi_type: Option<String>,
/// Icon identifier
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<String>,
/// Address
#[serde(skip_serializing_if = "Option::is_none")]
pub address: Option<String>,
/// Phone number
#[serde(skip_serializing_if = "Option::is_none")]
pub phone: Option<String>,
/// Website URL
#[serde(skip_serializing_if = "Option::is_none")]
pub website: Option<String>,
}
impl RideWithGpsClient {
/// List points of interest
///
/// Note: This endpoint is only available to organization accounts.
///
/// # 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 pois = client.list_points_of_interest(None).unwrap();
/// println!("Found {} POIs", pois.results.len());
/// ```
pub fn list_points_of_interest(
&self,
params: Option<&ListPointsOfInterestParams>,
) -> Result<PaginatedResponse<PointOfInterest>> {
let mut url = "/api/v1/points_of_interest.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)
}
/// Create a new point of interest
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `poi` - The POI data
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::{RideWithGpsClient, PointOfInterestRequest};
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// let poi_req = PointOfInterestRequest {
/// name: Some("Coffee Shop".to_string()),
/// description: Some("Great coffee stop".to_string()),
/// latitude: Some(37.7749),
/// longitude: Some(-122.4194),
/// poi_type: Some("cafe".to_string()),
/// icon: Some("coffee".to_string()),
/// address: None,
/// phone: None,
/// website: None,
/// };
///
/// let poi = client.create_point_of_interest(&poi_req).unwrap();
/// println!("Created POI: {}", poi.id);
/// ```
pub fn create_point_of_interest(
&self,
poi: &PointOfInterestRequest,
) -> Result<PointOfInterest> {
#[derive(Deserialize)]
struct PoiWrapper {
point_of_interest: PointOfInterest,
}
let wrapper: PoiWrapper = self.post("/api/v1/points_of_interest.json", poi)?;
Ok(wrapper.point_of_interest)
}
/// Get a specific point of interest by ID
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `id` - The POI ID
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// let poi = client.get_point_of_interest(12345).unwrap();
/// println!("POI: {:?}", poi);
/// ```
pub fn get_point_of_interest(&self, id: u64) -> Result<PointOfInterest> {
#[derive(Deserialize)]
struct PoiWrapper {
point_of_interest: PointOfInterest,
}
let wrapper: PoiWrapper = self.get(&format!("/api/v1/points_of_interest/{}.json", id))?;
Ok(wrapper.point_of_interest)
}
/// Update a point of interest
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `id` - The POI ID
/// * `poi` - The updated POI data
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::{RideWithGpsClient, PointOfInterestRequest};
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// let poi_req = PointOfInterestRequest {
/// name: Some("Updated Coffee Shop".to_string()),
/// description: None,
/// latitude: None,
/// longitude: None,
/// poi_type: None,
/// icon: None,
/// address: None,
/// phone: None,
/// website: None,
/// };
///
/// let poi = client.update_point_of_interest(12345, &poi_req).unwrap();
/// println!("Updated POI: {:?}", poi);
/// ```
pub fn update_point_of_interest(
&self,
id: u64,
poi: &PointOfInterestRequest,
) -> Result<PointOfInterest> {
#[derive(Deserialize)]
struct PoiWrapper {
point_of_interest: PointOfInterest,
}
let wrapper: PoiWrapper =
self.put(&format!("/api/v1/points_of_interest/{}.json", id), poi)?;
Ok(wrapper.point_of_interest)
}
/// Delete a point of interest
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `id` - The POI ID
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// client.delete_point_of_interest(12345).unwrap();
/// ```
pub fn delete_point_of_interest(&self, id: u64) -> Result<()> {
self.delete(&format!("/api/v1/points_of_interest/{}.json", id))
}
/// Associate a point of interest with a route
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `poi_id` - The POI ID
/// * `route_id` - The route ID
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// client.associate_poi_with_route(12345, 67890).unwrap();
/// ```
pub fn associate_poi_with_route(&self, poi_id: u64, route_id: u64) -> Result<()> {
let url = format!(
"/api/v1/points_of_interest/{}/routes/{}.json",
poi_id, route_id
);
let response = self
.client
.post(self.base_url.join(&url)?)
.headers(self.build_headers()?)
.send()?;
match response.status().as_u16() {
200 | 201 | 204 => Ok(()),
_ => {
let status = response.status();
let text = response.text().unwrap_or_default();
Err(self.error_from_status(status.as_u16(), &text))
}
}
}
/// Disassociate a point of interest from a route
///
/// Note: This endpoint is only available to organization accounts.
///
/// # Arguments
///
/// * `poi_id` - The POI ID
/// * `route_id` - The route ID
///
/// # Example
///
/// ```rust,no_run
/// use ridewithgps_client::RideWithGpsClient;
///
/// let client = RideWithGpsClient::new(
/// "https://ridewithgps.com",
/// "your-api-key",
/// Some("your-auth-token")
/// );
///
/// client.disassociate_poi_from_route(12345, 67890).unwrap();
/// ```
pub fn disassociate_poi_from_route(&self, poi_id: u64, route_id: u64) -> Result<()> {
let url = format!(
"/api/v1/points_of_interest/{}/routes/{}.json",
poi_id, route_id
);
self.delete(&url)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_poi_deserialization() {
let json = r#"{
"id": 999,
"name": "Coffee Shop",
"description": "Great coffee",
"latitude": 37.7749,
"longitude": -122.4194,
"poi_type": "cafe",
"icon": "coffee"
}"#;
let poi: PointOfInterest = serde_json::from_str(json).unwrap();
assert_eq!(poi.id, 999);
assert_eq!(poi.name.as_deref(), Some("Coffee Shop"));
assert_eq!(poi.lat, Some(37.7749));
assert_eq!(poi.lng, Some(-122.4194));
assert_eq!(poi.r#type.as_deref(), Some("cafe"));
}
#[test]
fn test_poi_request_serialization() {
let req = PointOfInterestRequest {
name: Some("Bike Shop".to_string()),
description: Some("Full service".to_string()),
latitude: Some(40.7128),
longitude: Some(-74.0060),
poi_type: Some("bike_shop".to_string()),
icon: Some("bicycle".to_string()),
address: Some("123 Main St".to_string()),
phone: Some("555-1234".to_string()),
website: Some("https://example.com".to_string()),
};
let json = serde_json::to_value(&req).unwrap();
assert_eq!(json.get("name").unwrap(), "Bike Shop");
assert_eq!(json.get("latitude").unwrap(), 40.7128);
assert_eq!(json.get("poi_type").unwrap(), "bike_shop");
}
#[test]
fn test_poi_wrapper_deserialization() {
let json = r#"{
"point_of_interest": {
"id": 777,
"name": "Wrapped POI",
"latitude": 40.0,
"longitude": -120.0,
"poi_type": "rest_stop"
}
}"#;
#[derive(Deserialize)]
struct PoiWrapper {
point_of_interest: PointOfInterest,
}
let wrapper: PoiWrapper = serde_json::from_str(json).unwrap();
assert_eq!(wrapper.point_of_interest.id, 777);
assert_eq!(
wrapper.point_of_interest.name.as_deref(),
Some("Wrapped POI")
);
assert_eq!(wrapper.point_of_interest.lat, Some(40.0));
assert_eq!(
wrapper.point_of_interest.r#type.as_deref(),
Some("rest_stop")
);
}
#[test]
fn test_poi_with_tags_and_type_info() {
let json = r#"{
"id": 444,
"name": "Tagged POI",
"poi_type": "cafe",
"type_id": 5,
"type_name": "Coffee Shop",
"tag_names": ["espresso", "wifi", "outdoor-seating"]
}"#;
let poi: PointOfInterest = serde_json::from_str(json).unwrap();
assert_eq!(poi.id, 444);
assert_eq!(poi.r#type.as_deref(), Some("cafe"));
assert_eq!(poi.type_id, Some(5));
assert_eq!(poi.type_name.as_deref(), Some("Coffee Shop"));
assert!(poi.tag_names.is_some());
let tags = poi.tag_names.unwrap();
assert_eq!(tags.len(), 3);
assert_eq!(tags[0], "espresso");
}
#[test]
fn test_poi_field_aliases() {
// Test that both latitude/lat and longitude/lng work
let json_with_full_names = r#"{
"id": 111,
"latitude": 37.5,
"longitude": -122.5,
"poi_type": "water"
}"#;
let poi1: PointOfInterest = serde_json::from_str(json_with_full_names).unwrap();
assert_eq!(poi1.lat, Some(37.5));
assert_eq!(poi1.lng, Some(-122.5));
assert_eq!(poi1.r#type.as_deref(), Some("water"));
}
}