// GENERATED by tools/gen — DO NOT EDIT.

///|
/// A canonical resource name failed validation before request construction.
#derive(@debug.Debug)
pub(all) suberror ValidationError {
  ValidationError(parameter~ : String, message~ : String)
}

///|
pub extend ValidationError with @debug.Debug::{to_repr}

///|
fn normalize_resource_name(
  value : String,
  parameter : String,
  pattern : String,
  prefix : String,
) -> String raise ValidationError {
  if value.is_empty() {
    raise ValidationError::ValidationError(
      parameter~,
      message="resource name must not be empty",
    )
  }
  // These characters can change URL structure or survive a second decoding
  // pass. Reject them even when a permissive provider regex accepts them.
  for char in value {
    if char.is_whitespace() ||
      char.to_int() < 32 ||
      char.to_int() == 127 ||
      char is ('?' | '#' | '&' | '%' | ':' | '\\') {
      raise ValidationError::ValidationError(
        parameter~,
        message="unsafe character in resource name",
      )
    }
  }
  for segment in value.split("/") {
    if segment == "" || segment == "." || segment == ".." {
      raise ValidationError::ValidationError(
        parameter~,
        message="invalid resource-name segment",
      )
    }
  }
  let normalized = if prefix != "" && !value.contains("/") {
    prefix + value
  } else {
    value
  }
  let matched = try (
    Regex::Regex("^(?:" + pattern + ")$").execute(normalized) is Option::Some(_)
  ) catch {
    _ => false
  }
  if !matched {
    raise ValidationError::ValidationError(
      parameter~,
      message="resource name does not match " + pattern,
    )
  }
  normalized
}

///|
fn[T : ToJson] parameter_value(value : T) -> String {
  match @json.Json::Json(value) {
    @json.Json::String(value) => value
    @json.Json::True => "true"
    @json.Json::False => "false"
    @json.Json::Number(value, repr~) =>
      match repr {
        Option::Some(text) => text
        Option::None => value.to_string()
      }
    other => other.stringify()
  }
}

///|
fn parse_json_body(body : Bytes) -> Json raise {
  @json.parse(@utf8.decode(body))
}

///|
fn[T : @json.FromJson] decode_response(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[T, @client.ResponseFailure] {
  Result::Ok(@json.from_json(parse_json_body(body))) catch {
    error =>
      Result::Err(
        @client.ResponseFailure::Deserialization(
          head,
          message=error.to_string(),
          body=body[:body.length().min(@client.max_error_body)].to_owned(),
        ),
      )
  }
}

///|
///
/// Updates CachedContent resource (only expiration is updatable). OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn cached_contents_patch_request(
  name : String,
  body : CachedContent,
  update_mask? : String,
) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^cachedContents/[^/]+$",
    "cachedContents/",
  )
  let mut url = "/v1beta/" + @http.percent_encode_path(parameter_value(name))
  match update_mask {
    Option::Some(value) =>
      url = @http.append_query(url, "updateMask", parameter_value(value))
    Option::None => ()
  }
  @http.Request::patch(url).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful cached_contents_patch response.
pub fn cached_contents_patch_decode_json(
  value : Json,
) -> CachedContent raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful cached_contents_patch response.
pub fn cached_contents_patch_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[CachedContent, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Reads CachedContent resource. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn cached_contents_get_request(
  name : String,
) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^cachedContents/[^/]+$",
    "cachedContents/",
  )
  @http.Request::get(
    "/v1beta/" + @http.percent_encode_path(parameter_value(name)),
  )
}

