Skip to content

Commit aaf432f

Browse files
committed
feat(videos): first primitive types and api
Now it should be possible to implement first version of actual insert handling, with everything there is about it. That should eventually help to generalize it, as I am definitely not going to hand-implemented these protocols ... . The great thing is, that if done right, one will be able to truly be first and make an impact !
1 parent d4869cf commit aaf432f

3 files changed

Lines changed: 183 additions & 3 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
*Youtube* is a library written in Rust to help interacting with your youtube account.
22
For now, all functionality is geared towards allowing interruptible video uploads
3-
and adjustments of video meta-data.
3+
and adjustments of video meta-data.
4+
5+
The library works using the builder pattern. If builders are instantiated, you will need to
6+
provide the minimal information right off the bat. Further calls to the builder allow
7+
to configure it. Some configuration will be in the form of callbacks, which allows you to
8+
control internal loops or behaviour.
9+
10+
It's the goal of each builder to maximize the chances of a successful result, and it will
11+
provide enough callbacks to be resilient against network errors, and authorization failures
12+
which require the token to be refreshed.
13+
14+
You will need authorization to perform most of the operations implemented here - it can be obtained
15+
and handled using the [yup-oauth2 library][oauth].
16+
17+
[oauth]: [https://crates.io/crates/yup-oauth2]

src/lib.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
1-
#[test]
2-
fn it_works() {
1+
#![feature(core)]
2+
//! # Usage
3+
//! ```test_harness
4+
//! extern crate youtube3;
5+
//! extern crate hyper;
6+
//!
7+
//! # #[test]
8+
//! # fn test() {
9+
//! let youtube = youtube3::new(hyper::Client::new());
10+
//! youtube.videos();
11+
//! # }
12+
extern crate hyper;
13+
extern crate "rustc-serialize" as rustc_serialize;
14+
15+
use std::marker::PhantomData;
16+
use std::borrow::BorrowMut;
17+
use std::cell::RefCell;
18+
19+
mod common;
20+
pub mod videos;
21+
22+
23+
/// Central instance to access all youtube related services
24+
pub struct YouTube<C, NC> {
25+
client: RefCell<C>,
26+
27+
_m: PhantomData<NC>
328
}
29+
30+
impl<'a, C, NC> YouTube<C, NC>
31+
where NC: hyper::net::NetworkConnector,
32+
C: BorrowMut<hyper::Client<NC>> + 'a {
33+
34+
pub fn new(client: C) -> YouTube<C, NC> {
35+
YouTube {
36+
client: RefCell::new(client),
37+
_m: PhantomData,
38+
}
39+
}
40+
41+
pub fn videos(&'a self) -> videos::Service<'a, C, NC> {
42+
videos::Service::new(&self.client)
43+
}
44+
}
45+
46+
47+
pub fn new<C, NC>(client: C) -> YouTube<C, NC>
48+
where NC: hyper::net::NetworkConnector,
49+
C: BorrowMut<hyper::Client<NC>> {
50+
YouTube::new(client)
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
use hyper;
57+
58+
59+
#[test]
60+
fn instantiate() {
61+
let yt = YouTube::new(hyper::Client::new());
62+
let v = yt.videos();
63+
64+
let mut c = hyper::Client::new();
65+
let yt = YouTube::new(&mut c);
66+
let v = yt.videos();
67+
}
68+
}

src/videos/service.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,67 @@ use rustc_serialize;
66

77
use hyper;
88

9+
/// Reresents all aspects of a youtube video resource. May only be partially
10+
/// available
11+
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
12+
pub struct Video {
13+
pub snippet: Option<VideoSnippet>,
14+
pub recordingDetails: Option<VideoRecordingDetails>,
15+
pub status: Option<VideoStatus>,
16+
}
17+
18+
#[allow(non_snake_case)]
19+
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
20+
pub struct VideoSnippet {
21+
pub categoryId: String,
22+
pub description: String,
23+
pub tags: Vec<String>,
24+
pub title: String,
25+
26+
pub status: Option<VideoStatus>,
27+
pub recordingDetails: Option<VideoRecordingDetails>,
28+
}
29+
30+
impl Video {
31+
fn parts(&self) -> String {
32+
let mut res = String::new();
33+
if self.status.is_some() {
34+
res = res + "status,";
35+
}
36+
if self.recordingDetails.is_some() {
37+
res = res + "recordingDetails";
38+
}
39+
if self.snippet.is_some() {
40+
res = res + "snippet,";
41+
}
42+
res
43+
}
44+
}
45+
46+
#[allow(non_snake_case)]
47+
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
48+
pub struct VideoStatus {
49+
pub privacyStatus: String,
50+
pub embeddable: bool,
51+
pub license: String,
52+
pub publicStatsViewable: bool,
53+
pub publishAt: String,
54+
}
55+
56+
#[allow(non_snake_case)]
57+
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
58+
pub struct VideoRecordingDetails {
59+
locationDescription: String,
60+
recordingDate: String,
61+
}
62+
63+
#[allow(non_snake_case)]
64+
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
65+
pub struct GeoPoint {
66+
altitude: f64,
67+
latitude: f64,
68+
longitude: f64,
69+
}
970

1071
/// The videos service - provides actual functionality through builders.
1172
pub struct Service<'a, C, NC>
@@ -27,13 +88,53 @@ impl<'a, C, NC> Service<'a, C, NC>
2788
_m: PhantomData,
2889
}
2990
}
91+
92+
pub fn insert(&self, parts: &str, video: &Video) -> VideosInsertBuilder<'a, C, NC> {
93+
VideosInsertBuilder {
94+
client: self.client,
95+
video: video.clone(),
96+
parts: parts.to_string(),
97+
_m: PhantomData,
98+
}
99+
}
100+
}
101+
102+
pub struct VideosInsertBuilder<'a, C, NC>
103+
where NC: 'a,
104+
C: 'a {
105+
106+
client: &'a RefCell<C>,
107+
video: Video,
108+
parts: String,
109+
110+
_m: PhantomData<NC>
111+
}
112+
113+
114+
impl<'a, C, NC> VideosInsertBuilder<'a, C, NC>
115+
where NC: hyper::net::NetworkConnector,
116+
C: BorrowMut<hyper::Client<NC>> + 'a {
117+
30118
}
31119

32120

33121

34122
#[cfg(test)]
35123
mod tests {
124+
use std::default::Default;
125+
use super::*;
126+
use hyper;
36127

128+
use std::cell::RefCell;
129+
130+
#[test]
131+
fn insert() {
132+
let c = RefCell::new(hyper::Client::new());
133+
let s = Service::new(&c);
134+
let v = <Video as Default>::default();
135+
// todo: set data
136+
let mut ib = s.insert("id, snippet", &v);
137+
}
37138

38139

39140
}

0 commit comments

Comments
 (0)