1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use crate::blocking::UploadSessionBlocking;
use crate::internal::{
    copy, create_dir, parse_content_disposition, FileConfig, HttpResponseBuilderExt, RangeIter,
    UploadSessionLink, MAX_FILE_NAME_LEN,
};
use graph_error::download::BlockingDownloadError;
use graph_error::{ErrorMessage, ErrorType, GraphFailure, GraphResult};
use std::io::Read;
use std::path::PathBuf;

pub trait ResponseBlockingExt {
    fn job_status(&self) -> Option<GraphResult<reqwest::blocking::Response>>;

    /// # Begin an upload session using any [`std::io::Reader`].<br>
    ///
    /// Converts the current request object into an upload session object for uploading large
    /// files to OneDrive or SharePoint.<br>
    ///
    /// This method takes a `reader` object that implements the [std::io::Read] and [Send] traits,
    /// and returns a [GraphResult] containing an [UploadSession] object.<br>
    ///
    /// The [UploadSession] object contains the upload URL for the file, as well as a [RangeIter] iterator
    /// that can be used to send the file contents to the server in multiple chunks (or "ranges").
    /// If the upload URL is not found in the response body, this method returns a `GraphFailure`
    /// with an error message indicating that no upload URL was found.<br>
    ///
    ///
    /// ## Requires reqwest::blocking::Response body to be valid JSON
    /// The body of the reqwest::blocking::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{ResponseBlockingExt};
    /// use graph_rs_sdk::*;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// // Put the path to your file and the file name itself that
    /// // you want to upload to one drive.
    /// static LOCAL_FILE_PATH: &str = "/path/to/file/file.txt";
    ///
    /// // Parent folder id of where to store this file.
    /// static DRIVE_PARENT_ID: &str = "PARENT_ID";
    ///
    /// // The conflict behavior can be one of: fail, rename, or replace.
    /// static CONFLICT_BEHAVIOR: &str = "rename";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let conflict_behavior = CONFLICT_BEHAVIOR.to_string();
    ///     let upload = serde_json::json!({
    ///         "@microsoft.graph.conflictBehavior": Some(conflict_behavior)
    ///     });
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item_by_path(PATH_IN_ONE_DRIVE)
    ///         .create_upload_session(&upload)
    ///         .send()
    ///         .unwrap();
    ///
    ///     let file = std::fs::File::open(PATH_IN_ONE_DRIVE)?;
    ///
    ///     let upload_session_task = response.into_upload_session(file)?;
    ///
    ///     for result in upload_session_task {
    ///         let response = result?;
    ///         println!("{:#?}", response);
    ///         let body: serde_json::Value = response.json().unwrap();
    ///         println!("{:#?}", body);
    ///     }
    ///
    ///
    ///     Ok(())
    /// }
    /// ```
    fn into_upload_session(
        self,
        reader: impl std::io::Read + Send,
    ) -> GraphResult<UploadSessionBlocking>;

