Skip to content

Commit

Permalink
feat(doit): simplify delegate calls
Browse files Browse the repository at this point in the history
Now we use the DefaultDelegate as standin in case there is user-delgate.
That way, we save plenty of complexity as no additional
`if let Some(ref mut dlg) = delegate` is necesary.

Fixes #30
  • Loading branch information
Byron committed Mar 19, 2015
1 parent 3a15430 commit 265b448
Show file tree
Hide file tree
Showing 5 changed files with 1,507 additions and 1,305 deletions.
2 changes: 1 addition & 1 deletion gen/youtube3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extern crate hyper;
extern crate "yup-oauth2" as oauth2;
extern crate "rustc-serialize" as rustc_serialize;
extern crate "google-youtube3" as youtube3;
use youtube3::cmn::Result;
use youtube3::Result;
use std::default::Default;
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
use youtube3::YouTube;
Expand Down
26 changes: 25 additions & 1 deletion gen/youtube3/src/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oauth2;
use hyper;
use hyper::header::{ContentType, ContentLength, Headers};
use hyper::http::LINE_ENDING;
use hyper::method::Method;

/// Identifies the Hub. There is only one per library, this trait is supposed
/// to make intended use more explicit.
Expand Down Expand Up @@ -58,6 +59,14 @@ pub struct JsonServerError {
/// uploading media
pub trait Delegate {

/// Called at the beginning of any API request. The delegate should store the method
/// information if he is interesting in knowing more context when further calls to it
/// are made.
/// The matching `finished()` call will always be made, no matter whether or not the API
/// request was sucessfull. That way, the delgate may easily maintain a clean state
/// between various API calls.
fn begin(&mut self, MethodInfo) {}

/// Called whenever there is an [HttpError](http://hyperium.github.io/hyper/hyper/error/enum.HttpError.html), usually if there are network problems.
///
/// Return retry information.
Expand Down Expand Up @@ -90,9 +99,18 @@ pub trait Delegate {

/// Called prior to sending the main request of the given method. It can be used to time
/// the call or to print progress information.
fn pre_request(&mut self, method_name: &str) { let _ = method_name; }
/// It's also useful as you can be sure that a request will definitely be made.
fn pre_request(&mut self) { }


/// Called before the API request method returns, in every case. It can be used to clean up
/// internal state between calls to the API.
/// This call always has a matching call to `begin(...)`.
fn finished(&mut self) {}
}

/// A delegate with a conservative default implementation, which is used if no other delegate is
/// set.
#[derive(Default)]
pub struct DefaultDelegate;

Expand Down Expand Up @@ -121,6 +139,12 @@ pub enum Result<T = ()> {
Success(T),
}

/// Contains information about an API request.
pub struct MethodInfo {
pub id: &'static str,
pub http_method: Method,
}

const BOUNDARY: &'static str = "MDuXWGyeE33QFXGchb2VFWc4Z7945d";

/// Provides a `Read` interface that converts multiple parts into the protocol
Expand Down
Loading

0 comments on commit 265b448

Please sign in to comment.