///|
///
/// Decodes a parsed successful cached_contents_get response.
pub fn cached_contents_get_decode_json(
  value : Json,
) -> CachedContent raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful cached_contents_get response.
pub fn cached_contents_get_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[CachedContent, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Creates CachedContent resource. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn cached_contents_create_request(body : CachedContent) -> @http.Request {
  @http.Request::post("/v1beta/cachedContents").body_json(
    @json.Json::Json(body),
  )
}

///|
///
/// Decodes a parsed successful cached_contents_create response.
pub fn cached_contents_create_decode_json(
  value : Json,
) -> CachedContent raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful cached_contents_create response.
pub fn cached_contents_create_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[CachedContent, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Lists CachedContents. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn cached_contents_list_request(
  page_size? : Int32,
  page_token? : String,
) -> @http.Request {
  let mut url = "/v1beta/cachedContents"
  match page_size {
    Option::Some(value) =>
      url = @http.append_query(url, "pageSize", parameter_value(value))
    Option::None => ()
  }
  match page_token {
    Option::Some(value) =>
      url = @http.append_query(url, "pageToken", parameter_value(value))
    Option::None => ()
  }
  @http.Request::get(url)
}

///|
///
/// Decodes a parsed successful cached_contents_list response.
pub fn cached_contents_list_decode_json(
  value : Json,
) -> ListCachedContentsResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful cached_contents_list response.
pub fn cached_contents_list_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[ListCachedContentsResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Deletes CachedContent resource. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage The caller handles the response as @http.Response.
pub fn cached_contents_delete_request(
  name : String,
) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^cachedContents/[^/]+$",
    "cachedContents/",
  )
  @http.Request::delete(
    "/v1beta/" + @http.percent_encode_path(parameter_value(name)),
  )
}

///|
///
/// Deletes the `File`. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage The caller handles the response as @http.Response.
pub fn files_delete_request(
  name : String,
) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^files/[^/]+$",
    "files/",
  )
  @http.Request::delete(
    "/v1beta/" + @http.percent_encode_path(parameter_value(name)),
  )
}

///|
///
/// Lists the metadata for `File`s owned by the requesting project. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn files_list_request(
  page_size? : Int32,
  page_token? : String,
) -> @http.Request {
  let mut url = "/v1beta/files"
  match page_size {
    Option::Some(value) =>
      url = @http.append_query(url, "pageSize", parameter_value(value))
    Option::None => ()
  }
  match page_token {
    Option::Some(value) =>
      url = @http.append_query(url, "pageToken", parameter_value(value))
    Option::None => ()
  }
  @http.Request::get(url)
}

