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
use crate::error::VtError;
use reqwest::{blocking::multipart::Form, blocking::Client, StatusCode};

/// GET from a URL
pub(crate) fn http_get(api_key: &str, user_agent: &str, url: &str) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .get(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .send()
        .unwrap();
    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// GET from a URL with query params
#[cfg(feature = "enterprise")]
pub(crate) fn http_get_with_params(
    api_key: &str,
    user_agent: &str,
    url: &str,
    query_params: &[(&str, &str)],
) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .get(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .query(query_params)
        .send()
        .unwrap();
    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// POST to a URL
pub(crate) fn http_post(
    api_key: &str,
    user_agent: &str,
    url: &str,
    form_data: &[(&str, &str)],
) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .post(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .form(form_data)
        .send()?;

    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// POST to a URL with multipart form_data
pub(crate) fn http_multipart_post(
    api_key: &str,
    user_agent: &str,
    url: &str,
    form_data: Form,
) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .post(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .multipart(form_data)
        .send()?;

    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// POST to a URL with data in the body
#[cfg(feature = "enterprise")]
pub(crate) fn http_body_post(
    api_key: &str,
    user_agent: &str,
    url: &str,
    data: String,
) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .post(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .body(data)
        .send()?;

    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// DELETE
#[cfg(feature = "enterprise")]
pub(crate) fn http_delete(api_key: &str, user_agent: &str, url: &str) -> Result<String, VtError> {
    let client = Client::builder().user_agent(user_agent).build().unwrap();
    let resp = client
        .delete(url)
        .header("x-apikey", api_key)
        .header("Accept", "application/json")
        .send()
        .unwrap();
    let status = resp.status();
    let text = resp.text().unwrap();

    match status {
        StatusCode::OK => Ok(text), // 200
        _ => Err(error_from_status(status, &text)),
    }
}

/// Return the VtError based on the http status code
fn error_from_status(status_code: StatusCode, resp_text: &str) -> VtError {
    match status_code {
        StatusCode::BAD_REQUEST => {
            if resp_text.contains("BadRequestError") {
                VtError::BadRequestError
            } else if resp_text.contains("InvalidArgumentError") {
                VtError::InvalidArgumentError
            } else if resp_text.contains("NotAvailableYet") {
                VtError::NotAvailableYet
            } else if resp_text.contains("UnselectiveContentQueryError") {
                VtError::UnselectiveContentQueryError
            } else {
                VtError::UnsupportedContentQueryError
            }
        } // 400
        StatusCode::UNAUTHORIZED => {
            if resp_text.contains("AuthenticationRequiredError") {
                VtError::AuthenticationRequiredError
            } else if resp_text.contains("UserNotActiveError") {
                VtError::UserNotActiveError
            } else {
                VtError::WrongCredentialsError
            }
        } // 401
        StatusCode::FORBIDDEN => VtError::ForbiddenError, // 403
        StatusCode::NOT_FOUND => VtError::NotFoundError,  // 404
        StatusCode::CONFLICT => VtError::AlreadyExistsError, // 409
        StatusCode::FAILED_DEPENDENCY => VtError::FailedDependencyError, // 424
        StatusCode::TOO_MANY_REQUESTS => {
            if resp_text.contains("QuotaExceededError") {
                VtError::QuotaExceededError
            } else {
                VtError::TooManyRequestsError
            }
        } // 429
        StatusCode::SERVICE_UNAVAILABLE => VtError::TransientError, // 503
        StatusCode::GATEWAY_TIMEOUT => VtError::DeadlineExceededError, // 504
        _ => VtError::Unknown,
    }
}