Expand description
§request_handlers Module
The request_handlers module provides utility functions for interacting with the US Congress API.
It includes methods for fetching data via HTTP requests and processing responses using external tools.
§Usage
use cdg_api::request_handlers::{get_congress_data, curl_and_jq};
use cdg_api::response_models::BillsResponse;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = "https://api.congress.gov/v3/bill/?format=json&limit=10&api_key=YOUR_API_KEY";
// Fetch and deserialize data
// Note:
// In this example it will fail because the api key is not valid
// otherwise it will return the data and attempt to deserialize it
// into the BillsResponse struct
let bills: BillsResponse = match get_congress_data(url) {
Ok(data) => data,
Err(err) => {
eprintln!("Error: {}", err);
BillsResponse::default()
}
};
// Fetch and process data with jq
// Note:
// In this example it will fail because the api key is not valid
// otherwise it will return the data and process it with jq
// using the provided filter
match curl_and_jq(url, ".bills[] | {number, title}") {
Ok(_) => println!("Data processed successfully."),
Err(err) => eprintln!("Error: {}", err),
}
Ok(())
}Functions§
- curl_
and_ jq - Executes a
curlrequest to the given URL and processes the JSON output withjq. - get_
congress_ data - Fetches data from the US Congress API and deserializes it into the specified response model.