1 个稳定版本
1.0.0 | 2022年8月15日 |
---|
#204 在 电子邮件
16KB
278 行
ZeroBounce 电子邮件验证库用于 Rust
入门指南
您需要创建一个 zerobounce 账户 才能开始。
一旦您有了账户,您需要获取一个 API密钥,以便在API调用中使用。
安装
将以下内容添加到您的 [dependencies]
部分的 Cargo.toml
zerobounce= {版本= "1.0" }
使用方法
use std::net::{IpAddr, Ipv4Addr};
use zerobounce::{Api, ResponseType};
// Example function to validate an email address
async fn validate(api: &Api, email: &str) {
// do the validation using a custom IP Address
let ip_address = Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
// or use no IP Address
// let ip_address = None;
// validate email address
let result = api.validate(email, ip_address).await;
match result {
Err(error) => {
// this means we got an error during the http call or after
println!("Error: {}", error);
},
Ok(response) => {
// this means our http call was ok
match response {
ResponseType::Success(s) => {
// the API call ran okay, we have a response
println!("This email is: {}", s.status.as_str());
println!("Entire server response is: {:?}", s);
},
ResponseType::Error(e) => {
// The api returned some sort of error:
println!("The API response: {}", e.error);
}
}
}
}
}
// Example function to show available credits
async fn credits(api: &Api) {
// get remaining credits:
let response = api.get_credits().await;
match response {
Err(error) => {
// this means we got an error during the http call or after
println!("Error: {}", error);
},
Ok(response) => {
// this means our http call was ok
match response {
ResponseType::Success(s) => {
// the API call ran okay, we have a response
println!("You have {} credits left", s.get_credits());
},
ResponseType::Error(e) => {
// The api returned some sort of error:
println!("The API response: {}", e.error);
}
}
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// instantiate the api
let api = Api::new("your-api-key");
// output the result of validation call for a valid email address
validate(&api, "[email protected]").await;
// output the result of validation call for an invalid email address
validate(&api, "[email protected]").await;
// output the result of the get credits call
credits(&api).await;
Ok(())
}
测试
将您的API密钥设置在 ZEROBOUNCE_API_KEY
环境变量中,然后运行 cargo test
。
依赖项
~6–19MB
~273K SLoC