gsuite-api 0.1.18

An API client for GSuite APIs: directory, resources, groups
Documentation
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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
/*!
 * A rust library for interacting with the GSuite APIs.
 *
 * For more information, the GSuite Directory API is documented at
 * [developers.google.com/admin-sdk/directory/v1/reference](https://developers.google.com/admin-sdk/directory/v1/reference)
 * and the Google Groups settings API is documented at
 * [developers.google.com/admin-sdk/groups-settings/v1/reference/groups](https://developers.google.com/admin-sdk/groups-settings/v1/reference/groups).
 *
 * Example:
 *
 * ```
 * use std::env;
 *
 * use gsuite_api::GSuite;
 * use yup_oauth2::{read_service_account_key, ServiceAccountAuthenticator};
 *
 * async fn get_users() {
 *     // Get the GSuite credentials file.
 *     let gsuite_credential_file = env::var("GADMIN_CREDENTIAL_FILE").unwrap();
 *     let gsuite_subject = env::var("GADMIN_SUBJECT").unwrap();
 *     let gsuite_secret = read_service_account_key(gsuite_credential_file).await.expect("failed to read gsuite credential file");
 *     let auth = ServiceAccountAuthenticator::builder(gsuite_secret)
 *         .subject(gsuite_subject.to_string())
 *         .build()
 *         .await
 *         .expect("failed to create authenticator");
 *
 *     // Add the scopes to the secret and get the token.
 *     let token = auth
 *         .token(&[
 *             "https://www.googleapis.com/auth/admin.directory.group",
 *             "https://www.googleapis.com/auth/admin.directory.resource.calendar",
 *             "https://www.googleapis.com/auth/admin.directory.user",
 *             "https://www.googleapis.com/auth/apps.groups.settings",
 *         ])
 *         .await
 *         .expect("failed to get token");
 *
 *     if token.as_str().is_empty() {
 *         panic!("empty token is not valid");
 *     }
 *
 *     // Initialize the GSuite client.
 *     let gsuite_client = GSuite::new("customer_id", "domain", token);
 *
 *     // List users.
 *     let users = gsuite_client.list_users().await;
 *
 *     // Iterate over the users.
 *     for user in users {
 *         println!("{:?}", user);
 *     }
 * }
 * ```
 */
use std::collections::HashMap;
use std::error;
use std::fmt;
use std::sync::Arc;

use chrono::naive::NaiveDate;
use chrono::offset::Utc;
use chrono::DateTime;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::{header, Client, Method, Request, StatusCode, Url};
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
use serde_json::value::Value;
use yup_oauth2::AccessToken;

/// The endpoint for the GSuite Directory API.
const DIRECTORY_ENDPOINT: &str = "https://www.googleapis.com/admin/directory/v1/";

/// Endpoint for the Google Groups settings API.
const GROUPS_SETTINGS_ENDPOINT: &str = "https://www.googleapis.com/groups/v1/groups/";

/// Endpoint for the Google Calendar API.
const CALENDAR_ENDPOINT: &str = "https://www.googleapis.com/calendar/v3/";

/// Entrypoint for interacting with the GSuite APIs.
pub struct GSuite {
    customer: String,
    domain: String,

    token: AccessToken,

    client: Arc<Client>,
}

impl GSuite {
    /// Create a new GSuite client struct. It takes a type that can convert into
    /// an &str (`String` or `Vec<u8>` for example). As long as the function is
    /// given a valid API Key and Secret your requests will work.
    pub fn new(customer: &str, domain: &str, token: AccessToken) -> Self {
        let client = Client::builder().build().expect("creating client failed");
        Self {
            customer: customer.to_string(),
            domain: domain.to_string(),
            token,
            client: Arc::new(client),
        }
    }

    /// Get the currently set authorization token.
    pub fn get_token(&self) -> &AccessToken {
        &self.token
    }

    fn request<B>(&self, endpoint: &str, method: Method, path: &str, body: B, query: Option<&[(&str, &str)]>) -> Request
    where
        B: Serialize,
    {
        let base = Url::parse(endpoint).unwrap();
        let url = base.join(path).unwrap();

        // Check if the token is expired and panic.
        if self.token.is_expired() {
            panic!("token is expired");
        }

        let bt = format!("Bearer {}", self.token.as_str());
        let bearer = header::HeaderValue::from_str(&bt).unwrap();

        // Set the default headers.
        let mut headers = header::HeaderMap::new();
        headers.append(header::AUTHORIZATION, bearer);
        headers.append(header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"));
        headers.append(header::ACCEPT, header::HeaderValue::from_static("application/json"));

        let mut rb = self.client.request(method.clone(), url).headers(headers);

        if let Some(val) = query {
            rb = rb.query(&val);
        }

        // Add the body, this is to ensure our GET and DELETE calls succeed.
        if method != Method::GET && method != Method::DELETE {
            rb = rb.json(&body);
        }

        // Build the request.
        rb.build().unwrap()
    }

    /// List Google groups.
    pub async fn list_groups(&self) -> Result<Vec<Group>, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::GET, "groups", (), Some(&[("customer", &self.customer), ("domain", &self.domain)]));

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: Groups = resp.json().await.unwrap();

