Skip to main content

votesmart/
lib.rs

1mod api;
2mod errors;
3mod types;
4pub use api::*;
5pub use errors::*;
6pub use types::*;
7
8const VOTESMART_BASE_URL: &str = "http://api.votesmart.org/";
9
10/// Stuct used to make calls to the Votesmart API
11pub struct VotesmartProxy {
12    client: reqwest::Client,
13    pub base_url: reqwest::Url,
14    api_key: String,
15}
16
17impl VotesmartProxy {
18    /// Instantiate new VotesmartProxy API client from .env api key
19    pub fn new() -> Result<Self, Error> {
20        dotenv::dotenv().ok();
21        let api_key = std::env::var("VOTESMART_API_KEY")?;
22        let client = reqwest::Client::new();
23
24        Ok(VotesmartProxy {
25            client,
26            base_url: reqwest::Url::parse(VOTESMART_BASE_URL).unwrap(),
27            api_key,
28        })
29    }
30
31    /// Instantiate new VotesmartProxy API client by passing api key to this function
32    pub fn new_from_key(api_key: String) -> Result<Self, Error> {
33        let client = reqwest::Client::new();
34
35        Ok(VotesmartProxy {
36            client,
37            base_url: reqwest::Url::parse(VOTESMART_BASE_URL).unwrap(),
38            api_key,
39        })
40    }
41}
42
43/// Endpoint function namespaces.
44impl VotesmartProxy {
45    /// Office endpoint methods.
46    pub const fn office(&self) -> Office<'_> {
47        Office(self)
48    }
49    /// Officials endpoint methods.
50    pub const fn officials(&self) -> Officials<'_> {
51        Officials(self)
52    }
53    /// Rating endpoint methods.
54    pub const fn rating(&self) -> Rating<'_> {
55        Rating(self)
56    }
57    /// State endpoint methods.
58    pub const fn state(&self) -> State<'_> {
59        State(self)
60    }
61    /// Address endpoint methods.
62    pub const fn address(&self) -> Address<'_> {
63        Address(self)
64    }
65    /// Candidates endpoint methods.
66    pub const fn candidates(&self) -> Candidates<'_> {
67        Candidates(self)
68    }
69    /// Committee endpoint methods.
70    pub const fn committee(&self) -> Committee<'_> {
71        Committee(self)
72    }
73    /// District endpoint methods.
74    pub const fn district(&self) -> District<'_> {
75        District(self)
76    }
77    /// Election endpoint methods.
78    pub const fn election(&self) -> Election<'_> {
79        Election(self)
80    }
81    /// Leadership endpoint methods.
82    pub const fn leadership(&self) -> Leadership<'_> {
83        Leadership(self)
84    }
85    /// Vote endpoint methods.
86    pub const fn votes(&self) -> Votes<'_> {
87        Votes(self)
88    }
89    /// CandidateBio endpoint methods.
90    pub const fn candidate_bio(&self) -> CandidateBio<'_> {
91        CandidateBio(self)
92    }
93}