gcp_bigquery_client/
project.rs

1//! There is no persistent data associated with this resource.
2use std::sync::Arc;
3
4use reqwest::Client;
5
6use crate::auth::Authenticator;
7use crate::error::BQError;
8use crate::model::get_service_account_response::GetServiceAccountResponse;
9use crate::model::project_list::ProjectList;
10use crate::{process_response, urlencode, BIG_QUERY_V2_URL};
11
12/// A project API handler.
13#[derive(Clone)]
14pub struct ProjectApi {
15    client: Client,
16    auth: Arc<dyn Authenticator>,
17    base_url: String,
18}
19
20impl ProjectApi {
21    pub(crate) fn new(client: Client, auth: Arc<dyn Authenticator>) -> Self {
22        Self {
23            client,
24            auth,
25            base_url: BIG_QUERY_V2_URL.to_string(),
26        }
27    }
28
29    pub(crate) fn with_base_url(&mut self, base_url: String) -> &mut Self {
30        self.base_url = base_url;
31        self
32    }
33
34    /// RPC to get the service account for a project used for interactions with Google Cloud KMS.
35    /// # Arguments
36    /// * `project_id`- ID of the project
37    pub async fn get_service_account(&self, project_id: &str) -> Result<GetServiceAccountResponse, BQError> {
38        let req_url = &format!(
39            "{base_url}/projects/{project_id}/serviceAccount",
40            base_url = self.base_url,
41            project_id = urlencode(project_id),
42        );
43
44        let access_token = self.auth.access_token().await?;
45
46        let request = self.client.get(req_url).bearer_auth(access_token).build()?;
47        let response = self.client.execute(request).await?;
48
49        process_response(response).await
50    }
51
52    /// RPC to list projects to which the user has been granted any project role.
53    ///
54    /// Users of this method are encouraged to consider the Resource Manager API, which provides
55    /// the underlying data for this method and has more capabilities.
56    /// # Arguments
57    /// * `options` - Get options.
58    pub async fn list(&self, options: GetOptions) -> Result<ProjectList, BQError> {
59        let req_url = format!("{base_url}/projects", base_url = self.base_url);
60
61        let access_token = self.auth.access_token().await?;
62
63        let request = self
64            .client
65            .get(req_url)
66            .bearer_auth(access_token)
67            .query(&options)
68            .build()?;
69
70        let resp = self.client.execute(request).await?;
71
72        process_response(resp).await
73    }
74}
75
76#[derive(Debug, Serialize, Deserialize, Default)]
77#[serde(rename_all = "camelCase")]
78pub struct GetOptions {
79    max_results: Option<u64>,
80    page_token: Option<String>,
81}
82
83impl GetOptions {
84    /// The maximum number of results to return in a single response page. Leverage the page tokens
85    /// to iterate through the entire collection.
86    pub fn max_results(mut self, value: u64) -> Self {
87        self.max_results = Some(value);
88        self
89    }
90
91    /// Page token, returned by a previous call, to request the next page of results
92    pub fn page_token(mut self, value: String) -> Self {
93        self.page_token = Some(value);
94        self
95    }
96}