Skip to main content

bigquery_client/gcloud/bigquery/table/
mod.rs

1mod insert_entities;
2
3use serde::{Serialize};
4use super::CrudResult;
5use crate::gcloud::{GCloud, client::Endpoint};
6
7use insert_entities::{InsertAll};
8pub struct Table {
9    gcloud_client: GCloud,
10    project_id: String,
11    dataset_id: String,
12    name: String,
13}
14
15
16impl Endpoint for Table {}
17
18impl Table {
19
20    pub async fn insert_many(&self, entities: &Vec<&impl Serialize>) -> CrudResult<()> {
21        let header_value = self.gcloud_client.header_value();
22        let resource = format!("bigquery/v2/projects/{project_id}/datasets/{dataset_id}/tables/{table_id}/insertAll",
23                                     project_id=self.project_id, dataset_id=self.dataset_id, table_id=self.name);
24        let endpoint = self.endpoint(resource.as_str());
25
26        let body = InsertAll::new(entities);
27
28        let request_client = reqwest::Client::new();
29        let response = request_client
30            .post(endpoint.as_str())
31            .json(&body)
32            .header("Authorization", header_value)
33            .send()
34            .await;
35
36        match response {
37            Ok(_) => Ok(()),
38            Err(err) => Err(Box::new(err)),
39        }
40        
41    }
42
43    pub async fn insert(&self, entity: &impl Serialize) -> CrudResult<()> {
44        let entities = &vec![entity];
45        self.insert_many(entities).await
46    }
47
48    pub fn new(gcloud_client: GCloud, project_id: &str, dataset_id: &str, name: &str) -> Table {
49        Table {
50            project_id: project_id.to_owned(),
51            dataset_id: dataset_id.to_owned(),
52            name: name.to_owned(),
53            gcloud_client
54        }
55    }
56}