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
use super::BoxError; use hyper::{body, Body, Client, Method, Request}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct TokenResponse { access_token: String, expires_in: u32, token_type: String, } pub async fn get_token() -> std::result::Result<String, BoxError> { let bytes = get_metadata( "instance/service-accounts/default/token?scopes=https://www.googleapis.com/auth/datastore", ) .await?; let body: TokenResponse = serde_json::from_slice(&bytes.to_vec())?; let token = body.access_token; Ok(token) } pub async fn get_project_id() -> std::result::Result<String, BoxError> { let bytes = get_metadata("project/project-id").await?; let project_id = String::from_utf8(bytes.to_vec())?; Ok(project_id) } pub async fn get_metadata(entry: &'static str) -> std::result::Result<body::Bytes, BoxError> { let request = Request::builder() .method(Method::GET) .uri(format!( "http://metadata.google.internal/computeMetadata/v1/{}", entry )) .header("Metadata-Flavor", "Google") .body(Body::empty())?; let client = Client::new(); let response = client.request(request).await?; let bytes = body::to_bytes(response.into_body()).await?; Ok(bytes) }