loc_api::simple_builders

Struct ApiClient

Source
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

Source

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?
examples/item_example.rs (line 6)
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
Hide additional examples
examples/collections_example.rs (line 7)
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(())
}
examples/search_example.rs (line 8)
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(())
}
examples/mediaformat_example.rs (line 9)
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(())
}
examples/collection_example.rs (line 8)
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(())
}
Source

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?
examples/search_example.rs (lines 9-20)
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(())
}
Source

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?
examples/item_example.rs (lines 7-14)
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(())
}
Source

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?
examples/mediaformat_example.rs (lines 10-21)
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(())
}
Source

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?
examples/collection_example.rs (lines 9-20)
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(())
}
Source

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?
examples/collections_example.rs (lines 8-18)
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(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more