use serde::{Deserialize, Serialize};
use crate::client::Bibby;
use crate::error::Result;
const PRODUCT: &str = "geo";
#[derive(Debug, Clone, Copy)]
pub struct GeoClient<'a> {
client: &'a Bibby,
}
impl<'a> GeoClient<'a> {
pub(crate) fn new(client: &'a Bibby) -> Self {
Self { client }
}
pub async fn lookup(&self, ip: &str) -> Result<IpLocation> {
self.client.get_json(PRODUCT, &format!("ips/{ip}")).await
}
pub async fn lookup_with_options(
&self,
ip: &str,
options: &LookupOptions,
) -> Result<IpLocation> {
self.client
.get_json_query(PRODUCT, &format!("ips/{ip}"), options)
.await
}
pub async fn random(&self, options: &RandomIpOptions) -> Result<IpLocation> {
self.client
.get_json_query(PRODUCT, "ips/random", options)
.await
}
pub async fn submit_correction(
&self,
ip: &str,
correction: &CorrectionRequest,
) -> Result<IpLocation> {
self.client
.post_json(PRODUCT, &format!("ips/{ip}/corrections"), correction)
.await
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct LookupOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub include_map: Option<bool>,
}
impl LookupOptions {
pub fn with_map() -> Self {
Self {
include_map: Some(true),
}
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct RandomIpOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub country_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub region_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<u8>,
}
impl RandomIpOptions {
pub fn country(mut self, code: impl Into<String>) -> Self {
self.country_code = Some(code.into());
self
}
pub fn region(mut self, code: impl Into<String>) -> Self {
self.region_code = Some(code.into());
self
}
pub fn version(mut self, version: u8) -> Self {
self.version = Some(version);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CorrectionRequest {
pub country_code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub region_code: Option<String>,
}
impl CorrectionRequest {
pub fn new(country_code: impl Into<String>) -> Self {
Self {
country_code: country_code.into(),
region_code: None,
}
}
pub fn region_code(mut self, region_code: impl Into<String>) -> Self {
self.region_code = Some(region_code.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NamedCode {
pub code: String,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkInfo {
pub asn: u32,
pub organisation: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConnectionType {
Hosting,
Relay,
Satellite,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RiskLevel {
Low,
Medium,
High,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IpRiskReason {
TorExit,
Hosting,
Relay,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpRisk {
pub level: RiskLevel,
#[serde(default)]
pub reasons: Vec<IpRiskReason>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GeoSource {
Registry,
Geofeed,
ReverseDns,
Latency,
Correction,
DbIp,
Estimate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Confidence {
Confirmed,
Likely,
Reported,
Estimated,
Uncertain,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IpLocation {
pub ip: String,
pub country: NamedCode,
pub country_flag_url: String,
pub region: Option<NamedCode>,
#[serde(default)]
pub possible_regions: Vec<NamedCode>,
pub city: Option<String>,
pub city_estimated: bool,
pub postal_code: Option<String>,
pub latitude: Option<f64>,
pub longitude: Option<f64>,
pub connection: Option<ConnectionType>,
pub timezone: Option<String>,
pub network: Option<NetworkInfo>,
pub risk: IpRisk,
pub source: GeoSource,
pub confidence: Confidence,
pub confidence_score: f64,
pub map: Option<String>,
}