Repository: gsquire/sendgrid-rs (language: rust)

Issue:
Return message id from v3 send API
I need to access the message id returned by the v3 send API, but couldn't see a way to do it. Am I missing something?

To get something working in the meantime, I ended up making [a branch](https://github.com/gsquire/sendgrid-rs/compare/master...philbooth:message-id?expand=1) that returns it instead of the response status, getting the value out of the `X-Message-Id` header. Is there some version of that change that would be worth me tidying up and opening as a PR?



Discussion / hints:
Thanks for opening this issue. I have debated returning the `reqwest::Resposne` type and just re-exporting it. This would give the user access to the headers and other parts of the response. Would this suffice? If so, I will make a PR soon.
> Would this suffice?

Yep, works for me!

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 52adb10..136825a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "sendgrid"
-version = "0.6.2"
+version = "0.7.0"
 authors = ["Garrett Squire <garrettsquire@gmail.com>"]
 description = "An unofficial client library for the SendGrid API"
 repository = "https://github.com/gsquire/sendgrid-rs"
@@ -17,4 +17,4 @@ reqwest = "0.8"
 serde = "1.0"
 serde_derive = "1.0"
 serde_json = "1.0"
-url = "1.6"
+url = "1.7"
diff --git a/src/sg_client.rs b/src/sg_client.rs
index 7f3189f..7b2a900 100644
--- a/src/sg_client.rs
+++ b/src/sg_client.rs
@@ -82,12 +82,18 @@ impl SGClient {
     pub fn send(self, mail_info: Mail) -> SendgridResult<String> {
         let client = Client::new();
         let mut headers = Headers::new();
-        headers.set(Authorization(Bearer { token: self.api_key.to_owned() }));
+        headers.set(Authorization(Bearer {
+            token: self.api_key.to_owned(),
+        }));
         headers.set(ContentType::form_url_encoded());
         headers.set(UserAgent::new("sendgrid-rs"));
 
         let post_body = make_post_body(mail_info)?;
-        let mut res = client.post(API_URL).headers(headers).body(post_body).send()?;
+        let mut res = client
+            .post(API_URL)
+            .headers(headers)
+            .body(post_body)
+            .send()?;
         let mut body = String::new();
         res.read_to_string(&mut body)?;
         Ok(body)
diff --git a/src/v3.rs b/src/v3.rs
index 89b2fe9..095d400 100644
--- a/src/v3.rs
+++ b/src/v3.rs
@@ -2,13 +2,15 @@ use errors::SendgridResult;
 
 use std::collections::HashMap;
 
-use reqwest::{Client, StatusCode};
 use reqwest::header::{Authorization, Bearer, ContentType, Headers, UserAgent};
+use reqwest::Client;
 
 use data_encoding::BASE64;
 
 use serde_json;
 
+pub use reqwest::Response;
+
 const V3_API_URL: &'static str = "https://api.sendgrid.com/v3/mail/send";
 
 /// Just a redefinition of a map to store string keys and values.
@@ -28,7 +30,7 @@ pub struct SGMailV3 {
     content: Vec<Content>,
     personalizations: Vec<Personalization>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     attachments: Option<Vec<Attachment>>,
 }
 
@@ -37,7 +39,7 @@ pub struct SGMailV3 {
 pub struct Email {
     email: String,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     name: Option<String>,
 }
 
@@ -55,25 +57,25 @@ pub struct Content {
 pub struct Personalization {
     to: Vec<Email>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     cc: Option<Vec<Email>>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     bcc: Option<Vec<Email>>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     subject: Option<String>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     headers: Option<SGMap>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     substitutions: Option<SGMap>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     custom_args: Option<SGMap>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     send_at: Option<u64>,
 }
 
@@ -86,24 +88,16 @@ pub struct Attachment {
 
     filename: String,
 
-    #[serde(rename = "type", skip_serializing_if = "check_encode")]
+    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
     mime_type: Option<String>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     disposition: Option<String>,
 
-    #[serde(skip_serializing_if = "check_encode")]
+    #[serde(skip_serializing_if = "Option::is_none")]
     content_id: Option<String>,
 }
 
-// Checks if a value in the V3 message should be added to the JSON or not.
-fn check_encode<T>(value: &Option<T>) -> bool {
-    match *value {
-        Some(_) => false,
-        None => true,
-    }
-}
-
 impl V3Sender {
     /// Construct a new V3 message sender.
     pub fn new(api_key: String) -> V3Sender {
@@ -111,16 +105,18 @@ impl V3Sender {
     }
 
     /// Send a V3 message and return the status code or an error from the request.
-    pub fn send(&self, mail: &SGMailV3) -> SendgridResult<StatusCode> {
+    pub fn send(&self, mail: &SGMailV3) -> SendgridResult<Response> {
         let client = Client::new();
         let mut headers = Headers::new();
-        headers.set(Authorization(Bearer { token: self.api_key.to_owned() }));
+        headers.set(Authorization(Bearer {
+            token: self.api_key.to_owned(),
+        }));
         headers.set(ContentType::json());
         headers.set(UserAgent::new("sendgrid-rs"));
 
         let body = mail.gen_json();
         let res = client.post(V3_API_URL).headers(headers).body(body).send()?;
-        Ok(res.status())
+        Ok(res)
     }
 }
 


Test patch: