Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lists and metadata #66

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion google-cloud/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "google-cloud"
version = "0.2.1"
version = "0.3.0"
description = "Asynchronous Rust bindings for Google Cloud Platform gRPC APIs"
authors = ["Nicolas Polomack <[email protected]>"]
edition = "2018"
Expand Down
9 changes: 9 additions & 0 deletions google-cloud/src/storage/api/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ use serde::{Deserialize, Serialize};

use crate::storage::api::object_acl::ObjectAclResource;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ObjectResources {
/// Value: "storage#objects"
pub kind: String,
#[serde(default)]
pub items: Vec<ObjectResource>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ObjectResource {
Expand Down
40 changes: 39 additions & 1 deletion google-cloud/src/storage/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};

use crate::storage::api::object::ObjectResource;
use crate::storage::api::object::*;
use crate::storage::{Client, Error, Object};

use serde::Serialize;
use std::collections::HashMap;

/// Represents a Cloud Storage bucket.
#[derive(Clone)]
pub struct Bucket {
Expand Down Expand Up @@ -55,7 +58,9 @@ impl Bucket {
Ok(Object::new(
client.clone(),
self.name.clone(),
resource.id,
resource.name,
resource.metadata.unwrap_or(HashMap::new()),
))
}

Expand All @@ -82,10 +87,43 @@ impl Bucket {
Ok(Object::new(
client.clone(),
self.name.clone(),
resource.id,
resource.name,
resource.metadata.unwrap_or(HashMap::new()),
))
}

/// List objects stored in the bucket.
pub async fn list<V: Serialize>(&mut self, list_options: &HashMap<String, V>) -> Result<Vec<Object>, Error> {
let client = &mut self.client;
let inner = &client.client;
let uri = format!(
"{}/b/{}/o",
Client::ENDPOINT,
utf8_percent_encode(&self.name, NON_ALPHANUMERIC),
);

let token = client.token_manager.lock().await.token().await?;
let request = inner
.get(uri.as_str())
.query(&list_options)
.header("authorization", token)
.send();
let response = request.await?;
let resources = response.
error_for_status()?
.json::<ObjectResources>()
.await?;

let objects = resources
.items
.into_iter()
.map(|resource| Object::new(client.clone(), resource.id, resource.name, resource.bucket, resource.metadata.unwrap_or(HashMap::new())))
.collect();

Ok(objects)
}

/// Delete the bucket.
pub async fn delete(self) -> Result<(), Error> {
let client = self.client;
Expand Down
18 changes: 18 additions & 0 deletions google-cloud/src/storage/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,40 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};

use crate::storage::{Client, Error};

use std::collections::HashMap;

/// Represents a Cloud Storage bucket.
#[derive(Clone)]
pub struct Object {
pub(crate) client: Client,
pub(crate) id: String,
pub(crate) name: String,
pub(crate) bucket: String,
pub(crate) metadata: HashMap<String, String>,
}

impl Object {
pub(crate) fn new(
client: Client,
bucket: impl Into<String>,
id: impl Into<String>,
name: impl Into<String>,
metadata: HashMap<String, String>,
) -> Object {
Object {
client,
id: id.into(),
name: name.into(),
bucket: bucket.into(),
metadata: metadata,
}
}

/// Get the object's id.
pub fn id(&self) -> &str {
self.id.as_str()
}

/// Get the object's name.
pub fn name(&self) -> &str {
self.name.as_str()
Expand All @@ -33,6 +46,11 @@ impl Object {
self.bucket.as_str()
}

/// Get the object's metadata
pub fn metadata(&self) -> &HashMap<String, String> {
&self.metadata
}

// /// Insert a new object into the bucket.
// pub async fn writer(&mut self, object: Object) -> Result<(), Error> {
// Ok(())
Expand Down