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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Library that eases the use of discogs API
// Copyright (C) 2016  Afonso Bordado <afonsobordado@az8.co>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
use data_structures::*;

#[cfg(test)]
use mockito::SERVER_URL;
#[cfg(test)]
use super::*;

#[cfg(test)]
pub const API_URL: &'static str = mockito::SERVER_URL;

/// The default host address for the API.
#[cfg(not(test))]
pub const API_URL: &'static str = "https://api.discogs.com";

/// The default rate limit for discogs
pub const API_RATE_LIMIT: u32 = 240;


pub struct Discogs {
    api_endpoint: String,
    user_agent: String,

    key: Option<String>,
    secret: Option<String>,

    // Maximum number of API Queries per minute
    rate_limit: u32,
}

impl Discogs {
    /// Constructs a new `Client` with the provided `user_agent`.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let client = Discogs::new("USER_AGENT");
    /// ```
    pub fn new(user_agent: &str) -> Self {
        Discogs {
            api_endpoint: API_URL.to_owned(),
            key: None,
            secret: None,
            user_agent: user_agent.to_owned(),
            rate_limit: API_RATE_LIMIT,
        }
    }

    /// Sets the discogs api client key
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let mut client = Discogs::new("USER_AGENT");
    /// client.key("CLIENT_KEY");
    /// ```
    //TODO: Come back and make a better example
    pub fn key(&mut self, key: &str) -> &mut Self {
        self.key = Some(key.to_owned());
        self
    }

    /// Sets the discogs api client secret
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let mut client = Discogs::new("USER_AGENT");
    /// client.secret("CLIENT_STRING");
    /// ```
    //TODO: Come back and make a better example
    pub fn secret(&mut self, secret: &str) -> &mut Self {
        self.secret = Some(secret.to_owned());
        self
    }

    /// Returns an instance of the `ArtistQueryBuilder` structure for the specified id
    /// This allows you to pass parameters to build a request.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let artist = Discogs::new("USER_AGENT")
    ///                       .artist(4567);
    /// ```
    pub fn artist(&mut self, id: u32) -> ArtistQueryBuilder {
        ArtistQueryBuilder::new(id,
                                self.api_endpoint.clone(),
                                self.user_agent.clone(),
                                self.key.clone(),
                                self.secret.clone())
    }

    /// Returns an instance of the `LabelQueryBuilder` structure for the specified id
    /// This allows you to pass parameters to build a request.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let label = Discogs::new("USER_AGENT")
    ///                       .label(1234);
    /// ```
    pub fn label(&mut self, id: u32) -> LabelQueryBuilder {
        LabelQueryBuilder::new(id,
                               self.api_endpoint.clone(),
                               self.user_agent.clone(),
                               self.key.clone(),
                               self.secret.clone())
    }

    /// Returns an instance of the `ReleaseQueryBuilder` structure for the specified id
    /// This allows you to pass parameters to build a request.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let release = Discogs::new("USER_AGENT")
    ///                       .label(1234);
    /// ```
    pub fn release(&mut self, id: u32) -> ReleaseQueryBuilder {
        ReleaseQueryBuilder::new(id,
                                 self.api_endpoint.clone(),
                                 self.user_agent.clone(),
                                 self.key.clone(),
                                 self.secret.clone())
    }

    /// Returns an instance of the `MasterQueryBuilder` structure for the specified id
    /// This allows you to pass parameters to build a request.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let master = Discogs::new("USER_AGENT")
    ///                       .master(1234);
    /// ```
    pub fn master(&mut self, id: u32) -> MasterQueryBuilder {
        MasterQueryBuilder::new(id,
                                 self.api_endpoint.clone(),
                                 self.user_agent.clone(),
                                 self.key.clone(),
                                 self.secret.clone())
    }

    /// Returns an instance of the `SearchQueryBuilder` structure.
    /// This allows you to pass parameters to build a request.
    ///
    /// # Examples
    ///
    /// ```
    /// use discogs::Discogs;
    ///
    /// let search = Discogs::new("USER_AGENT").search();
    /// ```
    pub fn search(&mut self) -> SearchQueryBuilder {
        SearchQueryBuilder::new(self.api_endpoint.clone(),
                                 self.user_agent.clone(),
                                 self.key.clone(),
                                 self.secret.clone())
    }
}

#[cfg(test)]
mod tests {
    use discogs::Discogs;

    #[test]
    fn user_agent_test() {
        let client = Discogs::new("USER_AGENT");

        assert_eq!(client.user_agent, "USER_AGENT".to_string());
    }

    #[test]
    fn key_test() {
        let mut client = Discogs::new("USER_AGENT");
        client.key("CLIENT_KEY");

        assert_eq!(client.user_agent, "USER_AGENT".to_string());
        assert_eq!(client.key, Some("CLIENT_KEY".to_string()));
    }

    #[test]
    fn secret_test() {
        let mut client = Discogs::new("USER_AGENT");
        client.secret("CLIENT_STRING");

        assert_eq!(client.user_agent, "USER_AGENT".to_string());
        assert_eq!(client.secret, Some("CLIENT_STRING".to_string()));
    }
}