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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! [`GmailClient`](struct.GmailClient.html) is the main entry point for this library.
//!
//! Library created with [`libninja`](https://www.libninja.com).
#![allow(non_camel_case_types)]
#![allow(unused)]
pub mod model;
pub mod request;
pub use httpclient::{Error, Result, InMemoryResponseExt};
use std::sync::{Arc, OnceLock};
use std::borrow::Cow;
use crate::model::*;
static SHARED_HTTPCLIENT: OnceLock<httpclient::Client> = OnceLock::new();
pub fn default_http_client() -> httpclient::Client {
    httpclient::Client::new().base_url("https://gmail.googleapis.com/")
}
/// Use this method if you want to add custom middleware to the httpclient.
/// It must be called before any requests are made, otherwise it will have no effect.
/// Example usage:
///
/// ```
/// init_http_client(default_http_client()
///     .with_middleware(..)
/// );
/// ```
pub fn init_http_client(init: httpclient::Client) {
    let _ = SHARED_HTTPCLIENT.set(init);
}
fn shared_http_client() -> Cow<'static, httpclient::Client> {
    Cow::Borrowed(SHARED_HTTPCLIENT.get_or_init(default_http_client))
}
static SHARED_OAUTH2FLOW: OnceLock<httpclient_oauth2::OAuth2Flow> = OnceLock::new();
pub fn init_oauth2_flow(init: httpclient_oauth2::OAuth2Flow) {
    let _ = SHARED_OAUTH2FLOW.set(init);
}
pub fn shared_oauth2_flow() -> &'static httpclient_oauth2::OAuth2Flow {
    SHARED_OAUTH2FLOW
        .get_or_init(|| httpclient_oauth2::OAuth2Flow {
            client_id: std::env::var("GMAIL_CLIENT_ID")
                .expect("GMAIL_CLIENT_ID must be set"),
            client_secret: std::env::var("GMAIL_CLIENT_SECRET")
                .expect("GMAIL_CLIENT_SECRET must be set"),
            init_endpoint: "https://accounts.google.com/o/oauth2/auth".to_string(),
            exchange_endpoint: "https://accounts.google.com/o/oauth2/token".to_string(),
            refresh_endpoint: "https://accounts.google.com/o/oauth2/token".to_string(),
            redirect_uri: std::env::var("GMAIL_REDIRECT_URI")
                .expect("GMAIL_REDIRECT_URI must be set"),
        })
}
#[derive(Clone)]
pub struct FluentRequest<'a, T> {
    pub(crate) client: &'a GmailClient,
    pub params: T,
}
pub struct GmailClient {
    client: Cow<'static, httpclient::Client>,
    authentication: GmailAuth,
}
impl GmailClient {
    pub fn from_env() -> Self {
        Self {
            client: shared_http_client(),
            authentication: GmailAuth::from_env(),
        }
    }
    pub fn with_auth(authentication: GmailAuth) -> Self {
        Self {
            client: shared_http_client(),
            authentication,
        }
    }
    pub fn new_with(client: httpclient::Client, authentication: GmailAuth) -> Self {
        Self {
            client: Cow::Owned(client),
            authentication,
        }
    }
}
impl GmailClient {
    pub(crate) fn authenticate<'a>(
        &self,
        mut r: httpclient::RequestBuilder<'a>,
    ) -> httpclient::RequestBuilder<'a> {
        match &self.authentication {
            GmailAuth::OAuth2 { middleware } => {
                r.middlewares.insert(0, middleware.clone());
            }
        }
        r
    }
    ///Lists the drafts in the user's mailbox.
    pub fn drafts_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsListRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsListRequest {
                include_spam_trash: None,
                max_results: None,
                page_token: None,
                q: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Creates a new draft with the `DRAFT` label.
    pub fn drafts_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsCreateRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsCreateRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Sends the specified, existing draft to the recipients in the `To`, `Cc`, and `Bcc` headers.
    pub fn drafts_send(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsSendRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsSendRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified draft.
    pub fn drafts_get(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsGetRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsGetRequest {
                format: None,
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Replaces a draft's content.
    pub fn drafts_update(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsUpdateRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsUpdateRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Immediately and permanently deletes the specified draft. Does not simply trash it.
    pub fn drafts_delete(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::DraftsDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::DraftsDeleteRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the history of all changes to the given mailbox. History results are returned in chronological order (increasing `historyId`).
    pub fn history_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::HistoryListRequest> {
        FluentRequest {
            client: self,
            params: request::HistoryListRequest {
                history_types: None,
                label_id: None,
                max_results: None,
                page_token: None,
                start_history_id: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists all labels in the user's mailbox.
    pub fn labels_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsListRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsListRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Creates a new label.
    pub fn labels_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsCreateRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsCreateRequest {
                color: None,
                id: None,
                label_list_visibility: None,
                message_list_visibility: None,
                messages_total: None,
                messages_unread: None,
                name: None,
                threads_total: None,
                threads_unread: None,
                type_: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified label.
    pub fn labels_get(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsGetRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsGetRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates the specified label.
    pub fn labels_update(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsUpdateRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsUpdateRequest {
                color: None,
                id: id.to_owned(),
                label_list_visibility: None,
                message_list_visibility: None,
                messages_total: None,
                messages_unread: None,
                name: None,
                threads_total: None,
                threads_unread: None,
                type_: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Immediately and permanently deletes the specified label and removes it from any messages and threads that it is applied to.
    pub fn labels_delete(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsDeleteRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Patch the specified label.
    pub fn labels_patch(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::LabelsPatchRequest> {
        FluentRequest {
            client: self,
            params: request::LabelsPatchRequest {
                color: None,
                id: id.to_owned(),
                label_list_visibility: None,
                message_list_visibility: None,
                messages_total: None,
                messages_unread: None,
                name: None,
                threads_total: None,
                threads_unread: None,
                type_: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the messages in the user's mailbox.
    pub fn messages_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesListRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesListRequest {
                include_spam_trash: None,
                label_ids: None,
                max_results: None,
                page_token: None,
                q: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Directly inserts a message into only this user's mailbox similar to `IMAP APPEND`, bypassing most scanning and classification. Does not send a message.
    pub fn messages_insert(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesInsertRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesInsertRequest {
                deleted: None,
                internal_date_source: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Deletes many messages by message ID. Provides no guarantees that messages were not already deleted or even existed at all.
    pub fn messages_batch_delete(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesBatchDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesBatchDeleteRequest {
                ids: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Modifies the labels on the specified messages.
    pub fn messages_batch_modify(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesBatchModifyRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesBatchModifyRequest {
                add_label_ids: None,
                ids: None,
                remove_label_ids: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Imports a message into only this user's mailbox, with standard email delivery scanning and classification similar to receiving via SMTP. This method doesn't perform SPF checks, so it might not work for some spam messages, such as those attempting to perform domain spoofing. This method does not send a message. Note: This function doesn't trigger forwarding rules or filters set up by the user.
    pub fn messages_import(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesImportRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesImportRequest {
                deleted: None,
                internal_date_source: None,
                never_mark_spam: None,
                process_for_calendar: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Sends the specified message to the recipients in the `To`, `Cc`, and `Bcc` headers.
    pub fn messages_send(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesSendRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesSendRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified message.
    pub fn messages_get(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesGetRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesGetRequest {
                format: None,
                id: id.to_owned(),
                metadata_headers: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Immediately and permanently deletes the specified message. This operation cannot be undone. Prefer `messages.trash` instead.
    pub fn messages_delete(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesDeleteRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Modifies the labels on the specified message.
    pub fn messages_modify(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesModifyRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesModifyRequest {
                add_label_ids: None,
                id: id.to_owned(),
                remove_label_ids: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Moves the specified message to the trash.
    pub fn messages_trash(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesTrashRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesTrashRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Removes the specified message from the trash.
    pub fn messages_untrash(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesUntrashRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesUntrashRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified message attachment.
    pub fn messages_attachments_get(
        &self,
        id: &str,
        message_id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::MessagesAttachmentsGetRequest> {
        FluentRequest {
            client: self,
            params: request::MessagesAttachmentsGetRequest {
                id: id.to_owned(),
                message_id: message_id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the current user's Gmail profile.
    pub fn get_profile(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::GetProfileRequest> {
        FluentRequest {
            client: self,
            params: request::GetProfileRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the auto-forwarding setting for the specified account.
    pub fn settings_get_auto_forwarding(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsGetAutoForwardingRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsGetAutoForwardingRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates the auto-forwarding setting for the specified account. A verified forwarding address must be specified when auto-forwarding is enabled. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_update_auto_forwarding(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsUpdateAutoForwardingRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsUpdateAutoForwardingRequest {
                disposition: None,
                email_address: None,
                enabled: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the delegates for the specified account. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_delegates_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsDelegatesListRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsDelegatesListRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Adds a delegate with its verification status set directly to `accepted`, without sending any verification email. The delegate user must be a member of the same G Suite organization as the delegator user. Gmail imposes limitations on the number of delegates and delegators each user in a G Suite organization can have. These limits depend on your organization, but in general each user can have up to 25 delegates and up to 10 delegators. Note that a delegate user must be referred to by their primary email address, and not an email alias. Also note that when a new delegate is created, there may be up to a one minute delay before the new delegate is available for use. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_delegates_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsDelegatesCreateRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsDelegatesCreateRequest {
                delegate_email: None,
                user_id: user_id.to_owned(),
                verification_status: None,
            },
        }
    }
    ///Gets the specified delegate. Note that a delegate user must be referred to by their primary email address, and not an email alias. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_delegates_get(
        &self,
        delegate_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsDelegatesGetRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsDelegatesGetRequest {
                delegate_email: delegate_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Removes the specified delegate (which can be of any verification status), and revokes any verification that may have been required for using it. Note that a delegate user must be referred to by their primary email address, and not an email alias. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_delegates_delete(
        &self,
        delegate_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsDelegatesDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsDelegatesDeleteRequest {
                delegate_email: delegate_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the message filters of a Gmail user.
    pub fn settings_filters_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsFiltersListRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsFiltersListRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Creates a filter. Note: you can only create a maximum of 1,000 filters.
    pub fn settings_filters_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsFiltersCreateRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsFiltersCreateRequest {
                action: None,
                criteria: None,
                id: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets a filter.
    pub fn settings_filters_get(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsFiltersGetRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsFiltersGetRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Deletes a filter.
    pub fn settings_filters_delete(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsFiltersDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsFiltersDeleteRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the forwarding addresses for the specified account.
    pub fn settings_forwarding_addresses_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsForwardingAddressesListRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsForwardingAddressesListRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Creates a forwarding address. If ownership verification is required, a message will be sent to the recipient and the resource's verification status will be set to `pending`; otherwise, the resource will be created with verification status set to `accepted`. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_forwarding_addresses_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsForwardingAddressesCreateRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsForwardingAddressesCreateRequest {
                forwarding_email: None,
                user_id: user_id.to_owned(),
                verification_status: None,
            },
        }
    }
    ///Gets the specified forwarding address.
    pub fn settings_forwarding_addresses_get(
        &self,
        forwarding_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsForwardingAddressesGetRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsForwardingAddressesGetRequest {
                forwarding_email: forwarding_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Deletes the specified forwarding address and revokes any verification that may have been required. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_forwarding_addresses_delete(
        &self,
        forwarding_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsForwardingAddressesDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsForwardingAddressesDeleteRequest {
                forwarding_email: forwarding_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets IMAP settings.
    pub fn settings_get_imap(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsGetImapRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsGetImapRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates IMAP settings.
    pub fn settings_update_imap(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsUpdateImapRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsUpdateImapRequest {
                auto_expunge: None,
                enabled: None,
                expunge_behavior: None,
                max_folder_size: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets language settings.
    pub fn settings_get_language(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsGetLanguageRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsGetLanguageRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates language settings. If successful, the return object contains the `displayLanguage` that was saved for the user, which may differ from the value passed into the request. This is because the requested `displayLanguage` may not be directly supported by Gmail but have a close variant that is, and so the variant may be chosen and saved instead.
    pub fn settings_update_language(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsUpdateLanguageRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsUpdateLanguageRequest {
                display_language: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets POP settings.
    pub fn settings_get_pop(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsGetPopRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsGetPopRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates POP settings.
    pub fn settings_update_pop(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsUpdatePopRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsUpdatePopRequest {
                access_window: None,
                disposition: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the send-as aliases for the specified account. The result includes the primary send-as address associated with the account as well as any custom "from" aliases.
    pub fn settings_send_as_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsListRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsListRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Creates a custom "from" send-as alias. If an SMTP MSA is specified, Gmail will attempt to connect to the SMTP service to validate the configuration before creating the alias. If ownership verification is required for the alias, a message will be sent to the email address and the resource's verification status will be set to `pending`; otherwise, the resource will be created with verification status set to `accepted`. If a signature is provided, Gmail will sanitize the HTML before saving it with the alias. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_send_as_create(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsCreateRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsCreateRequest {
                display_name: None,
                is_default: None,
                is_primary: None,
                reply_to_address: None,
                send_as_email: None,
                signature: None,
                smtp_msa: None,
                treat_as_alias: None,
                user_id: user_id.to_owned(),
                verification_status: None,
            },
        }
    }
    ///Gets the specified send-as alias. Fails with an HTTP 404 error if the specified address is not a member of the collection.
    pub fn settings_send_as_get(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsGetRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsGetRequest {
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates a send-as alias. If a signature is provided, Gmail will sanitize the HTML before saving it with the alias. Addresses other than the primary address for the account can only be updated by service account clients that have been delegated domain-wide authority.
    pub fn settings_send_as_update(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsUpdateRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsUpdateRequest {
                display_name: None,
                is_default: None,
                is_primary: None,
                reply_to_address: None,
                send_as_email: send_as_email.to_owned(),
                signature: None,
                smtp_msa: None,
                treat_as_alias: None,
                user_id: user_id.to_owned(),
                verification_status: None,
            },
        }
    }
    ///Deletes the specified send-as alias. Revokes any verification that may have been required for using it. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_send_as_delete(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsDeleteRequest {
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Patch the specified send-as alias.
    pub fn settings_send_as_patch(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsPatchRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsPatchRequest {
                display_name: None,
                is_default: None,
                is_primary: None,
                reply_to_address: None,
                send_as_email: send_as_email.to_owned(),
                signature: None,
                smtp_msa: None,
                treat_as_alias: None,
                user_id: user_id.to_owned(),
                verification_status: None,
            },
        }
    }
    ///Lists S/MIME configs for the specified send-as alias.
    pub fn settings_send_as_smime_info_list(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsSmimeInfoListRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsSmimeInfoListRequest {
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Insert (upload) the given S/MIME config for the specified send-as alias. Note that pkcs12 format is required for the key.
    pub fn settings_send_as_smime_info_insert(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsSmimeInfoInsertRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsSmimeInfoInsertRequest {
                encrypted_key_password: None,
                expiration: None,
                id: None,
                is_default: None,
                issuer_cn: None,
                pem: None,
                pkcs12: None,
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified S/MIME config for the specified send-as alias.
    pub fn settings_send_as_smime_info_get(
        &self,
        id: &str,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsSmimeInfoGetRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsSmimeInfoGetRequest {
                id: id.to_owned(),
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Deletes the specified S/MIME config for the specified send-as alias.
    pub fn settings_send_as_smime_info_delete(
        &self,
        id: &str,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsSmimeInfoDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsSmimeInfoDeleteRequest {
                id: id.to_owned(),
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Sets the default S/MIME config for the specified send-as alias.
    pub fn settings_send_as_smime_info_set_default(
        &self,
        id: &str,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsSmimeInfoSetDefaultRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsSmimeInfoSetDefaultRequest {
                id: id.to_owned(),
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Sends a verification email to the specified send-as alias address. The verification status must be `pending`. This method is only available to service account clients that have been delegated domain-wide authority.
    pub fn settings_send_as_verify(
        &self,
        send_as_email: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsSendAsVerifyRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsSendAsVerifyRequest {
                send_as_email: send_as_email.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets vacation responder settings.
    pub fn settings_get_vacation(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsGetVacationRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsGetVacationRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Updates vacation responder settings.
    pub fn settings_update_vacation(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::SettingsUpdateVacationRequest> {
        FluentRequest {
            client: self,
            params: request::SettingsUpdateVacationRequest {
                enable_auto_reply: None,
                end_time: None,
                response_body_html: None,
                response_body_plain_text: None,
                response_subject: None,
                restrict_to_contacts: None,
                restrict_to_domain: None,
                start_time: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Stop receiving push notifications for the given user mailbox.
    pub fn stop(&self, user_id: &str) -> FluentRequest<'_, request::StopRequest> {
        FluentRequest {
            client: self,
            params: request::StopRequest {
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Lists the threads in the user's mailbox.
    pub fn threads_list(
        &self,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsListRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsListRequest {
                include_spam_trash: None,
                label_ids: None,
                max_results: None,
                page_token: None,
                q: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Gets the specified thread.
    pub fn threads_get(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsGetRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsGetRequest {
                format: None,
                id: id.to_owned(),
                metadata_headers: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Immediately and permanently deletes the specified thread. This operation cannot be undone. Prefer `threads.trash` instead.
    pub fn threads_delete(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsDeleteRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsDeleteRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Modifies the labels applied to the thread. This applies to all messages in the thread.
    pub fn threads_modify(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsModifyRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsModifyRequest {
                add_label_ids: None,
                id: id.to_owned(),
                remove_label_ids: None,
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Moves the specified thread to the trash.
    pub fn threads_trash(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsTrashRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsTrashRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Removes the specified thread from the trash.
    pub fn threads_untrash(
        &self,
        id: &str,
        user_id: &str,
    ) -> FluentRequest<'_, request::ThreadsUntrashRequest> {
        FluentRequest {
            client: self,
            params: request::ThreadsUntrashRequest {
                id: id.to_owned(),
                user_id: user_id.to_owned(),
            },
        }
    }
    ///Set up or update a push notification watch on the given user mailbox.
    pub fn watch(&self, user_id: &str) -> FluentRequest<'_, request::WatchRequest> {
        FluentRequest {
            client: self,
            params: request::WatchRequest {
                label_filter_action: None,
                label_ids: None,
                topic_name: None,
                user_id: user_id.to_owned(),
            },
        }
    }
}
pub enum GmailAuth {
    OAuth2 { middleware: Arc<httpclient_oauth2::OAuth2> },
}
impl GmailAuth {
    pub fn from_env() -> Self {
        let access = std::env::var("GMAIL_ACCESS_TOKEN").unwrap();
        let refresh = std::env::var("GMAIL_REFRESH_TOKEN").unwrap();
        let mw = shared_oauth2_flow().bearer_middleware(access, refresh);
        Self::OAuth2 {
            middleware: std::sync::Arc::new(mw),
        }
    }
    pub fn oauth2(access: String, refresh: String) -> Self {
        let mw = shared_oauth2_flow().bearer_middleware(access, refresh);
        Self::OAuth2 {
            middleware: Arc::new(mw),
        }
    }
}