use reqwest::Method;
use crate::{objects, request_builder, Credentials, Error};
request_builder!(
pub GetRequest {},
Method::GET,
("https://www.googleapis.com/drive/v3/apps/{app_id}", app_id),
);
impl GetRequest {
pub fn execute( &self ) -> Result<objects::App, Error> {
let response = self.send()?;
Ok( serde_json::from_str( &response.text()? )? )
}
}
request_builder!(
pub ListRequest {
app_filter_extensions: Option<String>,
app_filter_mime_types: Option<String>,
language_code: Option<String>,
},
Method::GET,
("https://www.googleapis.com/drive/v3/apps"),
);
impl ListRequest {
pub fn execute( &self ) -> Result<objects::AppList, Error> {
let response = self.send()?;
Ok( serde_json::from_str( &response.text()? )? )
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Apps {
credentials: Credentials,
}
impl Apps {
pub fn new( credentials: &Credentials ) -> Self {
Self { credentials: credentials.clone() }
}
pub fn get<T: AsRef<str>> ( &self, app_id: T ) -> GetRequest {
GetRequest::new(&self.credentials, app_id)
}
pub fn list( &self ) -> ListRequest {
ListRequest::new(&self.credentials)
}
}
#[cfg(test)]
mod tests {
use super::Apps;
use crate::ErrorKind;
use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
fn get_resource() -> Apps {
Apps::new(&VALID_CREDENTIALS)
}
fn get_invalid_resource() -> Apps {
Apps::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 get_test() {
let google_docs_id = "619683526622";
let response = get_resource().get(&google_docs_id)
.execute();
assert!( response.is_ok() );
let app = response.unwrap();
assert_eq!( &app.id.unwrap(), google_docs_id );
assert_eq!( &app.name.unwrap(), "Google Docs" );
assert_eq!( &app.kind.unwrap(), "drive#app" );
}
#[test]
fn get_invalid_test() {
let google_docs_id = "619683526622";
let response = get_invalid_resource().get(&google_docs_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() );
assert!( response.unwrap().items.is_some() );
}
#[test]
fn list_invalid_test() {
let response = get_invalid_resource().list()
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
}