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 !
This commit is contained in:
Sebastian Thiel
2015-02-28 19:13:51 +01:00
parent d4869cfefc
commit aaf432fb54
3 changed files with 183 additions and 3 deletions

View File

@@ -1,3 +1,68 @@
#[test]
fn it_works() {
#![feature(core)]
//! # Usage
//! ```test_harness
//! extern crate youtube3;
//! extern crate hyper;
//!
//! # #[test]
//! # fn test() {
//! let youtube = youtube3::new(hyper::Client::new());
//! youtube.videos();
//! # }
extern crate hyper;
extern crate "rustc-serialize" as rustc_serialize;
use std::marker::PhantomData;
use std::borrow::BorrowMut;
use std::cell::RefCell;
mod common;
pub mod videos;
/// Central instance to access all youtube related services
pub struct YouTube<C, NC> {
client: RefCell<C>,
_m: PhantomData<NC>
}
impl<'a, C, NC> YouTube<C, NC>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> + 'a {
pub fn new(client: C) -> YouTube<C, NC> {
YouTube {
client: RefCell::new(client),
_m: PhantomData,
}
}
pub fn videos(&'a self) -> videos::Service<'a, C, NC> {
videos::Service::new(&self.client)
}
}
pub fn new<C, NC>(client: C) -> YouTube<C, NC>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> {
YouTube::new(client)
}
#[cfg(test)]
mod tests {
use super::*;
use hyper;
#[test]
fn instantiate() {
let yt = YouTube::new(hyper::Client::new());
let v = yt.videos();
let mut c = hyper::Client::new();
let yt = YouTube::new(&mut c);
let v = yt.videos();
}
}

View File

@@ -6,6 +6,67 @@ use rustc_serialize;
use hyper;
/// Reresents all aspects of a youtube video resource. May only be partially
/// available
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct Video {
pub snippet: Option<VideoSnippet>,
pub recordingDetails: Option<VideoRecordingDetails>,
pub status: Option<VideoStatus>,
}
#[allow(non_snake_case)]
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct VideoSnippet {
pub categoryId: String,
pub description: String,
pub tags: Vec<String>,
pub title: String,
pub status: Option<VideoStatus>,
pub recordingDetails: Option<VideoRecordingDetails>,
}
impl Video {
fn parts(&self) -> String {
let mut res = String::new();
if self.status.is_some() {
res = res + "status,";
}
if self.recordingDetails.is_some() {
res = res + "recordingDetails";
}
if self.snippet.is_some() {
res = res + "snippet,";
}
res
}
}
#[allow(non_snake_case)]
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct VideoStatus {
pub privacyStatus: String,
pub embeddable: bool,
pub license: String,
pub publicStatsViewable: bool,
pub publishAt: String,
}
#[allow(non_snake_case)]
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct VideoRecordingDetails {
locationDescription: String,
recordingDate: String,
}
#[allow(non_snake_case)]
#[derive(RustcEncodable, RustcDecodable, Default, Clone)]
pub struct GeoPoint {
altitude: f64,
latitude: f64,
longitude: f64,
}
/// The videos service - provides actual functionality through builders.
pub struct Service<'a, C, NC>
@@ -27,13 +88,53 @@ impl<'a, C, NC> Service<'a, C, NC>
_m: PhantomData,
}
}
pub fn insert(&self, parts: &str, video: &Video) -> VideosInsertBuilder<'a, C, NC> {
VideosInsertBuilder {
client: self.client,
video: video.clone(),
parts: parts.to_string(),
_m: PhantomData,
}
}
}
pub struct VideosInsertBuilder<'a, C, NC>
where NC: 'a,
C: 'a {
client: &'a RefCell<C>,
video: Video,
parts: String,
_m: PhantomData<NC>
}
impl<'a, C, NC> VideosInsertBuilder<'a, C, NC>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> + 'a {
}
#[cfg(test)]
mod tests {
use std::default::Default;
use super::*;
use hyper;
use std::cell::RefCell;
#[test]
fn insert() {
let c = RefCell::new(hyper::Client::new());
let s = Service::new(&c);
let v = <Video as Default>::default();
// todo: set data
let mut ib = s.insert("id, snippet", &v);
}
}