pub struct ApiClient { /* private fields */ }Expand description
A client for interacting with the Library of Congress API.
Provides high-level methods to perform API requests without manually constructing parameters or URLs.
Implementations§
Source§impl ApiClient
impl ApiClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ApiClient instance.
The base URL can be overridden by setting the LOC_API_BASE_URL environment variable.
base_url
§Examples
use loc_api::simple_builders::ApiClient;
let client = ApiClient::new();Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_item(
"2014717546",
Some(ItemAttributes {
cite_this: Some(true),
item: Some(true),
resources: Some(true),
}),
)?;
println!("url: {}", response.1);
// Handle the item details
if let Some(item) = response.0.item {
println!("{:#?}", item);
}
if let Some(resources) = response.0.resources {
println!("{:#?}", resources);
}
Ok(())
}More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_collections(
None,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
None,
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the collections
if let Some(results) = response.0.results {
for collection in results {
println!("{:#?}", collection);
}
}
Ok(())
}7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.search(
"baseball",
false,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:sports".to_string()] }),
Some(25),
Some(1),
Some(SortField::DateDesc),
)?;
println!("url: {}", response.1);
// Handle the search results
if let Some(results) = response.0.results {
for item in results {
println!("{:?}", item);
}
}
Ok(())
}8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_format(
MediaType::Maps,
Some("usa"),
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the format-specific results
if let Some(results) = response.0.results {
for item in results {
println!("{:#?}", item);
}
}
Ok(())
}7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_collection(
"civil war maps",
None,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the specific collection details
if let Some(results) = response.0.results {
for item in results {
println!("{:#?}", item);
}
}
Ok(())
}Sourcepub fn search(
&self,
query: &str,
include_collections: bool,
attributes: Option<AttributesSelect>,
filters: Option<FacetReq>,
per_page: Option<u32>,
page: Option<u32>,
sort: Option<SortField>,
) -> Result<(SearchResultResponse, String), Box<dyn Error>>
pub fn search( &self, query: &str, include_collections: bool, attributes: Option<AttributesSelect>, filters: Option<FacetReq>, per_page: Option<u32>, page: Option<u32>, sort: Option<SortField>, ) -> Result<(SearchResultResponse, String), Box<dyn Error>>
Performs a search query using the /search/ endpoint.
§Parameters
query: The search query string.include_collections: Whether to include collections in the search results.attributes: Attributes to include or exclude in the response.filters: Facet filters to apply.per_page: Number of results per page.page: Page number to retrieve.sort: Sorting field.
§Returns
Returns a SearchResultResponse on success.
§Examples
use loc_api::simple_builders::ApiClient;
use loc_api::param_models::{FacetReq, SearchParams};
use loc_api::attribute_models::{AttributesSelect, SortField};
use loc_api::format_models::Format;
let client = ApiClient::new();
let response = client.search(
"baseball",
false,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:sports".to_string()] }),
Some(25),
Some(1),
Some(SortField::DateDesc),
).unwrap();Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.search(
"baseball",
false,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:sports".to_string()] }),
Some(25),
Some(1),
Some(SortField::DateDesc),
)?;
println!("url: {}", response.1);
// Handle the search results
if let Some(results) = response.0.results {
for item in results {
println!("{:?}", item);
}
}
Ok(())
}Sourcepub fn get_item(
&self,
item_id: &str,
attributes: Option<ItemAttributes>,
) -> Result<(ItemResponse, String), Box<dyn Error>>
pub fn get_item( &self, item_id: &str, attributes: Option<ItemAttributes>, ) -> Result<(ItemResponse, String), Box<dyn Error>>
Retrieves detailed information about a specific item using the /item/{item_id}/ endpoint.
§Parameters
item_id: The unique identifier of the item.attributes: Attributes to include in the response.
§Returns
Returns an ItemResponse on success.
§Examples
use loc_api::simple_builders::ApiClient;
use loc_api::param_models::ItemParams;
use loc_api::attribute_models::ItemAttributes;
use loc_api::format_models::Format;
let client = ApiClient::new();
let response = client.get_item(
"2014717546",
Some(ItemAttributes {
cite_this: Some(true),
item: Some(true),
resources: Some(true),
}),
).unwrap();Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_item(
"2014717546",
Some(ItemAttributes {
cite_this: Some(true),
item: Some(true),
resources: Some(true),
}),
)?;
println!("url: {}", response.1);
// Handle the item details
if let Some(item) = response.0.item {
println!("{:#?}", item);
}
if let Some(resources) = response.0.resources {
println!("{:#?}", resources);
}
Ok(())
}Sourcepub fn get_format(
&self,
format_type: MediaType,
query: Option<&str>,
attributes: Option<AttributesSelect>,
filters: Option<FacetReq>,
per_page: Option<u32>,
page: Option<u32>,
sort: Option<SortField>,
) -> Result<(FormatResponse, String), Box<dyn Error>>
pub fn get_format( &self, format_type: MediaType, query: Option<&str>, attributes: Option<AttributesSelect>, filters: Option<FacetReq>, per_page: Option<u32>, page: Option<u32>, sort: Option<SortField>, ) -> Result<(FormatResponse, String), Box<dyn Error>>
Retrieves items of a specific format using the /{format}/ endpoint.
§Parameters
format_type: The specific format type (e.g.,MediaType::Maps).query: The search query string.attributes: Attributes to include or exclude in the response.filters: Facet filters to apply.per_page: Number of results per page.page: Page number to retrieve.sort: Sorting field.
§Returns
Returns a FormatResponse on success.
§Examples
use loc_api::simple_builders::ApiClient;
use loc_api::param_models::FacetReq;
use loc_api::attribute_models::{AttributesSelect, SortField};
use loc_api::format_models::{Format, MediaType};
let client = ApiClient::new();
let response = client.get_format(
MediaType::Maps,
Some("mountain"),
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
).unwrap();Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_format(
MediaType::Maps,
Some("usa"),
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the format-specific results
if let Some(results) = response.0.results {
for item in results {
println!("{:#?}", item);
}
}
Ok(())
}Sourcepub fn get_collection(
&self,
collection_name: &str,
query: Option<&str>,
attributes: Option<AttributesSelect>,
filters: Option<FacetReq>,
per_page: Option<u32>,
page: Option<u32>,
sort: Option<SortField>,
) -> Result<(CollectionResponse, String), Box<dyn Error>>
pub fn get_collection( &self, collection_name: &str, query: Option<&str>, attributes: Option<AttributesSelect>, filters: Option<FacetReq>, per_page: Option<u32>, page: Option<u32>, sort: Option<SortField>, ) -> Result<(CollectionResponse, String), Box<dyn Error>>
Retrieves detailed information about a specific collection using /collections/{name_of_collection}/.
§Parameters
collection_name: The name of the collection in kebab-case, auto-conversion will replace spaces and ‘_’ with hyphens.query: The search query string.attributes: Attributes to include or exclude in the response.filters: Facet filters to apply.per_page: Number of results per page.page: Page number to retrieve.sort: Sorting field.
§Returns
Returns a CollectionResponse on success.
§Examples
use loc_api::simple_builders::ApiClient;
use loc_api::param_models::FacetReq;
use loc_api::attribute_models::{AttributesSelect, SortField};
use loc_api::format_models::Format;
use loc_api::response_models::CollectionResponse;
let client = ApiClient::new();
let response = match client.get_collection(
"maps",
Some("mountain"),
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
) {
Ok(response) => {
println!("URL: {}", response.1);
response.0
},
Err(e) => {
eprintln!("Error: {}", e);
CollectionResponse::default()
}
};Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_collection(
"civil war maps",
None,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the specific collection details
if let Some(results) = response.0.results {
for item in results {
println!("{:#?}", item);
}
}
Ok(())
}Sourcepub fn get_collections(
&self,
query: Option<&str>,
attributes: Option<AttributesSelect>,
filters: Option<FacetReq>,
per_page: Option<u32>,
page: Option<u32>,
sort: Option<SortField>,
) -> Result<(CollectionsResponse, String), Box<dyn Error>>
pub fn get_collections( &self, query: Option<&str>, attributes: Option<AttributesSelect>, filters: Option<FacetReq>, per_page: Option<u32>, page: Option<u32>, sort: Option<SortField>, ) -> Result<(CollectionsResponse, String), Box<dyn Error>>
Retrieves all collections using the /collections/ endpoint.
§Parameters
query: The search query string.attributes: Attributes to include or exclude in the response.filters: Facet filters to apply.per_page: Number of results per page.page: Page number to retrieve.sort: Sorting field.
§Returns
Returns a CollectionsResponse on success.
§Examples
use loc_api::simple_builders::ApiClient;
use loc_api::param_models::FacetReq;
use loc_api::attribute_models::{AttributesSelect, SortField};
use loc_api::format_models::Format;
let client = ApiClient::new();
let response = client.get_collections(
Some("mountain"),
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
Some(FacetReq { filters: vec!["subject:geography".to_string()] }),
Some(10),
Some(1),
Some(SortField::TitleS),
).unwrap();Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ApiClient::new();
let response = client.get_collections(
None,
Some(AttributesSelect {
include: vec!["pagination".to_string(), "results".to_string()],
exclude: vec![],
}),
None,
Some(10),
Some(1),
Some(SortField::TitleS),
)?;
println!("url: {}", response.1);
// Handle the collections
if let Some(results) = response.0.results {
for collection in results {
println!("{:#?}", collection);
}
}
Ok(())
}