Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a58b0b

Browse files
committed
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.
1 parent 814c9c9 commit 9a58b0b

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/mako/lib.rs.mako

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use std::default::Default;
4444
use std::collections::BTreeMap;
4545
use std::io;
4646
use std::fs;
47+
use std::old_io::timer::sleep;
4748

4849
use cmn::{Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate};
4950

src/mako/lib/mbuild.mako

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
hide_rust_doc_test, build_all_params, REQUEST_VALUE_PROPERTY_NAME, organize_params,
88
indent_by, to_rust_type, rnd_arg_val_for_type, extract_parts, mb_type_params_s,
99
hub_type_params_s, method_media_params, enclose_in, mb_type_bounds, method_response,
10-
METHOD_BUILDER_MARKERT_TRAIT, pass_through, markdown_rust_block, parts_from_params)
10+
METHOD_BUILDER_MARKERT_TRAIT, pass_through, markdown_rust_block, parts_from_params,
11+
DELEGATE_PROPERTY_NAME)
1112
1213
def get_parts(part_prop):
1314
if not part_prop:
@@ -359,9 +360,18 @@ match result {
359360
field_params = [p for p in params if p.get('is_query_param', True)]
360361
361362
paddfields = 'self.' + api.properties.params
363+
364+
delegate = 'self.' + property(DELEGATE_PROPERTY_NAME)
362365
%>
363366
/// Perform the operation you have build so far.
364367
${action_fn} {
368+
use hyper::method::Method;
369+
use hyper::header::UserAgent;
370+
% if request_value or response_schema:
371+
use hyper::header::ContentType;
372+
use mime::{Mime, TopLevel, SubLevel};
373+
use rustc_serialize::json;
374+
% endif
365375
let mut params: Vec<(&str, String)> = Vec::with_capacity(${len(params)} + ${paddfields}.len());
366376
% for p in field_params:
367377
<%
@@ -413,12 +423,41 @@ else {
413423
unreachable!()
414424
};
415425
% else:
416-
let mut url = "${baseUrl}".to_string();
426+
let mut url = "${baseUrl}${m.path}".to_string();
417427
% endif
418428
419429
url.push('?');
420430
url.push_str(&url::form_urlencoded::serialize(params.iter().map(|t| (t.0, t.1.as_slice()))));
421431
432+
loop {
433+
match self.hub.client.borrow_mut().borrow_mut().request(Method::Extension("${m.httpMethod}".to_string()), &url)
434+
.header(UserAgent("google-api-rust-client/${cargo.build_version}".to_string()))
435+
% if request_value:
436+
.header(ContentType(Mime(TopLevel::Application, SubLevel::Json, Default::default())))
437+
.body(json::encode(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap())
438+
% endif
439+
.send() {
440+
Err(err) => {
441+
if ${delegate}.is_some() {
442+
match ${delegate}.as_mut().unwrap().http_error(&err) {
443+
oauth2::Retry::Abort => return cmn::Result::HttpError(err),
444+
oauth2::Retry::After(d) => {
445+
sleep(d);
446+
continue;
447+
}
448+
}
449+
} else {
450+
return cmn::Result::HttpError(err);
451+
}
452+
}
453+
Ok(mut res) => {
454+
455+
break;
456+
}
457+
}
458+
}
459+
460+
422461
% if response_schema:
423462
let response: ${response_schema.id} = Default::default();
424463
% else:

src/mako/lib/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
PART_MARKER_TRAIT = 'Part'
6262
NESTED_MARKER_TRAIT = 'NestedType'
6363
REQUEST_VALUE_PROPERTY_NAME = 'request'
64+
DELEGATE_PROPERTY_NAME = 'delegate'
6465

6566
PROTOCOL_TYPE_INFO = {
6667
'simple' : {
@@ -541,7 +542,7 @@ def build_all_params(c, m):
541542
if request_value:
542543
params.insert(0, schema_to_required_property(request_value, REQUEST_VALUE_PROPERTY_NAME))
543544
# add the delegate. It's a type parameter, which has to remain in sync with the type-parameters we actually build.
544-
dp = type(m)({ 'name': 'delegate',
545+
dp = type(m)({ 'name': DELEGATE_PROPERTY_NAME,
545546
TREF: "&'a mut %s" % DELEGATE_TYPE,
546547
'input_type': "&'a mut %s" % DELEGATE_TYPE,
547548
'clone_value': '{}',

src/rust/cmn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ struct JsonServerError {
5353
/// uploading media
5454
pub trait Delegate {
5555

56-
/// Called whenever there is an HttpError, usually if there are network problems.
56+
/// Called whenever there is an [HttpError](http://hyperium.github.io/hyper/hyper/error/enum.HttpError.html), usually if there are network problems.
5757
///
5858
/// Return retry information.
59-
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
59+
fn http_error(&mut self, &hyper::HttpError) -> oauth2::Retry {
6060
oauth2::Retry::Abort
6161
}
6262
}
@@ -77,4 +77,4 @@ pub enum Result<T = ()> {
7777

7878
/// It worked !
7979
Success(T),
80-
}
80+
}

0 commit comments

Comments
 (0)