        Ok(value.groups)
    }

    /// Get the settings for a Google group.
    pub async fn get_group_settings(&self, group_email: &str) -> Result<GroupSettings, APIError> {
        // Build the request.
        let request = self.request(GROUPS_SETTINGS_ENDPOINT, Method::GET, group_email, (), Some(&[("alt", "json")]));

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        Ok(resp.json().await.unwrap())
    }

    /// Update a Google group.
    pub async fn update_group(&self, group: &Group) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::PUT, &format!("groups/{}", group.id), group, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Update a Google group's settings.
    pub async fn update_group_settings(&self, settings: &GroupSettings) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(GROUPS_SETTINGS_ENDPOINT, Method::PUT, &settings.email, settings, Some(&[("alt", "json")]));

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Create a google group.
    pub async fn create_group(&self, group: &Group) -> Result<Group, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, "groups", group, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        Ok(resp.json().await.unwrap())
    }

    /// Update a Google group's aliases.
    pub async fn update_group_aliases<A>(&self, group_key: &str, aliases: A)
    where
        A: IntoIterator,
        A::Item: AsRef<str>,
    {
        for alias in aliases {
            self.update_group_alias(group_key, alias.as_ref()).await.unwrap();
        }
    }

    /// Update an alias for a Google group.
    pub async fn update_group_alias(&self, group_key: &str, alias: &str) -> Result<(), APIError> {
        let mut a: HashMap<&str, &str> = HashMap::new();
        a.insert("alias", alias);
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, &format!("groups/{}/aliases", group_key), a, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                let body = resp.text().await.unwrap();

                if body.contains("duplicate") {
                    // Ignore the error because we don't care about if it is a duplicate.
                    return Ok(());
                }

                return Err(APIError { status_code: s, body });
            }
        };

        Ok(())
    }

    /// Check if a user is a member of a Google group.
    pub async fn group_has_member(&self, group_id: &str, email: &str) -> Result<bool, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::GET, &format!("groups/{}/hasMember/{}", group_id, email), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: MembersHasMember = resp.json().await.unwrap();

        Ok(value.is_member)
    }

    /// Update a member of a Google group.
    pub async fn group_update_member(&self, group_id: &str, email: &str, role: &str) -> Result<(), APIError> {
        let member = Member {
            role: role.to_string(),
            email: email.to_string(),
            delivery_settings: "ALL_MAIL".to_string(),
            etag: "".to_string(),
            id: "".to_string(),
            kind: "".to_string(),
            status: "".to_string(),
            typev: "".to_string(),
        };

        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::PUT, &format!("groups/{}/members/{}", group_id, email), member, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Add a user as a member of a Google group.
    pub async fn group_insert_member(&self, group_id: &str, email: &str, role: &str) -> Result<(), APIError> {
        let member = Member {
            role: role.to_string(),
            email: email.to_string(),
            delivery_settings: "ALL_MAIL".to_string(),
            etag: "".to_string(),
            id: "".to_string(),
            kind: "".to_string(),
            status: "".to_string(),
            typev: "".to_string(),
        };

        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, &format!("groups/{}/members", group_id), member, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Remove a user as a member of a Google group.
    pub async fn group_remove_member(&self, group_id: &str, email: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::DELETE, &format!("groups/{}/members/{}", group_id, email), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Delete a group.
    /// The `group_key` can be the group's email address, group alias, or the unique group ID.
    /// FROM: https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/delete
    pub async fn delete_group(&self, group_key: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::DELETE, &format!("groups/{}", group_key), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// List users.
    pub async fn list_users(&self) -> Result<Vec<User>, APIError> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::GET,
            "users",
            (),
            Some(&[("customer", &self.customer), ("domain", &self.domain), ("projection", "full")]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: Users = resp.json().await.unwrap();

        Ok(value.users)
    }

    /// Update a user.
    pub async fn update_user(&self, user: &User) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::PUT, &format!("users/{}", user.id), user, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Create a user.
    pub async fn create_user(&self, user: &User) -> Result<User, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, "users", user, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        Ok(resp.json().await.unwrap())
    }

    /// Delete a user.
    /// The `user_key` can be the user's primary email address, alias email address, or unique user ID.
    /// FROM: https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/delete
    pub async fn delete_user(&self, user_key: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::DELETE, &format!("users/{}", user_key), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Update a user's aliases.
    pub async fn update_user_aliases<A>(&self, user_id: &str, aliases: A)
    where
        A: IntoIterator,
        A::Item: AsRef<str>,
    {
        for alias in aliases {
            self.update_user_alias(user_id, alias.as_ref()).await.unwrap();
        }
    }

    /// Update an alias for a user.
    pub async fn update_user_alias(&self, user_id: &str, alias: &str) -> Result<(), APIError> {
        let mut a: HashMap<&str, &str> = HashMap::new();
        a.insert("alias", alias);
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, &format!("users/{}/aliases", user_id), a, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            StatusCode::NOT_FOUND => {
                // Try again. It might just be that the user was just created.
                // TODO: this will result in an endless loop if that is not the case
                // clean this up eventually. Sorry future person.
                println!("Got 404 while updating user alias for user {} alias {}, trying again", user_id, alias);
                //return self.update_user_alias(user_id, alias).await;
            }
            s => {
                let body = resp.text().await.unwrap();

                if body.contains("duplicate") {
                    // Ignore the error because we don't care about if it is a duplicate.
                    return Ok(());
                }

                return Err(APIError { status_code: s, body });
            }
        };

        Ok(())
    }

    /// List calendar resources.
    pub async fn list_calendar_resources(&self) -> Result<Vec<CalendarResource>, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::GET, &format!("customer/{}/resources/calendars", self.customer), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: CalendarResources = resp.json().await.unwrap();

        Ok(value.items)
    }

    /// Update a calendar resource.
    pub async fn update_calendar_resource(&self, resource: &CalendarResource) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!("customer/{}/resources/calendars/{}", self.customer, resource.id),
            resource,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Create a calendar resource.
    pub async fn create_calendar_resource(&self, resource: &CalendarResource) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, &format!("customer/{}/resources/calendars", self.customer), resource, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Delete a calendar resource.
    /// FROM: https://developers.google.com/admin-sdk/directory/reference/rest/v1/resources.calendars/delete
    pub async fn delete_calendar_resource(&self, id: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::DELETE, &format!("customer/{}/resources/calendars/{}", self.customer, id), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            StatusCode::NO_CONTENT => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// List buildings.
    pub async fn list_buildings(&self) -> Result<Vec<Building>, APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::GET, &format!("customer/{}/resources/buildings", self.customer), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: Buildings = resp.json().await.unwrap();

        Ok(value.buildings)
    }

    /// Update a building.
    pub async fn update_building(&self, building: &Building) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(
            DIRECTORY_ENDPOINT,
            Method::PUT,
            &format!("customer/{}/resources/buildings/{}", self.customer, building.id),
            building,
            None,
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Create a building.
    pub async fn create_building(&self, building: &Building) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::POST, &format!("customer/{}/resources/buildings", self.customer), building, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// Delete a building.
    /// FROM: https://developers.google.com/admin-sdk/directory/reference/rest/v1/resources.buildings/delete
    pub async fn delete_building(&self, id: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(DIRECTORY_ENDPOINT, Method::DELETE, &format!("customer/{}/resources/buildings/{}", self.customer, id), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            StatusCode::NO_CONTENT => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }

    /// List calendars for a user.
    pub async fn list_calendars(&self) -> Result<Vec<Calendar>, APIError> {
        // Build the request.
        let request = self.request(CALENDAR_ENDPOINT, Method::GET, "users/me/calendarList", (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: Calendars = resp.json().await.unwrap();

        Ok(value.items)
    }

    /// List events on a calendar.
    pub async fn list_calendar_events(&self, calendar_id: &str, show_deleted: bool) -> Result<Vec<CalendarEvent>, APIError> {
        // Build the request.
        let request = self.request(
            CALENDAR_ENDPOINT,
            Method::GET,
            &format!("calendars/{}/events", calendar_id),
            (),
            Some(&[("singleEvents", "true"), ("maxResults", "2500"), ("showDeleted", &show_deleted.to_string())]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: CalendarEvents = resp.json().await.unwrap();

        Ok(value.items)
    }

    /// List past events on a calendar.
    pub async fn list_past_calendar_events(&self, calendar_id: &str) -> Result<Vec<CalendarEvent>, APIError> {
        // Build the request.
        let request = self.request(
            CALENDAR_ENDPOINT,
            Method::GET,
            &format!("calendars/{}/events", calendar_id),
            (),
            Some(&[("singleEvents", "true"), ("maxResults", "2500"), ("timeMax", &Utc::now().to_rfc3339())]),
        );

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        // Try to deserialize the response.
        let value: CalendarEvents = resp.json().await.unwrap();

        Ok(value.items)
    }

    /// Create a calendar event.
    pub async fn create_calendar_event(&self, calendar_id: &str, event: &CalendarEvent) -> Result<CalendarEvent, APIError> {
        // Build the request.
        let request = self.request(CALENDAR_ENDPOINT, Method::POST, &format!("calendars/{}/events", calendar_id), event, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(resp.json().await.unwrap())
    }

    /// Update a calendar event.
    pub async fn update_calendar_event(&self, calendar_id: &str, event_id: &str, event: &CalendarEvent) -> Result<CalendarEvent, APIError> {
        // Build the request.
        let request = self.request(CALENDAR_ENDPOINT, Method::PUT, &format!("calendars/{}/events/{}", calendar_id, event_id), event, None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(resp.json().await.unwrap())
    }

    /// Get a calendar event.
    pub async fn get_calendar_event(&self, calendar_id: &str, event_id: &str) -> Result<CalendarEvent, APIError> {
        // Build the request.
        let request = self.request(CALENDAR_ENDPOINT, Method::GET, &format!("calendars/{}/events/{}", calendar_id, event_id), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(resp.json().await.unwrap())
    }

    pub async fn delete_calendar_event(&self, calendar_id: &str, event_id: &str) -> Result<(), APIError> {
        // Build the request.
        let request = self.request(CALENDAR_ENDPOINT, Method::DELETE, &format!("calendars/{}/events/{}", calendar_id, event_id), (), None);

        let resp = self.client.execute(request).await.unwrap();
        match resp.status() {
            StatusCode::OK => (),
            StatusCode::NO_CONTENT => (),
            StatusCode::NOT_FOUND => (),
            s => {
                return Err(APIError {
                    status_code: s,
                    body: resp.text().await.unwrap(),
                });
            }
        };

        Ok(())
    }
}

/// Error type returned by our library.
pub struct APIError {
    pub status_code: StatusCode,
    pub body: String,
}

impl fmt::Display for APIError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "APIError: status code -> {}, body -> {}", self.status_code.to_string(), self.body)
    }
}

impl fmt::Debug for APIError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "APIError: status code -> {}, body -> {}", self.status_code.to_string(), self.body)
    }
}

// This is important for other errors to wrap this one.
impl error::Error for APIError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

/// Generate a random string that we can use as a temporary password for new users
/// when we set up their account.
pub fn generate_password() -> String {
    thread_rng().sample_iter(&Alphanumeric).take(30).collect()
}

/// A list of calendars.
/// FROM: https://developers.google.com/calendar/v3/reference/calendarList/list
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Calendars {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Token used at a later point in time to retrieve only the entries that have changed since
    /// this result was returned. Omitted if further results are available, in which case
    /// nextPageToken is provided.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextSyncToken")]
    pub next_sync_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of group objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<Calendar>,
}

/// A calendar.
/// FROM: https://developers.google.com/calendar/v3/reference/calendarList#resource
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Calendar {
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub summary: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub location: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "timeZone")]
    pub time_zone: String,
}

/// A list of calendar events.
/// FROM: https://developers.google.com/calendar/v3/reference/events/list
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarEvents {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Token used at a later point in time to retrieve only the entries that have changed since
    /// this result was returned. Omitted if further results are available, in which case
    /// nextPageToken is provided.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextSyncToken")]
    pub next_sync_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of group objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<CalendarEvent>,
}

/// A calendar event.
/// FROM: https://developers.google.com/calendar/v3/reference/events#resource
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct CalendarEvent {
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub status: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "htmlLink")]
    pub html_link: String,
    pub created: Option<DateTime<Utc>>,
    pub updated: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub summary: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub location: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub recurrence: Vec<String>,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "recurringEventId")]
    pub recurring_event_id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub transparency: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub visibility: String,
    #[serde(default)]
    pub sequence: i64,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attendees: Vec<Attendee>,
    pub start: Date,
    pub end: Date,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Date {
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "timeZone")]
    pub time_zone: String,
    #[serde(default)]
    pub date: Option<NaiveDate>,
    #[serde(default, rename = "dateTime")]
    pub date_time: Option<DateTime<Utc>>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Attendee {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub email: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "displayName")]
    pub display_name: String,
    #[serde(default)]
    pub organizer: bool,
    #[serde(default)]
    pub resource: bool,
    #[serde(default)]
    pub optional: bool,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "responseStatus")]
    pub response_status: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub comment: String,
    #[serde(default, rename = "additionalGuests")]
    pub additional_guests: i64,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Attachment {
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "fileUrl")]
    pub file_url: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub title: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "mimeType")]
    pub mime_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "iconLink")]
    pub icon_link: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "fileId")]
    pub file_id: String,
}

