-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.clj
46 lines (42 loc) · 1.55 KB
/
crud.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
(ns aidbox.sdk.crud
(:require [org.httpkit.client :as http]
[aidbox.sdk.utils :refer [parse-json generate-json]]))
(defn request [ctx opts]
(let [app (:app ctx)
box (:box ctx)
url (str (:base-url box) (:url opts))
client (:client ctx)
_ (println "HTTP to box:" box (or (:method opts) :get) url)
res @(http/request
{:url url
:method (or (:method opts) :get)
:basic-auth [(:id client) (:secret client)]
:headers {"content-type" "application/json"}
:body (when (:body opts) (generate-json (:body opts)))})]
(update res :body parse-json)))
(defn read
"res - resource"
[ctx res]
(let [res (request ctx {:url (str "/" (:resourceType res) "/" (:id res))})]
(if (and (:status res) (< (:status res) 300))
(:body res)
(do (println "ERROR: " res)
(throw (Exception. (pr-str res)))))))
(defn create
[ctx res]
(let [res (request ctx {:url (str "/" (:resourceType res))
:method :post
:body res})]
(if (and (:status res) (< (:status res) 300))
(:body res)
(do (println "ERROR: " res)
(throw (Exception. (pr-str res)))))))
(defn update
[ctx res]
(let [res (request ctx {:url (str "/" (:resourceType res) "/" (:id res))
:method :put
:body res})]
(if (and (:status res) (< (:status res) 300))
(:body res)
(do (println "ERROR: " res)
(throw (Exception. (pr-str res)))))))