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
use super::{models, ScanResultSerializer};

use crate::APP_CONFIG;
use reqwest::blocking::Client;

pub fn fetch_access_token(http_client: &Client) -> reqwest::Result<models::AuthResponse> {
    let url = format!("https://{}/oauth/token", APP_CONFIG.auth0_domain);
    let json_body = models::AuthBody {
        client_id: &APP_CONFIG.client_id,
        client_secret: &APP_CONFIG.client_secret,
        audience: &APP_CONFIG.audience,
        grant_type: &APP_CONFIG.grant_type,
        username: &APP_CONFIG.username,
        password: &APP_CONFIG.password,
    };

    http_client
        .post(url)
        .json(&json_body)
        .send()?
        .error_for_status()?
        .json()
}

pub fn fetch_bulk_job(
    http_client: &Client,
    access_token: &str,
    n_jobs: usize,
) -> reqwest::Result<Vec<models::Job>> {
    http_client
        .post(format!("{}/jobs", APP_CONFIG.base_url))
        .header("Authorization", format!("Bearer {access_token}"))
        .query(&[("batch", n_jobs)])
        .send()?
        .error_for_status()?
        .json()
}

pub fn fetch_rules(
    http_client: &Client,
    access_token: &str,
) -> reqwest::Result<models::RulesResponse> {
    http_client
        .get(format!("{}/rules", APP_CONFIG.base_url))
        .header("Authorization", format!("Bearer {access_token}"))
        .send()?
        .error_for_status()?
        .json()
}

pub fn send_result(
    http_client: &Client,
    access_token: &str,
    body: models::ScanResult,
) -> reqwest::Result<()> {
    let body: ScanResultSerializer = body.into();
    http_client
        .put(format!("{}/package", APP_CONFIG.base_url))
        .header("Authorization", format!("Bearer {access_token}"))
        .json(&body)
        .send()?
        .error_for_status()?;

    Ok(())
}