use reqwest::Method;
use drive_v3_macros::{DriveRequestBuilder, request};
use super::DriveRequestBuilder;
use crate::{objects, Credentials};
#[request(
method=Method::POST,
url="https://www.googleapis.com/drive/v3/drives",
returns=objects::DriveInfo,
)]
#[derive(DriveRequestBuilder)]
pub struct CreateRequest {
#[drive_v3(parameter)]
request_id: Option<String>,
#[drive_v3(body)]
drive_info: Option<objects::DriveInfo>
}
#[request(
method=Method::DELETE,
url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
returns=(),
)]
#[derive(DriveRequestBuilder)]
pub struct DeleteRequest {
#[drive_v3(parameter)]
use_domain_admin_access: Option<bool>,
#[drive_v3(parameter)]
allow_item_deletion: Option<bool>,
}
#[request(
method=Method::GET,
url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
returns=objects::DriveInfo,
)]
#[derive(DriveRequestBuilder)]
pub struct GetRequest {
#[drive_v3(parameter)]
use_domain_admin_access: Option<bool>,
}
#[request(
method=Method::POST,
url="https://www.googleapis.com/drive/v3/drives/{drive_id}/hide",
returns=objects::DriveInfo,
)]
#[derive(DriveRequestBuilder)]
pub struct HideRequest {}
#[request(
method=Method::GET,
url="https://www.googleapis.com/drive/v3/drives",
returns=objects::DriveInfoList,
)]
#[derive(DriveRequestBuilder)]
pub struct ListRequest {
#[drive_v3(parameter)]
page_size: Option<i64>,
#[drive_v3(parameter)]
page_token: Option<String>,
#[drive_v3(parameter)]
q: Option<String>,
#[drive_v3(parameter)]
use_domain_admin_access: Option<bool>,
}
#[request(
method=Method::POST,
url="https://www.googleapis.com/drive/v3/drives/{drive_id}/unhide",
returns=objects::DriveInfo,
)]
#[derive(DriveRequestBuilder)]
pub struct UnhideRequest {}
#[request(
method=Method::PATCH,
url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
returns=objects::DriveInfo,
)]
#[derive(DriveRequestBuilder)]
pub struct UpdateRequest {
#[drive_v3(parameter)]
use_domain_admin_access: Option<bool>,
#[drive_v3(body)]
drive_info: Option<objects::DriveInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Drives {
credentials: Credentials,
}
impl Drives {
pub fn new( credentials: &Credentials ) -> Self {
Self { credentials: credentials.clone() }
}
pub fn create( &self ) -> CreateRequest {
CreateRequest::new(&self.credentials)
}
pub fn delete<T: AsRef<str>> ( &self, drive_id: T ) -> DeleteRequest {
DeleteRequest::new(&self.credentials, drive_id)
}
pub fn get<T: AsRef<str>> ( &self, drive_id: T ) -> GetRequest {
GetRequest::new(&self.credentials, drive_id)
}
pub fn hide<T: AsRef<str>> ( &self, drive_id: T ) -> HideRequest {
HideRequest::new(&self.credentials, drive_id)
}
pub fn list( &self ) -> ListRequest {
ListRequest::new(&self.credentials)
}
pub fn unhide<T: AsRef<str>> ( &self, drive_id: T ) -> UnhideRequest {
UnhideRequest::new(&self.credentials, drive_id)
}
pub fn update<T: AsRef<str>> ( &self, drive_id: T ) -> UpdateRequest {
UpdateRequest::new(&self.credentials, drive_id)
}
}
#[cfg(test)]
mod tests {
use super::Drives;
use crate::ErrorKind;
use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
fn get_resource() -> Drives {
Drives::new(&VALID_CREDENTIALS)
}
fn get_invalid_resource() -> Drives {
Drives::new(&INVALID_CREDENTIALS)
}
#[test]
fn new_test() {
let valid_resource = get_resource();
let invalid_resource = get_invalid_resource();
assert_eq!( valid_resource.credentials, VALID_CREDENTIALS.clone() );
assert_eq!( invalid_resource.credentials, INVALID_CREDENTIALS.clone() );
}
#[test]
fn create_invalid_test() {
let response = get_invalid_resource().create()
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn delete_invalid_test() {
let response = get_invalid_resource().delete("invalid-id")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn get_invalid_test() {
let response = get_invalid_resource().get("invalid-id")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn hide_invalid_test() {
let response = get_invalid_resource().hide("invalid-id")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn list_test() {
let response = get_resource().list()
.execute();
assert!( response.is_ok() );
}
#[test]
fn list_invalid_test() {
let response = get_invalid_resource().list()
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn unhide_invalid_test() {
let response = get_invalid_resource().unhide("invalid-id")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn update_invalid_test() {
let response = get_invalid_resource().update("invalid-id")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
}