ip_api4rs/
client.rs

1use governor::DefaultDirectRateLimiter;
2#[cfg(feature = "blocking")]
3use reqwest::blocking;
4use serde::de::DeserializeOwned;
5use std::future::Future;
6
7use crate::error::IpApiError;
8use crate::model::ip_response::{IpDefaultResponse, IpFullResponse};
9
10/// The main client for the ip-api.com API.
11pub trait IpApi {
12    /// Gets the optional API key.
13    ///
14    /// # Returns
15    /// * `Option<String>` - The optional API key.
16    fn get_api_key(&self) -> &Option<String>;
17
18    /// Gets the rate limiter.
19    ///
20    /// # Returns
21    /// * `&DefaultDirectRateLimiter` - The rate limiter.
22    fn get_rate_limiter(&self) -> &Option<DefaultDirectRateLimiter>;
23}
24
25/// The blocking client for the ip-api.com API.
26#[cfg(feature = "blocking")]
27pub trait BlockingIpApi: IpApi {
28    /// Queries the API with the default fields.
29    ///
30    /// # Arguments
31    /// * `ip` - The IP address to query.
32    ///
33    /// # Returns
34    /// * `IpDefaultResponse` - The response from the API.
35    fn query_api_default(&self, ip: &str) -> Result<IpDefaultResponse, IpApiError>;
36
37    /// Queries the API with all fields.
38    ///
39    /// # Arguments
40    /// * `ip` - The IP address to query.
41    ///
42    /// # Returns
43    /// * `IpFullResponse` - The response from the API.
44    fn query_api_fully(&self, ip: &str) -> Result<IpFullResponse, IpApiError>;
45
46    /// Queries the API with a custom struct.
47    ///
48    /// # Arguments
49    /// * `ip` - The IP address to query.
50    /// * `T` - The custom struct to deserialize the response into.
51    ///
52    /// # Returns
53    /// * `T` - The response from the API.
54    fn query_api<T>(&self, ip: &str) -> Result<T, IpApiError>
55    where
56        T: DeserializeOwned;
57
58    /// Gets you the blocking http client.
59    ///
60    /// # Returns
61    /// * `&reqwest::blocking::Client` - The blocking http client.
62    fn get_http_client(&self) -> &blocking::Client;
63}
64
65/// The async client for the ip-api.com API.
66pub trait AsyncIpApi: IpApi {
67    /// Queries the API with the default fields.
68    ///
69    /// # Arguments
70    /// * `ip` - The IP address to query.
71    ///
72    /// # Returns
73    /// * `IpDefaultResponse` - The response from the API.
74    fn query_api_default(&self, ip: &str) -> impl Future<Output = Result<IpDefaultResponse, IpApiError>> + Send;
75
76    /// Queries the API with all fields.
77    ///
78    /// # Arguments
79    /// * `ip` - The IP address to query.
80    ///
81    /// # Returns
82    /// * `IpFullResponse` - The response from the API.
83    fn query_api_fully(&self, ip: &str) -> impl Future<Output = Result<IpFullResponse, IpApiError>> + Send;
84
85    /// Queries the API with a custom struct.
86    ///
87    /// # Arguments
88    /// * `ip` - The IP address to query.
89    /// * `T` - The custom struct to deserialize the response into.
90    ///
91    /// # Returns
92    /// * `T` - The response from the API.
93    fn query_api<T>(&self, ip: &str) -> impl Future<Output = Result<T, IpApiError>> + Send
94    where
95        T: DeserializeOwned;
96    /// Gets you the async http client.
97    ///
98    /// # Returns
99    /// * `&reqwest::Client` - The async http client.
100    fn get_http_client(&self) -> &reqwest::Client;
101}