#gov #serde #document #congress #api #api-bindings #api-request

loc_api

A simple library to interact with the loc.gov API

12 releases (stable)

1.0.8 Feb 5, 2025
1.0.7 Nov 4, 2024
1.0.4 Oct 31, 2024
0.1.2 Oct 29, 2024

#350 in Web programming

Download history 776/week @ 2024-10-26 227/week @ 2024-11-02 20/week @ 2024-11-09 4/week @ 2024-11-16 30/week @ 2024-12-07 120/week @ 2025-02-01 28/week @ 2025-02-08

148 downloads per month

Custom license

87KB
1K SLoC

loc-api

Crates.io MIT License

loc-api is a Rust library that provides an interface for interacting with the Library of Congress (LOC) APIs. It simplifies the process of constructing API requests, managing parameters, and handling responses, enabling developers to seamlessly integrate LOC data into their Rust applications.

Work in progress, not everything is implemented yet, however, the library is functional and can be used to interact with the LOC API. Check out the examples below to see how to use the library. The included examples directory within the repository contains a few more examples as well.

Please report any issues or bugs, I'm sure there are many. I'm also open to suggestions and contributions.

Installation

Add loc-api to your project's Cargo.toml:

[dependencies]
loc-api = "1.0.8"

OR

You can use cargo add to add the dependency to your Cargo.toml:

cargo add loc-api

This library depends on reqwest, serde and serde_json for making HTTP requests and serializing/deserializing JSON data, respectively:

[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Examples

Creating an API Client

First, initialize the ApiClient. You can optionally set the LOC_API_BASE_URL environment variable to override the default LOC API base URL. Other methods of setting the base URL include using the ApiClient::with_base_url constructor or directly modifying the loc_client::DEFAULT_BASE_URL constant.

use loc_api::loc_client::ApiClient;

fn main() {
    let client = ApiClient::new();
    // Now you can use [`client`] to interact with the LOC APIs
}

Using the modules directly to construct a URL for querying specific types of MediaType

use loc_api::{endpoints::*, param_models::*, format_models::*, attribute_models::*,
response_models::*};

fn main() -> Result<(), Box<dyn std::error::Error>> {
   let common_params = CommonParams {
       format: Format::Json.into(),
       attributes: AttributesSelect {
           include: vec!["pagination".to_string(), "results".to_string()],
           exclude: vec![],
       }.into(),
       query: "dog".to_string().into(),
       filter: FacetReq {
           filters: vec![Facet::Subject {value: "animals".to_string()}]
       }.into(),
       per_page: 25.into(),
       page: 1.into(),
       sort: SortField::TitleS.into(),
   };
   
   let format_endpoint = Endpoints::Format {
       format: MediaType::FilmAndVideos,
       params: common_params,
   };
   
   let url = format_endpoint.to_url().unwrap();
    
   println!("{}", url);

   Ok(())
}

Using the loc_client module to make a search request

use loc_api::loc_client::ApiClient;
use loc_api::param_models::{Facet, FacetReq};
use loc_api::attribute_models::AttributesSelect;
use loc_api::attribute_models::SortField;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ApiClient::new();
    let response = client.search(
        "baseball",
        false,
        AttributesSelect {
            include: vec!["pagination".to_string(), "results".to_string()],
            exclude: vec![],
        }.into(),
        FacetReq { filters: vec![Facet::Subject {value: "sports".to_string()}] }.into(),
        25.into(),
        1.into(),
        SortField::DateDesc.into(),
    )?;

    println!("url: {}", response.1);

    // Handle the search results
    if let Some(results) = response.0.results {
        for item in results {
            println!("{:#?}", item);
        }
    }

    Ok(())
}
  • **[cdg_api]: A Rust library for interacting with the Congress.gov API.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Repository

t-fbd

Acknowledgements

The data is sourced from the U.S. Library of Congress.

Contact

For questions or feedback, please contact me on github.

If you find this project helpful, consider donating PayPal.

Dependencies

~4–16MB
~219K SLoC