Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lookup_self_v6 #55

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/ipinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ use tokio::time::timeout;

const COUNTRY_FLAG_URL: &str =
"https://cdn.ipinfo.io/static/images/countries-flags/";

const BASE_URL: &str = "https://ipinfo.io";
const BASE_URL_V6: &str = "https://v6.ipinfo.io";

/// IpInfo structure configuration.
pub struct IpInfoConfig {
/// IPinfo access token.
Expand Down Expand Up @@ -75,7 +79,6 @@ impl Default for IpInfoConfig {

/// IPinfo requests context structure.
pub struct IpInfo {
url: String,
token: Option<String>,
client: reqwest::Client,
cache: LruCache<String, IpDetails>,
Expand Down Expand Up @@ -116,10 +119,7 @@ impl IpInfo {
let client =
reqwest::Client::builder().timeout(config.timeout).build()?;

let url = "https://ipinfo.io".to_owned();

let mut ipinfo_obj = Self {
url,
client,
token: config.token,
cache: LruCache::new(
Expand Down Expand Up @@ -260,7 +260,7 @@ impl IpInfo {
) -> Result<HashMap<String, IpDetails>, IpError> {
// Lookup cache misses which are not bogon
let response = client
.post(&format!("{}/batch", self.url))
.post(&format!("{}/batch", BASE_URL))
.headers(Self::construct_headers())
.bearer_auth(self.token.as_deref().unwrap_or_default())
.json(&json!(ips))
Expand Down Expand Up @@ -303,6 +303,19 @@ impl IpInfo {
/// }
/// ```
pub async fn lookup(&mut self, ip: &str) -> Result<IpDetails, IpError> {
self._lookup(ip, BASE_URL).await
}

// Lookup your own v6 IP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be made into a doc comment? With sufficient explanation of purpose and limitations.

pub async fn lookup_self_v6(&mut self) -> Result<IpDetails, IpError> {
self._lookup("", BASE_URL_V6).await
}

async fn _lookup(
&mut self,
ip: &str,
base_url: &str,
) -> Result<IpDetails, IpError> {
if is_bogon(ip) {
return Ok(IpDetails {
ip: ip.to_string(),
Expand All @@ -321,7 +334,7 @@ impl IpInfo {
// lookup in case of a cache miss
let response = self
.client
.get(&format!("{}/{}", self.url, ip))
.get(&format!("{}/{}", base_url, ip))
.headers(Self::construct_headers())
.bearer_auth(self.token.as_deref().unwrap_or_default())
.send()
Expand Down Expand Up @@ -370,7 +383,7 @@ impl IpInfo {
return Err(err!(MapLimitError));
}

let map_url = &format!("{}/tools/map?cli=1", self.url);
let map_url = &format!("{}/tools/map?cli=1", BASE_URL);
let client = self.client.clone();
let json_ips = serde_json::json!(ips);

Expand Down