Skip to content

Commit

Permalink
Add debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 20, 2020
1 parent a9e9fe8 commit 5685da4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ upgrading as the API may still undergo some changes. Contributions welcome.
(require '[cheshire.core :as json]) ;; optional
```

### GET

Simple `GET` request:

``` clojure
(curl/get "https://httpstat.us/200")
;;=> {:status 200, :body "200 OK", :headers { ... }}
```

### Headers

Passing headers:

``` clojure
(def resp (curl/get "https://httpstat.us/200" {:headers {"Accept" "application/json"}}))
(json/parse-string (:body resp)) ;;=> {"code" 200, "description" "OK"}
```

### Query parameters

Query parameters:

``` clojure
Expand All @@ -41,6 +47,8 @@ Query parameters:
;;=> {:q "clojure"}
```

### POST

A `POST` request with a `:body`:

``` clojure
Expand Down Expand Up @@ -75,13 +83,17 @@ Post a file as `multipart/form-data`:
;; => true
```

### Basic auth

Basic auth:

``` clojure
(:body (curl/get "https://postman-echo.com/basic-auth" {:basic-auth ["postman" "password"]}))
;; => "{\"authenticated\":true}"
```

### Download binary

Download a binary file as a stream:

``` clojure
Expand All @@ -93,6 +105,8 @@ Download a binary file as a stream:
;;=> 7748
```

### Passing through arguments

Passing raw arguments to `curl` can be done with `:raw-args`:

``` clojure
Expand All @@ -101,6 +115,8 @@ curl: (47) Maximum (0) redirects followed
301
```

### Unix sockets

Talking to a UNIX socket:

``` clojure
Expand All @@ -114,6 +130,8 @@ Talking to a UNIX socket:
;;=> ["borkdude/babashka:0.0.79-SNAPSHOT"]
```

### URL construction

Using the low-level API for fine grained(and safer) URL construction:

``` clojure
Expand All @@ -136,6 +154,22 @@ Using the low-level API for fine grained(and safer) URL construction:
:url "https://httpbin.org/get?q=test"}
```

### Debugging requests

Providing `:debug true` will provide extra debugging information along with the
response. The `:command` contains the command that was executed to obtain the
response. The `:options` value contains options that were used to construct the
command. Note that all of these values are for debugging only and contain
implementation details that may change in the future.

``` clojure
(def resp (curl/head "https://postman-echo.com/head" {:debug true}))
(:command resp) ;;=>
["curl" "--silent" "--show-error" "--location" "--dump-header" "/var/folders/2m/h3cvrr1x4296p315vbk7m32c0000gp/T/babashka.curl16567082489957878064.headers" "--head" "https://postman-echo.com/head"]
(:options resp)
{:debug true, :url "https://postman-echo.com/head", :method :head, :header-file #object[java.io.File 0x61d34b4 "/var/folders/2m/h3cvrr1x4296p315vbk7m32c0000gp/T/babashka.curl16567082489957878064.headers"]}
```

## Test

``` clojure
Expand Down
37 changes: 20 additions & 17 deletions src/babashka/curl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
[^String unencoded]
(URLEncoder/encode unencoded "UTF-8"))

(defn curl-command [opts]
(defn- curl-command [opts]
(let [body (:body opts)
opts (if body
(cond-> opts
Expand All @@ -62,7 +62,7 @@
method (when-let [method (:method opts)]
(case method
:head ["--head"]
["-X" (-> method name str/upper-case)]))
["--request" (-> method name str/upper-case)]))
headers (:headers opts)
headers (loop [headers* (transient [])
kvs (seq headers)]
Expand All @@ -77,7 +77,7 @@
(if kvs
(let [[k v] (first kvs)
v (if (file? v) (str "@" (.getPath ^File v)) v)
param ["-F" (str k "=" v)]]
param ["--form" (str k "=" v)]]
(recur (reduce conj! params* param) (next kvs)))
(persistent! params*)))
query-params (when-let [qp (:query-params opts)]
Expand Down Expand Up @@ -158,14 +158,14 @@
headers (read-headers (:header-file opts))
[status headers]
(reduce (fn [[status parsed-headers :as acc] header-line]
(if (str/starts-with? header-line "HTTP/")
[(Integer/parseInt (second (str/split header-line #" "))) parsed-headers]
(let [[k v] (str/split header-line #":" 2)]
(if (and k v)
[status (assoc parsed-headers (str/lower-case k) (str/trim v))]
acc))))
[nil {}]
headers)
(if (str/starts-with? header-line "HTTP/")
[(Integer/parseInt (second (str/split header-line #" "))) parsed-headers]
(let [[k v] (str/split header-line #":" 2)]
(if (and k v)
[status (assoc parsed-headers (str/lower-case k) (str/trim v))]
acc))))
[nil {}]
headers)
response {:status status
:headers headers
:body body
Expand All @@ -177,12 +177,15 @@
(defn request [opts]
(let [header-file (File/createTempFile "babashka.curl" ".headers")
opts (assoc opts :header-file header-file)
args (curl-command opts)]
(when (:debug? opts)
(println (str/join " " (map pr-str args))))
(let [response (-> (exec-curl args opts)
(curl-response->map))]
(.delete header-file)
args (curl-command opts)
response (let [response (-> (exec-curl args opts)
(curl-response->map))]
(.delete header-file)
response)]
(if (:debug opts)
(assoc response
:command args
:options opts)
response)))

(defn head
Expand Down
9 changes: 7 additions & 2 deletions test/babashka/curl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[clojure.java.io :as io]))
[clojure.test :refer [deftest is testing]]))

(deftest get-test
(is (str/includes? (:body (curl/get "https://httpstat.us/200"))
Expand Down Expand Up @@ -171,3 +170,9 @@
(is (= (repeat 10 "data: Stream Hello!") (take 10 (line-seq (io/reader body)))))
(.destroy proc))))

(deftest command-test
(let [resp (curl/head "https://postman-echo.com/head" {:debug true})
command (:command resp)
opts (:options resp)]
(is (pos? (.indexOf command "--head")))
(is (identical? :head (:method opts)))))

0 comments on commit 5685da4

Please sign in to comment.