-
Notifications
You must be signed in to change notification settings - Fork 44
/
core.clj
302 lines (259 loc) · 10 KB
/
core.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
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
(ns salesforce.core
(:require
[clojure.string :as str]
[cheshire.core :as json]
[clj-http.client :as http]
[clojure.string :as str]
[clojure.test :refer [is]])
(:import [java.time LocalDate Instant ZonedDateTime]))
(def ^:dynamic +token+ nil)
(defmacro with-token
[token & forms]
`(binding [+token+ ~token]
(do ~@forms)))
(defn conf [f]
(ref (binding [*read-eval* false]
(with-open [r (clojure.java.io/reader f)]
(read (java.io.PushbackReader. r))))))
(defn auth!
"Get security token and auth info from Salesforce
config is a map in the form
- client-id ID
- client-secret SECRET
- username USERNAME
- password PASSWORD
- security-token TOKEN
- login-host HOSTNAME (default login.salesforce.com"
[{:keys [client-id client-secret username password security-token login-host]}]
(let [hostname (or login-host "login.salesforce.com")
auth-url (format "https://%s/services/oauth2/token" hostname)
params {:grant_type "password"
:client_id client-id
:client_secret client-secret
:username username
:password (str password security-token)
:format "json"}
resp (http/post auth-url {:form-params params})]
(-> (:body resp)
(json/decode true))))
(def ^:private limit-info (atom {}))
(defn- parse-limit-info [v]
(let [[used available]
(->> (-> (str/split v #"=")
(second)
(str/split #"/"))
(map #(Integer/parseInt %)))]
{:used used
:available available}))
(defn read-limit-info
"Deref the value of the `limit-info` atom which is
updated with each call to the API. Returns a map,
containing the used and available API call counts:
{:used 11 :available 15000}"
[]
@limit-info)
(defn request
"Make a HTTP request to the Salesforce.com REST API
Token is the full map returned from (auth! @conf)"
[method url token & [params]]
(let [base-url (:instance_url token)
full-url (str base-url url)
resp (try (http/request
(merge (or params {})
{:method method
:url full-url
:headers {"Authorization" (str "Bearer " (:access_token token))}}))
(catch Exception e (:body (ex-data e))))]
(-> (get-in resp [:headers "sforce-limit-info"]) ;; Record limit info in atom
(parse-limit-info)
((partial reset! limit-info)))
(-> resp
:body
(json/decode true))))
(defn- safe-request
"Perform a request but catch any exceptions"
[method url token & params]
(try
(with-meta
(request method url token params)
{:method method :url url :token token})
(catch Exception e (.toString e))))
;; Salesforce API version information
(defn all-versions
"Lists all available versions of the Salesforce REST API"
[]
(->> (http/get "http://na1.salesforce.com/services/data/")
:body
(json/parse-string)))
(defn latest-version
"What is the latest API version?"
[]
(->> (last (all-versions))
(map (fn [[k _]] [(keyword k) _]))
(into {})
:version))
(defonce ^:dynamic +version+ (atom "39.0"))
(defn set-version! [v]
(reset! +version+ v))
(def latest-version*
"Memoized latest-version, used by (with-latest-version) macro"
(memoize latest-version))
(defmacro with-latest-version [& forms]
`(binding [+version+ (atom (latest-version*))]
(do ~@forms)))
(defmacro with-version [v & forms]
`(binding [+version+ (atom ~v)] (do ~@forms)))
;; Resources
(defn resources [token]
(request :get (format "/services/data/v%s/" @+version+) token))
(defn so->objects
"Lists all of the available sobjects"
[token]
(request :get (format "/services/data/v%s/sobjects" @+version+) token))
(defn so->all
"All sobjects i.e (so->all \"Account\" auth-info)"
[sobject token]
(request :get (format "/services/data/v%s/sobjects/%s" @+version+ sobject) token))
(defn so->recent
"The recently created items under an sobject identifier
e.g (so->recent \"Account\" auth-info)"
[sobject token]
(:recentItems (so->all sobject token)))
(defn so->get
"Fetch a single SObject or passing in a vector of attributes
return a subset of the data"
([sobject identifier fields token]
(when (or (seq? fields) (vector? fields))
(let [params (->> (into [] (interpose "," fields))
(str/join)
(conj ["?fields="])
(apply str))
uri (format "/services/data/v%s/sobjects/%s/%s%s"
@+version+ sobject identifier params)
response (request :get uri token)]
(dissoc response :attributes))))
([sobject identifier token]
(request :get
(format "/services/data/v%s/sobjects/%s/%s" @+version+ sobject identifier) token)))
(comment
;; Fetch all the info
(so->get "Account" "001i0000007nAs3" auth)
;; Fetch only the name and website attribute
(so->get "Account" "001i0000007nAs3" ["Name" "Website"] auth))
(defn so->describe
"Describe an SObject"
[sobject token]
(request :get
(format "/services/data/v%s/sobjects/%s/describe" @+version+ sobject) token))
(comment
(so->describe "Account" auth))
(defn so->create
"Create a new record"
[sobject record token]
(let [params
{:form-params record
:content-type :json}]
(request :post
(format "/services/data/v%s/sobjects/%s/" @+version+ sobject) token params)))
(comment
(so->create "Account" {:Name "My new account"} auth))
(defn so->update
"Update a record
- sojbect the name of the object i.e Account
- identifier the object id
- record map of data to update object with
- token your api auth info"
[sobject identifier record token]
(let [params
{:body (json/generate-string record)
:content-type :json}]
(request :patch
(format "/services/data/v%s/sobjects/%s/%s" @+version+ sobject identifier)
token params)))
(defn so->delete
"Delete a record
- sojbect the name of the object i.e Account
- identifier the object id
- token your api auth info"
[sobject identifier token]
(request :delete
(format "/services/data/v%s/sobjects/%s/%s" @+version+ sobject identifier)
token))
(comment
(so->delete "Account" "001i0000008Ge2OAAS" auth))
(defn so->flow
"Invoke a flow (see: https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_distribute_system_rest.htm)
- indentifier of flow (e.g. \"Escalate_to_Case\")
- inputs map (e.g. {:inputs [{\"CommentCount\" 6
\"FeedItemId\" \"0D5D0000000cfMY\"}]})
- token to your api auth info"
[identifier token & [data]]
(let [params {:body (json/generate-string (or data {:inputs []}))
:content-type :json}]
(request :post
(format "/services/data/v%s/actions/custom/flow/%s" @+version+ identifier)
token params)))
(comment
(so->flow "Escalate_to_Case" a {:inputs [{"CommentCount" 6
"FeedItemId" "0D5D0000000cfMY"}]}))
;; Salesforce Object Query Language
;; ------------------------------------------------------------------------------------
(defn gen-query-url
"Given an SOQL string, i.e \"SELECT name from Account\"
generate a Salesforce SOQL query url in the form:
/services/data/v20.0/query/?q=SELECT+name+from+Account"
([version query] (gen-query-url version "query" query))
([version type query]
(let [url (format "/services/data/v%s/%s" version type)
soql (java.net.URLEncoder/encode query "UTF-8")]
(str url "?q=" soql))))
(defprotocol SOQLable
(->soql [value] "Serialize the value to a format that Salesforce expect"))
(extend-protocol SOQLable
String (->soql [v] (str \' v \'))
clojure.lang.Ratio (->soql [v] (-> v double str))
Number (->soql [v] (str v))
Boolean (->soql [v] (str \' v \'))
nil (->soql [v] "'null'")
LocalDate (->soql [v] (str \' v \'))
Instant (->soql [v] (str \' v \'))
ZonedDateTime (->soql [v] (-> v .toInstant (#(str \' % \'))))
clojure.lang.APersistentSet (->soql [v]
(->> v
(map ->soql)
(interpose \,)
(apply str)
(#(str \( % \)))))
clojure.lang.Sequential (->soql [v]
(->> v
(map ->soql)
(interpose \,)
(apply str)
(#(str \( % \))))))
(comment (->soql [1 1/3 2.0 true false nil (LocalDate/now) (Instant/now) (ZonedDateTime/now) "Ter\n"]))
(comment (->soql (set [1 1/3 2.0 true false nil (LocalDate/now) (Instant/now) (ZonedDateTime/now) "Ter\n"])))
(defn sqlvec->query
"Receives a Sql vector in the clojure java.jdbc format, for example: [\"select * from fruit where appearance = ?\" \"rosy\"].
Return a String with all the '?' set and serialized to the expected format from Salesforce using the SOQLable protocol.
Suitable to use with HugSQL sqlvec functions from hugsql.core/def-sqlvec-fns
"
[[query & args]]
{:pre [(-> query string? is) (-> query empty? not is)]}
(let [queryf (str/replace query #"\?" "%s")
format-query (partial format queryf)]
(apply format-query (map ->soql args))))
(comment (sqlvec->query ["select * from fruit where name = ? and price >= ?" "apple" 9/5]))
(defprotocol Soql
(soql [query token] "Executes an arbitrary SOQL query"))
(extend-protocol Soql
String (soql [query token]
(request :get (gen-query-url @+version+ query) token))
clojure.lang.Sequential (soql [sqlvec token]
(soql (sqlvec->query sqlvec) token)))
(defn sosl
"Executes an arbitrary SOSL query
i.e FIND {Joe Smith} IN Name Fields RETURNING lead"
[query token]
(request :get (gen-query-url @+version+ "search" query) token))
(comment
(sosl "FIND {Joe Smith} IN Name Fields RETURNING lead" auth))