-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
60 lines (51 loc) · 3.2 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
(ns clj-rest-client.core
(:require
[clojure.spec.alpha :as s]
[clojure.string :refer [ends-with?]]
[clj-rest-client.spec :as spec]
[clj-rest-client.impl :refer :all]))
(defn default-val-transform
"Default transformation that is used on params' values"
[_ v] (if (keyword? v) (name v) v))
(defn prefix-middleware
"Function for creating a clj-http middleware that prepends to url."
[url-prefix]
(let [url-prefix (if (ends-with? url-prefix "/") url-prefix (str url-prefix "/"))]
(fn [client]
(fn
([req] (client (update req :url (partial str url-prefix))))
([req respond raise] (client (update req :url (partial str url-prefix)) respond raise))))))
(defmacro defrest-map [definition {:keys [json-responses jsonify-bodies param-transform val-transform client]
:or {json-responses true jsonify-bodies :smart client identity}}]
(let [cli-sym (gensym "__auto__cli")
opts-map {:jsonify-bodies jsonify-bodies
:json-resp json-responses
:xf (or param-transform 'identity)
:val-xf (or val-transform `default-val-transform)
:client cli-sym}
defs (extract-defs (s/conform ::spec/terms definition) "" [] opts-map true)]
`(let [~cli-sym ~client]
~@defs (quote ~(map second (filter #(= `defn (first %)) defs))))))
(s/fdef defrest-map :args (s/cat :def ::spec/terms :opts ::spec/options))
(defmacro defrest
"Defines multiple functions based on definition map. For map's structure see docs.
Valid definition parameters any of the following: map literal, a symbol (naming a var that resolves to a map), a string URL.
URL string can use `classpath:` or `file:` protocol.
Definition can be followed by opts key-value arguments, all of them are optional.
`:client` This option specifies function that is invoked with clj-http maps generated by api functions. Defaults to identity.
This is a good place to put your http client function if you want requests to be executed immediately.
`:param-transform` This option specifies function that is uset to transform query parameter names: parameter name (symbol) -> query parameter name (string). Default `identity`.
`:val-transform` This option specifies a function that is applied to all arguments after argument spec and conform and before being embedded into
request map. It's a function of two arguments: param name symbol and param value, returns new param value.
Defaults to a function that converts keywords to a string name (no ns).
`:jsonify-bodies` set to `:always`, `:smart`, `:never`. Body params will be ran through serializer if set to `:always`. Option `:smart` will not
run string or bytes bodies through JSON serializer. Defaults to :smart.
`:json-responses` If true then all requests specify `{:as :json}` and all responses are expected to be json responses. Default true.
"
[definition & {:keys [] :as args}]
`(defrest-map
~(cond
(symbol? definition) (var-get (resolve &env definition))
(string? definition) (load-from-url definition)
:default definition) ~(or args {})))
(s/fdef defrest :args (s/cat :def (s/or :map map? :classpath string? :var symbol?) :opts ::spec/options*))