fix(mbuild): simplification and cleanup

* renamed `*MethodsBuilder` type to `*Methods` type
* renamed `*CallBuilder` type to `*Call` type
* greatly simplified `doit()` signature if uploads are involved
* pass `auth` to upload helper
This commit is contained in:
Sebastian Thiel
2015-03-21 16:27:41 +01:00
parent 307d3f487c
commit 4bf280079e
7 changed files with 1092 additions and 1095 deletions

View File

@@ -10,51 +10,51 @@ The `youtube3` library allows access to all features of *YouTube*.
Handle the following *Resources* with ease from the central [hub](http://byron.github.io/google-apis-rs/google-youtube3/struct.YouTube.html) ...
* [activities](http://byron.github.io/google-apis-rs/google-youtube3/struct.Activity.html)
* [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ActivityInsertCallBuilder.html) and [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ActivityListCallBuilder.html)
* [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ActivityInsertCall.html) and [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ActivityListCall.html)
* channel banners
* [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelBannerInsertCallBuilder.html)
* [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelBannerInsertCall.html)
* [channel sections](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSection.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionListCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionUpdateCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionListCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelSectionUpdateCall.html)
* [channels](http://byron.github.io/google-apis-rs/google-youtube3/struct.Channel.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelListCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelUpdateCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelListCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelUpdateCall.html)
* [guide categories](http://byron.github.io/google-apis-rs/google-youtube3/struct.GuideCategory.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.GuideCategoryListCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.GuideCategoryListCall.html)
* [i18n languages](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nLanguage.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nLanguageListCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nLanguageListCall.html)
* [i18n regions](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nRegion.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nRegionListCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.I18nRegionListCall.html)
* [live broadcasts](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcast.html)
* [*bind*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastBindCallBuilder.html), [*control*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastControlCallBuilder.html), [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastListCallBuilder.html), [*transition*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastTransitionCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastUpdateCallBuilder.html)
* [*bind*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastBindCall.html), [*control*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastControlCall.html), [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastListCall.html), [*transition*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastTransitionCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveBroadcastUpdateCall.html)
* [live streams](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStream.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamListCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamUpdateCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamListCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.LiveStreamUpdateCall.html)
* [playlist items](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItem.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemListCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemUpdateCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemListCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemUpdateCall.html)
* [playlists](http://byron.github.io/google-apis-rs/google-youtube3/struct.Playlist.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistListCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistUpdateCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistListCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistUpdateCall.html)
* search
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SearchListCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SearchListCall.html)
* [subscriptions](http://byron.github.io/google-apis-rs/google-youtube3/struct.Subscription.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionDeleteCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionInsertCallBuilder.html) and [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionListCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionDeleteCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionInsertCall.html) and [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.SubscriptionListCall.html)
* [thumbnails](http://byron.github.io/google-apis-rs/google-youtube3/struct.Thumbnail.html)
* [*set*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ThumbnailSetCallBuilder.html)
* [*set*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ThumbnailSetCall.html)
* [video categories](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoCategory.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoCategoryListCallBuilder.html)
* [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoCategoryListCall.html)
* [videos](http://byron.github.io/google-apis-rs/google-youtube3/struct.Video.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoDeleteCallBuilder.html), [*get rating*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoGetRatingCallBuilder.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoInsertCallBuilder.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoListCallBuilder.html), [*rate*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoRateCallBuilder.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoUpdateCallBuilder.html)
* [*delete*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoDeleteCall.html), [*get rating*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoGetRatingCall.html), [*insert*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoInsertCall.html), [*list*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoListCall.html), [*rate*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoRateCall.html) and [*update*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoUpdateCall.html)
* watermarks
* [*set*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkSetCallBuilder.html) and [*unset*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkUnsetCallBuilder.html)
* [*set*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkSetCall.html) and [*unset*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkUnsetCall.html)
Upload supported by ...
* [*set watermarks*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkSetCallBuilder.html)
* [*insert channel banners*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelBannerInsertCallBuilder.html)
* [*set thumbnails*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ThumbnailSetCallBuilder.html)
* [*insert videos*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoInsertCallBuilder.html)
* [*set watermarks*](http://byron.github.io/google-apis-rs/google-youtube3/struct.WatermarkSetCall.html)
* [*insert channel banners*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ChannelBannerInsertCall.html)
* [*set thumbnails*](http://byron.github.io/google-apis-rs/google-youtube3/struct.ThumbnailSetCall.html)
* [*insert videos*](http://byron.github.io/google-apis-rs/google-youtube3/struct.VideoInsertCall.html)
Subscription supported by ...
* [*list playlist items*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemListCallBuilder.html)
* [*list playlist items*](http://byron.github.io/google-apis-rs/google-youtube3/struct.PlaylistItemListCall.html)
Everything else about the *YouTube* *v3* API can be found at the

View File

@@ -304,17 +304,19 @@ impl_header!(XUploadContentType,
Mime);
/// A utility type to perform a resumable upload from start to end.
pub struct ResumableUploadHelper<'a, NC: 'a> {
pub struct ResumableUploadHelper<'a, NC: 'a, A: 'a> {
pub client: &'a mut hyper::client::Client<NC>,
pub delegate: &'a mut Delegate,
pub auth: &'a mut A,
pub url: &'a str,
pub reader: &'a mut ReadSeek,
pub media_type: Mime,
pub content_size: u64
}
impl<'a, NC> ResumableUploadHelper<'a, NC>
where NC: hyper::net::NetworkConnector {
impl<'a, NC, A> ResumableUploadHelper<'a, NC, A>
where NC: hyper::net::NetworkConnector,
A: oauth2::GetToken {
pub fn upload(&'a mut self) -> hyper::HttpResult<hyper::client::Response> {
Err(hyper::error::HttpError::HttpStatusError)
}

File diff suppressed because it is too large Load Diff

View File

@@ -127,7 +127,7 @@ ${schema.new(s, c)}
% endif
// ###################
// CallBuilders ###
// MethodBuilders ###
// #################
% for resource in c.rta_map:

View File

@@ -396,27 +396,20 @@ match result {
rtype = 'Result<(hyper::client::Response, %s)>' % (unique_type_name(response_schema.id))
mtype_param = 'RS'
mtype_where = 'ReadSeek'
possible_urls = [m.path]
simple_media_param = None
resumable_media_param = None
if media_params:
stripped = lambda s: s.strip().strip(',')
type_params = '<%s>' % mtype_param
qualifier = ''
type_params = mtype_param
where = mtype_param + ': ' + mtype_where
where = '\n\t\twhere ' + mtype_param + ': ReadSeek'
add_args = (', mut reader: %s, reader_mime_type: mime::Mime' % mtype_param) + ", protocol: &'static str"
for p in media_params:
add_args += 'mut ' + p.type.arg_name + ': ' + ('Option<(%s, mime::Mime)>' % mtype_param) + ', '
possible_urls.append(p.path)
if p.protocol == 'simple':
simple_media_param = p
elif p.protocol == 'resumable':
resumable_media_param = p
# end for each param
where = ' where ' + stripped(where)
type_params = '<' + stripped(type_params) + '>'
add_args = ', ' + stripped(add_args)
# end handle media params
action_fn = qualifier + 'fn ' + api.terms.action + type_params + ('(mut self%s)' % add_args) + ' -> ' + rtype + where
@@ -563,15 +556,15 @@ match result {
% endif ## response schema
% if media_params:
let (mut url, protocol) = \
let mut url = \
% for mp in media_params:
% if loop.first:
if \
% else:
else if \
% endif
${mp.type.arg_name}.is_some() {
("${join_url(rootUrl, mp.path)}".to_string(), "${mp.protocol}")
protocol == "${mp.protocol}" {
"${join_url(rootUrl, mp.path)}".to_string()
} \
% endfor
else {
@@ -673,22 +666,22 @@ else {
% if request_value:
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();
% endif
let mut req_result = {
% if request_value and simple_media_param:
let mut mp_reader: MultiPartReader = Default::default();
let (mut body_reader, content_type) = match ${simple_media_param.type.arg_name}.as_mut() {
Some(&mut (ref mut reader, ref mime)) => {
mp_reader.reserve_exact(2);
${READER_SEEK | indent_all_but_first_by(5)}
mp_reader.add_part(&mut request_value_reader, request_size, json_mime_type.clone())
.add_part(reader, size, mime.clone());
let mime_type = mp_reader.mime_type();
(&mut mp_reader as &mut io::Read, ContentType(mime_type))
},
None => (&mut request_value_reader as &mut io::Read, ContentType(json_mime_type.clone())),
};
let mut mp_reader: MultiPartReader = Default::default();
let (mut body_reader, content_type) = match protocol {
"${simple_media_param.protocol}" => {
mp_reader.reserve_exact(2);
${READER_SEEK | indent_all_but_first_by(5)}
mp_reader.add_part(&mut request_value_reader, request_size, json_mime_type.clone())
.add_part(&mut reader, size, reader_mime_type.clone());
let mime_type = mp_reader.mime_type();
(&mut mp_reader as &mut io::Read, ContentType(mime_type))
},
_ => (&mut request_value_reader as &mut io::Read, ContentType(json_mime_type.clone())),
};
% endif
let mut req_result = {
let mut client = &mut *self.hub.client.borrow_mut();
let mut req = client.borrow_mut().request(${method_name_to_variant(m.httpMethod)}, url.as_slice())
.header(UserAgent(self.hub._user_agent.clone()))\
@@ -708,23 +701,23 @@ else {
.body(body_reader.into_body())\
% endif ## not simple_media_param
% endif
;
;
% if simple_media_param and not request_value:
if let Some(&mut (ref mut reader, ref mime)) = ${simple_media_param.type.arg_name}.as_mut() {
if protocol == "${simple_media_param.protocol}" {
${READER_SEEK | indent_all_but_first_by(4)}
req = req.header(ContentType(mime.clone()))
req = req.header(ContentType(reader_mime_type.clone()))
.header(ContentLength(size))
.body(reader.into_body());
}
% endif ## media upload handling
% if resumable_media_param:
if let Some(&mut (_, ref mime)) = ${resumable_media_param.type.arg_name}.as_mut() {
req = req.header(cmn::XUploadContentType(mime.clone()));
if protocol == "${resumable_media_param.protocol}" {
req = req.header(cmn::XUploadContentType(reader_mime_type.clone()));
}
% endif
dlg.pre_request();
req.send()
req.send()
};
match req_result {
@@ -749,17 +742,17 @@ else {
return Result::Failure(res)
}
% if resumable_media_param:
if let Some((ref mut reader, ref mime)) = ${resumable_media_param.type.arg_name} {
let request_size = reader.seek(io::SeekFrom::End(0)).unwrap();
reader.seek(io::SeekFrom::Start(0)).unwrap();
if protocol == "${resumable_media_param.protocol}" {
${READER_SEEK | indent_all_but_first_by(6)}
let mut client = &mut *self.hub.client.borrow_mut();
match (cmn::ResumableUploadHelper {
client: &mut client.borrow_mut(),
delegate: dlg,
auth: &mut *self.hub.auth.borrow_mut(),
url: &res.headers.get::<hyper::header::Location>().expect("Location header is part of protocol").0,
reader: reader,
media_type: mime.clone(),
content_size: request_size
reader: &mut reader,
media_type: reader_mime_type.clone(),
content_size: size
}.upload()) {
Err(err) => {
## Do not ask the delgate again, as it was asked by the helper !
@@ -811,16 +804,8 @@ if enable_resource_parsing \
/// * *${split_camelcase_s(item_name)}*: ${isinstance(item, (list, tuple)) and put_and(enclose_in("'", item)) or str(item)}
% endfor
pub fn ${upload_action_fn(api.terms.upload_action, p.type.suffix)}<${mtype_param}>(self, ${p.type.arg_name}: ${mtype_param}, mime_type: mime::Mime) -> ${rtype}
where ${mtype_param}: ${mtype_where} {
self.${api.terms.action}(\
% for _ in range(0, loop.index):
None, \
% endfor
Some((${p.type.arg_name}, mime_type)), \
% for _ in range(loop.index+1, len(media_params)):
None, \
% endfor
)
where ${mtype_param}: ReadSeek {
self.${api.terms.action}(${p.type.arg_name}, mime_type, "${p.protocol}")
}
% endfor
</%def>

View File

@@ -787,7 +787,7 @@ def library_to_crate_name(name):
# return type name of a resource method builder, from a resource name
def rb_type(r):
return "%sMethodsBuilder" % singular(canonical_type_name(r))
return "%sMethods" % singular(canonical_type_name(r))
def _to_type_params_s(p):
return '<%s>' % ', '.join(p)
@@ -837,7 +837,7 @@ def unique_type_name(type_name):
# return type name for a method on the given resource
def mb_type(r, m):
return "%s%sCallBuilder" % (singular(canonical_type_name(r)), dot_sep_to_canonical_type_name(m))
return "%s%sCall" % (singular(canonical_type_name(r)), dot_sep_to_canonical_type_name(m))
# canonicalName = util.canonical_name()
def hub_type(schemas, canonicalName):

View File

@@ -302,17 +302,19 @@ impl_header!(XUploadContentType,
Mime);
/// A utility type to perform a resumable upload from start to end.
pub struct ResumableUploadHelper<'a, NC: 'a> {
pub struct ResumableUploadHelper<'a, NC: 'a, A: 'a> {
pub client: &'a mut hyper::client::Client<NC>,
pub delegate: &'a mut Delegate,
pub auth: &'a mut A,
pub url: &'a str,
pub reader: &'a mut ReadSeek,
pub media_type: Mime,
pub content_size: u64
}
impl<'a, NC> ResumableUploadHelper<'a, NC>
where NC: hyper::net::NetworkConnector {
impl<'a, NC, A> ResumableUploadHelper<'a, NC, A>
where NC: hyper::net::NetworkConnector,
A: oauth2::GetToken {
pub fn upload(&'a mut self) -> hyper::HttpResult<hyper::client::Response> {
Err(hyper::error::HttpError::HttpStatusError)
}