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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
use crate::core::BodyRead;
use crate::internal::{
    copy_async, create_dir_async, FileConfig, HttpResponseBuilderExt, RangeIter, UploadSession,
};
use crate::traits::UploadSessionLink;
use async_trait::async_trait;
use graph_error::download::AsyncDownloadError;
use graph_error::{ErrorMessage, ErrorType, GraphFailure, GraphResult};
use reqwest::header::HeaderMap;
use reqwest::Response;
use std::ffi::OsString;
use std::path::PathBuf;
use tokio::io::AsyncReadExt;

pub(crate) const MAX_FILE_NAME_LEN: usize = 255;

#[allow(clippy::single_char_pattern)]
pub(crate) fn parse_content_disposition(headers: &HeaderMap) -> Option<OsString> {
    if let Some(value) = headers.get("content-disposition") {
        if let Ok(header) = std::str::from_utf8(value.as_ref()) {
            let mut v: Vec<&str> = header.split(';').collect();
            v.retain(|s| !s.is_empty());

            // The filename* indicates that the filename is encoded
            if let Some(value) = v.iter().find(|s| s.starts_with("filename*=utf-8''")) {
                let s = value.replace("filename*=utf-8''", "");
                if let Ok(s) = percent_encoding::percent_decode(s.as_bytes()).decode_utf8() {
                    return Some(OsString::from(s.to_string().replace('/', "-")));
                }
            }

            if let Some(value) = v.last() {
                if value.trim_start().starts_with("filename=") {
                    return Some(OsString::from(
                        value
                            .replace("\"", "")
                            .replace("filename=", "")
                            .replace('/', "-")
                            .trim(),
                    ));
                }
            }
        }
    }
    None
}

#[async_trait]
pub trait ResponseExt {
    async fn job_status(&self) -> Option<GraphResult<reqwest::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::Response body to be valid JSON
    /// The body of the reqwest::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{AsyncIterator, ResponseExt};
    /// 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()
    ///         .await
    ///         .unwrap();
    ///
    ///     let file = std::fs::File::open(PATH_IN_ONE_DRIVE)?;
    ///
    ///     let mut iter = response.into_upload_session(file).await?;
    ///
    ///     while let Some(result) = iter.next().await {
    ///         let response = result?;
    ///         println!("{response:#?}");
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn into_upload_session(
        self,
        reader: impl std::io::Read + Send,
    ) -> GraphResult<UploadSession>;

    /// # Begin an upload session using any [tokio::io::AsyncReadExt].<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 [tokio::io::AsyncReadExt], [Send], and [Unpin] 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::Response body can be deserialized to valid JSON
    /// The body of the reqwest::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{AsyncIterator, ResponseExt};
    /// 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()
    ///         .await
    ///         .unwrap();
    ///
    ///     let file = tokio::fs::File::open(PATH_IN_ONE_DRIVE).await?;
    ///
    ///     let mut iter = response.into_upload_session_async_read(file).await?;
    ///
    ///     while let Some(result) = iter.next().await {
    ///         let response = result?;
    ///         println!("{response:#?}");
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn into_upload_session_async_read(
        self,
        reader: impl AsyncReadExt + Send + Unpin,
    ) -> GraphResult<UploadSession>;

    /// # 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 recursively create the directory
    /// at the path specified if it doesn't exist yet. If it is set to false and the target directory doesn't exist,
    /// this method will return an [AsyncDownloadError] 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 [AsyncDownloadError::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 [AsyncDownloadError::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()
    ///         .await?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let response2 = response.download(&FileConfig::new(DOWNLOAD_DIRECTORY))
    ///         .send()
    ///         .await?;
    ///
    ///     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()
    ///         .await?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let file_config = FileConfig::new(DOWNLOAD_DIRECTORY)
    ///         .file_name(OsStr::new(FILE_NAME));
    ///
    ///     let response2 = response.download(file_config)
    ///         .send()
    ///         .await?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn download(
        self,
        file_config: &FileConfig,
    ) -> Result<http::Response<PathBuf>, AsyncDownloadError>;

    /// 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().await.unwrap();
    ///     println!("{error_message:#?}");
    ///
    ///     // This is the same thing as doing
    ///     let error_message: ErrorMessage = response.json().await.unwrap();
    /// }
    /// ```
    async 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>;
}

