google_places_api/endpoints/
api.rs

1use crate::endpoints::place_search::PlaceSearch;
2use dotenv::dotenv;
3use reqwest::Client;
4
5pub struct GooglePlacesAPI {
6    api_key: String,
7    client: Client,
8}
9
10impl GooglePlacesAPI {
11    /// Creates a new instance of `GooglePlacesAPI`.
12    ///
13    /// Loads environment variables using `dotenv` and retrieves the
14    /// `GOOGLE_PLACES_API_KEY` from the environment to initialize the API key.
15    /// Initializes a new `reqwest::Client` for HTTP requests.
16    ///
17    /// # Panics
18    ///
19    /// Panics if the `GOOGLE_PLACES_API_KEY` environment variable is not set.
20    pub fn new() -> Self {
21        dotenv().ok();
22        Self {
23            api_key: String::from(
24                std::env::var("GOOGLE_PLACES_API_KEY").expect("GOOGLE_PLACES_API_KEY must be set."),
25            ),
26            client: Client::new(),
27        }
28    }
29
30    /// Returns a new `PlaceSearch` instance with the API key and client.
31
32    pub fn place_search(&self) -> PlaceSearch {
33        PlaceSearch::new(self.api_key.as_str(), &self.client)
34    }
35}