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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
extern crate reqwest;
extern crate serde;

#[cfg(test)]
use mockito;

use reqwest::blocking::{Client as NetworkClient, RequestBuilder, Response};
use reqwest::header::CONNECTION;
use reqwest::{Method, StatusCode};
use serde::Serialize;

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

use crate::error::MMCError;
use crate::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)>;

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

#[derive(Serialize)]
struct MoveTarget {
    #[serde(skip_serializing_if = "Option::is_none")]
    show: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    season: Option<String>,
}

impl MoveTarget {
    pub fn for_endpoint(endpoint: &Endpoints, id: &str) -> MMCResult<MoveTarget> {
        match *endpoint {
            Endpoints::Season => Ok(MoveTarget {
                show: None,
                season: Some(id.to_string()),
            }),
            Endpoints::Show => Ok(MoveTarget {
                show: Some(id.to_string()),
                season: None,
            }),
            _ => Err(MMCError::UnsupportedMoveParent(endpoint.to_string())),
        }
    }
}

#[derive(Serialize)]
struct Move {
    #[serde(rename = "type")]
    _type: String,
    id: String,
    attributes: MoveTarget,
}

#[derive(Serialize)]
struct MoveRequest {
    data: Move,
}

/// 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,
}

impl Endpoints {
    fn singular(&self) -> String {
        match *self {
            Endpoints::Asset => "asset",
            Endpoints::Changelog => "changelog",
            Endpoints::Collection => "collection",
            Endpoints::Episode => "episode",
            Endpoints::Franchise => "franchise",
            Endpoints::Season => "season",
            Endpoints::Show => "show",
            Endpoints::Special => "special",
        }
        .to_string()
    }
}

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::builder()
            .build()
            .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, params: Option<Params>) -> MMCResult<String> {
        self.rq_get(
            Client::build_url(
                self.base.as_str(),
                None,
                endpoint,
                Some(id),
                params.unwrap_or(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 fetch a list of child objects of the requested Media Manager API type belonging
    /// to the requested parent object augmeted by the requested parameters
    pub fn child_list(
        &self,
        endpoint: Endpoints,
        parent_id: &str,
        parent_endpoint: Endpoints,
        params: Option<Params>,
    ) -> MMCResult<String> {
        self.rq_get(
            Client::build_url(
                self.base.as_str(),
                Some((parent_endpoint, parent_id)),
                endpoint,
                None,
                params.unwrap_or(Vec::new()),
            )
            .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(),
        )
    }

    /// Attempts to change the parent of an object
    pub fn change_parent(
        &self,
        parent_endpoint: Endpoints,
        parent_id: &str,
        child_endpoint: Endpoints,
        child_id: &str,
    ) -> MMCResult<String> {
        let move_request = MoveRequest {
            data: Move {
                _type: child_endpoint.singular(),
                id: child_id.to_string(),
                attributes: MoveTarget::for_endpoint(&parent_endpoint, parent_id)?,
            },
        };

        self.rq_patch(
            Client::build_url(
                self.base.as_str(),
                None,
                child_endpoint,
                Some(child_id),
                vec![],
            )
            .as_str(),
            &move_request,
        )
    }

    /// 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, params: Option<Params>) -> MMCResult<String> {
        self.get(Endpoints::Asset, id, params)
    }

    /// Shorthand for accessing a list of assets
    pub fn assets(
        &self,
        parent_id: &str,
        parent_endpoint: Endpoints,
        params: Option<Params>,
    ) -> MMCResult<String> {
        self.child_list(Endpoints::Asset, parent_id, parent_endpoint, params)
    }

    /// 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, params: Option<Params>) -> MMCResult<String> {
        self.get(Endpoints::Collection, id, params)
    }

    /// 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, params: Option<Params>) -> MMCResult<String> {
        self.get(Endpoints::Episode, id, params)
    }

    /// Shorthand for accessing a list of episodes
    pub fn episodes(&self, season_id: &str, params: Option<Params>) -> MMCResult<String> {
        self.child_list(Endpoints::Episode, season_id, Endpoints::Season, params)
    }

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

    /// 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, params: Option<Params>) -> MMCResult<String> {
        self.get(Endpoints::Season, id, params)
    }

    /// Shorthand for accessing a list of seasons
    pub fn seasons(&self, show_id: &str, params: Option<Params>) -> MMCResult<String> {
        self.child_list(Endpoints::Season, show_id, Endpoints::Show, params)
    }

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

    /// Shorthand for accessing a list of specials
    pub fn specials(&self, show_id: &str, params: Option<Params>) -> MMCResult<String> {
        self.child_list(Endpoints::Special, show_id, Endpoints::Show, params)
    }

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

    /// 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.basic_auth(self.key.to_string(), 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::NO_CONTENT => Client::parse_success(response),
            StatusCode::BAD_REQUEST => Client::parse_bad_request(response),
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Err(MMCError::NotAuthorized),
            StatusCode::NOT_FOUND => 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
        r#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)),
        }
    }
}