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
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
open! Core_kernel
open Thing
module Sequencer = struct
module T = struct
type t = More_children [@@deriving hash, compare, sexp, bin_io]
end
include T
include Comparable.Make (T)
include Hashable.Make (T)
end
module Request = struct
module T = struct
type t =
| Get of { uri : Uri_sexp.t }
| Post_form of
{ uri : Uri_sexp.t
; params : (string * string list) list
}
[@@deriving sexp, compare]
end
include T
include Comparable.Make (T)
end
module Param_dsl = struct
type t = (string * string list) list
let required f key values : t = [ key, List.map values ~f ]
let required' f key value : t = [ key, [ f value ] ]
let optional f key values_opt : t =
Option.to_list values_opt |> List.bind ~f:(required f key)
;;
let optional' f key value_opt : t =
Option.to_list value_opt |> List.bind ~f:(required' f key)
;;
let include_optional f value_opt : t = Option.to_list value_opt |> List.bind ~f
let _ = optional
let combine = List.join
let bool = Bool.to_string
let string = Fn.id
let int = Int.to_string
let ( : Listing.Pagination.t) =
let key =
match pagination with
| Before _ -> "before"
| After _ -> "after"
in
let value =
match pagination with
| Before page_id | After page_id -> Listing.Page_id.to_string page_id
in
[ key, [ value ] ]
;;
let fullname_ = Thing.Fullname.to_string
let username_ = Username.to_string
let json = Json.value_to_string
let time = Time.to_string_iso8601_basic ~zone:Time.Zone.utc
end
module Parameters = struct
module Report_target = struct
let params_of_t t =
match t with
| `Modmail_conversation id ->
[ "modmail_conv_id", [ Modmail_conversation.Id.to_string id ] ]
| `Link _ | `Comment _ | `Message _ -> [ "thing_id", [ Param_dsl.fullname_ t ] ]
;;
end
module Flair_target = struct
type t =
| Link of Link.Id.t
| User of Username.t
let params_of_t t =
let open Param_dsl in
match t with
| Link link_id -> required' fullname_ "link" (`Link link_id)
| User username -> required' username_ "name" username
;;
end
module Color = struct
type t = string
let validate component value =
let lower = Maybe_bound.Incl 0x00 in
let upper = Maybe_bound.Incl 0xFF in
match Maybe_bound.interval_contains_exn value ~lower ~upper ~compare with
| true -> ()
| false ->
raise_s
[%message
"Color component out of range"
(component : string)
(value : int)
(lower : int Maybe_bound.t)
(upper : int Maybe_bound.t)]
;;
let create ~red ~green ~blue =
validate "red" red;
validate "green" green;
validate "blue" blue;
sprintf "#%02X%02X%02X" red green blue
;;
end
module Sticky_state = struct
type t =
| Sticky of { slot : int option }
| Unsticky
[@@deriving sexp]
let params_of_t t =
let state_field bool = "state", [ Bool.to_string bool ] in
match t with
| Unsticky -> [ state_field false ]
| Sticky { slot = None } -> [ state_field true ]
| Sticky { slot = Some slot } -> [ state_field true; "num", [ Int.to_string slot ] ]
;;
end
module Modmail_recipient = struct
type t =
| User of Username.t
| Internal
let params_of_t t =
match t with
| Internal -> []
| User user -> [ "to", [ Param_dsl.username_ user ] ]
;;
end
module Link_kind = struct
module Self_post_body = struct
type t =
| Markdown of string
| Richtext_json of Json.t
[@@deriving sexp]
end
type t =
| Link of { url : string }
| Self of Self_post_body.t
| Crosspost of Link.Id.t
[@@deriving sexp]
let params_of_t t =
match t with
| Link { url } -> [ "kind", [ "link" ]; "url", [ url ] ]
| Self body ->
[ "kind", [ "self" ]
; (match body with
| Markdown markdown -> "text", [ markdown ]
| Richtext_json json -> "richtext_json", [ Json.value_to_string json ])
]
| Crosspost link_id ->
[ "kind", [ "crosspost" ]
; "crosspost_fullname", [ Param_dsl.fullname_ (`Link link_id) ]
]
;;
end
module Vote_direction = struct
type t =
| Up
| Neutral
| Down
[@@deriving sexp]
let int_of_t t =
match t with
| Up -> 1
| Neutral -> 0
| Down -> -1
;;
let params_of_t t = [ "dir", [ int_of_t t |> Int.to_string ] ]
end
module Info_query = struct
type t =
| Id of
[ `Link of Link.Id.t | `Comment of Comment.Id.t | `Subreddit of Subreddit.Id.t ]
list
| Subreddit_name of Subreddit_name.t list
| Url of Uri_sexp.t
[@@deriving sexp]
let params_of_t t =
match t with
| Id fullnames -> [ "id", List.map fullnames ~f:Param_dsl.fullname_ ]
| Subreddit_name subreddits ->
[ "sr_name", List.map subreddits ~f:Subreddit_name.to_string ]
| Url uri -> [ "url", [ Uri.to_string uri ] ]
;;
end
module Duplicate_sort = struct
type t =
| New
let params_of_t t =
[ ( "sort"
, match t with
| Number_of_comments -> [ "number_of_comments" ]
| New -> [ "new" ] )
]
;;
end
module Historical_span = struct
module T = struct
type t =
| Hour
| Day
| Week
| Month
| Year
| All
[@@deriving sexp]
end
include T
include Sexpable.To_stringable (T)
let params_of_t t = [ "t", [ to_string t |> String.lowercase ] ]
end
module Mod_filter = struct
type t =
| Moderators of Username.t list
| Admin
let params_of_t t =
[ ( "mod"
, match t with
| Admin -> [ "a" ]
| Moderators moderators -> List.map moderators ~f:Username.to_string )
]
;;
end
module How_to_distinguish = struct
type t =
| Mod
| Admin
| Special
| Undistinguish
let params_of_t t =
[ ( "how"
, [ (match t with
| Mod -> "yes"
| Admin -> "admin"
| Special -> "special"
| Undistinguish -> "no")
] )
]
;;
end
module Search_sort = struct
type t =
| Relevance
| Hot
| Top
| New
let params_of_t t =
[ ( "sort"
, [ (match t with
| Relevance -> "relevance"
| Hot -> "hot"
| Top -> "top"
| New -> "new"
| Comments -> "comments")
] )
]
;;
end
module Search_type = struct
module T = struct
type t =
| Subreddit
| Link
| User
[@@deriving compare, sexp, enumerate]
end
include T
include Comparable.Make (T)
let to_string t =
match t with
| Subreddit -> "sr"
| Link -> "link"
| User -> "user"
;;
end
module Link_type = struct
type t =
| Any
| Link
| Self
let params_of_t t =
[ ( "link_type"
, [ (match t with
| Any -> "any"
| Link -> "link"
| Self -> "self")
] )
]
;;
end
module Spam_level = struct
type t =
| Low
| High
| All
let to_string t =
match t with
| Low -> "low"
| High -> "high"
| All -> "all"
;;
end
module Subreddit_type = struct
type t =
| Gold_restricted
| Archived
| Restricted
| Employees_only
| Gold_only
| Private
| User
| Public
let to_string t =
match t with
| Gold_restricted -> "gold_restricted"
| Archived -> "archived"
| Restricted -> "restricted"
| Employees_only -> "employees_only"
| Gold_only -> "gold_only"
| Private -> "private"
| User -> "user"
| Public -> "public"
;;
end
module Wiki_mode = struct
type t =
| Disabled
| Mod_only
| Anyone
let to_string t =
match t with
| Disabled -> "disabled"
| Mod_only -> "modonly"
| Anyone -> "anyone"
;;
end
module Subscription_action = struct
type t =
| Subscribe
| Unsubscribe
let to_string t =
match t with
| Subscribe -> "sub"
| Unsubscribe -> "unsub"
;;
end
module Subscription_list = struct
type t =
| By_id of Subreddit.Id.t list
| By_name of Subreddit_name.t list
let params_of_t t =
match t with
| By_id ids ->
[ "sr", List.map ids ~f:(fun id -> `Subreddit id |> Thing.Fullname.to_string) ]
| By_name names -> [ "sr_name", List.map names ~f:Subreddit_name.to_string ]
;;
end
module Image_file_extension = struct
type t =
| Png
| Jpg
end
module Subreddit_image = struct
type t =
| Stylesheet_image of { name : string }
| Mobile_icon
| Mobile_banner
end
module Relevance_or_activity = struct
type t =
| Relevance
| Activity
let to_string t =
match t with
| Relevance -> "relevance"
| Activity -> "activity"
;;
end
module Subreddit_relationship = struct
type t =
| Subscriber
| Contributor
| Moderator
| Stream_subscriber
let to_string t =
match t with
| Subscriber -> "subscriber"
| Contributor -> "contributor"
| Moderator -> "moderator"
| Stream_subscriber -> "streams"
;;
end
module Subreddit_listing_sort = struct
type t =
| Popular
| New
| Gold
| Default
let to_string t =
match t with
| Popular -> "popular"
| New -> "new"
| Gold -> "gold"
| Default -> "default"
;;
end
module User_subreddit_sort = struct
type t =
| Popular
| New
let to_string t =
match t with
| Popular -> "popular"
| New -> "new"
;;
end
module Relationship_spec = struct
module Duration = struct
type t =
| Permanent
| Days of int
[@@deriving sexp, compare, equal]
let params_of_t t =
[ ( "duration"
, match t with
| Permanent -> []
| Days days -> [ Int.to_string days ] )
]
;;
end
type t =
| Friend
| Moderator
| Moderator_invite
| Contributor
| Banned
| Muted
| Wiki_banned
| Wiki_contributor
[@@deriving sexp]
let to_string t =
match t with
| Friend -> "friend"
| Moderator -> "moderator"
| Moderator_invite -> "moderator_invite"
| Contributor -> "contributor"
| Banned -> "banned"
| Muted -> "muted"
| Wiki_banned -> "wikibanned"
| Wiki_contributor -> "wikicontributor"
;;
let params_of_t t = [ "type", [ to_string t ] ]
end
end
module Api_error = struct
type t =
| Cohttp_raised of Exn.t
| Reddit_reported_error of Cohttp.Response.t * Cohttp.Body.t
[@@deriving sexp_of]
end
type 'a t =
{ request : Request.t
; handle_response : Cohttp.Response.t * Cohttp.Body.t -> ('a, Api_error.t) Result.t
; sequencer : Sequencer.t option
}
let map t ~f =
{ t with handle_response = (fun v -> t.handle_response v |> Result.map ~f) }
;;
let call_api
?sequencer
handle_response
?(param_list_override = Fn.id)
()
~endpoint
~http_verb
~params
=
let params = ("raw_json", [ "1" ]) :: params in
let params = param_list_override params in
let uri = sprintf "https://oauth.reddit.com%s" endpoint |> Uri.of_string in
let request : Request.t =
match http_verb with
| `GET ->
let uri = Uri.add_query_params uri params in
Get { uri }
| `POST -> Post_form { uri; params }
in
{ request; handle_response; sequencer }
;;
let get = call_api ~http_verb:`GET
let post = call_api ~http_verb:`POST
let optional_subreddit_endpoint ?subreddit suffix =
let subreddit_part =
Option.value_map subreddit ~default:"" ~f:(sprintf !"/r/%{Subreddit_name}")
in
sprintf !"%s%s" subreddit_part suffix
;;
let simple_post_fullnames_as_id endpoint fullnames k =
let endpoint = sprintf "/api/%s" endpoint in
post ~endpoint ~params:Param_dsl.(required fullname_ "id" fullnames) k
;;
let simple_post_fullname_as_id endpoint fullname k =
simple_post_fullnames_as_id endpoint [ fullname ] k
;;
let api_type : Param_dsl.t = [ "api_type", [ "json" ] ]
open Parameters
open Result.Let_syntax
let result_of_response (response, body) =
match Cohttp.Response.status response with
| #Cohttp.Code.success_status -> Ok (response, body)
| _ -> Error (Api_error.Reddit_reported_error (response, body))
;;
let handle_json_response f response =
let%bind _response, body = result_of_response response in
let body_string = Cohttp.Body.to_string body in
Json.of_string body_string |> f |> Ok
;;
let assert_no_errors =
handle_json_response (fun json ->
match Json.find json [ "json"; "errors" ] |> Json.get_list ident with
| [] -> ()
| errors ->
raise_s
[%message
"Unexpected errors in body of non-error HTTP response" (errors : Json.t list)])
;;
let ignore_success_response response =
let%bind (_ : Cohttp.Response.t * Cohttp.Body.t) = result_of_response response in
return ()
;;
let ignore_empty_object =
handle_json_response (fun json ->
match json with
| `O [] -> ()
| _ -> raise_s [%message "Unexpected JSON response" (json : Json.t)])
;;
let json =
let thing = Thing.Poly.of_json json in
match thing with
| (`Link _ | `Comment _) as thing -> thing
| _ -> raise_s [%message "Expected link or comment" (thing : Thing.Poly.t)]
;;
let json =
let thing = Thing.Poly.of_json json in
match thing with
| (`Comment _ | `More_comments _) as thing -> thing
| _ -> raise_s [%message "Expected comment or more_comments" (thing : Thing.Poly.t)]
;;
let json =
let thing = Thing.Poly.of_json json in
match thing with
| (`Comment _ | `Message _) as thing -> thing
| _ -> raise_s [%message "Expected comment or message" (thing : Thing.Poly.t)]
;;
let get_listing child_of_json = handle_json_response (Listing.of_json child_of_json)
let get_link_listing = get_listing Link.of_json
let get_subreddit_listing = get_listing Subreddit.of_json
let me = get ~endpoint:"/api/v1/me" ~params:[] (handle_json_response User.of_json)
let karma =
get ~endpoint:"/api/v1/me/karma" ~params:[] (handle_json_response Karma_list.of_json)
;;
let trophies =
get
~endpoint:"/api/v1/me/trophies"
~params:[]
(handle_json_response (fun json ->
match json with
| `O
[ ("kind", `String "TrophyList")
; ("data", `O [ ("trophies", `A trophies) ])
] -> List.map trophies ~f:Award.of_json
| _ -> raise_s [%message "Unexpected \"TrophyList\" JSON" (json : Json.t)]))
;;
let with_listing_params k ? ?count ?limit ?show_all =
let listing_params =
let open Param_dsl in
combine
[ include_optional pagination_ pagination
; optional' int "count" count
; optional' int "limit" limit
; optional' (const "all") "show" show_all
]
in
k ~listing_params
;;
let prefs' which k ~listing_params =
let endpoint = sprintf "/prefs/%s" which in
get k ~endpoint ~params:listing_params
;;
let prefs endpoint k = with_listing_params (prefs' endpoint k)
let userlist json =
let of_array_item json =
Json.find json [ "data"; "children" ] |> Json.get_list User_list.Item.of_json
in
match json with
| `O _ -> of_array_item json
| `A l -> List.concat_map l ~f:of_array_item
| _ -> raise_s [%message "Unexpect User_list.t JSON" (json : Json.t)]
;;
let friends = prefs "friends" (handle_json_response userlist)
let blocked = prefs "blocked" (handle_json_response userlist)
let messaging = prefs "messaging" (handle_json_response userlist)
let trusted = prefs "trusted" (handle_json_response userlist)
let select_flair
?background_color
?css_class
?flair_template_id
?text
?text_color
~subreddit
~target
=
let endpoint = optional_subreddit_endpoint ~subreddit "/api/selectflair" in
let params =
let open Param_dsl in
combine
[ Flair_target.params_of_t target
; optional' string "background_color" background_color
; optional' string "css_class" css_class
; optional' Uuid.to_string "flair_template_id" flair_template_id
; optional' string "text" text
; optional' string "text_color" text_color
]
in
post ~endpoint ~params ignore_success_response
;;
let handle_things_response =
handle_json_response (fun json ->
Json.find json [ "json"; "data"; "things" ]
|> Json.get_list ident
|> List.hd_exn
|> Thing.Poly.of_json)
;;
let ?return_rtjson ?richtext_json ~parent ~text =
let endpoint = "/api/comment" in
let params =
let open Param_dsl in
combine
[ api_type
; required' fullname_ "thing_id" parent
; required' string "text" text
; optional' bool "return_rtjson" return_rtjson
; optional' json "richtext_json" richtext_json
]
in
post ~endpoint ~params (fun response ->
match%bind handle_things_response response with
| `Comment c -> Ok c
| response ->
raise_s [%message "Expected comment response" (response : Thing.Poly.t)])
;;
let delete ~id =
let endpoint = "/api/del" in
let params =
let open Param_dsl in
Param_dsl.combine [ required' fullname_ "id" id ]
in
post ~endpoint ~params ignore_empty_object
;;
let edit ?return_rtjson ?richtext_json ~id ~text =
let endpoint = "/api/editusertext" in
let params =
let open Param_dsl in
combine
[ api_type
; required' fullname_ "thing_id" id
; required' string "text" text
; optional' bool "return_rtjson" return_rtjson
; optional' json "richtext_json" richtext_json
]
in
post ~endpoint ~params (fun response ->
match%bind handle_things_response response with
| (`Link _ | `Comment _) as v -> Ok v
| response ->
raise_s [%message "Expected link or comment response" (response : Thing.Poly.t)])
;;
let simple_toggle verb fullnames k direction =
let verb =
match direction with
| `Do -> verb
| `Undo -> "un" ^ verb
in
simple_post_fullnames_as_id verb fullnames k
;;
let simple_toggle' verb fullname k direction = simple_toggle verb [ fullname ] k direction
let hide' ~links =
simple_toggle "hide" (List.map links ~f:(fun x -> `Link x)) ignore_empty_object
;;
let hide = hide' `Do
let unhide = hide' `Undo
let info ?subreddit query =
let endpoint = optional_subreddit_endpoint ?subreddit "/api/info" in
let params = Info_query.params_of_t query in
get ~endpoint ~params (fun response ->
let handle_json json =
let thing = Thing.Poly.of_json json in
match thing with
| (`Link _ | `Comment _ | `Subreddit _) as thing -> thing
| _ -> raise_s [%message "Unexpected kind in listing" (thing : Thing.Poly.t)]
in
get_listing handle_json response >>| Listing.children)
;;
let lock' ~id = simple_toggle' "lock" id ignore_empty_object
let lock = lock' `Do
let unlock = lock' `Undo
let mark_nsfw' ~link = simple_toggle' "marknsfw" (`Link link) ignore_empty_object
let mark_nsfw = mark_nsfw' `Do
let unmark_nsfw = mark_nsfw' `Undo
let more_children ?limit_children ~link ~ ~sort =
let endpoint = "/api/morechildren" in
let children = More_comments.Details.By_children.children more_comments in
let params =
let open Param_dsl in
combine
[ api_type
; required Comment.Id.to_string "children" children
; required' fullname_ "link_id" (`Link link)
; optional' bool "limit_children" limit_children
; required' Comment_sort.to_string "sort" sort
]
in
get
~endpoint
~params
(handle_json_response (fun json ->
Json.find json [ "json"; "data"; "things" ]
|> Json.get_list comment_or_more_of_json))
;;
let report
?from_modmail
?from_help_desk
?additional_info
?custom_text
?other_reason
?rule_reason
?site_reason
?sr_name
~target
~reason
=
let endpoint = "/api/report" in
let params =
let open Param_dsl in
combine
[ Report_target.params_of_t target
; api_type
; required' string "reason" reason
; optional' string "additional_info" additional_info
; optional' string "custom_text" custom_text
; optional' string "other_reason" other_reason
; optional' string "rule_reason" rule_reason
; optional' string "site_reason" site_reason
; optional' string "sr_name" sr_name
; optional' bool "from_modmail" from_modmail
; optional' bool "from_help_desk" from_help_desk
]
in
post ~endpoint ~params assert_no_errors
;;
let report_award ~award_id =
let endpoint = "/api/report_award" in
let params = [ "award_id", [ award_id ] ] in
post ~endpoint ~params return
;;
let save ?category ~id =
let endpoint = "/api/save" in
let params =
let open Param_dsl in
combine [ required' fullname_ "id" id; optional' string "category" category ]
in
post ~endpoint ~params ignore_empty_object
;;
let unsave ~id =
let endpoint = "/api/save" in
let params = [ "id", [ Param_dsl.fullname_ id ] ] in
post ~endpoint ~params ignore_empty_object
;;
let saved_categories = get ~endpoint:"/api/saved_categories" ~params:[] return
let send_replies ~id ~enabled =
let endpoint = "/api/sendreplies" in
let params =
let open Param_dsl in
combine [ required' fullname_ "id" id; required' bool "state" enabled ]
in
post ~endpoint ~params ignore_empty_object
;;
let set_contest_mode ~link ~enabled =
let endpoint = "/api/set_contest_mode" in
let params =
let open Param_dsl in
combine
[ api_type; required' fullname_ "id" (`Link link); required' bool "state" enabled ]
in
post ~endpoint ~params assert_no_errors
;;
let set_subreddit_sticky ?to_profile ~link ~sticky_state =
let endpoint = "/api/set_subreddit_sticky" in
let params =
let open Param_dsl in
combine
[ Sticky_state.params_of_t sticky_state
; api_type
; required' fullname_ "id" (`Link link)
; optional' bool "to_profile" to_profile
]
in
post ~endpoint ~params assert_no_errors
;;
let set_suggested_sort ~link ~sort =
let endpoint = "/api/set_suggested_sort" in
let params =
let open Param_dsl in
combine
[ api_type
; required' fullname_ "id" (`Link link)
; required'
string
"sort"
(Option.value_map sort ~f:Comment_sort.to_string ~default:"blank")
]
in
post ~endpoint ~params assert_no_errors
;;
let spoiler' ~link = simple_toggle' "spoiler" (`Link link) ignore_empty_object
let spoiler = spoiler' `Do
let unspoiler = spoiler' `Undo
let store_visits ~links =
let endpoint = "/api/store_visits" in
let params =
let open Param_dsl in
combine [ required fullname_ "links" (List.map links ~f:(fun x -> `Link x)) ]
in
post ~endpoint ~params return
;;
let submit
?ad
?nsfw
?resubmit
?sendreplies
?spoiler
?flair_id
?flair_text
?collection_id
?event_start
?event_end
?event_tz
~subreddit
~title
~kind
=
let endpoint = "/api/submit" in
let params =
let open Param_dsl in
combine
[ Link_kind.params_of_t kind
; api_type
; required' Subreddit_name.to_string "sr" subreddit
; required' string "title" title
; optional' bool "ad" ad
; optional' bool "nsfw" nsfw
; optional' bool "resubmit" resubmit
; optional' bool "sendreplies" sendreplies
; optional' bool "spoiler" spoiler
;
optional' string "flair_id" flair_id
; optional' string "flair_text" flair_text
; optional' string "collection_id" collection_id
; optional' time "event_start" event_start
; optional' time "event_end" event_end
; optional' string "event_tz" event_tz
]
in
post
~endpoint
~params
(handle_json_response (fun json ->
let json = Json.find json [ "json"; "data" ] in
let id = Json.find json [ "id" ] |> Json.get_string |> Link.Id.of_string in
let url = Json.find json [ "url" ] |> Json.get_string |> Uri.of_string in
id, url))
;;
let vote ?rank ~direction ~target =
let endpoint = "/api/vote" in
let params =
let open Param_dsl in
combine
[ Vote_direction.params_of_t direction
; required' fullname_ "id" target
; optional' int "rank" rank
]
in
post ~endpoint ~params ignore_empty_object
;;
let best' ~listing_params ?include_categories =
let endpoint = "/best" in
let params =
let open Param_dsl in
combine [ listing_params; optional' bool "include_categories" include_categories ]
in
get ~endpoint ~params get_link_listing
;;
let best = with_listing_params best'
let links_by_id ~links =
let endpoint =
List.map links ~f:(fun link -> Param_dsl.fullname_ (`Link link))
|> String.concat ~sep:","
|> sprintf "/by_id/%s"
in
get ~endpoint ~params:[] get_link_listing
;;
let
?subreddit
?
?context
?depth
?limit
?showedits
?showmore
?sort
?threaded
?truncate
~link
=
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf !"/comments/%{Link.Id}" link)
in
let params =
let open Param_dsl in
combine
[ optional' Comment.Id.to_string "comment" comment
; optional' int "context" context
; optional' int "depth" depth
; optional' int "limit" limit
; optional' bool "showedits" showedits
; optional' bool "showmore" showmore
; optional' Comment_sort.to_string "sort" sort
; optional' bool "threaded" threaded
; optional' int "truncate" truncate
]
in
get
~endpoint
~params
(handle_json_response (fun json ->
match Json.get_list ident json with
| [ link_json; ] ->
let link =
Listing.of_json Link.of_json link_json |> Listing.children |> List.hd_exn
in
let =
Listing.of_json comment_or_more_of_json comment_forest_json
|> Listing.children
in
{ Comment_response.link; comment_forest }
| json -> raise_s [%message "Expected two-item response" (json : Json.t list)]))
;;
let duplicates' ~listing_params ?crossposts_only ?sort ~link =
let endpoint = sprintf !"/duplicates/%{Link.Id}" link in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "crossposts_only" crossposts_only
; include_optional Duplicate_sort.params_of_t sort
]
in
get ~endpoint ~params get_link_listing
;;
let duplicates = with_listing_params duplicates'
let basic_post_listing'
endpoint_part
~listing_params
?include_categories
?subreddit
~
=
let endpoint = optional_subreddit_endpoint ?subreddit endpoint_part in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "include_categories" include_categories
; extra_params
]
in
get ~endpoint ~params get_link_listing
;;
let basic_post_listing endpoint =
with_listing_params (basic_post_listing' endpoint ~extra_params:[])
;;
let hot' ~listing_params ?location =
let =
let open Param_dsl in
optional' string "location" location
in
basic_post_listing' "/hot" ~extra_params ~listing_params
;;
let hot = with_listing_params hot'
let new_ = basic_post_listing "/new"
let rising = basic_post_listing "/rising"
let top' ~listing_params ?since =
let = Param_dsl.include_optional Historical_span.params_of_t since in
basic_post_listing' "/top" ~extra_params ~listing_params
;;
let top = with_listing_params top'
let controversial' ~listing_params ?since =
let = Param_dsl.include_optional Historical_span.params_of_t since in
basic_post_listing' "/controversial" ~extra_params ~listing_params
;;
let controversial = with_listing_params controversial'
let link_id_from_redirect (response, (_ : Cohttp.Body.t)) =
let () =
match Cohttp.Response.status response with
| `Found -> ()
| status ->
raise_s
[%message
"Unexpected HTTP response code"
(status : Cohttp.Code.status_code)
(response : Cohttp.Response.t)]
in
let uri =
Cohttp.Response.headers response |> Cohttp.Header.get_location |> Option.value_exn
in
let id =
match Uri.path uri |> String.split ~on:'/' with
| "" :: "r" :: _subreddit :: "comments" :: id :: _rest -> Link.Id.of_string id
| _ -> raise_s [%message "Unexpected Uri format" (uri : Uri_sexp.t)]
in
return id
;;
let random ?subreddit =
let endpoint = optional_subreddit_endpoint ?subreddit "/random" in
get ~endpoint ~params:[] link_id_from_redirect
;;
let block_author ~id = simple_post_fullname_as_id "block" id ignore_empty_object
let collapse_message' ~messages =
simple_toggle
"collapse_message"
(List.map messages ~f:(fun x -> `Message x))
ignore_empty_object
;;
let collapse_message = collapse_message' `Do
let uncollapse_message = collapse_message' `Undo
let compose_message ?g_recaptcha_response ?from_subreddit ~to_ ~subject ~text =
let endpoint = "/api/compose" in
let params =
let open Param_dsl in
combine
[ api_type
; optional' string "g-recaptcha-response" g_recaptcha_response
; optional' Subreddit_name.to_string "from_sr" from_subreddit
; required' username_ "to" to_
; required' string "subject" subject
; required' string "text" text
]
in
post ~endpoint ~params assert_no_errors
;;
let delete_message ~message =
simple_post_fullname_as_id "del_msg" (`Message message) return
;;
let read_message' ~messages =
simple_toggle
"read_message"
(List.map messages ~f:(fun x -> `Message x))
ignore_empty_object
;;
let read_message = read_message' `Do
let unread_message = read_message' `Undo
let message_listing' k endpoint ~listing_params ?include_categories ?mid ~mark_read =
let endpoint = "/message/" ^ endpoint in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "include_categories" include_categories
; optional' string "mid" mid
; required' bool "mark" mark_read
]
in
get ~endpoint ~params k
;;
let message_listing endpoint k = with_listing_params (message_listing' k endpoint)
let inbox =
message_listing
"inbox"
(handle_json_response (Listing.of_json comment_or_message_of_json))
;;
let unread =
message_listing
"unread"
(handle_json_response (Listing.of_json comment_or_message_of_json))
;;
let sent =
message_listing
"sent"
(handle_json_response (Listing.of_json Message.of_json))
~mark_read:true
;;
let =
message_listing "comments" (handle_json_response (Listing.of_json Comment.of_json))
;;
let ~listing_params ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/comments" subreddit in
let params = listing_params in
get ~endpoint ~params (handle_json_response (Listing.of_json Comment.of_json))
;;
let = with_listing_params subreddit_comments'
let moderation_endpoint ?(subreddit = Subreddit_name.of_string "mod") endpoint =
sprintf !"/r/%{Subreddit_name}/about/%s" subreddit endpoint
;;
let log' ~listing_params ?mod_filter ?subreddit ?type_ =
let endpoint = moderation_endpoint ?subreddit "log" in
let params =
let open Param_dsl in
combine
[ listing_params
; include_optional Mod_filter.params_of_t mod_filter
; optional' string "type" type_
]
in
get ~endpoint ~params (get_listing Mod_action.of_json)
;;
let log = with_listing_params log'
let mod_listing' ~listing_params ?location ?only ?subreddit ~endpoint =
let endpoint = moderation_endpoint ?subreddit endpoint in
let params =
let open Param_dsl in
combine
[ listing_params
; include_optional Links_or_comments.params_of_t only
; optional' string "location" location
]
in
get ~endpoint ~params (get_listing link_or_comment_of_json)
;;
let mod_listing = with_listing_params mod_listing'
let reports = mod_listing ~endpoint:"reports"
let spam = mod_listing ~endpoint:"spam"
let modqueue = mod_listing ~endpoint:"modqueue"
let unmoderated = mod_listing ~endpoint:"unmoderated"
let edited = mod_listing ~endpoint:"edited"
let accept_moderator_invite ~subreddit =
let endpoint = sprintf !"/%{Subreddit_name}/api/accept_moderator_invite" subreddit in
post ~endpoint ~params:api_type return
;;
let approve ~id = simple_post_fullname_as_id "approve" id ignore_empty_object
let remove ~id ~spam =
let endpoint = "/api/remove" in
let params =
let open Param_dsl in
combine [ required' fullname_ "id" id; required' bool "spam" spam ]
in
post ~endpoint ~params ignore_empty_object
;;
let distinguish ?sticky ~id ~how =
let endpoint = "/api/distinguish" in
let params =
let open Param_dsl in
combine
[ api_type
; How_to_distinguish.params_of_t how
; required' fullname_ "id" id
; optional' bool "sticky" sticky
]
in
post
~endpoint
~params
(handle_json_response (fun json ->
let thing =
Json.find json [ "json"; "data"; "things" ]
|> Json.get_list ident
|> List.hd_exn
|> Thing.Poly.of_json
in
match thing with
| (`Comment _ | `Link _) as thing -> thing
| _ -> raise_s [%message "Expected comment or link" (thing : Thing.Poly.t)]))
;;
let ignore_reports' ~id = simple_toggle' "ignore_reports" id ignore_empty_object
let ignore_reports = ignore_reports' `Do
let unignore_reports = ignore_reports' `Undo
let leavecontributor ~subreddit =
simple_post_fullname_as_id "leavecontributor" (`Subreddit subreddit) ignore_empty_object
;;
let leavemoderator ~subreddit =
simple_post_fullname_as_id "leavemoderator" (`Subreddit subreddit) ignore_empty_object
;;
let mute_message_author' ~message =
simple_toggle' "mute_message_author" (`Message message) ignore_empty_object
;;
let mute_message_author = mute_message_author' `Do
let unmute_message_author = mute_message_author' `Undo
let stylesheet ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/about/stylesheet" subreddit in
get ~endpoint ~params:[] (handle_json_response Stylesheet.of_json)
;;
let create_modmail_conversation ~subject ~body ~subreddit ~to_ ~hide_author =
let endpoint = "/api/mod/conversations" in
let params =
let open Param_dsl in
combine
[ required' string "subject" subject
; required' string "body" body
; Modmail_recipient.params_of_t to_
; required' Subreddit_name.to_string "srName" subreddit
; required' Bool.to_string "isAuthorHidden" hide_author
]
in
post ~endpoint ~params (handle_json_response Modmail.Conversation.of_json)
;;
let search'
~listing_params
?category
?include_facets
?restrict_to_subreddit
?since
?sort
?types
~query
=
let subreddit_part, restrict_param =
match restrict_to_subreddit with
| None -> "", []
| Some subreddit ->
sprintf !"/r/%{Subreddit_name}" subreddit, [ "restrict_sr", [ "true" ] ]
in
let endpoint = sprintf !"%s/search" subreddit_part in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' string "category" category
; optional' bool "include_facets" include_facets
; required' string "q" query
; include_optional Historical_span.params_of_t since
; include_optional Search_sort.params_of_t sort
; optional
Search_type.to_string
"type"
(Option.map types ~f:Search_type.Set.to_list)
; restrict_param
]
in
get
~endpoint
~params
(handle_json_response (fun json ->
let to_link_opt thing =
match thing with
| `Link link -> Some link
| _ -> None
in
let to_user_or_subreddit_opt thing =
match thing with
| (`User _ | `Subreddit _) as v -> Some v
| _ -> None
in
let listings =
let jsons =
match json with
| `O _ as json -> [ json ]
| `A listings -> listings
| _ -> raise_s [%message "Unexpected search response" (json : Json.t)]
in
List.map jsons ~f:(Listing.of_json Thing.Poly.of_json)
in
let find_kinded_listing error_message =
List.find_map listings ~f:(fun listing ->
match
Listing.children listing
|> List.hd
|> Option.bind ~f:extract_subkind
|> Option.is_some
with
| false -> None
| true ->
Some
(Listing.map listing ~f:(fun thing ->
match extract_subkind thing with
| Some v -> v
| None -> raise_s [%message error_message (json : Json.t)])))
in
let link_listing =
find_kinded_listing
to_link_opt
"Expected only links in search response listing"
in
let user_or_subreddit_listing =
find_kinded_listing
to_user_or_subreddit_opt
"Expected only users or subreddits in search response listing"
in
link_listing, user_or_subreddit_listing))
;;
let search = with_listing_params search'
let about_endpoint' endpoint k ~listing_params ?include_categories ?user ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/about/%s" subreddit endpoint in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "include_categories" include_categories
; optional' username_ "user" user
]
in
get ~endpoint ~params k
;;
let about_endpoint endpoint k = with_listing_params (about_endpoint' endpoint k)
let banned = about_endpoint "banned" (get_listing Relationship.Ban.of_json)
let muted = about_endpoint "muted" (get_listing Relationship.Mute.of_json)
let wiki_banned = about_endpoint "wikibanned" (get_listing Relationship.Ban.of_json)
let contributors =
about_endpoint "contributors" (get_listing Relationship.Contributor.of_json)
;;
let wiki_contributors =
about_endpoint "wikicontributors" (get_listing Relationship.Contributor.of_json)
;;
let moderators = about_endpoint "moderators" (get_listing Relationship.Moderator.of_json)
let removal_endpoints ~endpoint ~ ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/api/%s" subreddit endpoint in
post ~endpoint ~params:(Param_dsl.combine [ api_type; extra_params ]) assert_no_errors
;;
let delete_subreddit_image ~subreddit ~(image : Subreddit_image.t) =
let endpoint, =
match image with
| Header -> "delete_sr_header", []
| Mobile_icon -> "delete_sr_icon", []
| Mobile_banner -> "delete_sr_banner", []
| Stylesheet_image { name } ->
"delete_sr_img", Param_dsl.(required' string "img_name" name)
in
removal_endpoints ~endpoint ~extra_params ~subreddit
;;
let search_subreddits_by_name ?exact ?include_over_18 ?include_unadvertisable ~query =
let endpoint = "/api/search_reddit_names" in
let params =
let open Param_dsl in
combine
[ required' string "query" query
; optional' bool "exact" exact
; optional' bool "include_over_18" include_over_18
; optional' bool "include_unadvertisable" include_unadvertisable
]
in
get
~endpoint
~params
(handle_json_response (fun json ->
Json.find json [ "names" ]
|> Json.get_list (Fn.compose Subreddit_name.of_string Json.get_string)))
;;
let create_or_edit_subreddit
?
?wiki_edit_age
?wiki_edit_karma
~all_original_content
~allow_discovery
~allow_images
~allow_post_crossposts
~allow_top
~allow_videos
~api_type
~
~crowd_control_mode
~description
~disable_contributor_requests
~exclude_banned_modqueue
~free_form_reports
~g_recaptcha_response
~
~hide_ads
~key_color
~lang
~link_type
~name
~original_content_tag_enabled
~over_18
~public_description
~
~restrict_posting
~show_media
~show_media_preview
~
~spam_links
~spam_selfposts
~spoilers_enabled
~subreddit
~submit_link_label
~submit_text
~submit_text_label
~
~title
~type_
~wiki_mode
=
let endpoint = "/api/site_admin" in
let params =
let open Param_dsl in
combine
[ optional' int "comment_score_hide_mins" comment_score_hide_mins
; optional' int "wiki_edit_age" wiki_edit_age
; optional' int "wiki_edit_karma" wiki_edit_karma
; required' bool "all_original_content" all_original_content
; required' bool "allow_discovery" allow_discovery
; required' bool "allow_images" allow_images
; required' bool "allow_post_crossposts" allow_post_crossposts
; required' bool "allow_top" allow_top
; required' bool "allow_videos" allow_videos
; api_type
; required' bool "collapse_deleted_comments" collapse_deleted_comments
; required' bool "crowd_control_mode" crowd_control_mode
; required' string "description" description
; required' bool "disable_contributor_requests" disable_contributor_requests
; required' bool "exclude_banned_modqueue" exclude_banned_modqueue
; required' bool "free_form_reports" free_form_reports
; optional' string "g_recaptcha_response" g_recaptcha_response
; required' string "header_title" header_title
; required' bool "hide_ads" hide_ads
; required' string "key_color" key_color
; required' string "lang" lang
; Link_type.params_of_t link_type
; required' string "name" name
; required' bool "original_content_tag_enabled" original_content_tag_enabled
; required' bool "over_18" over_18
; required' string "public_description" public_description
; required' bool "restrict_commenting" restrict_commenting
; required' bool "restrict_posting" restrict_posting
; required' bool "show_media" show_media
; required' bool "show_media_preview" show_media_preview
; required' Spam_level.to_string "spam_comments" spam_comments
; required' Spam_level.to_string "spam_links" spam_links
; required' Spam_level.to_string "spam_selfposts" spam_selfposts
; required' bool "spoilers_enabled" spoilers_enabled
; required' Subreddit_name.to_string "sr" subreddit
; required' string "submit_link_label" submit_link_label
; required' string "submit_text" submit_text
; required' string "submit_text_label" submit_text_label
; required' Comment_sort.to_string "suggested_comment_sort" suggested_comment_sort
; required' string "title" title
; required' Subreddit_type.to_string "type_" type_
; required' Wiki_mode.to_string "wikimode" wiki_mode
]
in
post ~endpoint ~params return
;;
let submit_text ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/api/submit_text" subreddit in
get ~endpoint ~params:[] (handle_json_response Submit_text.of_json)
;;
let subreddit_autocomplete
?limit
?include_categories
?include_over_18
?include_profiles
~query
=
let endpoint = "/api/subreddit_autocomplete_v2" in
let params =
let open Param_dsl in
combine
[ optional' int "limit" limit
; optional' bool "include_categories" include_categories
; optional' bool "include_over_18" include_over_18
; optional' bool "include_profiles" include_profiles
; required' string "query" query
]
in
get ~endpoint ~params (get_listing Subreddit.of_json)
;;
let set_subreddit_stylesheet ?reason ~subreddit ~stylesheet_contents =
let endpoint = sprintf !"/r/%{Subreddit_name}/api/subreddit_stylesheet" subreddit in
let params =
let open Param_dsl in
combine
[ api_type
; optional' string "reason" reason
;
required' string "op" "save"
; required' string "stylesheet_contents" stylesheet_contents
]
in
post ~endpoint ~params assert_no_errors
;;
let subscribe ?skip_initial_defaults ~action ~subreddits =
let endpoint = "/api/subscribe" in
let params =
let open Param_dsl in
combine
[ required' Subscription_action.to_string "action" action
; Subscription_list.params_of_t subreddits
; optional' bool "skip_initial_defaults" skip_initial_defaults
]
in
post ~endpoint ~params ignore_empty_object
;;
let search_users' ~listing_params ?sort ~query =
let endpoint = "/users/search" in
let params =
let open Param_dsl in
combine
[ listing_params
; required' string "q" query
; optional' Relevance_or_activity.to_string "sort" sort
]
in
get ~endpoint ~params (get_listing User.of_json)
;;
let search_users = with_listing_params search_users'
let about_subreddit ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/about" subreddit in
get ~endpoint ~params:[] (handle_json_response Subreddit.of_json)
;;
let subreddit_about ?(params = []) ~subreddit endpoint =
let endpoint = sprintf !"/r/%{Subreddit_name}/about/%s" subreddit endpoint in
get ~endpoint ~params
;;
let subreddit_settings ?created ?location =
let params =
let open Param_dsl in
combine [ optional' bool "created" created; optional' string "location" location ]
in
subreddit_about ~params "edit" (handle_json_response Subreddit_settings.of_json)
;;
let subreddit_rules =
subreddit_about "rules" (handle_json_response Subreddit_rules.of_json)
;;
let subreddit_traffic =
subreddit_about "traffic" (handle_json_response Subreddit_traffic.of_json)
;;
let get_sticky ?number ~subreddit =
let endpoint = sprintf !"/r/%{Subreddit_name}/about/sticky" subreddit in
let params =
let open Param_dsl in
combine [ optional' int "num" number ]
in
get ~endpoint ~params link_id_from_redirect
;;
let get_subreddits' ~listing_params ?include_categories ~relationship =
let endpoint = sprintf !"/subreddits/mine/%{Subreddit_relationship}" relationship in
let params =
let open Param_dsl in
combine [ listing_params; optional' bool "include_categories" include_categories ]
in
get ~endpoint ~params get_subreddit_listing
;;
let get_subreddits = with_listing_params get_subreddits'
let search_subreddits_by_title_and_description' ~listing_params ?show_users ?sort ~query =
let endpoint = "/subreddits/search" in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "show_users" show_users
; required' string "q" query
; optional' Relevance_or_activity.to_string "sort" sort
]
in
get ~endpoint ~params get_subreddit_listing
;;
let search_subreddits_by_title_and_description =
with_listing_params search_subreddits_by_title_and_description'
;;
let list_subreddits' ~listing_params ?include_categories ?show_users ~sort =
let endpoint = sprintf !"/subreddits/%{Subreddit_listing_sort}" sort in
let params =
let open Param_dsl in
combine
[ listing_params
; optional' bool "include_categories" include_categories
; optional' bool "show_users" show_users
]
in
get ~endpoint ~params get_subreddit_listing
;;
let about_user ~username =
let endpoint = sprintf !"/user/%{Username}/about" username in
get ~endpoint ~params:[] (handle_json_response User.of_json)
;;
let list_subreddits = with_listing_params list_subreddits'
let list_user_subreddits' ~listing_params ?include_categories ~sort =
let endpoint = sprintf !"/users/%{User_subreddit_sort}" sort in
let params =
let open Param_dsl in
combine [ listing_params; optional' bool "include_categories" include_categories ]
in
get ~endpoint ~params get_subreddit_listing
;;
let list_user_subreddits = with_listing_params list_user_subreddits'
let add_relationship
~relationship
~username
~duration
?subreddit
?note
?ban_reason
?ban_message
?ban_context
=
let endpoint = optional_subreddit_endpoint ?subreddit "/api/friend" in
let params =
Relationship_spec.Duration.params_of_t duration
@ Relationship_spec.params_of_t relationship
@
let open Param_dsl in
combine
[ Relationship_spec.Duration.params_of_t duration
; Relationship_spec.params_of_t relationship
; api_type
; required' username_ "name" username
; optional' string "note" note
; optional' string "ban_reason" ban_reason
; optional' string "ban_message" ban_message
; optional' string "ban_context" ban_context
]
in
post ~endpoint ~params ignore_success_response
;;
let remove_relationship ~relationship ~username ?subreddit =
let endpoint = optional_subreddit_endpoint ?subreddit "/api/unfriend" in
let params =
let open Param_dsl in
combine
[ Relationship_spec.params_of_t relationship
; api_type
; required' username_ "name" username
]
in
post ~endpoint ~params ignore_success_response
;;
let add_or_remove_wiki_editor ~act ~page:({ subreddit; page } : Wiki_page.Id.t) ~user =
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf "/api/wiki/alloweditor/%s" act)
in
let params =
let open Param_dsl in
combine [ required' string "page" page; required' username_ "username" user ]
in
post ~endpoint ~params ignore_empty_object
;;
let add_wiki_editor = add_or_remove_wiki_editor ~act:"add"
let remove_wiki_editor = add_or_remove_wiki_editor ~act:"del"
let edit_wiki_page ?previous ?reason ~content ~page:({ subreddit; page } : Wiki_page.Id.t)
=
let endpoint = optional_subreddit_endpoint ?subreddit "/api/wiki/edit" in
let params =
let open Param_dsl in
combine
[ required' string "content" content
; required' string "page" page
; optional' Wiki_page.Revision.Id.to_string "previous" previous
; optional' string "reason" reason
]
in
post ~endpoint ~params (fun (response, body) ->
let%bind json = Cohttp.Body.to_string body |> Ok >>| Json.of_string in
match Cohttp.Response.status response, json with
| #Cohttp.Code.success_status, `O [] -> return (Ok ())
| `Conflict, json -> return (Error (Wiki_page.Edit_conflict.of_json json))
| _, _ -> Error (Api_error.Reddit_reported_error (response, body)))
;;
let toggle_wiki_revision_visibility ~page:({ subreddit; page } : Wiki_page.Id.t) ~revision
=
let endpoint = optional_subreddit_endpoint ?subreddit "/api/wiki/hide" in
let params =
let open Param_dsl in
combine
[ required' string "page" page
; required' Wiki_page.Revision.Id.to_string "revision" revision
]
in
post
~endpoint
~params
(handle_json_response (function
| `O [ ("status", `Bool true) ] -> `Became_hidden
| `O [ ("status", `Bool false) ] -> `Became_visible
| json ->
raise_s
[%message
"Unexpected toggle_wiki_revision_visibility response" (json : Json.t)]))
;;
let revert_wiki_page ~page:({ subreddit; page } : Wiki_page.Id.t) ~revision =
let endpoint = optional_subreddit_endpoint ?subreddit "/api/wiki/revert" in
let params =
let open Param_dsl in
combine
[ required' string "page" page
; required' Wiki_page.Revision.Id.to_string "revision" revision
]
in
post ~endpoint ~params ignore_empty_object
;;
let wiki_discussions' ~listing_params ~page:({ subreddit; page } : Wiki_page.Id.t) =
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf "/wiki/discussions/%s" page)
in
get ~endpoint ~params:listing_params get_link_listing
;;
let wiki_discussions = with_listing_params wiki_discussions'
let wiki_pages ?subreddit =
let endpoint = optional_subreddit_endpoint ?subreddit "/wiki/pages" in
get
~endpoint
~params:[]
(handle_json_response (fun json ->
Json.find json [ "data" ] |> Json.get_list Json.get_string))
;;
let subreddit_wiki_revisions' ~listing_params ?subreddit =
let endpoint = optional_subreddit_endpoint ?subreddit "/wiki/revisions" in
get ~endpoint ~params:listing_params (get_listing Wiki_page.Revision.of_json)
;;
let subreddit_wiki_revisions = with_listing_params subreddit_wiki_revisions'
let wiki_page_revisions' ~listing_params ~page:({ subreddit; page } : Wiki_page.Id.t) =
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf "/wiki/revisions/%s" page)
in
get ~endpoint ~params:listing_params (get_listing Wiki_page.Revision.of_json)
;;
let wiki_page_revisions = with_listing_params wiki_page_revisions'
let wiki_permissions ~page:({ subreddit; page } : Wiki_page.Id.t) =
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf "/wiki/settings/%s" page)
in
get ~endpoint ~params:[] (handle_json_response Wiki_page.Permissions.of_json)
;;
let set_wiki_permissions ~page:({ subreddit; page } : Wiki_page.Id.t) ~listed ~level =
let endpoint =
optional_subreddit_endpoint ?subreddit (sprintf "/wiki/settings/%s" page)
in
let params =
let open Param_dsl in
combine
[ required' bool "listed" listed
; required' string "page" page
; required' (Fn.compose int Wiki_page.Permissions.Level.to_int) "permlevel" level
]
in
post ~endpoint ~params (handle_json_response Wiki_page.Permissions.of_json)
;;
let wiki_page ?compare_revisions ~page:({ subreddit; page } : Wiki_page.Id.t) =
let endpoint = optional_subreddit_endpoint ?subreddit (sprintf "/wiki/%s" page) in
let v1, v2 = Option.value compare_revisions ~default:(None, None) in
let params =
let open Param_dsl in
combine
[ required' string "page" page; optional' string "v" v1; optional' string "v2" v2 ]
in
get ~endpoint ~params (handle_json_response Wiki_page.of_json)
;;