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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
extern crate reqwest;
extern crate serde;

#[cfg(test)]
use mockito;

use self::reqwest::Client as NetworkClient;
use self::reqwest::header::{Authorization, Basic, Connection};
use self::reqwest::{Method, RequestBuilder, Response, StatusCode};
use self::serde::Serialize;

use std::fmt;
use std::io::Read;
use std::str;

use error::MMCError;
use error::MMCResult;

#[cfg(not(test))]
const LIVE_URL: &'static str = "https://media.services.pbs.org/api/v1";
#[cfg(not(test))]
const STAGING_URL: &'static str = "https://media-staging.services.pbs.org/api/v1";

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

/// A client for communicating with the Media Manager API
#[derive(Debug)]
pub struct Client {
    key: String,
    secret: String,
    base: String,
    client: NetworkClient,
}

pub type Params<'a> = Vec<(&'a str, &'a str)>;

/// The Media Manager endpoints that are supported by [Client](struct.Client.html)
#[derive(Clone, Debug)]
pub enum Endpoints {
    /// Represents the assets endpoint
    Asset,

    /// Represents the changelog endpoint
    Changelog,

    /// Represents the collections endpoint
    Collection,

    /// Represents the episodes endpoint
    Episode,

    /// Represents the franchises endpoint
    Franchise,

    /// Represents the seasons endpoint
    Season,

    /// Represents the shows endpoint
    Show,

    /// Represents the specials endpoint
    Special,
}

type ParentEndpoint<'a> = (Endpoints, &'a str);

impl fmt::Display for Endpoints {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let string_form = match *self {
            Endpoints::Asset => "assets",
            Endpoints::Changelog => "changelog",
            Endpoints::Collection => "collections",
            Endpoints::Episode => "episodes",
            Endpoints::Franchise => "franchises",
            Endpoints::Season => "seasons",
            Endpoints::Show => "shows",
            Endpoints::Special => "specials",
        };

        write!(f, "{}", string_form)
    }
}

impl str::FromStr for Endpoints {
    type Err = MMCError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "asset" | "assets" => Ok(Endpoints::Asset),
            "changelog" => Ok(Endpoints::Changelog),
            "collection" | "collections" => Ok(Endpoints::Collection),
            "episode" | "episodes" => Ok(Endpoints::Episode),
            "franchise" | "franchises" => Ok(Endpoints::Franchise),
            "season" | "seasons" => Ok(Endpoints::Season),
            "show" | "shows" => Ok(Endpoints::Show),
            "special" | "specials" => Ok(Endpoints::Special),
            x => Err(MMCError::UnknownEndpoint(x.to_string())),
        }
    }
}

impl Client {
    /// Generates a new client for the production Media Manager API
    pub fn new(key: &str, secret: &str) -> MMCResult<Client> {
        Client::client_builder(key, secret, LIVE_URL)
    }

    /// Generates a new client for the staging Media Manager API
    pub fn staging(key: &str, secret: &str) -> MMCResult<Client> {
        Client::client_builder(key, secret, STAGING_URL)
    }

    fn client_builder(key: &str, secret: &str, base: &str) -> MMCResult<Client> {
        NetworkClient::new().map_err(MMCError::Network).and_then(|net_client| {
            Ok(Client {
                key: String::from(key),
                secret: String::from(secret),
                base: String::from(base),
                client: net_client,
            })
        })
    }

    /// Attempts to fetch a single object with the requested id from the requested
    /// Media Manager API endpoint
    pub fn get(&self, endpoint: Endpoints, id: &str) -> MMCResult<String> {
        self.rq_get(Client::build_url(self.base.as_str(), None, endpoint, Some(id), vec![])
            .as_str())
    }

    /// Attempts to fetch a list of objects from the requested Media Manager API endpoint augmented
    /// by the requested parameters
    pub fn list(&self, endpoint: Endpoints, params: Params) -> MMCResult<String> {
        self.rq_get(Client::build_url(self.base.as_str(), None, endpoint, None, params).as_str())
    }

    /// Attempts to create a new object of the provided [Endpoints](enum.Endpoints.html) for the
    /// provided parent [Endpoints](enum.Endpoints.html)
    pub fn create<T: Serialize>(&self,
                                parent: Endpoints,
                                id: &str,
                                endpoint: Endpoints,
                                body: &T)
                                -> MMCResult<String> {
        self.rq_post(Client::build_url(self.base.as_str(),
                                       Some((parent, id)),
                                       endpoint,
                                       None,
                                       vec![])
                         .as_str(),
                     body)
    }

    /// Attempts to fetch the edit object specified by the  [Endpoints](enum.Endpoints.html) and id
    pub fn edit(&self, endpoint: Endpoints, id: &str) -> MMCResult<String> {
        self.rq_get(Client::build_edit_url(self.base.as_str(), None, endpoint, Some(id), vec![])
            .as_str())
    }

    /// Attempts to update the object specified by the  [Endpoints](enum.Endpoints.html) and id
    pub fn update<T: Serialize>(&self,
                                endpoint: Endpoints,
                                id: &str,
                                body: &T)
                                -> MMCResult<String> {
        self.rq_patch(Client::build_edit_url(self.base.as_str(), None, endpoint, Some(id), vec![])
                          .as_str(),
                      body)
    }

    /// Attempts to delete the object specified by the  [Endpoints](enum.Endpoints.html) and id
    pub fn delete(&self, endpoint: Endpoints, id: &str) -> MMCResult<String> {
        self.rq_delete(Client::build_edit_url(self.base.as_str(), None, endpoint, Some(id), vec![])
            .as_str())
    }

