Skip to content

Commit

Permalink
Add latitude/longitude fields to Location
Browse files Browse the repository at this point in the history
  • Loading branch information
dynco-nym committed Nov 29, 2024
1 parent a9e6288 commit 346c4f7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nym-node-status-api/nym-node-status-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[package]
name = "nym-node-status-api"
version = "1.0.0-rc.4"
version = "1.0.0-rc.4-fix"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
Expand Down
65 changes: 52 additions & 13 deletions nym-node-status-api/nym-node-status-api/src/monitor/geodata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{Addr, Coin};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};

pub(crate) struct IpInfoClient {
client: reqwest::Client,
Expand All @@ -15,11 +15,7 @@ impl IpInfoClient {
}

pub(crate) async fn locate_ip(&self, ip: impl AsRef<str>) -> anyhow::Result<Location> {
let url = format!(
"https://ipinfo.io/{}/country?token={}",
ip.as_ref(),
&self.token
);
let url = format!("https://ipinfo.io/{}?token={}", ip.as_ref(), &self.token);
let response = self
.client
.get(url)
Expand All @@ -33,11 +29,12 @@ impl IpInfoClient {
}
anyhow::Error::from(err)
})?;
let response_text = response.text().await?.trim().to_string();
let raw_response = response.text().await?;
let response: LocationResponse =
serde_json::from_str(&raw_response).inspect_err(|e| tracing::error!("{e}"))?;
let location = response.into();

Ok(Location {
two_letter_iso_country_code: response_text,
})
Ok(location)
}

/// check DOESN'T consume bandwidth allowance
Expand All @@ -64,23 +61,65 @@ impl IpInfoClient {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub(crate) struct NodeGeoData {
pub(crate) identity_key: String,
pub(crate) owner: Addr,
pub(crate) pledge_amount: Coin,
pub(crate) location: Location,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub(crate) struct Location {
pub(crate) two_letter_iso_country_code: String,
#[serde(flatten)]
pub(crate) location: Coordinates,
}

impl From<LocationResponse> for Location {
fn from(value: LocationResponse) -> Self {
Self {
two_letter_iso_country_code: value.two_letter_iso_country_code,
location: value.loc,
}
}
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct LocationResponse {
#[serde(rename = "country")]
pub(crate) two_letter_iso_country_code: String,
#[serde(deserialize_with = "deserialize_loc")]
pub(crate) loc: Coordinates,
}

fn deserialize_loc<'de, D>(deserializer: D) -> Result<Coordinates, D::Error>
where
D: Deserializer<'de>,
{
let loc_raw = String::deserialize(deserializer)?;
return match loc_raw.split_once(',') {
Some((lat, long)) => Ok(Coordinates {
latitude: lat.parse().map_err(|err| serde::de::Error::custom(err))?,
longitude: long.parse().map_err(|err| serde::de::Error::custom(err))?,
}),
None => Err(serde::de::Error::custom("coordinates")),
};
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub(crate) struct Coordinates {
pub(crate) latitude: f64,
pub(crate) longitude: f64,
}

impl Location {
pub(crate) fn empty() -> Self {
Self {
two_letter_iso_country_code: String::new(),
two_letter_iso_country_code: String::default(),
location: Coordinates {
..Default::default()
},
}
}
}
Expand Down

0 comments on commit 346c4f7

Please sign in to comment.