Skip to content

Commit

Permalink
support keywords as query and form param keys, non str values (#39)
Browse files Browse the repository at this point in the history
* support keywords as query and form param keys, non str values

* add test for form params

* add coerce-key
  • Loading branch information
lispyclouds authored Oct 6, 2021
1 parent 75389c5 commit ddae74b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ flycheck_*.el
# directory configuration
.dir-locals.el


# End of https://www.gitignore.io/api/emacs,clojure

**/.clj-kondo/.cache

.idea/
.lsp/
15 changes: 11 additions & 4 deletions src/babashka/curl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
[^String unencoded]
(URLEncoder/encode unencoded "UTF-8"))

(defn- coerce-key
"Coreces a key to str"
[k]
(if (keyword? k)
(-> k str (subs 1))
(str k)))

(defn- curl-command [opts]
(let [body (:body opts)
opts (if body
Expand All @@ -76,23 +83,23 @@
(case method
:head ["--head"]
["--request" (-> method name str/upper-case)]))
headers (into [] (mapcat (fn [[k v]] ["-H" (str (name k) ": " v)])) (:headers opts))
headers (into [] (mapcat (fn [[k v]] ["-H" (str (coerce-key k) ": " v)])) (:headers opts))
accept-header (accept-header opts)
form-params (when-let [form-params (:form-params opts)]
(loop [params* (transient [])
kvs (seq form-params)]
(if kvs
(let [[k v] (first kvs)
v (url-encode v)
param ["--data" (str (url-encode k) "=" v)]]
v (url-encode (str v))
param ["--data" (str (url-encode (coerce-key k)) "=" v)]]
(recur (reduce conj! params* param) (next kvs)))
(persistent! params*))))
query-params (when-let [qp (:query-params opts)]
(loop [params* (transient [])
kvs (seq qp)]
(if kvs
(let [[k v] (first kvs)]
(recur (conj! params* (str (url-encode k) "=" (url-encode v))) (next kvs)))
(recur (conj! params* (str (url-encode (coerce-key k)) "=" (url-encode (str v)))) (next kvs)))
(str/join "&" (persistent! params*)))))
data-raw (:data-raw opts)
data-raw (when data-raw
Expand Down
8 changes: 5 additions & 3 deletions test/babashka/curl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
(json/parse-string true)
:code)))
(testing "query params"
(is (= {:foo1 "bar1", :foo2 "bar2"}
(-> (curl/get "https://postman-echo.com/get" {:query-params {"foo1" "bar1" "foo2" "bar2"}})
(is (= {:foo1 "bar1" :foo2 "bar2" :foo3 "bar3" :not-string "42" :namespaced/key "foo"}
(-> (curl/get "https://postman-echo.com/get" {:query-params {"foo1" "bar1" "foo2" "bar2" :foo3 "bar3" :not-string 42 :namespaced/key "foo"}})
:body
(json/parse-string true)
:args)))))
Expand Down Expand Up @@ -59,7 +59,9 @@
"babashka.curl")))
(testing "form-params"
(let [body (:body (curl/post "https://postman-echo.com/post"
{:form-params {"name" "Michiel Borkent"}}))
{:form-params {"name" "Michiel Borkent"
:location "NL"
:this-isnt-a-string 42}}))
body (json/parse-string body true)
headers (:headers body)
content-type (:content-type headers)]
Expand Down

0 comments on commit ddae74b

Please sign in to comment.