-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
24 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,17 @@ | ||
# cdg_api | ||
|
||
A simple Rust library to interact with the [US Congress API](https://api.congress.gov/). | ||
|
||
 | ||
 | ||
|
||
`cdg_api` provides a Rust interface for interacting with the US Congress API. It simplifies constructing API endpoints, building URLs with parameters, and retrieving legislative data. Whether fetching information about bills, members, amendments, or laws, `cdg_api` offers a streamlined and type-safe approach to accessing this data. | ||
|
||
The library is a work in progress but is very much functional. It is designed to be modular, extensible, and easy to use. The `CongressApiClient` struct serves as the central component for making requests, handling responses, and managing API keys. The library includes specific models for various API responses, such as bills, members, nominations, and treaties, as well as a versatile `GenericResponse` for handling unknown response structures. | ||
|
||
Please, if you find any issues, particularly with the API responses or models as I find it tedious to test all of them, feel free to open an issue or submit a pull request. | ||
|
||
## Table of Contents | ||
|
||
- [Overview](#overview) | ||
- [Features](#features) | ||
- [Installation](#installation) | ||
- [Getting Started](#getting-started) | ||
- [Setting Up Your API Key](#setting-up-your-api-key) | ||
- [Using `CongressApiClient`](#using-congressapiclient) | ||
- [Example 1: Fetching Members](#example-1-fetching-members) | ||
- [Example 2: Using `GenericResponse` with `parse_response`](#example-2-using-genericresponse-with-parse_response) | ||
- [Example 3: Using `Endpoints::Generic` for Custom Endpoints](#example-3-using-endpointsgeneric-for-custom-endpoints) | ||
- [Other Projects](#other-projects) | ||
- [License](#license) | ||
- [Repository](#repository) | ||
|
||
## Quick Info | ||
|
||
Around **100+** endpoints models available for interacting with the US Congress API, as well as the ability to create custom endpoints using `Endpoints::Generic`. | ||
|
||
Around **150+** response models available for parsing API responses, including specific models for bills, members, nominations, treaties, and more. | ||
|
||
- **Modules**: | ||
- **endpoints**: Models representing available API endpoints, including `Endpoints::Generic` for custom endpoints. | ||
- **url_builders**: Utility functions for constructing API URLs with query parameters. | ||
- **param_models**: Models and enums for different query parameters. | ||
- **param_chains**: Build chains for every param_model and the macro that constructs them. | ||
- **response_models**: Models for API responses, including specific models and the versatile `GenericResponse`. | ||
- **cdg_client**: `CongressApiClient` struct for interacting with the API. | ||
- **cdg_types**: Enums and structs and implementations for various custom types used in the API. | ||
- **ser_deser_cdg**: Response handling functions. See below. | ||
|
||
- **Api Client**: | ||
- **CongressApiClient**: Centralized client managing API keys, constructing URLs, making requests, and deserializing responses. | ||
|
||
- **Generic Response Handling**: | ||
- **GenericResponse**: A catch-all response model for endpoints without a specific response type or when the response model is unknown. | ||
|
||
- **Response Handling** | ||
- **parse_response**: A method to parse `GenericResponse` into a specific response model when the structure is known. | ||
- **serialize_response**: A method to serialize `GenericResponse` into a JSON string for debugging or creating a specific response model, a good fallback when parsing fails. | ||
|
||
- **Modules by Feature Flags**: | ||
- **Feature Flag: `request_handlers` (enabled by default)**: | ||
- **request_handlers**: Functions for making HTTP requests and handling responses, parts of which are used by `CongressApiClient`. | ||
A simple Rust library to interact with the [US Congress API](https://api.congress.gov/). | ||
|
||
## Installation | ||
|
||
Add `cdg_api` to your `Cargo.toml`: | ||
|
||
```toml | ||
[dependencies] | ||
cdg_api = "*" | ||
cdg_api = "1.3.6" | ||
``` | ||
|
||
Or use `cargo` to add the dependency: | ||
|
@@ -75,7 +24,7 @@ If you don't want to pull in reqwest as a dependency, you can disable the `reque | |
|
||
```toml | ||
[dependencies] | ||
cdg_api = { version = "*", default-features = false } | ||
cdg_api = { version = "1.3.6", default-features = false } | ||
``` | ||
|
||
or | ||
|
@@ -108,15 +57,15 @@ Obtain an API key from the [US Congress API](https://api.congress.gov/). Provide | |
|
||
## Using `CongressApiClient` | ||
|
||
[`CongressApiClient`] allows you to interact with various API endpoints. Below are examples demonstrating how to fetch different types of data, including the new `Endpoints::Generic` variant. | ||
[`CongressApiClient`] allows you to interact with various API endpoints. Below are examples demonstrating how to fetch different types of data, including the fetch-all `Endpoints::Generic` variant. | ||
|
||
For more detailed information, see the [documentation](https://docs.rs/cdg_api/1.3.6/cdg_api/), the | ||
[examples directory](examples/), and the [US Congress API documentation](https://github.com/LibraryOfCongress/api.congress.gov/). | ||
|
||
#### Example 1: Fetching Members | ||
|
||
Fetch a list of current members of Congress and display their names, party affiliations, and states. | ||
|
||
ps. Instead of using the standard `unwrap` methods, you can use `cdg_api::unwrap_option()`], `cdg_api::unwrap_option_string()`, or `cdg_api::unwrap_option_u32()` | ||
in order to quickly unwrap the `Option` values and provide a default value if the value is `None`. Pretty much the same as `unwrap_or_default()`. | ||
|
||
```rust | ||
use cdg_api::CongressApiClient; | ||
use cdg_api::endpoints::{Endpoints, NewEndpoint}; | ||
|
@@ -218,13 +167,18 @@ use std::error::Error; | |
fn main() -> Result<(), Box<dyn Error>> { | ||
let client = CongressApiClient::new(None)?; // Use API key from environment | ||
|
||
// Manually specify the endpoint string | ||
// Manually specify the endpoint string and parameters | ||
let endpoint = Endpoints::new_generic( | ||
"daily-congressional-record".to_string(), | ||
GenericParams::default().format(FormatType::Json) | ||
); | ||
|
||
// Fetch the data as GenericResponse | ||
// Fetch the data as GenericResponse. Theoretically, you could use any endpoint string | ||
// and parameters here, but the response structure may not be known, we parse it into the | ||
// suspected response model after fetching. We do this by calling `parse_response`, it | ||
// will return a specific response model if the structure is known, else it will return | ||
// the GenericResponse. This ensures that the response is always parsed, rather than throwing | ||
// an error if the structure is unknown or unexpected during the fetch. | ||
let response: GenericResponse = client.fetch(endpoint)?; | ||
|
||
// Parse the response into DailyCongressionalRecordResponse | ||
|
@@ -255,11 +209,11 @@ fn main() -> Result<(), Box<dyn Error>> { | |
} | ||
``` | ||
|
||
## Other Projects | ||
## Related Projects | ||
|
||
- **[`loc_api`](https://crates.io/crates/loc_api)**: A Rust library for interacting with the Library of Congress API. | ||
|
||
- **[`congress_rollcalls`](https://github.com/t-fbd/congress_rollcalls)**: Github repository holding mass data of roll call votes in the US Congress. | ||
- **loc_api** : A Rust library for interacting with the Library of Congress API. Less polished than `cdg_api`, barebones interface, but functional. | ||
- [crate](https://crates.io/crates/loc_api) | ||
- [repository](https://github.com/t-fbd/loc_api) | ||
|
||
## License | ||
|
||
|
@@ -269,11 +223,14 @@ This project is licensed under the terms of the [MIT license](https://github.com | |
|
||
[https://github.com/t-fbd/cdg_api](https://github.com/t-fbd/cdg_api) | ||
|
||
## Acknowledgements | ||
|
||
The data is sourced from the [U.S. Congress API](https://api.congress.gov/). | ||
See their repository for more information: [LibraryOfCongress/api.congress.gov](https://github.com/LibraryOfCongress/api.congress.gov/). | ||
|
||
## Contact | ||
|
||
For questions or feedback, please contact me on [github](https://www.github.com/t-fbd) or email me [here](mailto:[email protected]). | ||
For questions or feedback, please contact me on [github](https://www.github.com/t-fbd). | ||
|
||
If you find this project helpful, consider donating [PayPal](https://paypal.me/imturn?country.x=US&locale.x=en_US). | ||
|