/// A Google group.
/// FROM: https://developers.google.com/admin-sdk/directory/v1/reference/groups
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Group {
    /// Was the group created by admin (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "adminCreated")]
    pub admin_created: Option<bool>,
    /// List of aliases (read-only)
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub aliases: Vec<String>,
    /// Description of the group
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    /// Group direct members count
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "directMembersCount")]
    pub direct_members_count: String,
    /// Email of group
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub email: String,
    /// ETag of the resource
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Unique identifier of group (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    /// Kind of resource this is
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// Group name
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    /// List of non editable aliases (Read-only)
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "nonEditableAliases")]
    pub non_editable_aliases: Vec<String>,
}

/// A Google group's settings.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct GroupSettings {
    /// Permission to ban users. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanBanUsers")]
    pub who_can_ban_users: String,
    /// Permission for content assistants. Possible values are: Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanAssistContent")]
    pub who_can_assist_content: String,
    /// Are external members allowed to join the group.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "allowExternalMembers")]
    pub allow_external_members: String,
    /// Permission to enter free form tags for topics in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanEnterFreeFormTags")]
    pub who_can_enter_free_form_tags: String,
    /// Permission to approve pending messages in the moderation queue. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanApproveMessages")]
    pub who_can_approve_messages: String,
    /// Permission to mark a topic as a duplicate of another topic. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMarkDuplicate")]
    pub who_can_mark_duplicate: String,
    /// Permissions to join the group. Possible values are: ANYONE_CAN_JOIN ALL_IN_DOMAIN_CAN_JOIN INVITED_CAN_JOIN CAN_REQUEST_TO_JOIN
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanJoin")]
    pub who_can_join: String,
    /// Permission to change tags and categories. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanModifyTagsAndCategories")]
    pub who_can_modify_tags_and_categories: String,
    /// Permission to mark a topic as not needing a response. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMarkNoResponseNeeded")]
    pub who_can_mark_no_response_needed: String,
    /// Permission to unmark any post from a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanUnmarkFavoriteReplyOnAnyTopic")]
    pub who_can_unmark_favorite_reply_on_any_topic: String,
    /// Permission for content moderation. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanModerateContent")]
    pub who_can_moderate_content: String,
    /// Primary language for the group.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "primaryLanguage")]
    pub primary_language: String,
    /// Permission to mark a post for a topic they started as a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMarkFavoriteReplyOnOwnTopic")]
    pub who_can_mark_favorite_reply_on_own_topic: String,
    /// Permissions to view membership. Possible values are: ALL_IN_DOMAIN_CAN_VIEW ALL_MEMBERS_CAN_VIEW ALL_MANAGERS_CAN_VIEW ALL_OWNERS_CAN_VIEW
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanViewMembership")]
    pub who_can_view_membership: String,
    /// If favorite replies should be displayed above other replies.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "favoriteRepliesOnTop")]
    pub favorite_replies_on_top: String,
    /// Permission to mark any other user's post as a favorite reply. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMarkFavoriteReplyOnAnyTopic")]
    pub who_can_mark_favorite_reply_on_any_topic: String,
    /// Whether to include custom footer.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "includeCustomFooter")]
    pub include_custom_footer: String,
    /// Permission to move topics out of the group or forum. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMoveTopicsOut")]
    pub who_can_move_topics_out: String,
    /// Default message deny notification message
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "defaultMessageDenyNotificationText")]
    pub default_message_deny_notification_text: String,
    /// If this groups should be included in global address list or not.
    #[serde(default, rename = "includeInGlobalAddressList", skip_serializing_if = "String::is_empty")]
    pub include_in_global_address_list: String,
    /// If the group is archive only
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "archiveOnly")]
    pub archive_only: String,
    /// Permission to delete topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanDeleteTopics")]
    pub who_can_delete_topics: String,
    /// Permission to delete replies to topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanDeleteAnyPost")]
    pub who_can_delete_any_post: String,
    /// If the contents of the group are archived.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "isArchived")]
    pub is_archived: String,
    /// Can members post using the group email address.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "membersCanPostAsTheGroup")]
    pub members_can_post_as_the_group: String,
    /// Permission to make topics appear at the top of the topic list. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMakeTopicsSticky")]
    pub who_can_make_topics_sticky: String,
    /// If any of the settings that will be merged have custom roles which is anything other than owners, managers, or group scopes.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customRolesEnabledForSettingsToBeMerged")]
    pub custom_roles_enabled_for_settings_to_be_merged: String,
    /// Email id of the group
    pub email: String,
    /// Permission for who can discover the group. Possible values are: ALL_MEMBERS_CAN_DISCOVER ALL_IN_DOMAIN_CAN_DISCOVER ANYONE_CAN_DISCOVER
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanDiscoverGroup")]
    pub who_can_discover_group: String,
    /// Permission to modify members (change member roles). Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanModifyMembers")]
    pub who_can_modify_members: String,
    /// Moderation level for messages. Possible values are: MODERATE_ALL_MESSAGES MODERATE_NON_MEMBERS MODERATE_NEW_MEMBERS MODERATE_NONE
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "messageModerationLevel")]
    pub message_moderation_level: String,
    /// Description of the group
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    /// Permission to unassign any topic in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanUnassignTopic")]
    pub who_can_unassign_topic: String,
    /// Whome should the default reply to a message go to. Possible values are: REPLY_TO_CUSTOM REPLY_TO_SENDER REPLY_TO_LIST REPLY_TO_OWNER REPLY_TO_IGNORE REPLY_TO_MANAGERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "replyTo")]
    pub reply_to: String,
    /// Default email to which reply to any message should go.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customReplyTo")]
    pub custom_reply_to: String,
    /// Should the member be notified if his message is denied by owner.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "sendMessageDenyNotification")]
    pub send_message_deny_notification: String,
    /// If a primary Collab Inbox feature is enabled.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "enableCollaborativeInbox")]
    pub enable_collaborative_inbox: String,
    /// Permission to contact owner of the group via web UI. Possible values are: ANYONE_CAN_CONTACT ALL_IN_DOMAIN_CAN_CONTACT ALL_MEMBERS_CAN_CONTACT ALL_MANAGERS_CAN_CONTACT
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanContactOwner")]
    pub who_can_contact_owner: String,
    /// Default message display font. Possible values are: DEFAULT_FONT FIXED_WIDTH_FONT
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "messageDisplayFont")]
    pub message_display_font: String,
    /// Permission to leave the group. Possible values are: ALL_MANAGERS_CAN_LEAVE ALL_OWNERS_CAN_LEAVE ALL_MEMBERS_CAN_LEAVE NONE_CAN_LEAVE
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanLeaveGroup")]
    pub who_can_leave_group: String,
    /// Permissions to add members. Possible values are: ALL_MANAGERS_CAN_ADD ALL_OWNERS_CAN_ADD ALL_MEMBERS_CAN_ADD NONE_CAN_ADD
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanAdd")]
    pub who_can_add: String,
    /// Permissions to post messages to the group. Possible values are: NONE_CAN_POST ALL_MANAGERS_CAN_POST ALL_MEMBERS_CAN_POST ALL_OWNERS_CAN_POST ALL_IN_DOMAIN_CAN_POST ANYONE_CAN_POST
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanPostMessage")]
    pub who_can_post_message: String,
    /// Permission to move topics into the group or forum. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanMoveTopicsIn")]
    pub who_can_move_topics_in: String,
    /// Permission to take topics in a forum. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanTakeTopics")]
    pub who_can_take_topics: String,
    /// Name of the group
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    /// The type of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// Maximum message size allowed.
    #[serde(default, skip_serializing_if = "Option::is_none", rename = "maxMessageBytes")]
    pub max_message_bytes: Option<i32>,
    /// Permissions to invite members. Possible values are: ALL_MEMBERS_CAN_INVITE ALL_MANAGERS_CAN_INVITE ALL_OWNERS_CAN_INVITE NONE_CAN_INVITE
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanInvite")]
    pub who_can_invite: String,
    /// Permission to approve members. Possible values are: ALL_OWNERS_CAN_APPROVE ALL_MANAGERS_CAN_APPROVE ALL_MEMBERS_CAN_APPROVE NONE_CAN_APPROVE
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanApproveMembers")]
    pub who_can_approve_members: String,
    /// Moderation level for messages detected as spam. Possible values are: ALLOW MODERATE SILENTLY_MODERATE REJECT
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "spamModerationLevel")]
    pub spam_moderation_level: String,
    /// If posting from web is allowed.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "allowWebPosting")]
    pub allow_web_posting: String,
    /// Permission for membership moderation. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanModerateMembers")]
    pub who_can_moderate_members: String,
    /// Permission to add references to a topic. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanAddReferences")]
    pub who_can_add_references: String,
    /// Permissions to view group. Possible values are: ANYONE_CAN_VIEW ALL_IN_DOMAIN_CAN_VIEW ALL_MEMBERS_CAN_VIEW ALL_MANAGERS_CAN_VIEW ALL_OWNERS_CAN_VIEW
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanViewGroup")]
    pub who_can_view_group: String,
    /// Is the group listed in groups directory
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "showInGroupGSuite")]
    pub show_in_group_directory: String,
    /// Permission to post announcements, a special topic type. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanPostAnnouncements")]
    pub who_can_post_announcements: String,
    /// Permission to lock topics. Possible values are: NONE OWNERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanLockTopics")]
    pub who_can_lock_topics: String,
    /// Permission to assign topics in a forum to another user. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanAssignTopics")]
    pub who_can_assign_topics: String,
    /// Custom footer text.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customFooterText")]
    pub custom_footer_text: String,
    /// Is google allowed to contact admins.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "allowGoogleCommunication")]
    pub allow_google_communication: String,
    /// Permission to hide posts by reporting them as abuse. Possible values are: NONE OWNERS_ONLY MANAGERS_ONLY OWNERS_AND_MANAGERS ALL_MEMBERS
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "whoCanHideAbuse")]
    pub who_can_hide_abuse: String,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Groups {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of group objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub groups: Vec<Group>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct MembersHasMember {
    /// Identifies whether the given user is a member of the group. Membership can be direct or nested.
    #[serde(default, rename = "isMember")]
    pub is_member: bool,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Members {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of member objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub members: Vec<Member>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Member {
    /// Status of member (immutable)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub status: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// Delivery settings of member
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub delivery_settings: String,
    /// Email of member (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub email: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Role of member
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub role: String,
    /// Type of member (Immutable)
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
    /// Unique identifier of customer member (Read-only) Unique identifier of group (Read-only) Unique identifier of member (Read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
}

/// A user.
/// FROM: https://developers.google.com/admin-sdk/directory/v1/reference/users#resource
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct User {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub addresses: Vec<UserAddress>,
    /// Indicates if user has agreed to terms (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "agreedToTerms")]
    pub agreed_to_terms: Option<bool>,
    /// List of aliases (read-only)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub aliases: Vec<String>,
    /// Indicates if user is archived (read-only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub archived: Option<bool>,
    /// Boolean indicating if the user should change password in next login
    #[serde(default, rename = "changePasswordAtNextLogin")]
    pub change_password_at_next_login: bool,
    /// User's G Suite account creation time (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "creationTime")]
    pub creation_time: Option<DateTime<Utc>>,
    /// Custom fields of the user
    #[serde(default, skip_serializing_if = "HashMap::is_empty", rename = "customSchemas")]
    pub custom_schemas: HashMap<String, UserCustomProperties>,
    /// CustomerId of User (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customerId")]
    pub customer_id: String,
    /// User's G Suite account deletion time (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "deletionTime")]
    pub deletion_time: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub emails: Vec<UserEmail>,
    /// ETag of the resource (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// A list of external IDs for the user, such as an employee or network ID.
    /// The maximum allowed data size for this field is 2Kb.
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "externalIds")]
    pub external_ids: Vec<UserExternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gender: Option<UserGender>,
    /// Hash function name for password. Supported are MD5, SHA-1 and crypt
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "hashFunction")]
    pub hash_function: String,
    /// Unique identifier of User (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    /// The user's Instant Messenger (IM) accounts. A user account can have
    /// multiple ims properties. But, only one of these ims properties can be
    /// the primary IM contact. The maximum allowed data size for this field is
    /// 2Kb.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub ims: Vec<UserInstantMessenger>,
    /// Boolean indicating if user is included in Global Address List
    #[serde(default, rename = "includeInGlobalAddressList")]
    pub include_in_global_address_list: bool,
    /// Boolean indicating if ip is whitelisted
    #[serde(default, rename = "ipWhitelisted")]
    pub ip_whitelisted: bool,
    /// Boolean indicating if the user is admin (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isAdmin")]
    pub is_admin: Option<bool>,
    /// Boolean indicating if the user is delegated admin (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isDelegatedAdmin")]
    pub is_delegated_admin: Option<bool>,
    /// Is 2-step verification enforced (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isEnforcedIn2Sv")]
    pub is_enforced_in2_sv: Option<bool>,
    /// Is enrolled in 2-step verification (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isEnrolledIn2Sv")]
    pub is_enrolled_in2_sv: Option<bool>,
    /// Is mailbox setup (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "isMailboxSetup")]
    pub is_mailbox_setup: Option<bool>,
    /// The user's keywords. The maximum allowed data size for this field is 1Kb.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keywords: Vec<UserKeyword>,
    /// Kind of resource this is (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub languages: Vec<UserLanguage>,
    /// User's last login time (read-only)
    #[serde(skip_serializing_if = "Option::is_none", rename = "lastLoginTime")]
    pub last_login_time: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub locations: Vec<UserLocation>,
    /// User's name
    pub name: UserName,
    /// List of non editable aliases (read-only)
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "nonEditableAliases")]
    pub non_editable_aliases: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<UserNotes>,
    /// OrgUnit of User
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "orgUnitPath")]
    pub org_unit_path: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub organizations: Vec<Organization>,
    /// User's password
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub password: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub phones: Vec<UserPhone>,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "posixAccounts")]
    pub posix_accounts: Vec<UserPosixAccount>,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "primaryEmail")]
    pub primary_email: String,
    /// Recovery email of the user
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "recoveryEmail")]
    pub recovery_email: String,
    /// Recovery phone of the user
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "recoveryPhone")]
    pub recovery_phone: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub relations: Vec<UserRelation>,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "sshPublicKeys")]
    pub ssh_public_keys: Vec<UserSSHKey>,
    /// Indicates if user is suspended
    #[serde(default)]
    pub suspended: bool,
    /// Suspension reason if user is suspended (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "suspensionReason")]
    pub suspension_reason: String,
    /// ETag of the user's photo (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "thumbnailPhotoEtag")]
    pub thumbnail_photo_etag: String,
    /// Photo Url of the user (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "thumbnailPhotoUrl")]
    pub thumbnail_photo_url: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub websites: Vec<UserWebsite>,
}

