dragonfly_client_rs/client/
methods.rs

1use super::{models, ScanResultSerializer};
2
3use crate::APP_CONFIG;
4use reqwest::blocking::Client;
5
6pub fn fetch_access_token(http_client: &Client) -> reqwest::Result<models::AuthResponse> {
7    let url = format!("https://{}/oauth/token", APP_CONFIG.auth0_domain);
8    let json_body = models::AuthBody {
9        client_id: &APP_CONFIG.client_id,
10        client_secret: &APP_CONFIG.client_secret,
11        audience: &APP_CONFIG.audience,
12        grant_type: &APP_CONFIG.grant_type,
13        username: &APP_CONFIG.username,
14        password: &APP_CONFIG.password,
15    };
16
17    http_client
18        .post(url)
19        .json(&json_body)
20        .send()?
21        .error_for_status()?
22        .json()
23}
24
25pub fn fetch_bulk_job(
26    http_client: &Client,
27    access_token: &str,
28    n_jobs: usize,
29) -> reqwest::Result<Vec<models::Job>> {
30    http_client
31        .post(format!("{}/jobs", APP_CONFIG.base_url))
32        .header("Authorization", format!("Bearer {access_token}"))
33        .query(&[("batch", n_jobs)])
34        .send()?
35        .error_for_status()?
36        .json()
37}
38
39pub fn fetch_rules(
40    http_client: &Client,
41    access_token: &str,
42) -> reqwest::Result<models::RulesResponse> {
43    http_client
44        .get(format!("{}/rules", APP_CONFIG.base_url))
45        .header("Authorization", format!("Bearer {access_token}"))
46        .send()?
47        .error_for_status()?
48        .json()
49}
50
51pub fn send_result(
52    http_client: &Client,
53    access_token: &str,
54    body: models::ScanResult,
55) -> reqwest::Result<()> {
56    let body: ScanResultSerializer = body.into();
57    http_client
58        .put(format!("{}/package", APP_CONFIG.base_url))
59        .header("Authorization", format!("Bearer {access_token}"))
60        .json(&body)
61        .send()?
62        .error_for_status()?;
63
64    Ok(())
65}