-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.clj
49 lines (44 loc) · 1.77 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
(ns pradpi.core
(:require [pradpi.url :as url]
[pradpi.hmac :as hmac]
[pradpi.group :as group]
[naive-xml-reader.core :refer [read-xml]]
[org.httpkit.client :as http]))
(def default-config
{:host "webservices.amazon.com"
:path "/onca/xml"
:version "2013-08-01"})
(defn- create-params
"Creates params for product advertising api"
[operation config params]
{:pre [(every? config #{:associate-tag :key-id})
(map? params)]}
(let [merged-config (merge default-config config)
version (:version merged-config)]
(-> (assoc params "Service" "AWSECommerceService")
(assoc "Operation" operation)
(assoc "AssociateTag" (:associate-tag config))
(assoc "AWSAccessKeyId" (:key-id config))
(assoc "Version" version))))
(defn- handle-response
"Unpack a an xml response into a hash map"
[resp params]
(future (let [{body :body} @resp]
(read-xml body (group/xml-config params)))))
(defn request
"Perform a request against the Amazon Product Advertising API"
([operation protocol config params]
{:pre [(not (nil? (:secret config)))]}
(let [merged-config (merge default-config config)
host (:host merged-config)
path (:path merged-config)
query (create-params operation merged-config params)
uri (url/create (str protocol "://" host path) query)
signed (hmac/signed uri (:secret config))
response (http/get signed merged-config)]
(handle-response response params)))
([operation config params] (request "http" operation config params)))
(defn item-lookup
"Perform an ItemLookup operation"
([protocol config params] (request "ItemLookup" protocol config params))
([config params] (item-lookup "http" config params)))