pub struct Ipify {
pub t: Op,
pub endp: String,
}Expand description
The main API struct
This struct represents a client for interacting with the Ipify API. It allows users to configure and perform operations for retrieving their public IP addresses, either in plain text or JSON format.
§Fields
t- The current operation to perform (e.g., IPv4, IPv6, JSON outputs).endp- The API endpoint used for the operation.
§Examples
Basic usage:
use ipify_rs::{Ipify, Op};
let mut client = Ipify::new();
client = client.set(Op::IPv4);
let ip = client.call().unwrap();
println!("Public IPv4 address: {}", ip);Using the default settings (IPv6):
use ipify_rs::Ipify;
let ip = Ipify::new().call().unwrap();
println!("Public IPv6 address: {}", ip);Fields§
§t: OpCurrent type of operation
endp: StringEndpoint, different for every operation
Implementations§
Source§impl Ipify
API Implementation
impl Ipify
API Implementation
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new API instance client with the defaults
§Example:
use ipify_rs::*;
let a = Ipify::new();
println!("{}", a.call().unwrap());Sourcepub fn set(&self, op: Op) -> Self
pub fn set(&self, op: Op) -> Self
Specify the subsequent operation to perform on call()
§Example:
use ipify_rs::{Ipify, Op};
let mut a = Ipify::new();
a.set(Op::IPv6J);
println!("{}", a.call()?);Sourcepub fn call(self) -> Result<String>
pub fn call(self) -> Result<String>
Actually perform the API call
§Example:
use ipify_rs::Ipify;
let r = Ipify::new().call()?;
println!("my ip = {}", r);Sourcepub async fn call_async(self) -> Result<String>
pub async fn call_async(self) -> Result<String>
Perform the API call asynchronously to retrieve the IP address.
This function communicates with the configured Ipify endpoint, sending an
HTTP GET request and retrieving the response body as a string. The result of
the call is typically a public IP address of the client in either plain text
or JSON format, based on the selected operation (Op).
§Example
use ipify_rs::Ipify;
let ip = Ipify::new().call_async().await?;
println!("My public IP address: {}", ip);§Errors
This function will panic if:
- The HTTP client fails to build properly (e.g., invalid user-agent).
- The GET request to the endpoint fails (e.g., network error or invalid endpoint).
- The response cannot be transformed into a plain string (e.g., invalid encoding).
To avoid panics, consider handling errors explicitly by using a custom implementation that propagates errors instead of unwrapping results.