handlebars_misc_helpers/
http_helpers.rs

1#[cfg(feature = "http_attohttpc")]
2use attohttpc;
3use handlebars::{handlebars_helper, Handlebars};
4#[cfg(all(feature = "http_reqwest", not(feature = "http_attohttpc")))]
5use reqwest;
6
7#[cfg(feature = "http_attohttpc")]
8fn http_get_fct<T: AsRef<str>>(url: T) -> Result<String, attohttpc::Error> {
9    attohttpc::get(url.as_ref()).send()?.text()
10}
11
12#[cfg(all(feature = "http_reqwest", not(feature = "http_attohttpc")))]
13fn http_get_fct<T: AsRef<str>>(url: T) -> Result<String, reqwest::Error> {
14    reqwest::blocking::get(url.as_ref())?.text()
15}
16
17#[cfg(any(feature = "http_reqwest", feature = "http_attohttpc"))]
18pub fn register(handlebars: &mut Handlebars) {
19    {
20        handlebars_helper!(http_get: |v: str| http_get_fct(v).map_err(crate::to_nested_error)?);
21        handlebars.register_helper("http_get", Box::new(http_get))
22    }
23    {
24        handlebars_helper!(gitignore_io: |v: str| http_get_fct(format!("https://www.gitignore.io/api/{}", v)).map_err(crate::to_nested_error)?);
25        handlebars.register_helper("gitignore_io", Box::new(gitignore_io))
26    }
27}
28
29// #[cfg(test)]
30// mod tests {
31//     use super::*;
32//     // use crate::tests::assert_renders;
33//     use pretty_assertions::assert_eq;
34//     use std::error::Error;
35
36//     #[test]
37//     fn try_http_get_fct() -> Result<(), Box<dyn Error>> {
38//         assert_eq!(http_get_fct("https://www.gitignore.io/api/text")?,
39//             r#"
40// # Created by https://www.toptal.com/developers/gitignore/api/text
41// # Edit at https://www.toptal.com/developers/gitignore?templates=text
42
43// ### Text ###
44// *.doc
45// *.docx
46// *.log
47// *.msg
48// *.pages
49// *.rtf
50// *.txt
51// *.wpd
52// *.wps
53
54// # End of https://www.toptal.com/developers/gitignore/api/text
55// "#
56//             .to_string(),
57//         );
58//         Ok(())
59//     }
60// }