geocodio_lib_rust/request/
fetch.rs

1use crate::{errors::Error, response::GeocodeBatchResponse, GeocodioProxy};
2
3const GEOCODIO_BASE_URL: &str = "https://api.geocod.io/v1.7/";
4
5#[macro_export]
6macro_rules! single_fetch {
7    ($data:ident, $endpoint:ident, $params:ident, $res:ty) => {{
8        let response = $data.request($endpoint, &$params).await?;
9        let json = response.json::<serde_json::Value>().await.unwrap();
10        let result = serde_json::from_value::<$res>(json);
11        match result {
12            Ok(geocode_response) => Ok(geocode_response),
13            Err(err) => Err(Error::BadInputData(err)),
14        }
15    }};
16}
17
18pub(crate) async fn batch_fetch(data: &GeocodioProxy, endpoint: String, params: Vec<String>) -> Result<GeocodeBatchResponse, Error> {
19    let res = data.request_batch(endpoint.as_str(), params).await?;
20    let json = res.json::<serde_json::Value>().await?;
21    let result = serde_json::from_value::<GeocodeBatchResponse>(json);
22    match result {
23        Ok(geocode_response) => Ok(geocode_response),
24        Err(err) => Err(Error::BadInputData(err)),
25    }
26}
27
28pub(crate) fn proxy_new(api_key: String) -> Result<GeocodioProxy, Error> {
29    let client = reqwest::Client::new();
30
31    Ok(GeocodioProxy {
32        client,
33        base_url: reqwest::Url::parse(GEOCODIO_BASE_URL).unwrap(),
34        api_key,
35    })
36}