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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use chrono::Utc;
use flate2::Compression;
use http::{HttpTryFrom, Method};
use http::header::ACCEPT_CHARSET;
use http::header::CONTENT_ENCODING;
use http::header::CONTENT_TYPE;
use http::header::HeaderValue;
use http::request::Builder as RequestBuilder;
use hyper::{Body, Request};

use crate::body::IngestBody;
use crate::error::{RequestError, TemplateError};
use crate::params::Params;

/// A reusable template to generate requests from
#[derive(Debug)]
pub struct RequestTemplate {
    /// HTTP method, default is POST
    pub method: Method,
    /// Content charset, default is utf8
    pub charset: HeaderValue,
    /// Content type, default is application/json
    pub content: HeaderValue,
    /// Content encoding, default is gzip
    pub encoding: Encoding,
    /// Http schema, default is https
    pub schema: Schema,
    /// Host / domain, default is logs.logdna.com
    pub host: String,
    /// Ingest endpoint, default is /logs/ingest
    pub endpoint: String,
    /// Query parameters appended to the url
    pub params: Params,
    /// LogDNA ingestion key
    pub api_key: String,
}

impl RequestTemplate {
    /// Constructs a new TemplateBuilder
    pub fn builder() -> TemplateBuilder {
        TemplateBuilder::new()
    }
    /// Uses the template to create a new request
    pub fn new_request(&self, body: &IngestBody) -> Result<Request<Body>, RequestError> {
        let mut builder = RequestBuilder::new();

        let params = serde_urlencoded::to_string(self.params.clone().set_now(Utc::now().timestamp()))
            .expect("cant'fail!");

        builder.method(self.method.clone())
            .header(ACCEPT_CHARSET, self.charset.clone())
            .header(CONTENT_TYPE, self.content.clone())
            .header("apiKey", self.api_key.clone())
            .uri(self.schema.to_string() + &self.host + &self.endpoint + "?" + &params);

        self.encoding.set_builder_encoding(&mut builder);
        let body = body.as_http_body(&self.encoding)?;

        Ok(builder.body(body)?)
    }
}

/// Used to build an instance of a RequestTemplate
pub struct TemplateBuilder {
    method: Method,
    charset: HeaderValue,
    content: HeaderValue,
    encoding: Encoding,
    schema: Schema,
    host: String,
    endpoint: String,
    params: Option<Params>,
    api_key: Option<String>,
    err: Option<TemplateError>,
}

/// Represents the encoding to be used when sending an IngestRequest
#[derive(Debug, Clone)]
pub enum Encoding {
    Json,
    GzipJson(Compression),
}

impl TemplateBuilder {
    /// Constructs a new TemplateBuilder
    pub fn new() -> Self {
        Self {
            method: Method::POST,
            charset: HeaderValue::from_str("utf8").expect("charset::from_str()"),
            content: HeaderValue::from_str("application/json").expect("content::from_str()"),
            encoding: Encoding::GzipJson(Compression::new(2)),
            schema: Schema::Https,
            host: "logs.logdna.com".into(),
            endpoint: "/logs/ingest".into(),
            params: None,
            api_key: None,
            err: None,
        }
    }
    /// Set the method field
    pub fn method<T: Into<Method>>(&mut self, method: T) -> &mut Self {
        self.method = method.into();
        self
    }
    /// Set the charset field
    pub fn charset<T>(&mut self, charset: T) -> &mut Self
        where HeaderValue: HttpTryFrom<T>
    {
        self.charset = match HttpTryFrom::try_from(charset) {
            Ok(v) => v,
            Err(e) => {
                self.err = Some(TemplateError::InvalidHeader(e.into()));
                return self;
            }
        };
        self
    }
    /// Set the content field
    pub fn content<T>(&mut self, content: T) -> &mut Self
        where HeaderValue: HttpTryFrom<T>
    {
        self.content = match HttpTryFrom::try_from(content) {
            Ok(v) => v,
            Err(e) => {
                self.err = Some(TemplateError::InvalidHeader(e.into()));
                return self;
            }
        };
        self
    }
    /// Set the encoding field
    pub fn encoding<T: Into<Encoding>>(&mut self, encoding: T) -> &mut Self {
        self.encoding = encoding.into();
        self
    }
    /// Set the schema field
    pub fn schema<T: Into<Schema>>(&mut self, schema: T) -> &mut Self {
        self.schema = schema.into();
        self
    }
    /// Set the host field
    pub fn host<T: Into<String>>(&mut self, host: T) -> &mut Self {
        self.host = host.into();
        self
    }
    /// Set the endpoint field
    pub fn endpoint<T: Into<String>>(&mut self, endpoint: T) -> &mut Self {
        self.endpoint = endpoint.into();
        self
    }
    /// Set the api_key field
    pub fn api_key<T: Into<String>>(&mut self, api_key: T) -> &mut Self {
        self.api_key = Some(api_key.into());
        self
    }
    /// Set the params field
    pub fn params<T: Into<Params>>(&mut self, params: T) -> &mut Self {
        self.params = Some(params.into());
        self
    }
    /// Build a RequestTemplate using the current builder
    pub fn build(&mut self) -> Result<RequestTemplate, TemplateError> {
        Ok(RequestTemplate {
            method: self.method.clone(),
            charset: self.charset.clone(),
            content: self.content.clone(),
            encoding: self.encoding.clone(),
            schema: self.schema.clone(),
            host: self.host.clone(),
            endpoint: self.endpoint.clone(),
            params: self.params.clone()
                .ok_or(TemplateError::RequiredField("params is required in a TemplateBuilder".into()))?,
            api_key: self.api_key.clone()
                .ok_or(TemplateError::RequiredField("api_key is required in a TemplateBuilder".to_string()))?,
        })
    }
}

impl Encoding {
    fn set_builder_encoding<'a>(&self, builder: &'a mut RequestBuilder) -> &'a mut RequestBuilder {
        use crate::request::Encoding::*;

        match self {
            GzipJson(_) => builder.header(CONTENT_ENCODING, "gzip"),
            Json => builder,
        }
    }
}

/// Represents HTTP vs HTTPS for requests
#[derive(Debug, Clone)]
pub enum Schema {
    Http,
    Https,
}

impl Schema {
    fn to_string(&self) -> String {
        use crate::request::Schema::*;

        match self {
            Http => "http://".to_string(),
            Https => "https://".to_string(),
        }
    }
}