1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pub(crate) mod bucket;

use std::sync::Arc;

use reqwest::{Method, Url};

use crate::{errors::Error, token_provider::TokenProvider};

const GCS_API_URL: &str = "https://www.googleapis.com/storage/v1/";

/// A Google Cloud Storage client.
/// Note: This type does not need to be wrapped in an `Arc` as it uses it internally.
#[derive(Clone)]
pub struct Client {
    token_provider: Arc<dyn TokenProvider>,
    client: reqwest::Client,
    api_url: Arc<Url>,
}

impl Client {
    /// Creates a new GCS client.
    pub fn new(token_provider: impl TokenProvider + 'static) -> Self {
        Self::new_with_client(token_provider, Default::default())
    }

    /// Creates a new GCS client using a provided reqwest::Client.
    pub fn new_with_client(
        token_provider: impl TokenProvider + 'static,
        client: reqwest::Client,
    ) -> Self {
        Self {
            token_provider: Arc::new(token_provider),
            client,
            api_url: Arc::new(Url::parse(GCS_API_URL).unwrap()),
        }
    }

    /// Returns a `BucketClient` scoped to the provided bucket.
    pub fn into_bucket_client(self, bucket_name: String) -> bucket::GcsBucketClient {
        bucket::GcsBucketClient::new(self, bucket_name)
    }

    async fn build_request(
        &self,
        method: Method,
        path: &str,
    ) -> Result<reqwest::RequestBuilder, Error> {
        self.build_request_with_url(method, self.api_url.join(path).expect("malformed url"))
            .await
    }

    async fn build_request_with_url(
        &self,
        method: Method,
        url: Url,
    ) -> Result<reqwest::RequestBuilder, Error> {
        let auth_token = self
            .token_provider
            .get_token()
            .await
            .map_err(Error::TokenFetch)?;
        Ok(self
            .client
            .request(method, url)
            .bearer_auth(&auth_token.token))
    }
}