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
use crate::{Error, LegiscanProxy};
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetDatasetResponse {
    pub status: String,
    pub dataset: Dataset,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Dataset {
    pub state_id: i64,
    pub session_id: i64,
    pub session_name: String,
    pub dataset_hash: String,
    pub dataset_date: String,
    pub dataset_size: i64,
    pub mime_type: String,
    pub zip: String,
}

impl LegiscanProxy {
    /// Retrieve an individual dataset for a specific `session_id`
    // weekly
    pub async fn get_dataset(&self, session_id: i32, access_key: &str) -> Result<Dataset, Error> {
        let url = format!(
            "{base_url}?key={key}&op={operation}&id={session_id}&access_key={access_key}",
            base_url = self.base_url,
            key = self.api_key,
            operation = "getDataset",
            session_id = session_id,
            access_key = access_key
        );
        println!("{}", url);

        let response = self.client.get(url).send().await.unwrap();

        match crate::handle_legiscan_response(response).await {
            Ok(json) => {
                let json: GetDatasetResponse = serde_json::from_value(json).unwrap();
                Ok(json.dataset)
            }
            Err(e) => Err(e),
        }
    }
}

#[tokio::test]
#[ignore = "Access key changes to often to write a non-brittle test"]
async fn test_get_dataset() {
    let proxy = LegiscanProxy::new().unwrap();
    let dataset = proxy
        .get_dataset(1789, "55jFZUhExATO7PdWI5vJJS")
        .await
        .unwrap();
    assert_eq!(dataset.state_id, 6);
    assert_eq!(dataset.session_name, "2020 First Special Session");

    // Dataset doesnt exist
    let result = proxy.get_dataset(1234, "some-bad-key").await;
    assert!(matches!(result, Err(Error::Api(_))));
}