    /// Allows for calling any arbitrary url from the Media Manager API
    pub fn url(&self, url: &str) -> MMCResult<String> {
        self.rq_get(url)
    }

    /// Shorthand for accessing a single asset
    pub fn asset(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Episode, id)
    }

    /// Shorthand for accessing a list of changes
    pub fn changelog(&self, params: Params) -> MMCResult<String> {
        self.list(Endpoints::Changelog, params)
    }

    /// Shorthand for accessing a single collection
    pub fn collection(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Collection, id)
    }

    /// Shorthand for accessing a list of collections
    pub fn collections(&self, params: Params) -> MMCResult<String> {
        self.list(Endpoints::Collection, params)
    }

    /// Shorthand for accessing a single episode
    pub fn episode(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Episode, id)
    }

    /// Shorthand for accessing a single franchise
    pub fn franchise(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Franchise, id)
    }

    /// Shorthand for accessing a list of franchises
    pub fn franchises(&self, params: Params) -> MMCResult<String> {
        self.list(Endpoints::Franchise, params)
    }

    /// Shorthand for accessing a single season
    pub fn season(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Season, id)
    }

    /// Shorthand for accessing a single special
    pub fn special(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Special, id)
    }

    /// Shorthand for accessing a single show
    pub fn show(&self, id: &str) -> MMCResult<String> {
        self.get(Endpoints::Show, id)
    }

    /// Shorthand for accessing a list of shows
    pub fn shows(&self, params: Params) -> MMCResult<String> {
        self.list(Endpoints::Show, params)
    }

    // Handle read endpoints of the API
    fn rq_get(&self, url: &str) -> MMCResult<String> {
        self.rq_send(self.client.get(url))
    }

    // Handle create endpoints of the API
    fn rq_post<T: Serialize>(&self, url: &str, body: &T) -> MMCResult<String> {
        self.rq_send(self.client.post(url).json(body))
    }

    // Handle update endpoints of the API
    fn rq_patch<T: Serialize>(&self, url: &str, body: &T) -> MMCResult<String> {
        self.rq_send(self.client.request(Method::Patch, url).json(body))
    }

    // Handle update endpoints of the API
    fn rq_delete(&self, url: &str) -> MMCResult<String> {
        self.rq_send(self.client.request(Method::Delete, url))
    }

    // Handle authentication and response mapping
    fn rq_send(&self, req: RequestBuilder) -> MMCResult<String> {
        req.header(Authorization(Basic {
                username: self.key.to_string(),
                password: Some(self.secret.to_string()),
            }))
            .header(Connection::close())
            .send()
            .map_err(MMCError::Network)
            .and_then(Client::handle_response)
    }

    fn build_edit_url(base_url: &str,
                      parent: Option<ParentEndpoint>,
                      endpoint: Endpoints,
                      id: Option<&str>,
                      params: Params)
                      -> String {
        let mut url = Client::build_url(base_url, parent, endpoint, id, params);
        url.push_str("edit/");

        url
    }

    fn build_url(base_url: &str,
                 parent: Option<ParentEndpoint>,
                 endpoint: Endpoints,
                 id: Option<&str>,
                 params: Params)
                 -> String {

        // Create the new base for the returned url
        let mut url = base_url.to_string();
        url.push('/');

        // Add the parent endpoint if an endpoint and id was supplied
        if let Some(p_endpoint) = parent {
            url.push_str(p_endpoint.0.to_string().as_str());
            url.push('/');
            url.push_str(p_endpoint.1);
            url.push('/');
        }

        // Parse the requested endpoint
        let endpoint_string = endpoint.to_string();
        url.push_str(endpoint_string.as_str());
        url.push('/');

        // Optional add the id if it was supplied
        if let Some(id_val) = id {
            url.push_str(id_val);
            url.push('/');
        }

        // Add the query parameters to the url
        url + Client::format_params(params).as_str()
    }

    fn format_params(params: Params) -> String {
        if !params.is_empty() {
            let param_string = params.iter()
                .map(|&(name, value)| format!("{}={}", name, value))
                .collect::<Vec<String>>()
                .join("&");

            let mut args = "?".to_owned();
            args.push_str(param_string.as_str());
            args
        } else {
            String::new()
        }
    }

    fn handle_response(response: Response) -> MMCResult<String> {
        match *response.status() {
            StatusCode::Ok | StatusCode::NoContent => Client::parse_success(response),
            StatusCode::BadRequest => Client::parse_bad_request(response),
            StatusCode::Unauthorized | StatusCode::Forbidden => Err(MMCError::NotAuthorized),
            StatusCode::NotFound => Err(MMCError::ResourceNotFound),
            x => Err(MMCError::APIFailure(x)),
        }
    }

    fn parse_success(response: Response) -> MMCResult<String> {
        Client::parse_response_body(response)
    }

    fn parse_bad_request(response: Response) -> MMCResult<String> {
        Client::parse_response_body(response).and_then(|body| Err(MMCError::BadRequest(body)))
    }

    fn parse_response_body(mut response: Response) -> MMCResult<String> {

        // Create a buffer to read the response stream into
        let mut buffer = Vec::new();

        // Try to read the response into the buffer and return with a
        // io error in the case of a failure
        try!(response.read_to_end(&mut buffer).map_err(MMCError::Io));

        // Generate a string from the buffer
        let result = String::from_utf8(buffer);

        // Return either successfully generated string or a conversion error
        match result {
            Ok(string) => Ok(string),
            Err(err) => Err(MMCError::Convert(err)),
        }
    }
}