drive_v3/resources/
channels.rs

1use 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)]
13/// A request builder to stop watching resources through a channel.
14pub struct StopRequest {
15    /// The channel which will be stopped.
16    #[drive_v3(body)]
17    channel: Option<objects::Channel>
18}
19
20/// A notification channel used to watch for resource changes.
21///
22/// # Examples:
23///
24/// ```no_run
25/// use drive_v3::objects::Channel;
26/// # use drive_v3::{Error, Credentials, Drive};
27/// #
28/// # let drive = Drive::new( &Credentials::from_file(
29/// #     "../.secure-files/google_drive_credentials.json",
30/// #     &["https://www.googleapis.com/auth/drive.file"],
31/// # )? );
32///
33/// // You should use the channel return by requests like files.watch or
34/// // changes.watch
35/// let channel = Channel::new();
36///
37/// let response = drive.channels.stop()
38///     .channel(&channel)
39///     .execute();
40///
41/// assert!( response.is_ok() );
42/// # Ok::<(), Error>(())
43/// ```
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct Channels {
46    /// Credentials used to authenticate a user's access to this resource.
47    credentials: Credentials,
48}
49
50impl Channels {
51    /// Creates a new [`Channels`] resource with the given [`Credentials`].
52    pub fn new( credentials: &Credentials ) -> Self {
53        Self { credentials: credentials.clone() }
54    }
55
56    /// Stops watching resources through this channel.
57    ///
58    /// See Google's
59    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/channels/stop)
60    /// for more information.
61    ///
62    /// # Requires one of the following OAuth scopes:
63    ///
64    /// - `https://www.googleapis.com/auth/docs`
65    /// - `https://www.googleapis.com/auth/drive`
66    /// - `https://www.googleapis.com/auth/drive.appdata`
67    /// - `https://www.googleapis.com/auth/drive.apps`
68    /// - `https://www.googleapis.com/auth/drive.file`
69    /// - `https://www.googleapis.com/auth/drive.metadata`
70    /// - `https://www.googleapis.com/auth/drive.metadata.readonly`
71    /// - `https://www.googleapis.com/auth/drive.photos.readonly`
72    /// - `https://www.googleapis.com/auth/drive.readonly`
73    ///
74    /// # Examples:
75    ///
76    /// ```no_run
77    /// use drive_v3::objects::Channel;
78    /// # use drive_v3::{Error, Credentials, Drive};
79    /// #
80    /// # let drive = Drive::new( &Credentials::from_file(
81    /// #     "../.secure-files/google_drive_credentials.json",
82    /// #     &["https://www.googleapis.com/auth/drive.file"],
83    /// # )? );
84    ///
85    /// // You should use the channel return by requests like files.watch or
86    /// // changes.watch
87    /// let channel = Channel::new();
88    ///
89    /// let response = drive.channels.stop()
90    ///     .channel(&channel)
91    ///     .execute();
92    ///
93    /// assert!( response.is_ok() );
94    /// # Ok::<(), Error>(())
95    /// ```
96    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}