Source file ezSendgrid.ml

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
(**************************************************************************)
(*                                                                        *)
(*                 Copyright 2018-2023 OCamlPro                           *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open EzSendgrid_types
open EzSendgrid_services

let sendgrid_url = "https://api.sendgrid.com/v3"
let headers key = ["Authorization", "Bearer " ^ key]

let send ?encoding ~api_key input =
  EzReq_lwt.post0
    ~headers:(headers api_key)
    ~input
    sendgrid_host
    (send encoding)

let send_one ?reply_to ~api_key ~dst ~from ~subject content =
  let person = {
    dst = [ {email = fst dst; name = snd dst} ];
    cc = None; bcc = None; psubject = None; data = None } in
  let reply_to = Option.map (fun (email, name) -> {email; name}) reply_to in
  let content =
    List.map
      (fun (content_type, content_value) ->
         {content_type; content_value} ) content in
  let mail = {
    person = [ person ];
    from = {email = fst from; name = snd from};
    subject = Some subject;
    content = Some content;
    template_id = None;
    reply_to;
    more_fields = None;
  } in
  send ~api_key mail

let send_template ?reply_to ~api_key ~dst ~from template_id data =
  let person = {
    dst = List.map (fun (email, name) -> {email; name}) dst;
    cc = None; bcc = None; psubject = None;
    data = Some (EzEncoding.destruct Json_encoding.any_value data) } in
  let reply_to = Option.map (fun (email, name) -> {email; name}) reply_to in
  let mail = {
    person = [ person ];
    from = {email = fst from; name = snd from};
    reply_to;
    subject = None;
    content = None;
    template_id = Some template_id;
    more_fields = None;
  } in
  send ~api_key mail

let add_contacts ~api_key ?list_ids contacts =
  EzReq_lwt.post0
    ~headers:(headers api_key)
    ~input:(list_ids, contacts)
    sendgrid_host
    add_contacts

let delete_contacts ~api_key ?(all=false) ids =
  let params =
    if all then [delete_all_param, EzAPI.S "true"]
    else [ids_param, EzAPI.S (String.concat "," ids)] in
  EzReq_lwt.post0
    ~headers:(headers api_key)
    ~input:()
    ~params
    sendgrid_host
    delete_contacts

let remove_contact_list ~api_key list_id contact_ids =
  let params = [contact_ids_param, EzAPI.S (String.concat "," contact_ids)] in
  EzReq_lwt.get1
    ~headers:(headers api_key)
    ~params
    sendgrid_host
    remove_contact_list
    list_id

let contacts_count ~api_key =
  EzReq_lwt.get0
    ~headers:(headers api_key)
    sendgrid_host
    contacts_count

let get_contact ~api_key id =
  EzReq_lwt.get1
    ~headers:(headers api_key)
    sendgrid_host
    get_contact
    id

let search_contacts ~api_key input =
  EzReq_lwt.post0
    ~headers:(headers api_key)
    ~input
    sendgrid_host
    search_contacts