1
 2
 3
 4
 5
 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
mod api;
mod errors;
mod types;
pub use api::*;
pub use errors::*;
pub use types::*;

const VOTESMART_BASE_URL: &str = "http://api.votesmart.org/";

/// Stuct used to make calls to the Votesmart API
pub struct VotesmartProxy {
    client: reqwest::Client,
    pub base_url: reqwest::Url,
    api_key: String,
}

impl VotesmartProxy {
    /// Instantiate new VotesmartProxy API client from .env api key
    pub fn new() -> Result<Self, Error> {
        dotenv::dotenv().ok();
        let api_key = std::env::var("VOTESMART_API_KEY")?;
        let client = reqwest::Client::new();

        Ok(VotesmartProxy {
            client,
            base_url: reqwest::Url::parse(VOTESMART_BASE_URL).unwrap(),
            api_key,
        })
    }

    /// Instantiate new VotesmartProxy API client by passing api key to this function
    pub fn new_from_key(api_key: String) -> Result<Self, Error> {
        let client = reqwest::Client::new();

        Ok(VotesmartProxy {
            client,
            base_url: reqwest::Url::parse(VOTESMART_BASE_URL).unwrap(),
            api_key,
        })
    }
}

/// Endpoint function namespaces.
impl VotesmartProxy {
    /// Offic endpoint methods.
    pub const fn office(&self) -> Office<'_> {
        Office(self)
    }
    /// Officials endpoint methods.
    pub const fn officials(&self) -> Officials<'_> {
        Officials(self)
    }
    /// Rating endpoint methods.
    pub const fn rating(&self) -> Rating<'_> {
        Rating(self)
    }
    /// State endpoint methods.
    pub const fn state(&self) -> State<'_> {
        State(self)
    }
    /// Address endpont methods.
    pub const fn address(&self) -> Address<'_> {
        Address(self)
    }
    /// Candidates endpont methods.
    pub const fn candidates(&self) -> Candidates<'_> {
        Candidates(self)
    }
    /// Committee endpont methods.
    pub const fn committee(&self) -> Committee<'_> {
        Committee(self)
    }
    /// District endpont methods.
    pub const fn district(&self) -> District<'_> {
        District(self)
    }
    /// Election endpont methods.
    pub const fn election(&self) -> Election<'_> {
        Election(self)
    }
    /// Leadership endpont methods.
    pub const fn leadership(&self) -> Leadership<'_> {
        Leadership(self)
    }
    /// Vote endpoint methods.
    pub const fn votes(&self) -> Votes<'_> {
        Votes(self)
    }
    /// CandidateBio endpoint methods.
    pub const fn candidate_bio(&self) -> CandidateBio<'_> {
        CandidateBio(self)
    }
}