/// A user's address.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserAddress {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub country: String,
    /// The country code. Uses the ISO 3166-1 standard.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "countryCode")]
    pub country_code: String,
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    /// For extended addresses, such as an address that includes a sub-region.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "extendedAddress")]
    pub extended_address: String,
    /// A full and unstructured postal address. This is not synced with the structured address fields.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub formatted: String,
    /// The town or city of the address.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub locality: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "poBox")]
    pub po_box: String,
    /// The ZIP or postal code, if applicable.
    #[serde(default, rename = "postalCode", skip_serializing_if = "String::is_empty")]
    pub postal_code: String,
    #[serde(default)]
    pub primary: bool,
    /// The abbreviated province or state.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub region: String,
    /// Indicates if the user-supplied address was formatted. Formatted addresses are not currently supported.
    #[serde(default, rename = "sourceIsStructured")]
    pub source_is_structured: bool,
    /// The street address, such as 1600 Amphitheatre Parkway.
    /// Whitespace within the string is ignored; however, newlines are significant.
    #[serde(default, rename = "street_address", skip_serializing_if = "String::is_empty")]
    pub street_address: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
}

/// A user's email.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserEmail {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub address: String,
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default)]
    pub primary: bool,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
}

/// A user's external id.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserExternalId {
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// A user's gender.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserGender {
    #[serde(default, rename = "addressMeAs", skip_serializing_if = "String::is_empty")]
    pub address_me_as: String,
    #[serde(default, rename = "customGender", skip_serializing_if = "String::is_empty")]
    pub custom_gender: String,
    #[serde(default, rename = "type", skip_serializing_if = "String::is_empty")]
    pub typev: String,
}

