Skip to main content

manual_endpoint/
manual_endpoint.rs

1use cdg_api::cdg_types::FormatType;
2use cdg_api::endpoints::{Endpoints, NewEndpoint};
3use cdg_api::param_models::GenericParams;
4use cdg_api::response_models::{
5    parse_response, serialize_response, DailyCongressionalRecordResponse, 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 API key from environment
13
14    // Assume there's an endpoint that doesn't have a specific response model
15    let endpoint = Endpoints::new_generic(
16        "daily-congressional-record".to_string(),
17        GenericParams::default().format(FormatType::Json).limit(250),
18    );
19
20    // Fetch the data as GenericResponse
21    let response: GenericResponse = client.fetch(endpoint)?;
22
23    // Parse the response into a generic JSON value if specific parsing fails
24    match parse_response::<DailyCongressionalRecordResponse, GenericResponse>(&response) {
25        Ok(json) => {
26            json.daily_congressional_record.iter().for_each(|records| {
27                let record = records.clone();
28                println!("Date: {}", unwrap_option_string(record.issue_date));
29                println!("Update Date: {}", unwrap_option_string(record.update_date));
30                println!("Volume: {}", record.volume_number.unwrap_or_default());
31                println!("Issue: {}", unwrap_option_string(record.issue_number));
32                println!("Sess. #: {}", record.session_number.unwrap_or_default());
33                println!("Congress: {}", record.congress.unwrap_or_default());
34                println!("URL: {}", unwrap_option_string(record.url));
35                println!();
36                println!("Full Issue: {:#?}", record.full_issue.unwrap_or_default());
37            });
38        }
39        Err(e) => {
40            println!("Failed to parse response: {}", e);
41            // Serialize the response as a generic JSON value, pretty-printed = true
42            println!("{}", serialize_response(&response, true)?);
43        }
44    }
45
46    Ok(())
47}