google_cloud_bigquery/http/tabledata/
insert_all.rs

1use reqwest_middleware::{ClientWithMiddleware as Client, RequestBuilder};
2use serde::Serialize;
3
4#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Debug)]
5#[serde(rename_all = "camelCase")]
6pub struct Row<T: Serialize> {
7    /// [Optional] A unique ID for each row. BigQuery uses this
8    /// property to detect duplicate insertion requests on a best-effort basis.
9    pub insert_id: Option<String>,
10
11    /// [Required] A JSON object that contains a row of data. The
12    /// object's properties and values must match the destination table's schema.
13    pub json: T,
14}
15
16#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Debug)]
17#[serde(rename_all = "camelCase")]
18pub struct InsertAllRequest<T: Serialize> {
19    /// Optional. Insert all valid rows of a request, even if invalid rows exist.
20    /// The default value is false, which causes the entire request to fail if any invalid rows exist.
21    pub skip_invalid_rows: Option<bool>,
22    /// Optional. Accept rows that contain values that do not match the schema.
23    /// The unknown values are ignored. Default is false, which treats unknown values as errors.
24    pub ignore_unknown_values: Option<bool>,
25    /// Optional. If specified, treats the destination table as a base template, and inserts the rows into an instance table named "{destination}{templateSuffix}". BigQuery will manage creation of the instance table, using the schema of the base template table.
26    /// See https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables for considerations when working with templates tables.
27    pub template_suffix: Option<String>,
28    /// Data to insert
29    pub rows: Vec<Row<T>>,
30    /// Optional. Unique request trace id. Used for debugging purposes only.
31    /// It is case-sensitive, limited to up to 36 ASCII characters. A UUID is recommended.
32    pub trace_id: Option<String>,
33}
34
35impl<T: Serialize> Default for InsertAllRequest<T> {
36    fn default() -> Self {
37        Self {
38            skip_invalid_rows: None,
39            ignore_unknown_values: None,
40            template_suffix: None,
41            rows: vec![],
42            trace_id: None,
43        }
44    }
45}
46
47#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Default, Debug)]
48#[serde(rename_all = "camelCase")]
49pub struct ErrorMessage {
50    /// A short error code that summarizes the error.
51    pub reason: String,
52    /// Specifies where the error occurred, if present.
53    pub location: String,
54    /// Debugging information. This property is internal to Google and should not be used.
55    pub debug_info: String,
56    /// A human-readable description of the error.
57    pub message: String,
58}
59
60#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Default, Debug)]
61#[serde(rename_all = "camelCase")]
62pub struct Error {
63    pub index: i32,
64    pub errors: Vec<ErrorMessage>,
65}
66
67#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Default, Debug)]
68#[serde(rename_all = "camelCase")]
69pub struct InsertAllResponse {
70    #[serde(default)]
71    pub kind: String,
72    pub insert_errors: Option<Vec<Error>>,
73}
74
75pub fn build<T: Serialize>(
76    base_url: &str,
77    client: &Client,
78    project_id: &str,
79    dataset_id: &str,
80    table_id: &str,
81    data: &InsertAllRequest<T>,
82) -> RequestBuilder {
83    let url = format!(
84        "{}/projects/{}/datasets/{}/tables/{}/insertAll",
85        base_url, project_id, dataset_id, table_id
86    );
87    client.post(url).json(data)
88}