pexels_api/photos/
photo.rs

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
use crate::{Pexels, PexelsError, Photo, PEXELS_API, PEXELS_VERSION};
use url::Url;

/// Path to get a specific photo.
const PEXELS_GET_PHOTO_PATH: &str = "photos";

/// Retrieve a specific Photo from its id.
pub struct FetchPhoto {
    id: usize,
}

impl FetchPhoto {
    /// Creates [`FetchPhotoBuilder`] for building URI's.
    pub fn builder() -> FetchPhotoBuilder {
        FetchPhotoBuilder::default()
    }

    /// Creates a URI from the values provided by the [`FetchPhotoBuilder`].
    pub fn create_uri(&self) -> crate::BuilderResult {
        let uri =
            format!("{}/{}/{}/{}", PEXELS_API, PEXELS_VERSION, PEXELS_GET_PHOTO_PATH, self.id);

        let url = Url::parse(uri.as_str())?;

        Ok(url.into())
    }

    /// Fetches the photo data from the Pexels API using the provided client.
    pub async fn fetch(&self, client: &Pexels) -> Result<Photo, PexelsError> {
        let url = self.create_uri()?;
        let response = client.make_request(url.as_str()).await?;
        let photo: Photo = serde_json::from_value(response)?;
        Ok(photo)
    }
}

/// Builder for [`FetchPhoto`].
#[derive(Default)]
pub struct FetchPhotoBuilder {
    id: usize,
}

impl FetchPhotoBuilder {
    /// Create a new [`FetchPhotoBuilder`].
    pub fn new() -> Self {
        Self { id: 0 }
    }

    /// Sets the ID of the photo to be requested.
    pub fn id(mut self, id: usize) -> Self {
        self.id = id;
        self
    }

    /// Create [`FetchPhoto`] from the [`FetchPhotoBuilder`]
    pub fn build(self) -> FetchPhoto {
        FetchPhoto { id: self.id }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use dotenvy::dotenv;
    use std::env;
    use tokio;

    #[test]
    fn test_id() {
        let uri = FetchPhotoBuilder::new().id(123).build();
        assert_eq!("https://api.pexels.com/v1/photos/123", uri.create_uri().unwrap());
    }

    #[tokio::test]
    async fn test_fetch_photo() {
        dotenv().ok();
        let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
        let client = Pexels::new(api_key);

        let get_photo = FetchPhoto::builder().id(10967).build();
        let result = get_photo.fetch(&client).await;
        println!("get_photo result: {:?}", result);
        assert!(result.is_ok());
    }
}