Skip to content

Commit b2ab2ed

Browse files
Update to support for v2 API.
1 parent a74733a commit b2ab2ed

4 files changed

Lines changed: 93 additions & 50 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mailboxvalidator"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
authors = ["MailboxValidator <support@mailboxvalidator.com>"]
55
edition = "2021"
66
description = "Email verification package for Rust using MailboxValidator API. It validates if the email is valid, from a free provider, contains high-risk keywords, whether it's a catch-all address and so much more."
@@ -11,6 +11,7 @@ repository = "https://github.com/MailboxValidator/mailboxvalidator-rust/"
1111
license = "MIT"
1212
keywords = ["email", "validation", "validator", "free", "disposable"]
1313
categories = ["email", "accessibility"]
14+
readme = "readme.md"
1415

1516
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1617

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 MailboxValidator.com
3+
Copyright (c) 2023 MailboxValidator.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

readme.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This module can be useful in many types of projects, for example
1111

1212
# Installation
1313

14-
Just add `mailboxvalidator = "1.0.0"` into your *Cargo.toml*.
14+
Just add `mailboxvalidator = "1.1.0"` into your *Cargo.toml*.
1515

1616
# Dependencies
1717

@@ -90,8 +90,7 @@ let validation_result = mailboxvalidator::validate_email("example@example.com",P
9090
match validation_result {
9191
Ok(num) => {
9292
let ok_result = num;
93-
assert_eq!(ok_result["status"], "False");
94-
assert_eq!(ok_result["error_code"], "");
93+
println!("{:#?}", ok_result);
9594
},
9695
Err(err) => println!("{:#?}", err),
9796
};
@@ -107,8 +106,7 @@ let validation_result = mailboxvalidator::is_disposable_email("example@example.c
107106
match validation_result {
108107
Ok(num) => {
109108
let ok_result = num;
110-
assert_eq!(ok_result["is_disposable"], "True");
111-
assert_eq!(ok_result["error_code"], "");
109+
println!("{:#?}", ok_result);
112110
},
113111
Err(err) => println!("{:#?}", err),
114112
};
@@ -124,8 +122,7 @@ let validation_result = mailboxvalidator::is_free_email("example@example.com",PA
124122
match validation_result {
125123
Ok(num) => {
126124
let ok_result = num;
127-
assert_eq!(ok_result["is_free"], "False");
128-
assert_eq!(ok_result["error_code"], "");
125+
println!("{:#?}", ok_result);
129126
},
130127
Err(err) => println!("{:#?}", err),
131128
};

src/lib.rs

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@
3535
use serde::Deserialize;
3636
use serde::Serialize;
3737

38+
use reqwest::StatusCode;
39+
3840
pub use reqwest::Error as ReqError;
3941

42+
// #[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
43+
// pub enum ALLELE {
44+
// bool(bool),
45+
// null(null),
46+
// }
47+
4048
///! Wrapper result type returning `reqwest` errors
4149
pub type MailboxValidatorResult<T> = Result<T, ReqError>;
4250

@@ -45,46 +53,53 @@ pub type MailboxValidatorResult<T> = Result<T, ReqError>;
4553
pub struct SingleEmailValidationRecord {
4654
email_address: String,
4755
domain: String,
48-
is_free: String,
49-
is_syntax: String,
50-
is_domain: String,
51-
is_smtp: String,
52-
is_verified: String,
53-
is_server_down: String,
54-
is_greylisted: String,
55-
is_disposable: String,
56-
is_suppressed: String,
57-
is_role: String,
58-
is_high_risk: String,
59-
is_catchall: String,
60-
status: String,
61-
error_code: String,
62-
error_message: String,
63-
mailboxvalidator_score: String,
64-
time_taken: String,
56+
is_free: Option<bool>,
57+
is_syntax: Option<bool>,
58+
is_domain: Option<bool>,
59+
is_smtp: Option<bool>,
60+
is_verified: Option<bool>,
61+
is_server_down: Option<bool>,
62+
is_greylisted: Option<bool>,
63+
is_disposable: Option<bool>,
64+
is_suppressed: Option<bool>,
65+
is_role: Option<bool>,
66+
is_high_risk: Option<bool>,
67+
is_catchall: Option<bool>,
68+
status: Option<bool>,
69+
mailboxvalidator_score: f64,
70+
time_taken: f64,
6571
credits_available: i64,
6672
}
6773

6874
/// MailboxValidator Disposable Email API result record.
6975
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
7076
pub struct DisposableEmailRecord {
7177
email_address: String,
72-
is_disposable: String,
73-
error_code: String,
74-
error_message: String,
78+
is_disposable: Option<bool>,
7579
credits_available: i64,
7680
}
7781

7882
/// MailboxValidator Free Email API result record.
7983
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
8084
pub struct FreeEmailRecord {
8185
email_address: String,
82-
is_free: String,
83-
error_code: String,
84-
error_message: String,
86+
is_free: Option<bool>,
8587
credits_available: i64,
8688
}
8789

90+
/// MailboxValidator Error object
91+
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
92+
pub struct ErrorRecord {
93+
error: ErrorRecord1,
94+
}
95+
96+
/// MailboxValidator Error Response object
97+
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
98+
pub struct ErrorRecord1 {
99+
error_code: i64,
100+
error_message: String,
101+
}
102+
88103
/// Validates email address using MailboxValidator Single Validation API.
89104
///
90105
/// # Examples
@@ -108,17 +123,27 @@ pub struct FreeEmailRecord {
108123
pub fn validate_email(email_address: &str, apikey: &str) -> MailboxValidatorResult<serde_json::value::Value> {
109124
let client = reqwest::blocking::Client::new();
110125
let url = format!(
111-
"https://api.mailboxvalidator.com/v1/validation/single?email={}&key={}&format=json",
126+
"https://api.mailboxvalidator.com/v2/validation/single?email={}&key={}&format=json&source=sdk-rust-mbv",
112127
email_address, apikey
113128
);
114129
let res = client
115130
.get(url)
116-
.send()?
117-
.error_for_status()?;
131+
.send()?;
118132

119-
let parsed: SingleEmailValidationRecord = res.json()?;
120-
let json_value = serde_json::json!(&parsed);
121-
Ok(json_value)
133+
if res.status() == StatusCode::OK {
134+
let parsed: SingleEmailValidationRecord = res.json()?;
135+
let json_value = serde_json::json!(&parsed);
136+
return Ok(json_value);
137+
} else if (res.status() == StatusCode::BAD_REQUEST) || (res.status() == StatusCode::UNAUTHORIZED) {
138+
let parsed: ErrorRecord = res.json()?;
139+
let json_value = serde_json::json!(&parsed);
140+
return Ok(json_value);
141+
} else {
142+
println!("Something else happened. Status: {:?}", res.status());
143+
}
144+
145+
// Ok(())
146+
Ok(().into())
122147
}
123148

124149
/// Validates email address using MailboxValidator Disposable Email API.
@@ -144,17 +169,27 @@ pub fn validate_email(email_address: &str, apikey: &str) -> MailboxValidatorResu
144169
pub fn is_disposable_email(email_address: &str, apikey: &str) -> MailboxValidatorResult<serde_json::value::Value> {
145170
let client1 = reqwest::blocking::Client::new();
146171
let url = format!(
147-
"https://api.mailboxvalidator.com/v1/email/disposable?email={}&key={}&format=json",
172+
"https://api.mailboxvalidator.com/v2/email/disposable?email={}&key={}&format=json&source=sdk-rust-mbv",
148173
email_address, apikey
149174
);
150175
let res = client1
151176
.get(url)
152-
.send()?
153-
.error_for_status()?;
177+
.send()?;
178+
179+
if res.status() == StatusCode::OK {
180+
let parsed: DisposableEmailRecord = res.json()?;
181+
let json_value = serde_json::json!(&parsed);
182+
return Ok(json_value);
183+
} else if (res.status() == StatusCode::BAD_REQUEST) || (res.status() == StatusCode::UNAUTHORIZED) {
184+
let parsed: ErrorRecord = res.json()?;
185+
let json_value = serde_json::json!(&parsed);
186+
return Ok(json_value);
187+
} else {
188+
println!("Something else happened. Status: {:?}", res.status());
189+
}
154190

155-
let parsed: DisposableEmailRecord = res.json()?;
156-
let json_value = serde_json::json!(&parsed);
157-
Ok(json_value)
191+
// Ok(())
192+
Ok(().into())
158193
}
159194

160195
/// Validates email address using MailboxValidator Free Email API.
@@ -180,15 +215,25 @@ pub fn is_disposable_email(email_address: &str, apikey: &str) -> MailboxValidato
180215
pub fn is_free_email(email_address: &str, apikey: &str) -> MailboxValidatorResult<serde_json::value::Value> {
181216
let client = reqwest::blocking::Client::new();
182217
let url = format!(
183-
"https://api.mailboxvalidator.com/v1/email/free?email={}&key={}&format=json",
218+
"https://api.mailboxvalidator.com/v2/email/free?email={}&key={}&format=json&source=sdk-rust-mbv",
184219
email_address, apikey
185220
);
186221
let res = client
187222
.get(url)
188-
.send()?
189-
.error_for_status()?;
223+
.send()?;
224+
225+
if res.status() == StatusCode::OK {
226+
let parsed: FreeEmailRecord = res.json()?;
227+
let json_value = serde_json::json!(&parsed);
228+
return Ok(json_value);
229+
} else if (res.status() == StatusCode::BAD_REQUEST) || (res.status() == StatusCode::UNAUTHORIZED) {
230+
let parsed: ErrorRecord = res.json()?;
231+
let json_value = serde_json::json!(&parsed);
232+
return Ok(json_value);
233+
} else {
234+
println!("Something else happened. Status: {:?}", res.status());
235+
}
190236

191-
let parsed: FreeEmailRecord = res.json()?;
192-
let json_value = serde_json::json!(&parsed);
193-
Ok(json_value)
237+
// Ok(())
238+
Ok(().into())
194239
}

0 commit comments

Comments
 (0)