feat(doit): attempt to send json-encoded request

This doesn't work yet, as I am unable to unwrap the client properly.
It's a refcell that contains a BorrowMut to a hyper::Client, and
lets just, it's complicated.
This commit is contained in:
Sebastian Thiel
2015-03-16 10:35:26 +01:00
parent 814c9c9ffa
commit 9a58b0badd
4 changed files with 47 additions and 6 deletions

View File

@@ -44,6 +44,7 @@ use std::default::Default;
use std::collections::BTreeMap;
use std::io;
use std::fs;
use std::old_io::timer::sleep;
use cmn::{Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate};

View File

@@ -7,7 +7,8 @@
hide_rust_doc_test, build_all_params, REQUEST_VALUE_PROPERTY_NAME, organize_params,
indent_by, to_rust_type, rnd_arg_val_for_type, extract_parts, mb_type_params_s,
hub_type_params_s, method_media_params, enclose_in, mb_type_bounds, method_response,
METHOD_BUILDER_MARKERT_TRAIT, pass_through, markdown_rust_block, parts_from_params)
METHOD_BUILDER_MARKERT_TRAIT, pass_through, markdown_rust_block, parts_from_params,
DELEGATE_PROPERTY_NAME)
def get_parts(part_prop):
if not part_prop:
@@ -359,9 +360,18 @@ match result {
field_params = [p for p in params if p.get('is_query_param', True)]
paddfields = 'self.' + api.properties.params
delegate = 'self.' + property(DELEGATE_PROPERTY_NAME)
%>
/// Perform the operation you have build so far.
${action_fn} {
use hyper::method::Method;
use hyper::header::UserAgent;
% if request_value or response_schema:
use hyper::header::ContentType;
use mime::{Mime, TopLevel, SubLevel};
use rustc_serialize::json;
% endif
let mut params: Vec<(&str, String)> = Vec::with_capacity(${len(params)} + ${paddfields}.len());
% for p in field_params:
<%
@@ -413,12 +423,41 @@ else {
unreachable!()
};
% else:
let mut url = "${baseUrl}".to_string();
let mut url = "${baseUrl}${m.path}".to_string();
% endif
url.push('?');
url.push_str(&url::form_urlencoded::serialize(params.iter().map(|t| (t.0, t.1.as_slice()))));
loop {
match self.hub.client.borrow_mut().borrow_mut().request(Method::Extension("${m.httpMethod}".to_string()), &url)
.header(UserAgent("google-api-rust-client/${cargo.build_version}".to_string()))
% if request_value:
.header(ContentType(Mime(TopLevel::Application, SubLevel::Json, Default::default())))
.body(json::encode(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap())
% endif
.send() {
Err(err) => {
if ${delegate}.is_some() {
match ${delegate}.as_mut().unwrap().http_error(&err) {
oauth2::Retry::Abort => return cmn::Result::HttpError(err),
oauth2::Retry::After(d) => {
sleep(d);
continue;
}
}
} else {
return cmn::Result::HttpError(err);
}
}
Ok(mut res) => {
break;
}
}
}
% if response_schema:
let response: ${response_schema.id} = Default::default();
% else:

View File

@@ -61,6 +61,7 @@ METHOD_BUILDER_MARKERT_TRAIT = 'MethodBuilder'
PART_MARKER_TRAIT = 'Part'
NESTED_MARKER_TRAIT = 'NestedType'
REQUEST_VALUE_PROPERTY_NAME = 'request'
DELEGATE_PROPERTY_NAME = 'delegate'
PROTOCOL_TYPE_INFO = {
'simple' : {
@@ -541,7 +542,7 @@ def build_all_params(c, m):
if request_value:
params.insert(0, schema_to_required_property(request_value, REQUEST_VALUE_PROPERTY_NAME))
# add the delegate. It's a type parameter, which has to remain in sync with the type-parameters we actually build.
dp = type(m)({ 'name': 'delegate',
dp = type(m)({ 'name': DELEGATE_PROPERTY_NAME,
TREF: "&'a mut %s" % DELEGATE_TYPE,
'input_type': "&'a mut %s" % DELEGATE_TYPE,
'clone_value': '{}',

View File

@@ -53,10 +53,10 @@ struct JsonServerError {
/// uploading media
pub trait Delegate {
/// Called whenever there is an HttpError, usually if there are network problems.
/// 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.
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
fn http_error(&mut self, &hyper::HttpError) -> oauth2::Retry {
oauth2::Retry::Abort
}
}
@@ -77,4 +77,4 @@ pub enum Result<T = ()> {
/// It worked !
Success(T),
}
}