Skip to content

Commit

Permalink
Add hickory output
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwood committed Oct 6, 2018
1 parent 72bf445 commit 1941116
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Clojurl

An example Clojure CLI HTTP/S client using GraalVM native image.
An example HTTP/S client CLI using Clojure and GraalVM native image.

Generated with [clj.native-cli template](https://github.com/taylorwood/clj.native-cli).
Uses deps.edn and [clj.native-image](https://github.com/taylorwood/clj.native-image).
Expand Down Expand Up @@ -68,17 +68,19 @@ Compile the program with GraalVM `native-image`:
$ clojure -A:native-image
```

Print CLI options:
CLI options:
```
$ ./clojurl -h
-u, --uri URI URI of request
-H, --header HEADER Request header(s)
-d, --data DATA Request data
-m, --method METHOD GET Request method e.g. GET, POST, etc.
-o, --output FORMAT edn Output format e.g. edn, hickory
-h, --help
```
Responses can be printed in EDN or Hickory format.

Make a request and the entire response map will be pretty-printed as EDN to stdout:
Make a request and print response to stdout:
```
$ ./clojurl -u https://postman-echo.com/get
{:headers
Expand All @@ -93,7 +95,11 @@ $ ./clojurl -u https://postman-echo.com/get
:status 200,
:body
"{\"args\":{},\"headers\":{\"host\":\"postman-echo.com\",\"accept\":\"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\",\"accept-encoding\":\"gzip, deflate\",\"user-agent\":\"Java/1.8.0_172\",\"x-forwarded-port\":\"443\",\"x-forwarded-proto\":\"https\"},\"url\":\"https://postman-echo.com/get\"}"}
$ ./clojurl -H Accept=application/json -H X-Session-Id=1234 -u https://postman-echo.com/post -m post -d "{'foo':true}" -H Content-Type=application/json
```
```
$ ./clojurl -H Accept=application/json -H X-Session-Id=1234 -H Content-Type=application/json \
-u https://postman-echo.com/post \
-m post -d "{'foo':true}"
{:headers
{"content-encoding" "gzip",
"content-type" "application/json; charset=utf-8",
Expand Down
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:deps {org.clojure/clojure {:mvn/version "1.9.0"}
org.clojure/tools.cli {:mvn/version "0.4.1"}
clj-http-lite {:mvn/version "0.3.0"}}
clj-http-lite {:mvn/version "0.3.0"}
hickory {:mvn/version "0.7.1"}}
:aliases {:native-image
{:main-opts ["-m clj.native-image clojurl"
"--report-unsupported-elements-at-runtime"
Expand Down
12 changes: 9 additions & 3 deletions src/clojurl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [clj-http.lite.client :as http]
[clojure.string :as cs]
[clojure.tools.cli :as cli]
[clojure.pprint :refer [pprint]])
[clojure.pprint :refer [pprint]]
[hickory.core :as hick])
(:gen-class))

(set! *warn-on-reflection* true)
Expand All @@ -18,17 +19,22 @@
["-m" "--method METHOD" "Request method e.g. GET, POST, etc."
:default :get, :parse-fn (comp keyword cs/lower-case)
:default-desc "GET"]
["-o" "--output FORMAT" "Output format e.g. edn, hickory"
:id :output
:parse-fn {"edn" pprint
"hickory" (comp prn hick/as-hickory hick/parse :body)}
:default pprint :default-desc "edn"]
["-h" "--help" :id :help?]])

(defn -main [& args]
;; for sunec native lib loading at native-image runtime
(System/setProperty "java.library.path"
(str (System/getenv "GRAALVM_HOME") "/jre/lib"))
(let [{:keys [options summary]} (cli/parse-opts args cli-options)
{:keys [help? uri method headers data]} options]
{:keys [help? uri method headers data output]} options]
(when help? (println summary))
(when uri
(pprint (http/request {:url uri
(output (http/request {:url uri
:method (keyword method)
:headers headers
:body data})))))

0 comments on commit 1941116

Please sign in to comment.