// GENERATED by tools/gen — DO NOT EDIT.
///|
/// How the following page is addressed; cursor names come from the API contract.
pub(all) enum PageKind {
Cursor(String)
NextUrl
}
///|
priv suberror PageDecodeError {
PageDecodeError(String)
}
///|
fn page_field(value : Json, path : String) -> Json? raise PageDecodeError {
let mut current = value
for part in path.split(".") {
match current {
@json.Json::Object(fields) =>
match fields.get(part.to_owned()) {
Option::Some(value) => current = value
Option::None => return Option::None
}
_ =>
raise PageDecodeError::PageDecodeError(
"pagination field path traverses a non-object",
)
}
}
Option::Some(current)
}
///|
/// Paginates JSON arrays by cursor query parameter or next URL. Offset pagination
/// is not supported. A page advances only after successful decoding.
pub fn[T : @json.FromJson] Gemini::paginate(
self : Gemini,
first : @http.Request,
kind~ : PageKind,
items~ : String,
next~ : String,
operation~ : String = self.infer_operation(first),
) -> @client.Paginator[T, Failure] {
let cursor : Ref[String?] = Ref::Ref(Option::None)
let done = Ref::Ref(false)
@client.Paginator::new(() => {
if done.val {
return Result::Ok(Option::None)
}
for ;; {
let request = match cursor.val {
Option::None => first
Option::Some(value) =>
match kind {
PageKind::NextUrl => { ..first, url: value, }
PageKind::Cursor(param) => {
let url = @http.Url::parse(first.url)
{
..first,
url: url.merge_query_params([(param, value)]).to_string(),
}
}
}
}
match self.send(request, operation~) {
Result::Err(error) => return Result::Err(error)
Result::Ok(reply) =>
try {
let json = @json.parse(@utf8.decode(reply.body))
let values = match page_field(json, items) {
Option::Some(@json.Json::Array(values)) => values
Option::None => []
_ =>
raise PageDecodeError::PageDecodeError(
"pagination items must be an array",
)
}
let next_cursor = match page_field(json, next) {
Option::Some(@json.Json::String(value)) if value != "" =>
Option::Some(value)
Option::Some(@json.Json::Null) | Option::None => Option::None
Option::Some(@json.Json::String(_)) => Option::None
_ =>
raise PageDecodeError::PageDecodeError(
"pagination next must be a string or null",
)
}
let typed : Array[T] = values.map(value => @json.from_json(value))
let next_cursor = match (kind, next_cursor) {
(PageKind::NextUrl, Option::Some(reference)) => {
let current = reply.attributes
.get(@client.final_url)
.unwrap_or(reply.request.url)
Option::Some(
@http.Url::parse(current)
.resolve(@http.Url::parse(reference))
.to_string(),
)
}
_ => next_cursor
}
cursor.val = next_cursor
done.val = next_cursor is Option::None
if !typed.is_empty() {
return Result::Ok(Option::Some(typed))
}
if done.val {
return Result::Ok(Option::None)
}
} catch {
error =>
return Result::Err(
Failure::Decode(
@client.ResponseFailure::Deserialization(
{ status: reply.status, headers: reply.headers, },
message=error.to_string(),
body=reply.body[:reply.body
.length()
.min(@client.max_error_body)].to_owned(),
),
),
)
}
}
}
})
}
///|
/// Pages models_list, building the first request from the operation parameters.
pub fn Gemini::list_models_pages(
self : Gemini,
page_size? : Int32,
page_token? : String,
) -> @client.Paginator[@gen.Model, Failure] {
let first = @gen.models_list_request(page_size?, page_token?)
self.paginate(
first,
kind=PageKind::Cursor("pageToken"),
items="models",
next="nextPageToken",
operation="models_list",
)
}
///|
/// Pages cached_contents_list, building the first request from the operation parameters.
pub fn Gemini::list_cached_contents_pages(
self : Gemini,
page_token? : String,
page_size? : Int32,
) -> @client.Paginator[@gen.CachedContent, Failure] {
let first = @gen.cached_contents_list_request(page_token?, page_size?)
self.paginate(
first,
kind=PageKind::Cursor("pageToken"),
items="cachedContents",
next="nextPageToken",
operation="cached_contents_list",
)
}
///|
/// Pages files_list, building the first request from the operation parameters.
pub fn Gemini::list_files_pages(
self : Gemini,
page_token? : String,
page_size? : Int32,
) -> @client.Paginator[@gen.File, Failure] {
let first = @gen.files_list_request(page_token?, page_size?)
self.paginate(
first,
kind=PageKind::Cursor("pageToken"),
items="files",
next="nextPageToken",
operation="files_list",
)
}