Skip to main content

IpWhois

Struct IpWhois 

Source
pub struct IpWhois { /* private fields */ }
Expand description

Async client for the ipwhois.io IP Geolocation API.

§Quick start

use ipwhois::{IpWhois, Options};

// Free plan (no API key, ~1 request/second per client IP)
let ipwhois = IpWhois::new();
let info = ipwhois.lookup("8.8.8.8").await?;

// Paid plan (with API key, higher limits, bulk, security data, …)
let ipwhois = IpWhois::with_key("YOUR_API_KEY");
let info = ipwhois
    .lookup_with("8.8.8.8", &Options::new().with_lang("en").with_security(true))
    .await?;

// Bulk lookup — up to 100 IPs in one call (paid only)
let list = ipwhois.bulk_lookup(["8.8.8.8", "1.1.1.1", "208.67.222.222"]).await?;

§Error handling

Every fallible method returns Result<_, Error>. The error enum carries categories (api, network, invalid_argument) and metadata (http_status, retry_after).

Implementations§

Source§

impl IpWhois

Source

pub fn new() -> Self

Create a free-plan client (no API key).

The free endpoint (ipwho.is) rate-limits roughly to 1 request per second per client IP. Suitable for low-traffic and non-commercial use.

Source

pub fn with_key(api_key: impl Into<String>) -> Self

Create a paid-plan client using the supplied API key.

Higher limits, plus access to bulk lookups and threat-detection data. Get your key at https://ipwhois.io.

This constructor does not validate the key — an empty or whitespace-only key will produce a client that talks to the paid host with ?key= and let the API itself reject the request. Use try_with_key if you’d rather validate up-front.

Source

pub fn try_with_key(api_key: impl Into<String>) -> Result<Self, Error>

Like with_key, but rejects empty or whitespace-only keys with Error::InvalidArgument before any request is made.

§Example
use ipwhois::IpWhois;

// A valid-looking key is accepted (no network call is made here).
IpWhois::try_with_key("MY_KEY").expect("constructed");

// Empty / whitespace-only keys are rejected up-front.
assert!(IpWhois::try_with_key("").is_err());
assert!(IpWhois::try_with_key("   ").is_err());
Source

pub fn with_language(self, lang: impl Into<String>) -> Self

Set the default language used when none is supplied per call. Must be one of SUPPORTED_LANGUAGES.

Source

pub fn with_fields<I, S>(self, fields: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Restrict every response to a fixed set of fields by default.

Include "success" in the list if you rely on the success flag for error checking — when fields is set, the API only returns the fields you ask for.

Source

pub fn with_security(self, enabled: bool) -> Self

Enable or disable the security block on every call by default.

Source

pub fn with_rate(self, enabled: bool) -> Self

Enable or disable the rate block on every call by default.

Source

pub fn with_timeout(self, timeout: Duration) -> Self

Set the per-request total timeout (default: 10 seconds).

Source

pub fn with_connect_timeout(self, timeout: Duration) -> Self

Set the connection timeout (default: 5 seconds).

Source

pub fn with_user_agent(self, ua: impl Into<String>) -> Self

Override the User-Agent header sent with every request.

Source

pub fn with_ssl(self, ssl: bool) -> Self

Toggle HTTPS. On by default; pass false to fall back to HTTP (e.g. in environments without an up-to-date CA bundle).

HTTPS is strongly recommended for production traffic — your API key is sent in the query string and would otherwise travel in clear text.

Source

pub async fn lookup(&self, ip: impl AsRef<str>) -> Result<LookupResponse, Error>

Look up a single IP address using the client’s default options.

Accepts any string-like value (&str, String, &String, Cow<str>, …). To look up the caller’s own public IP, use lookup_self.

§Example
let ipwhois = IpWhois::new();
let info = ipwhois.lookup("8.8.8.8").await?;
println!("{} — {}", info.ip.unwrap_or_default(), info.country.unwrap_or_default());

// String / &String also work, no `.as_str()` needed:
let owned = String::from("1.1.1.1");
let info = ipwhois.lookup(&owned).await?;
Source

pub async fn lookup_with( &self, ip: impl AsRef<str>, options: &Options, ) -> Result<LookupResponse, Error>

Look up a single IP address with per-call option overrides.

Per-call options always win over the client’s defaults.

Source

pub async fn lookup_self(&self) -> Result<LookupResponse, Error>

Look up the caller’s own public IP, using the client’s default options.

Equivalent to GET / against the API endpoint, as documented at https://ipwhois.io/documentation.

Source

pub async fn lookup_self_with( &self, options: &Options, ) -> Result<LookupResponse, Error>

Look up the caller’s own public IP with per-call option overrides.

Source

pub async fn bulk_lookup<I, S>( &self, ips: I, ) -> Result<Vec<LookupResponse>, Error>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Look up multiple IP addresses in a single request.

Uses the GET / comma-separated form of the bulk endpoint (https://ipwhois.io/documentation/bulk). Up to BULK_LIMIT addresses per call. Each address counts as one credit. Available on the Business and Unlimited plans only.

Per-IP errors come back inline with success = false for the affected entry; the rest of the batch is still usable. Whole-batch failures (network outage, bad API key, rate limit, …) surface as Err.

Accepts any iterable of string-like items, so &[&str], Vec<String>, &[String], an iterator chain, etc. all work.

Source

pub async fn bulk_lookup_with<I, S>( &self, ips: I, options: &Options, ) -> Result<Vec<LookupResponse>, Error>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Same as bulk_lookup with per-call option overrides.

Trait Implementations§

Source§

impl Clone for IpWhois

Source§

fn clone(&self) -> IpWhois

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for IpWhois

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IpWhois

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more