/// A user's instant messanger.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserInstantMessenger {
    /// If the protocol value is custom_protocol, this property holds the custom protocol's string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customProtocol")]
    pub custom_protocol: String,
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub im: String,
    #[serde(default)]
    pub primary: bool,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub protocol: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
}

/// A user's keyword.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserKeyword {
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// A user's language.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserLanguage {
    /// Other language. A user can provide their own language name if there is no corresponding
    /// Google III language code. If this is set, LanguageCode can't be set
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customLanguage")]
    pub custom_language: String,
    /// Language Code. Should be used for storing Google III LanguageCode string representation for language. Illegal values cause SchemaException.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "languageCode")]
    pub language_code: String,
}

/// A user's location.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserLocation {
    #[serde(default)]
    pub area: String,
    /// Unique ID for the building a resource is located in.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "buildingId")]
    pub building_id: String,
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    /// Most specific textual code of individual desk location.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "deskCode")]
    pub desk_code: String,
    /// Name of the floor a resource is located on.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "floorName")]
    pub floor_name: String,
    /// Floor section. More specific location within the floor. For example, if a floor is divided into sections "A", "B", and "C", this field would identify one of those values.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "floorSection")]
    pub floor_section: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
}

/// A user's name.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserName {
    /// Last name
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "familyName")]
    pub family_name: String,
    /// Full name
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "fullName")]
    pub full_name: String,
    /// First name
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "givenName")]
    pub given_name: String,
}