    /// # Downloads the content of the HTTP response and saves it to a file.<br>
    ///
    /// This method takes a `file_config` object containing various parameters that control how the
    /// file is downloaded and saved. The `file_config` object includes the file path, file name,
    /// whether to create the directory recursively, whether to overwrite existing files, and the
    /// desired file extension.<br><br>
    ///
    /// If `create_dir_all` is set to true (default is true), this method will create the directory at the specified
    /// path if it doesn't exist yet. If it is set to false and the target directory doesn't exist,
    /// this method will return an [BlockingDownloadError] with an error message indicating that the
    /// target directory does not exist.<br><br>
    ///
    /// The [`FileConfig::file_name`] parameter can be used to specify a custom file name for the downloaded file.
    /// If it is not provided, the method will attempt to parse the `Content-Disposition` header to extract the file name.
    /// If no file name can be obtained from the header, this method will return an [BlockingDownloadError::NoFileName]
    /// with an error message indicating that no file name was found.<br><br>
    ///
    /// If the [`FileConfig::extension`] parameter is set to a non-empty string,
    /// this method will set the file extension of the downloaded file to the specified value. <br><br>
    ///
    /// If the target file already exists and [`overwrite_existing_file`] is set to false,
    /// this method will return an [BlockingDownloadError::FileExists] with an error message
    /// indicating that the file already exists and cannot be overwritten. <br><br>
    ///
    /// If the file is downloaded and saved successfully, this method returns a [`http::Response<PathBuf>`] object
    /// containing the path to the downloaded file.
    ///
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{BodyRead, FileConfig};
    /// use graph_rs_sdk::*;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// static ITEM_ID: &str = "ITEM_ID";
    ///
    /// static FORMAT: &str = "pdf";
    ///
    /// static DOWNLOAD_DIRECTORY: &str = "./examples";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item(ITEM_ID)
    ///         .get_items_content()
    ///         .format(FORMAT)
    ///         .send()?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let response2 = response.download(&FileConfig::new(DOWNLOAD_DIRECTORY))
    ///         .send()?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    /// <br><br>
    /// # Example format and rename
    ///
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{BodyRead, FileConfig};
    /// use graph_rs_sdk::*;
    /// use std::ffi::OsStr;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// static ITEM_ID: &str = "ITEM_ID";
    ///
    /// static FORMAT: &str = "pdf";
    ///
    /// static DOWNLOAD_DIRECTORY: &str = "./examples";
    ///
    /// static FILE_NAME: &str = "new_file_name.pdf";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item(ITEM_ID)
    ///         .get_items_content()
    ///         .format(FORMAT)
    ///         .send()?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let file_config = FileConfig::new(DOWNLOAD_DIRECTORY)
    ///         .file_name(OsStr::new(FILE_NAME));
    ///
    ///     let response2 = response.download(file_config)
    ///         .send()?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    fn download(
        self,
        file_config: &FileConfig,
    ) -> Result<http::Response<PathBuf>, BlockingDownloadError>;

    /// If the response is a server error then Microsoft Graph will return
    /// an error in the response body. The [`ErrorMessage`] type maps to these
    /// errors and this method deserializes to this type.
    ///
    /// Microsoft Graph does not return this error message in all situations so it
    /// make sure to handle cases where the body could not be deserialized properly.
    /// ```rust,ignore
    /// let status = response.status();
    ///
    /// if status.is_server_error() || status.is_client_error() {
    ///     let error_message = response.into_error_message().unwrap();
    ///     println!("{error_message:#?}");
    ///
    ///     // This is the same thing as doing
    ///     let error_message: ErrorMessage = response.json().unwrap();
    /// }
    /// ```
    fn into_graph_error_message(self) -> Result<ErrorMessage, reqwest::Error>;

    /// Microsoft Graph specific status code errors mapped from the response [StatusCode].
    /// Not all status codes map to a Microsoft Graph error.
    ///
    /// Use [`ErrorType::as_str`] to get a short description of the Microsoft Graph specific error.
    /// ```rust,ignore
    /// let error_type = response.graph_error_type().unwrap();
    /// println!("{:#?}", error_type.as_str());
    /// ```
    fn graph_error_type(&self) -> Option<ErrorType>;
}

impl ResponseBlockingExt for reqwest::blocking::Response {
    fn job_status(&self) -> Option<GraphResult<reqwest::blocking::Response>> {
        let url = self
            .headers()
            .get(reqwest::header::LOCATION)?
            .to_str()
            .ok()?;
        let result = reqwest::blocking::Client::new()
            .get(url)
            .send()
            .map_err(GraphFailure::from);

        Some(result)
    }

