Skip to content

Commit

Permalink
feat(service): added authenticator arg
Browse files Browse the repository at this point in the history
That will allow interaction between client and authentication attempts.
It also shows how cumbersome it is to deal with all these
generics ... but hey, you gotta do what you gotta do.

If boxes of pointers would be used, it would be easier to handle, but
enforces a certain memory model. That, of course, is not desired.
  • Loading branch information
Byron committed Mar 1, 2015
1 parent 6fe9a5d commit f13c296
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
*.sublime-workspace
*.xml
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]

name = "youtube3"
name = "youtube3-dev"
version = "0.0.1"
authors = ["Sebastian Thiel <[email protected]>"]
description = "A library to facilitate interacting with your youtube account"
Expand All @@ -9,10 +9,13 @@ license = "MIT"
keywords = ["youtube", "google", "protocol"]

[dependencies]
# Just to get hyper to work !
openssl = "= 0.4.3"
# Just to get hyper to work !
cookie = "= 0.1.13"
hyper = "*"
rustc-serialize = "*"
yup-oauth2 = "*"

[dev-dependencies]
yup-hyper-mock = "*"
49 changes: 31 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#![feature(core)]
//!
//! # Usage
//!
//! ```test_harness
//! extern crate youtube3;
//! extern crate "youtube3-dev" as youtube3;
//! extern crate hyper;
//!
//! # #[test]
//! # fn test() {
//! let youtube = youtube3::new(hyper::Client::new());
//! youtube.videos();
//! # // TODO - generate !
//! # }
//! ```
extern crate hyper;
extern crate "rustc-serialize" as rustc_serialize;
extern crate "yup-oauth2" as oauth2;

use std::marker::PhantomData;
use std::borrow::BorrowMut;
Expand All @@ -21,48 +24,58 @@ pub mod videos;


/// Central instance to access all youtube related services
pub struct YouTube<C, NC> {
pub struct YouTube<C, NC, A> {
client: RefCell<C>,

auth: RefCell<A>,
_m: PhantomData<NC>
}

impl<'a, C, NC> YouTube<C, NC>
impl<'a, C, NC, A> YouTube<C, NC, A>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> + 'a {
C: BorrowMut<hyper::Client<NC>> + 'a,
A: oauth2::GetToken {

pub fn new(client: C) -> YouTube<C, NC> {
pub fn new(client: C, authenticator: A) -> YouTube<C, NC, A> {
YouTube {
client: RefCell::new(client),
auth: RefCell::new(authenticator),
_m: PhantomData,
}
}

pub fn videos(&'a self) -> videos::Service<'a, C, NC> {
videos::Service::new(&self.client)
pub fn videos(&'a self) -> videos::Service<'a, C, NC, A> {
videos::Service::new(&self.client, &self.auth)
}
}


pub fn new<C, NC>(client: C) -> YouTube<C, NC>
pub fn new<C, NC, A>(client: C, authenticator: A) -> YouTube<C, NC, A>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> {
YouTube::new(client)
C: BorrowMut<hyper::Client<NC>>,
A: oauth2::GetToken {
YouTube::new(client, authenticator)
}

#[cfg(test)]
mod tests {
use super::*;
use hyper;
use oauth2;

use std::default::Default;


#[test]
fn instantiate() {
let yt = YouTube::new(hyper::Client::new());
let v = yt.videos();
let secret = <oauth2::ApplicationSecret as Default>::default();
let auth = oauth2::Authenticator::new(
&secret,
oauth2::DefaultAuthenticatorDelegate,
hyper::Client::new(),
<oauth2::MemoryStorage as Default>::default(),
None);
let yt = YouTube::new(hyper::Client::new(), auth);

let mut c = hyper::Client::new();
let yt = YouTube::new(&mut c);
let v = yt.videos();
let v = yt.videos().insert("snippet", &Default::default());
}
}
42 changes: 19 additions & 23 deletions src/videos/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::marker::PhantomData;
use rustc_serialize;

use hyper;
use oauth2;

/// Reresents all aspects of a youtube video resource. May only be partially
/// available
Expand Down Expand Up @@ -69,51 +70,59 @@ pub struct GeoPoint {
}

/// The videos service - provides actual functionality through builders.
pub struct Service<'a, C, NC>
pub struct Service<'a, C, NC, A>
where NC: 'a,
C: 'a {
C: 'a,
A: 'a, {

client: &'a RefCell<C>,
auth: &'a RefCell<A>,

_m: PhantomData<NC>
}

impl<'a, C, NC> Service<'a, C, NC>
impl<'a, C, NC, A> Service<'a, C, NC, A>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> + 'a {
C: BorrowMut<hyper::Client<NC>> + 'a,
A: oauth2::GetToken + 'a {

pub fn new(client: &'a RefCell<C>) -> Service<'a, C, NC> {
pub fn new(client: &'a RefCell<C>, authenticator: &'a RefCell<A>) -> Service<'a, C, NC, A> {
Service {
client: client,
auth: authenticator,
_m: PhantomData,
}
}

pub fn insert(&self, parts: &str, video: &Video) -> VideosInsertBuilder<'a, C, NC> {
pub fn insert(&self, parts: &str, video: &Video) -> VideosInsertBuilder<'a, C, NC, A> {
VideosInsertBuilder {
client: self.client,
auth: self.auth,
video: video.clone(),
parts: parts.to_string(),
_m: PhantomData,
}
}
}

pub struct VideosInsertBuilder<'a, C, NC>
pub struct VideosInsertBuilder<'a, C, NC, A>
where NC: 'a,
C: 'a {
C: 'a,
A: 'a {

client: &'a RefCell<C>,
auth: &'a RefCell<A>,
video: Video,
parts: String,

_m: PhantomData<NC>
}


impl<'a, C, NC> VideosInsertBuilder<'a, C, NC>
impl<'a, C, NC, A> VideosInsertBuilder<'a, C, NC, A>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> + 'a {
C: BorrowMut<hyper::Client<NC>> + 'a,
A: oauth2::GetToken + 'a {

}

Expand All @@ -123,18 +132,5 @@ impl<'a, C, NC> VideosInsertBuilder<'a, C, NC>
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);
}


}

0 comments on commit f13c296

Please sign in to comment.