-
Notifications
You must be signed in to change notification settings - Fork 2
/
baidu.clj
43 lines (38 loc) · 1.79 KB
/
baidu.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
(ns waimai.baidu
(:require [clojure.data.json :as json]
digest
[org.httpkit.client :as httpc])
(:import [clojure.lang IPersistentMap]))
;(set! *warn-on-reflection* true)
(defn- ^{:tag IPersistentMap :static true} make-base-query-params
[^String source]
{"timestamp" (quot (System/currentTimeMillis) 1000)
"version" 3
"ticket" (clojure.string/upper-case (java.util.UUID/randomUUID))
"source" source
"encrypt" ""})
(defn- ^{:tag String :static true} make-sign
[^String source ^String secret form-params]
(let [joinstr (clojure.string/join
"&"
(sort (map #(str (first %) "=" (last %))
(select-keys (assoc form-params "secret" secret)
["timestamp" "version" "ticket" "source" "encrypt" "fields" "cmd" "body" "secret"]))))]
(-> joinstr digest/md5 clojure.string/upper-case )))
(defn ^{:static true} request
[^String cmd params & {:keys [^String source ^String secret ^String url]
:or {^String source (System/getProperty "waimai.baidu.source")
^String secret (System/getProperty "waimai.baidu.secret")
^String url (or (System/getProperty "waimai.baidu.url") "https://api.waimai.baidu.com") }
:as opts}]
(let [form-params (assoc (make-base-query-params source)
"cmd" cmd
"body" (json/write-str (or params {})))
payload (assoc form-params "sign" (make-sign source secret form-params))]
(httpc/request
(merge
{:method :post
:url url
:headers {"content-type" "application/x-www-form-urlencoded"}
:form-params payload }
(dissoc opts :source :secret)))))