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

Skip to content

Commit 687ace4

Browse files
authored
feat: support Range header for media downloads
All but ids that end with `files.export`.
1 parent 92d8782 commit 687ace4

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/generator/lib/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,5 +1342,13 @@ def unique(
13421342
return unique(original, desired, attempts + 1)
13431343

13441344

1345+
# Check if method supports Range header.
1346+
# files.export doesn't support Range headers.
1347+
def supports_range_download(m):
1348+
return m.get("supportsMediaDownload", False) and not m.get("id", "").endswith(
1349+
".files.export"
1350+
)
1351+
1352+
13451353
if __name__ == "__main__":
13461354
raise AssertionError("For import only")

src/generator/templates/api/lib/mbuild.mako

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
re_find_replacements, ADD_PARAM_FN, ADD_PARAM_MEDIA_EXAMPLE, upload_action_fn, METHODS_RESOURCE,
1313
method_name_to_variant, size_to_bytes, method_default_scope,
1414
is_repeated_property, setter_fn_name, ADD_SCOPE_FN, ADD_SCOPES_FN, rust_doc_sanitize,
15-
CLEAR_SCOPES_FN, items, string_impl)
15+
CLEAR_SCOPES_FN, items, string_impl, supports_range_download)
1616
1717
SIMPLE = "simple"
1818
RESUMABLE = "resumable"
@@ -130,6 +130,9 @@ pub struct ${ThisType}
130130
${activity_rust_type(schemas, p)},
131131
% endif
132132
% endfor
133+
% if supports_range_download(m):
134+
_range: Option<String>,
135+
% endif
133136
## A generic map for additinal parameters. Sometimes you can set some that are documented online only
134137
${api.properties.params}: HashMap<String, String>,
135138
% if method_default_scope(m):
@@ -211,6 +214,19 @@ ${self._setter_fn(resource, method, m, p, part_prop, ThisType, c)}\
211214
self
212215
}
213216
% endif
217+
% if supports_range_download(m):
218+
219+
/// Sets the *Range* header for partial downloads.
220+
///
221+
/// Use this to download only a portion of the file by specifying a byte range.
222+
/// For example: "bytes=0-1023" downloads the first 1024 bytes.
223+
///
224+
/// This is only effective when using `alt=media` parameter.
225+
pub fn range(mut self, value: impl Into<String>) -> ${ThisType} {
226+
self._range = Some(value.into());
227+
self
228+
}
229+
% endif
214230
}
215231
</%def>
216232

@@ -727,6 +743,12 @@ else {
727743
}
728744
% endif
729745
746+
% if supports_range_download(m):
747+
if let Some(range_value) = self._range.as_ref() {
748+
req_builder = req_builder.header("Range", range_value.clone());
749+
}
750+
% endif
751+
730752
% if resumable_media_param:
731753
upload_url_from_server = true;
732754
if protocol == ${PROTOCOL_TYPE_MAP[resumable_media_param.protocol]} {

src/generator/templates/api/lib/rbuild.mako

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
rust_copy_value_s, organize_params, REQUEST_VALUE_PROPERTY_NAME,
77
build_all_params, rb_type_params_s, hub_type_params_s, mb_type_params_s, mb_additional_type_params,
88
struct_type_bounds_s, METHODS_RESOURCE, SPACES_PER_TAB, prefix_all_but_first_with,
9-
METHODS_BUILDER_MARKER_TRAIT, remove_empty_lines, method_default_scope, rust_doc_sanitize)
9+
METHODS_BUILDER_MARKER_TRAIT, remove_empty_lines, method_default_scope, rust_doc_sanitize,
10+
supports_range_download)
1011
%>\
1112
<%namespace name="util" file="../../../lib/util.mako"/>\
1213
<%namespace name="lib" file="lib.mako"/>\
@@ -119,6 +120,9 @@ impl${rb_params} ${ThisType} {
119120
% for p in optional_props:
120121
${property(p.name)}: Default::default(),
121122
% endfor
123+
% if supports_range_download(m):
124+
_range: Default::default(),
125+
% endif
122126
% for prop_key, custom_name in api.properties.items():
123127
% if prop_key == 'scopes' and not method_default_scope(m):
124128
<% continue %>\

0 commit comments

Comments
 (0)