-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.clj
213 lines (149 loc) · 5.58 KB
/
contact.clj
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
(ns hubspot.contact
(:require [clojure.spec.alpha :as s]
[hubspot.http :as h]
[hubspot.spec :as hs]
[toolbelt.core :as tb]))
;; ==============================================================================
;; spec =========================================================================
;; ==============================================================================
(s/def ::property
(s/or :property string? :properties (s/+ string?)))
(s/def ::properties
map?)
(s/def ::vid
integer?)
(s/def ::profile-url
string?)
(s/def ::identity-profiles
(s/* map?)) ; TODO
(s/def ::form-submissions
(s/* map?)) ; TODO
(s/def ::contact
(s/keys :req-un [::properties ::vid ::profile-url]
:opt-un [::identity-profiles ::form-submissions]))
;; create =======================================================================
(s/def ::email
hs/email?)
(s/def ::create-params
(s/keys :opt-un [::email]))
;; fetch ========================================================================
(s/def ::propertyMode
#{"value_only" "value_and_history"})
(s/def ::formSubmissionMode
#{"all" "none" "newest" "oldest"})
(s/def ::showListMemberships
boolean?)
(s/def ::fetch-id
(s/or :vid ::vid :email hs/email?))
(s/def ::fetch-params
(s/keys :opt-un [::propertyMode ::formSubmissionMode ::showListMemberships]))
;; search =======================================================================
(s/def ::query
string?)
(s/def ::count
(s/and pos-int? #(<= % 100)))
(s/def ::offset
pos-int?)
(s/def ::search-params
(s/keys :opt-un [::count ::offset ::property]))
(s/def ::has-more
boolean?)
(s/def ::total
integer?)
(s/def ::contacts
(s/+ ::contact))
(s/def ::search-res
(s/keys :req-un [::query ::offset ::has-more ::total ::contacts]))
;; ==============================================================================
;; HTTP API =====================================================================
;; ==============================================================================
(defn- params->properties
[params]
(reduce (fn [acc [k v]] (conj acc {:property k :value v})) [] params))
(defn create!
"Create a new contact."
([email]
(create! email {} {}))
([email params]
(create! email params {}))
([email params opts]
(letfn [(-parse-params [params]
(-> (params->properties params)
(tb/conj-when (when-some [e email] {:property "email" :value e}))))]
(h/post-req "contacts/v1/contact"
(assoc-in opts [:params :properties] (-parse-params params))))))
(s/fdef create!
:args (s/alt :unary (s/cat :email (s/nilable ::email))
:binary (s/cat :email (s/nilable ::email)
:params ::create-params)
:ternary (s/cat :email (s/nilable ::email)
:params ::create-params
:opts h/request-options?))
:ret (hs/async ::contact))
(defn fetch
"Fetch a contact by email or vid."
([id]
(fetch id {} {}))
([id params]
(fetch id params {}))
([id params opts]
(if-not (s/valid? ::fetch-id id)
(throw (ex-info "Invalid id!" {:id id}))
(let [[id-name id-val] (s/conform ::fetch-id id)]
(h/get-req (format "contacts/v1/contact/%s/%s/profile" (name id-name) id-val)
(assoc opts :params params))))))
(s/fdef fetch
:args (s/alt :unary (s/cat :id ::fetch-id)
:binary (s/cat :id ::fetch-id
:params ::fetch-params)
:ternary (s/cat :id ::fetch-id
:params ::fetch-params
:opts h/request-options?))
:ret (hs/async ::contact))
(defn search
"Search contacts given a `query` string. Can optionally provide count, offset
and property.
https://developers.hubspot.com/docs/methods/contacts/search_contacts"
([query]
(search query {} {}))
([query params]
(search query params {}))
([query params opts]
(h/get-req "/contacts/v1/search/query"
(assoc opts :params (assoc params :q query)))))
(s/fdef search
:args (s/alt :unary (s/cat :query ::query)
:binary (s/cat :query ::query
:params ::search-params)
:ternary (s/cat :query ::query
:params ::search-params
:opts h/request-options?))
:ret (hs/async ::search-res))
(defn update!
([id params]
(update! id params {}))
([id params opts]
(h/post-req (format "contacts/v1/contact/vid/%s/profile" id)
(assoc-in opts [:params :properties] (params->properties params)))))
(s/fdef update!
:args (s/cat :id ::fetch-id
:params map?
:opts (s/? h/request-options?))
:ret (hs/async nil?))
(defn update-by-email!
([email params]
(update-by-email! email params {}))
([email params opts]
(h/post-req (format "contacts/v1/contact/email/%s/profile" email)
(assoc-in opts [:params :properties] (params->properties params)))))
(comment
(def api-key "")
(require '[clojure.spec.test.alpha :as stest])
(stest/instrument)
(create! "test@clojure.com" {:api-key api-key})
(h/with-api-key api-key
(fetch "test2@test.com"))
(h/with-api-key api-key
(update! "test@test.com" {:closedate (.getTime (java.util.Date.))}))
(.getTime (java.util.Date.))
)