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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use log::{debug, info};
use serde::{Deserialize, Serialize};
use serde_yaml;
use std::{
    collections::{HashMap, HashSet},
    fs, io,
    path::PathBuf,
};

use thiserror::Error;

mod template;

pub use template::Template;

#[derive(Error, Debug)]
pub enum RequestrError {
    #[error("Following parameter are missing from the input: {0:#?}")]
    MissingParameter(Vec<String>),
    #[error("Unable to load Template from {0}")]
    OpeningTemplateFailed(String, io::Error),
    #[error("Parsing template failed")]
    TemplateParsingFailed(#[from] serde_yaml::Error),
    #[error("Wrong request config: {0}")]
    BrokenRequestConfig(String),
    #[error("Request failed")]
    UnknownRequestError(#[from] reqwest::Error),
    #[error("Unknown Requestr Error")]
    Unknown,
}

pub type ResultT<T> = Result<T, RequestrError>;

fn default_header() -> HashMap<String, String> {
    HashMap::new()
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct RequestConfig {
    pub url: String,
    #[serde(default = "default_header")]
    pub header: HashMap<String, String>,
    pub body: Option<String>,
    pub method: Option<String>,
}

pub fn load_request_template(filename: &PathBuf) -> ResultT<Template> {
    let contents = match fs::read_to_string(filename) {
        Ok(contents) => contents,
        Err(err) => {
            return Err(RequestrError::OpeningTemplateFailed(
                filename.to_string_lossy().to_string(),
                err,
            ))
        }
    };

    let request_config_template = Template::new(contents.as_str());
    debug!("{:#?}", request_config_template);

    Ok(request_config_template)
}

pub fn validate_parameter(template: &Template, parameter: &HashMap<String, String>) -> ResultT<()> {
    debug!("{:#?}", parameter);

    let provided_names: HashSet<_> = parameter.keys().cloned().collect();
    let names: HashSet<_> = template.names.iter().cloned().collect();

    let from_input: HashSet<_> = provided_names.difference(&names).collect();
    let from_template: HashSet<_> = names.difference(&provided_names).collect();

    if from_input.len() > 0 {
        info!(
            "Following parameters are defined but not used: {:?}",
            from_input
        );
    }

    if from_template.len() > 0 {
        Err(
            RequestrError::MissingParameter(from_template.into_iter().map(|p| p.clone()).collect())
                .into(),
        )
    } else {
        Ok(())
    }
}

pub fn load_request_definition(
    template: &Template,
    parameter: &HashMap<String, String>,
) -> ResultT<RequestConfig> {
    let request_config_string = template.render(parameter);

    let request_config: RequestConfig = serde_yaml::from_str(request_config_string.as_str())?;

    debug!("{:#?}", request_config);

    Ok(request_config)
}

pub fn make_request(
    url: &str,
    body: Option<String>,
    method: Option<String>,
    header: HashMap<String, String>,
) -> ResultT<String> {
    let client = reqwest::blocking::Client::new();

    let request_builder = match method.unwrap_or("GET".to_string()).to_uppercase().as_str() {
        "DELETE" => client.delete(url),
        "GET" => client.get(url),
        "POST" => client.post(url),
        "PUT" => client.put(url),
        "PATCH" => client.put(url),
        method => {
            return Err(RequestrError::BrokenRequestConfig(format!(
                "Unknown http method: {}",
                method
            )))
        }
    };

    let request_builder = match body {
        Some(body) => request_builder.body(body),
        None => request_builder,
    };

    let request_builder = header
        .into_iter()
        .fold(request_builder, |request_builder, (name, value)| {
            request_builder.header(name.as_str(), value.as_str())
        });

    let response = request_builder.send()?;

    debug!("{:#?}", response);

    let response_body = response.text()?;

    Ok(response_body)
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}