Git Product home page Git Product logo

zerobounce-rust's Introduction

ZeroBounce Email Verification Library for Rust

Getting Started

You will need a zerobounce account to get started.
Once you get an account, you will need to get an api key to use it in the API calls.

Installation

Add below to your [dependencies] section of Cargo.toml
zerobounce = { version = "1.0" }

Usage

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(())
}

Testing

Set your api key in the ZEROBOUNCE_API_KEY environment variable, then run cargo test.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.