/// A user's notes.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserNotes {
    #[serde(default, rename = "contentType", skip_serializing_if = "String::is_empty")]
    pub content_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// An organization
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Organization {
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "costCenter")]
    pub cost_center: String,
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub department: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub domain: String,
    #[serde(default, rename = "fullTimeEquivalent")]
    pub full_time_equivalent: u64,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub location: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    #[serde(default)]
    pub primary: bool,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub symbol: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub title: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "type")]
    pub typev: String,
}

/// A user's phone.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserPhone {
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default)]
    pub primary: bool,
    #[serde(default, rename = "type", skip_serializing_if = "String::is_empty")]
    pub typev: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// A user's posix account.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserPosixAccount {
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "accountId")]
    pub account_id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub gecos: String,
    #[serde(default)]
    pub gid: isize,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "homeDirectory")]
    pub home_directory: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "operatingSystemType")]
    pub operating_system_type: String,
    #[serde(default)]
    pub primary: bool,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub shell: String,
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "systemId")]
    pub system_id: String,
    #[serde(default)]
    pub uid: isize,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub username: String,
}

/// A user's relation.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserRelation {
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, rename = "type", skip_serializing_if = "String::is_empty")]
    pub typev: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// A user's ssh key.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserSSHKey {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub key: String,
    /// An expiration time in microseconds since epoch.
    #[serde(default, skip_serializing_if = "Option::is_none", rename = "expirationTimeUsec")]
    pub expiration_time_usec: Option<i128>,
    /// A SHA-256 fingerprint of the SSH public key (read-only)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub fingerprint: String,
}