    /// # Begin an upload session using any [`std::io::Reader`].<br>
    ///
    /// Converts the current request object into an upload session object for uploading large
    /// files to OneDrive or SharePoint.<br>
    ///
    /// This method takes a `reader` object that implements the [std::io::Read] and [Send] traits,
    /// and returns a [GraphResult] containing an [UploadSession] object.<br>
    ///
    /// The [UploadSession] object contains the upload URL for the file, as well as a [RangeIter] iterator
    /// that can be used to send the file contents to the server in multiple chunks (or "ranges").
    /// If the upload URL is not found in the response body, this method returns a `GraphFailure`
    /// with an error message indicating that no upload URL was found.<br>
    ///
    ///
    /// ## Requires reqwest::blocking::Response body to be valid JSON
    /// The body of the reqwest::blocking::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{ResponseBlockingExt};
    /// use graph_rs_sdk::*;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// // Put the path to your file and the file name itself that
    /// // you want to upload to one drive.
    /// static LOCAL_FILE_PATH: &str = "/path/to/file/file.txt";
    ///
    /// // Parent folder id of where to store this file.
    /// static DRIVE_PARENT_ID: &str = "PARENT_ID";
    ///
    /// // The conflict behavior can be one of: fail, rename, or replace.
    /// static CONFLICT_BEHAVIOR: &str = "rename";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let conflict_behavior = CONFLICT_BEHAVIOR.to_string();
    ///     let upload = serde_json::json!({
    ///         "@microsoft.graph.conflictBehavior": Some(conflict_behavior)
    ///     });
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item_by_path(PATH_IN_ONE_DRIVE)
    ///         .create_upload_session(&upload)
    ///         .send()
    ///         .unwrap();
    ///
    ///     let file = std::fs::File::open(PATH_IN_ONE_DRIVE)?;
    ///
    ///     let upload_session_task = response.into_upload_session(file)?;
    ///
    ///     for result in upload_session_task {
    ///         let response = result?;
    ///         println!("{:#?}", response);
    ///         let body: serde_json::Value = response.json().unwrap();
    ///         println!("{:#?}", body);
    ///     }
    ///
    ///
    ///     Ok(())
    /// }
    /// ```
    fn into_upload_session(self, reader: impl Read + Send) -> GraphResult<UploadSessionBlocking> {
        let body: serde_json::Value = self.json()?;
        let url = body
            .upload_session_link()
            .ok_or_else(|| GraphFailure::not_found("No uploadUrl found in response body"))?;

        let range_iter = RangeIter::from_reader(reader)?;
        Ok(UploadSessionBlocking::new(
            reqwest::Url::parse(url.as_str())?,
            range_iter,
        ))
    }

