diff --git a/src/lib.rs b/src/lib.rs index 4c5977d..c217a04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,12 +2,27 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, ItemEnum, Lit, DeriveInput, Fields, Data}; +#[async_trait] +pub trait Queryable { + ///send the query types query and get the response returned as the + ///proper response type + pub fn send( + &self, + base_url: &str, + headers: Option>, + ) -> Result; +} + +pub trait Responsable { + pub fn receive(resp: Response) -> Result; +} + #[proc_macro_derive(HttpRequest, attributes(http_get))] pub fn derive_http_get_request(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = &input.ident; - // Parse #[http_get(url = "...")] attribute + // Extract the base URL from #[http_get(url = "...")] let mut base_url = None; for attr in &input.attrs { if attr.path().is_ident("http_get") { @@ -22,7 +37,6 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { }); } } - let base_url = base_url.expect("Missing #[http_get(url = \"...\")] attribute"); let expanded = match &input.data { @@ -46,22 +60,18 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { quote! { impl Queryable for #name { - pub async fn send( + fn send( &self, - client: std::sync::Arc, + base_url: &str, headers: Option>, - api_key: Option<&str>, - ) -> Result { + ) -> Result { use urlencoding::encode; + use awc::Client; let mut query_params: Vec<(String, String)> = Vec::new(); #(#query_param_code)* - if let Some(key) = api_key { - query_params.push(("api_key".to_string(), key.to_string())); - } - - let mut url = #base_url.to_string(); + let mut url = base_url.to_string(); if !query_params.is_empty() { let query_parts: Vec = query_params.iter() .map(|(k, v)| format!("{}={}", k, encode(v))) @@ -70,6 +80,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { url.push_str(&query_parts.join("&")); } + let client = Client::default(); let mut request = client.get(url); if let Some(hdrs) = headers { @@ -78,8 +89,18 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { } } - let response = request.send().await?; - Ok(response) + let response = actix_web::rt::System::new() + .block_on(async { request.send().await }) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; + + let body_bytes = actix_web::rt::System::new() + .block_on(async { response.body().await }) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; + + let body = Response::receive(body_bytes.to_vec()) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; + + Ok(body) } } } @@ -92,7 +113,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { match &variant.fields { Fields::Unnamed(fields) if fields.unnamed.len() == 1 => { variant_arms.push(quote! { - #name::#vname(inner) => inner.send(client.clone(), headers.clone(), api_key).await, + #name::#vname(inner) => inner.send(base_url, headers.clone()), }); } _ => panic!("#[derive(HttpRequest)] enum variants must have a single unnamed field"), @@ -100,13 +121,12 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream { } quote! { - impl #name { - pub async fn send( + impl Queryable for #name { + fn send( &self, - client: std::sync::Arc, + base_url: &str, headers: Option>, - api_key: Option<&str>, - ) -> Result { + ) -> Result { match self { #(#variant_arms)* }