Skip to main content

Module request_handlers

Module request_handlers 

Source
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 curl request to the given URL and processes the JSON output with jq.
get_congress_data
Fetches data from the US Congress API and deserializes it into the specified response model.