    /// # Downloads the content of the HTTP response and saves it to a file.<br>
    ///
    /// This method takes a `file_config` object containing various parameters that control how the
    /// file is downloaded and saved. The `file_config` object includes the file path, file name,
    /// whether to create the directory recursively, whether to overwrite existing files, and the
    /// desired file extension.<br><br>
    ///
    /// If `create_dir_all` is set to true (default is true), this method will create the directory at the specified
    /// path if it doesn't exist yet. If it is set to false and the target directory doesn't exist,
    /// this method will return an [BlockingDownloadError] with an error message indicating that the
    /// target directory does not exist.<br><br>
    ///
    /// The [`FileConfig::file_name`] parameter can be used to specify a custom file name for the downloaded file.
    /// If it is not provided, the method will attempt to parse the `Content-Disposition` header to extract the file name.
    /// If no file name can be obtained from the header, this method will return an [BlockingDownloadError::NoFileName]
    /// with an error message indicating that no file name was found.<br><br>
    ///
    /// If the [`FileConfig::extension`] parameter is set to a non-empty string,
    /// this method will set the file extension of the downloaded file to the specified value. <br><br>
    ///
    /// If the target file already exists and [`overwrite_existing_file`] is set to false,
    /// this method will return an [BlockingDownloadError::FileExists] with an error message
    /// indicating that the file already exists and cannot be overwritten. <br><br>
    ///
    /// If the file is downloaded and saved successfully, this method returns a [`http::Response<PathBuf>`] object
    /// containing the path to the downloaded file.
    ///
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{BodyRead, FileConfig, ResponseBlockingExt};
    /// use graph_rs_sdk::*;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// static ITEM_ID: &str = "ITEM_ID";
    ///
    /// static FORMAT: &str = "pdf";
    ///
    /// static DOWNLOAD_DIRECTORY: &str = "./examples";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item(ITEM_ID)
    ///         .get_items_content()
    ///         .format(FORMAT)
    ///         .send()?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let response2 = response.download(&FileConfig::new(DOWNLOAD_DIRECTORY))
    ///         .send()?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    /// <br><br>
    /// # Example format and rename
    ///
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{BodyRead, FileConfig};
    /// use graph_rs_sdk::*;
    /// use std::ffi::OsStr;
    ///
    /// static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
    ///
    /// static ITEM_ID: &str = "ITEM_ID";
    ///
    /// static FORMAT: &str = "pdf";
    ///
    /// static DOWNLOAD_DIRECTORY: &str = "./examples";
    ///
    /// static FILE_NAME: &str = "new_file_name.pdf";
    ///
    /// #[tokio::main]
    /// async fn main() -> GraphResult<()> {
    ///     let client = Graph::new(ACCESS_TOKEN);
    ///
    ///     let response = client
    ///         .me()
    ///         .drive()
    ///         .item(ITEM_ID)
    ///         .get_items_content()
    ///         .format(FORMAT)
    ///         .send()?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let file_config = FileConfig::new(DOWNLOAD_DIRECTORY)
    ///         .file_name(OsStr::new(FILE_NAME));
    ///
    ///     let response2 = response.download(file_config)
    ///         .send()?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    fn download(
        self,
        file_config: &FileConfig,
    ) -> Result<http::Response<PathBuf>, BlockingDownloadError> {
        let path = file_config.path.clone();
        let file_name = file_config.file_name.clone();
        let create_dir_all = file_config.create_directory_all;
        let overwrite_existing_file = file_config.overwrite_existing_file;
        let extension = file_config.extension.clone();

        if create_dir_all {
            create_dir(path.as_path())?;
        } else if !path.exists() {
            return Err(BlockingDownloadError::TargetDoesNotExist(
                path.to_string_lossy().to_string(),
            ));
        }

        let path = {
            if let Some(name) = file_name.or_else(|| parse_content_disposition(self.headers())) {
                if name.len() > MAX_FILE_NAME_LEN {
                    return Err(BlockingDownloadError::FileNameTooLong);
                }
                path.join(name)
            } else {
                return Err(BlockingDownloadError::NoFileName);
            }
        };

        if let Some(ext) = extension.as_ref() {
            path.with_extension(ext.as_os_str());
        }

        if path.exists() && !overwrite_existing_file {
            return Err(BlockingDownloadError::FileExists(
                path.to_string_lossy().to_string(),
            ));
        }

        let status = self.status();
        let url = self.url().clone();
        let _headers = self.headers().clone();
        let version = self.version();

        Ok(http::Response::builder()
            .url(url)
            .status(http::StatusCode::from(&status))
            .version(version)
            .body(copy(path, self)?)?)
    }

    /// If the response is a server error then Microsoft Graph will return
    /// an error in the response body. The [`ErrorMessage`] type maps to these
    /// errors and this method deserializes to this type.
    ///
    /// Microsoft Graph does not return this error message in all situations so it
    /// make sure to handle cases where the body could not be deserialized properly.
    /// ```rust,ignore
    /// let status = response.status();
    ///
    /// if status.is_server_error() || status.is_client_error() {
    ///     let error_message = response.into_error_message().unwrap();
    ///     println!("{error_message:#?}");
    ///
    ///     // This is the same thing as doing
    ///     let error_message: ErrorMessage = response.json().unwrap();
    /// }
    /// ```
    fn into_graph_error_message(self) -> Result<ErrorMessage, reqwest::Error> {
        self.json()
    }

    /// Microsoft Graph specific status code errors mapped from the response [StatusCode].
    /// Not all status codes map to a Microsoft Graph error.
    ///
    /// Use [`ErrorType::as_str`] to get a short description of the Microsoft Graph specific error.
    /// ```rust,ignore
    /// let error_type = response.graph_error_type().unwrap();
    /// println!("{:#?}", error_type.as_str());
    /// ```
    fn graph_error_type(&self) -> Option<ErrorType> {
        let status = self.status();
        ErrorType::from_u16(status.as_u16())
    }
}