forked from clj-nakano/situated-program-challenge
/
core.clj
29 lines (26 loc) · 1.01 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
(ns rest-client.core
(:gen-class)
(:require [cheshire.core :as cheshire]
[clj-http.client :as client]
[clojure.string :as str])
(:import (java.io BufferedReader)))
(defn read-json-from-stdin []
(cheshire/parse-stream (BufferedReader. *in*)))
(defmulti http (fn [_ method] (str/upper-case method)))
(defmethod http "GET" [url _]
(client/get url {:accept :json
:throw-exceptions false}))
(defmethod http "POST" [url _]
(client/post url {:form-params (read-json-from-stdin)
:content-type :json
:accept :json
:throw-exceptions false}))
(defmethod http :default [_ method]
(throw (UnsupportedOperationException. (str "Unsupported HTTP method: " method))))
(defn -main [& args]
(when (not= (count args) 2)
(throw (IllegalArgumentException. "Exactly 2 arguments must be specified")))
(let [{:keys [status body]} (apply http args)]
(when (not= status 200)
(print (str status \tab)))
(println body)))