/// A user's website.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct UserWebsite {
    /// If the value of type is custom, this property contains the custom type string.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "customType")]
    pub custom_type: String,
    #[serde(default, alias = "is_group_admin")]
    pub primary: bool,
    #[serde(default, rename = "type", skip_serializing_if = "String::is_empty")]
    pub typev: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub value: String,
}

/// Custom properties for a user.
#[derive(Default, Clone, Debug, Deserialize)]
pub struct UserCustomProperties(pub Option<HashMap<String, Value>>);

impl Serialize for UserCustomProperties {
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let ucp = self.0.as_ref().unwrap();
        let mut map = s.serialize_map(Some(ucp.len())).unwrap();
        for (k, v) in ucp {
            if v.is_string() {
                map.serialize_entry(&k, v.as_str().unwrap()).unwrap();
            } else if v.is_array() {
                let val: Vec<HashMap<String, String>> = serde_json::from_str(&v.to_string()).unwrap();
                map.serialize_entry(&k, &val).unwrap();
            }
        }
        map.end()
    }
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Users {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of user objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub users: Vec<User>,
}

/// A calendar resource.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarResource {
    /// The type of the resource. For calendar resources, the value is admin#directory#resources#calendars#CalendarResource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// Capacity of a resource, number of seats in a room.
    pub capacity: Option<i32>,
    /// The type of the calendar resource, intended for non-room resources.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "resourceType")]
    pub typev: String,
    /// Description of the resource, visible only to admins.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "resourceDescription")]
    pub description: String,
    /// The read-only auto-generated name of the calendar resource which includes metadata about the resource such as building name, floor, capacity, etc. For example, "NYC-2-Training Room 1A (16)".
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "generatedResourceName")]
    pub generated_resource_name: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etags: String,
    /// The category of the calendar resource. Either CONFERENCE_ROOM or OTHER. Legacy data is set to CATEGORY_UNKNOWN.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "resourceCategory")]
    pub category: String,
    /// The read-only email for the calendar resource. Generated as part of creating a new calendar resource.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "resourceEmail")]
    pub email: String,
    /// The name of the calendar resource. For example, "Training Room 1A".
    #[serde(rename = "resourceName", skip_serializing_if = "String::is_empty", default)]
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "featureInstances")]
    pub feature_instances: Vec<CalendarFeatures>,
    /// Name of the section within a floor a resource is located in.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "floorSection")]
    pub floor_section: String,
    /// The unique ID for the calendar resource.
    #[serde(default, rename = "resourceId", skip_serializing_if = "String::is_empty")]
    pub id: String,
    /// Unique ID for the building a resource is located in.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "buildingId")]
    pub building_id: String,
    /// Name of the floor a resource is located on.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "floorName")]
    pub floor_name: String,
    /// Description of the resource, visible to users and admins.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "userVisibleDescription")]
    pub user_visible_description: String,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct CalendarResources {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of calendar resource objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<CalendarResource>,
}