#[async_trait]
impl ResponseExt for reqwest::Response {
    async fn job_status(&self) -> Option<GraphResult<Response>> {
        let url = self
            .headers()
            .get(reqwest::header::LOCATION)?
            .to_str()
            .ok()?;
        let result = reqwest::Client::new()
            .get(url)
            .send()
            .await
            .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::Response body to be valid JSON
    /// The body of the reqwest::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{AsyncIterator, ResponseExt};
    /// 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()
    ///         .await
    ///         .unwrap();
    ///
    ///     let file = std::fs::File::open(PATH_IN_ONE_DRIVE)?;
    ///
    ///     let mut iter = response.into_upload_session(file).await?;
    ///
    ///     while let Some(result) = iter.next().await {
    ///         let response = result?;
    ///         println!("{response:#?}");
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn into_upload_session(
        self,
        reader: impl std::io::Read + Send,
    ) -> GraphResult<UploadSession> {
        let body: serde_json::Value = self.json().await?;
        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(UploadSession::new(
            reqwest::Url::parse(url.as_str())?,
            range_iter,
        ))
    }

    /// # Begin an upload session using any [tokio::io::AsyncReadExt].<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 [tokio::io::AsyncReadExt], [Send], and [Unpin] 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::Response body can be deserialized to valid JSON
    /// The body of the reqwest::Response must be valid JSON with an
    /// [uploadUrl] field.
    ///
    /// # Example
    /// ```rust,ignore
    /// use graph_rs_sdk::http::{AsyncIterator, ResponseExt};
    /// 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()
    ///         .await
    ///         .unwrap();
    ///
    ///     let file = tokio::fs::File::open(PATH_IN_ONE_DRIVE).await?;
    ///
    ///     let mut iter = response.into_upload_session_async_read(file).await?;
    ///
    ///     while let Some(result) = iter.next().await {
    ///         let response = result?;
    ///         println!("{response:#?}");
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn into_upload_session_async_read(
        self,
        reader: impl AsyncReadExt + Send + Unpin,
    ) -> GraphResult<UploadSession> {
        let body: serde_json::Value = self.json().await?;
        let url = body
            .upload_session_link()
            .ok_or_else(|| GraphFailure::not_found("No uploadUrl found in response body"))?;

        let range_iter = RangeIter::from_async_read(reader).await?;
        Ok(UploadSession::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, 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 `AsyncDownloadError` 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 [AsyncDownloadError::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 [AsyncDownloadError::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()
    ///         .await?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let response2 = response.download(&FileConfig::new(DOWNLOAD_DIRECTORY))
    ///         .send()
    ///         .await?;
    ///
    ///     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()
    ///         .await?;
    ///
    ///     println!("{response:#?}");
    ///
    ///     let file_config = FileConfig::new(DOWNLOAD_DIRECTORY)
    ///         .file_name(OsStr::new(FILE_NAME));
    ///
    ///     let response2 = response.download(file_config)
    ///         .send()
    ///         .await?;
    ///
    ///     let path_buf = response2.body();
    ///     println!("{:#?}", path_buf.metadata());
    ///
    ///     Ok(())
    /// }
    /// ```
    async fn download(
        self,
        file_config: &FileConfig,
    ) -> Result<http::Response<PathBuf>, AsyncDownloadError> {
        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_async(path.as_path()).await?;
        } else if !path.exists() {
            return Err(AsyncDownloadError::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(AsyncDownloadError::FileNameTooLong);
                }
                path.join(name)
            } else {
                return Err(AsyncDownloadError::NoFileName);
            }
        };

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

        if path.exists() && !overwrite_existing_file {
            return Err(AsyncDownloadError::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_async(path, self).await?)?)
    }

    /// 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().await.unwrap();
    ///     println!("{error_message:#?}");
    ///
    ///     // This is the same thing as doing
    ///     let error_message: ErrorMessage = response.json().await.unwrap();
    /// }
    /// ```
    async fn into_graph_error_message(self) -> Result<ErrorMessage, reqwest::Error> {
        self.json().await
    }

    /// 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())
    }
}

pub trait BodyExt<RHS = Self> {
    fn into_body(self) -> GraphResult<BodyRead>;
}

impl<U> BodyExt for &U
where
    U: serde::Serialize,
{
    fn into_body(self) -> GraphResult<BodyRead> {
        BodyRead::from_serialize(self)
    }
}

impl BodyExt for &FileConfig {
    fn into_body(self) -> GraphResult<BodyRead> {
        BodyRead::try_from(self)
    }
}

impl BodyExt for reqwest::Body {
    fn into_body(self) -> GraphResult<BodyRead> {
        Ok(BodyRead::from(self))
    }
}

impl BodyExt for reqwest::blocking::Body {
    fn into_body(self) -> GraphResult<BodyRead> {
        Ok(BodyRead::from(self))
    }
}