Skip to main content

generic_example/
generic_example.rs

1use cdg_api::cdg_types::FormatType;
2use cdg_api::endpoints::Endpoints;
3use cdg_api::param_models::BillListParams;
4use cdg_api::response_models::{
5    parse_response, serialize_response, BillsResponse, GenericResponse,
6};
7use cdg_api::{unwrap_option_string, CongressApiClient};
8
9use std::error::Error;
10
11fn main() -> Result<(), Box<dyn Error>> {
12    let client = CongressApiClient::new(None)?; // Use default API key
13
14    // Define parameters
15    let params = BillListParams::default().format(FormatType::Json).limit(10);
16
17    // Create the endpoint
18    let endpoint = Endpoints::BillList(params);
19
20    // Fetch the data
21    let response: GenericResponse = client.fetch(endpoint)?;
22
23    // Parse the response into a specific primary response,
24    // if it fails, it will just print the json response
25    let bill_list: BillsResponse = match parse_response(&response) {
26        Ok(bill_list) => bill_list,
27        Err(_) => {
28            println!("{}", serialize_response(&response, true)?);
29            return Ok(());
30        }
31    };
32
33    // Print the bill list
34    for bill in bill_list.bills {
35        println!("Bill: {}", unwrap_option_string(bill.bill_type));
36        println!("Title: {}", unwrap_option_string(bill.title));
37        println!("Number: {}", unwrap_option_string(bill.number));
38        println!(
39            "Origin Chamber: {}",
40            unwrap_option_string(bill.origin_chamber)
41        );
42        println!("Update Date: {}", unwrap_option_string(bill.update_date));
43        println!("URL: {}", unwrap_option_string(bill.url));
44        println!();
45    }
46
47    Ok(())
48}