1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
macro_rules! execute { ($ty:tt) => { #[async_trait] impl<'a> Execute for $ty<'a> { fn request<U>( &self, method: Method, url: U, body: Option<String>, ) -> Result<hyper::Request<hyper::Body>, TwilioErr> where U: AsRef<str>, { use hyper::{ header::{HeaderMap, HeaderValue, CONTENT_TYPE}, Request, }; use typed_headers::HeaderMapExt; const BASE: &str = "https://api.twilio.com/2010-04-01/Accounts"; let url = format!("{}/{}/{}", BASE, self.client.sid, url.as_ref()) .parse::<hyper::Uri>()?; let mut request = Request::builder().method(method).uri(url); let mut hmap = HeaderMap::new(); hmap.typed_insert(&self.client.auth); for (k, v) in hmap { request = request.header(k.unwrap().as_str(), v); } Ok(match body { Some(body) => request .header( CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded"), ) .body(hyper::Body::from(body))?, None => request.body(hyper::Body::empty())?, }) } async fn execute<U, D>( &self, method: Method, url: U, body: Option<String>, ) -> TwilioResp<crate::TwilioJson<D>> where U: AsRef<str> + Send, D: for<'de> serde::Deserialize<'de>, { use bytes::buf::ext::BufExt; use serde_json; let req = self.request(method, url, body)?; let res = self .client .client .request(req) .await .map_err(TwilioErr::NetworkErr)?; let body = hyper::body::aggregate(res).await?; let json_resp = serde_json::from_reader(body.reader())?; Ok(json_resp) } } }; } macro_rules! from { ($x:ty, $variant:ident) => { impl From<$x> for TwilioErr { fn from(e: $x) -> Self { $variant(e) } } }; } macro_rules! pair { ($x:ident, $field:ident, $name:tt, $vec:ident) => { if let Some($field) = $x.$field { $vec.push(($name, $field)); } }; }