drive_v3/resources/
channels.rs1use reqwest::Method;
2use drive_v3_macros::{DriveRequestBuilder, request};
3
4use super::DriveRequestBuilder;
5use crate::{objects, Credentials};
6
7#[request(
8 method=Method::POST,
9 url="https://www.googleapis.com/drive/v3/channels/stop",
10 returns=()
11)]
12#[derive(DriveRequestBuilder)]
13pub struct StopRequest {
15 #[drive_v3(body)]
17 channel: Option<objects::Channel>
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct Channels {
46 credentials: Credentials,
48}
49
50impl Channels {
51 pub fn new( credentials: &Credentials ) -> Self {
53 Self { credentials: credentials.clone() }
54 }
55
56 pub fn stop( &self ) -> StopRequest {
97 StopRequest::new(&self.credentials)
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::Channels;
104 use crate::{ErrorKind, objects, resources};
105 use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
106
107 fn get_resource() -> Channels {
108 Channels::new(&VALID_CREDENTIALS)
109 }
110
111 fn get_invalid_resource() -> Channels {
112 Channels::new(&INVALID_CREDENTIALS)
113 }
114
115 fn get_files_resource() -> resources::Files {
116 resources::Files::new(&VALID_CREDENTIALS)
117 }
118
119 fn delete_file( file: &objects::File ) -> crate::Result<()> {
120 get_files_resource().delete( file.clone().id.unwrap() ).execute()
121 }
122
123 fn get_test_file_metadata() -> objects::File {
124 objects::File {
125 name: Some( "test.txt".to_string() ),
126 description: Some( "a test file".to_string() ),
127 mime_type: Some( "text/plain".to_string() ),
128 ..Default::default()
129 }
130 }
131
132 fn get_test_drive_file() -> crate::Result<objects::File> {
133 let metadata = get_test_file_metadata();
134
135 get_files_resource().create()
136 .upload_type(objects::UploadType::Multipart)
137 .metadata(&metadata)
138 .content_string("content")
139 .execute()
140 }
141
142 fn get_test_channel( file: &objects::File ) -> crate::Result<objects::Channel> {
143 let channel_id = "test-channel-id";
144 let channel_address = "https://gitlab.com/mderr/drive-v3";
145 let channel = objects::Channel::from(&channel_id, &channel_address);
146
147 get_files_resource().watch( file.clone().id.unwrap() )
148 .channel(&channel)
149 .execute()
150 }
151
152 #[test]
153 fn new_test() {
154 let valid_resource = get_resource();
155 let invalid_resource = get_invalid_resource();
156
157 assert_eq!( valid_resource.credentials, VALID_CREDENTIALS.clone() );
158 assert_eq!( invalid_resource.credentials, INVALID_CREDENTIALS.clone() );
159 }
160
161 #[test]
162 fn stop_test() {
163 let test_drive_file = get_test_drive_file().unwrap();
164 let test_channel = get_test_channel(&test_drive_file).unwrap();
165
166 let response = get_resource().stop()
167 .channel(&test_channel)
168 .execute();
169
170 assert!( response.is_ok() );
171
172 delete_file(&test_drive_file).expect("Failed to cleanup created file");
173 }
174
175 #[test]
176 fn stop_invalid_test() {
177 let response = get_invalid_resource().stop()
178 .execute();
179
180 assert!( response.is_err() );
181 assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
182 }
183}