/// A feature of a calendar.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarFeature {
    /// The continuation token, used to page through large result sets. Provide this value in a subsequent request to return the next page of results.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    /// Identifies this as a collection of CalendarFeatures. This is always admin#directory#resources#calendars#calendarFeaturesList.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etags: String,
}

/// A calendar's features.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct CalendarFeatures {
    pub feature: Option<CalendarFeature>,
}

/// A building.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Building {
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// The building name as seen by users in Calendar. Must be unique for the customer. For example, "NYC-CHEL". The maximum length is 100 characters.
    #[serde(rename = "buildingName", default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    /// The geographic coordinates of the center of the building, expressed as latitude and longitude in decimal degrees.
    pub coordinates: Option<BuildingCoordinates>,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etags: String,
    /// The postal address of the building. See PostalAddress for details. Note that only a single address line and region code are required.
    #[serde(default)]
    pub address: BuildingAddress,
    /// The display names for all floors in this building. The floors are expected to be sorted in ascending order, from lowest floor to highest floor. For example, ["B2", "B1", "L", "1", "2", "2M", "3", "PH"] Must contain at least one entry.
    #[serde(rename = "floorNames", default, skip_serializing_if = "Vec::is_empty")]
    pub floor_names: Vec<String>,
    /// Unique identifier for the building. The maximum length is 100 characters.
    #[serde(rename = "buildingId", default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    /// A brief description of the building. For example, "Chelsea Market".
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
struct Buildings {
    /// Token used to access next page of this result.
    #[serde(default, skip_serializing_if = "String::is_empty", rename = "nextPageToken")]
    pub next_page_token: String,
    /// Kind of resource this is.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub kind: String,
    /// ETag of the resource.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub etag: String,
    /// Event that triggered this response (only used in case of Push Response)
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub trigger_event: String,
    /// List of building objects.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub buildings: Vec<Building>,
}

/// A building's coordinates.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct BuildingCoordinates {
    /// Latitude in decimal degrees.
    #[serde(default)]
    pub latitude: f64,
    /// Longitude in decimal degrees.
    #[serde(default)]
    pub longitude: f64,
}

/// A building's address.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct BuildingAddress {
    /// Optional. BCP-47 language code of the contents of this address (if known).
    #[serde(rename = "languageCode", default, skip_serializing_if = "String::is_empty")]
    pub language_code: String,
    /// Optional. Highest administrative subdivision which is used for postal addresses of a country or region.
    #[serde(rename = "administrativeArea", default, skip_serializing_if = "String::is_empty")]
    pub administrative_area: String,
    /// Required. CLDR region code of the country/region of the address.
    #[serde(rename = "regionCode", default, skip_serializing_if = "String::is_empty")]
    pub region_code: String,
    /// Optional. Generally refers to the city/town portion of the address. Examples: US city, IT comune, UK post town. In regions of the world where localities are not well defined or do not fit into this structure well, leave locality empty and use addressLines.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub locality: String,
    /// Optional. Postal code of the address.
    #[serde(rename = "postalCode", default, skip_serializing_if = "String::is_empty")]
    pub postal_code: String,
    /// Optional. Sublocality of the address.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub sublocality: String,
    /// Unstructured address lines describing the lower levels of an address.
    #[serde(rename = "addressLines", default, skip_serializing_if = "Vec::is_empty")]
    pub address_lines: Vec<String>,
}