///|
///
/// Decodes a parsed successful files_list response.
pub fn files_list_decode_json(
  value : Json,
) -> ListFilesResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful files_list response.
pub fn files_list_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[ListFilesResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Creates a `File`. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn media_upload_request(body : CreateFileRequest) -> @http.Request {
  @http.Request::post("/v1beta/files").body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful media_upload response.
pub fn media_upload_decode_json(
  value : Json,
) -> CreateFileResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful media_upload response.
pub fn media_upload_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[CreateFileResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Gets the metadata for the given `File`. OAuth scope `https://www.googleapis.com/auth/devstorage.read_only`: View your data in Google Cloud Storage
pub fn files_get_request(name : String) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^files/[^/]+$",
    "files/",
  )
  @http.Request::get(
    "/v1beta/" + @http.percent_encode_path(parameter_value(name)),
  )
}

///|
///
/// Decodes a parsed successful files_get response.
pub fn files_get_decode_json(value : Json) -> File raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful files_get response.
pub fn files_get_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[File, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Lists the [`Model`s](https://aistudio.google.com/docs/models) available through the Gemini API.
pub fn models_list_request(
  page_size? : Int32,
  page_token? : String,
) -> @http.Request {
  let mut url = "/v1beta/models"
  match page_size {
    Option::Some(value) =>
      url = @http.append_query(url, "pageSize", parameter_value(value))
    Option::None => ()
  }
  match page_token {
    Option::Some(value) =>
      url = @http.append_query(url, "pageToken", parameter_value(value))
    Option::None => ()
  }
  @http.Request::get(url)
}

///|
///
/// Decodes a parsed successful models_list response.
pub fn models_list_decode_json(
  value : Json,
) -> ListModelsResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_list response.
pub fn models_list_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[ListModelsResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Generates a [streamed response](https://aistudio.google.com/docs/text-generation#streaming-responses) from the model given an input `GenerateContentRequest`.
pub fn models_stream_generate_content_request(
  model : String,
  body : GenerateContentRequest,
) -> @http.Request raise ValidationError {
  let model = normalize_resource_name(
    parameter_value(model),
    "model",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::post(
    "/v1beta/" +
    @http.percent_encode_path(parameter_value(model)) +
    ":streamGenerateContent",
  ).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful models_stream_generate_content response.
pub fn models_stream_generate_content_decode_json(
  value : Json,
) -> GenerateContentResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_stream_generate_content response.
pub fn models_stream_generate_content_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[GenerateContentResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Gets information about a specific `Model` such as its version number, token limits, [parameters](https://aistudio.google.com/docs/prompting-strategies#model-parameters) and other metadata. Refer to the [Gemini models guide](https://aistudio.google.com/docs/models) for detailed model information.
pub fn models_get_request(name : String) -> @http.Request raise ValidationError {
  let name = normalize_resource_name(
    parameter_value(name),
    "name",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::get(
    "/v1beta/" + @http.percent_encode_path(parameter_value(name)),
  )
}

///|
///
/// Decodes a parsed successful models_get response.
pub fn models_get_decode_json(
  value : Json,
) -> Model raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_get response.
pub fn models_get_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[Model, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Generates a text embedding vector from the input `Content` using the specified [Gemini Embedding model](https://aistudio.google.com/docs/models#specialized-task-models).
pub fn models_embed_content_request(
  model : String,
  body : EmbedContentRequest,
) -> @http.Request raise ValidationError {
  let model = normalize_resource_name(
    parameter_value(model),
    "model",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::post(
    "/v1beta/" +
    @http.percent_encode_path(parameter_value(model)) +
    ":embedContent",
  ).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful models_embed_content response.
pub fn models_embed_content_decode_json(
  value : Json,
) -> EmbedContentResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_embed_content response.
pub fn models_embed_content_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[EmbedContentResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Runs a model's tokenizer on input `Content` and returns the token count. Refer to the [tokens guide](https://aistudio.google.com/docs/tokens) to learn more about tokens.
pub fn models_count_tokens_request(
  model : String,
  body : CountTokensRequest,
) -> @http.Request raise ValidationError {
  let model = normalize_resource_name(
    parameter_value(model),
    "model",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::post(
    "/v1beta/" +
    @http.percent_encode_path(parameter_value(model)) +
    ":countTokens",
  ).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful models_count_tokens response.
pub fn models_count_tokens_decode_json(
  value : Json,
) -> CountTokensResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_count_tokens response.
pub fn models_count_tokens_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[CountTokensResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Generates a model response given an input `GenerateContentRequest`. Refer to the [text generation guide](https://aistudio.google.com/docs/text-generation) for detailed usage information. Input capabilities differ between models, including tuned models. Refer to the [model guide](https://aistudio.google.com/docs/models) and [tuning guide](https://aistudio.google.com/docs/model-tuning) for details.
pub fn models_generate_content_request(
  model : String,
  body : GenerateContentRequest,
) -> @http.Request raise ValidationError {
  let model = normalize_resource_name(
    parameter_value(model),
    "model",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::post(
    "/v1beta/" +
    @http.percent_encode_path(parameter_value(model)) +
    ":generateContent",
  ).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful models_generate_content response.
pub fn models_generate_content_decode_json(
  value : Json,
) -> GenerateContentResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_generate_content response.
pub fn models_generate_content_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[GenerateContentResponse, @client.ResponseFailure] {
  decode_response(head, body)
}

///|
///
/// Generates multiple embedding vectors from the input `Content` which consists of a batch of strings represented as `EmbedContentRequest` objects.
pub fn models_batch_embed_contents_request(
  model : String,
  body : BatchEmbedContentsRequest,
) -> @http.Request raise ValidationError {
  let model = normalize_resource_name(
    parameter_value(model),
    "model",
    "^models/[^/]+$",
    "models/",
  )
  @http.Request::post(
    "/v1beta/" +
    @http.percent_encode_path(parameter_value(model)) +
    ":batchEmbedContents",
  ).body_json(@json.Json::Json(body))
}

///|
///
/// Decodes a parsed successful models_batch_embed_contents response.
pub fn models_batch_embed_contents_decode_json(
  value : Json,
) -> BatchEmbedContentsResponse raise @json.JsonDecodeError {
  @json.from_json(value)
}

///|
///
/// Decodes a successful models_batch_embed_contents response.
pub fn models_batch_embed_contents_decode(
  head : @http.ResponseHead,
  body : Bytes,
) -> Result[BatchEmbedContentsResponse, @client.ResponseFailure] {
  decode_response(head, body)
}