Skip to content

Instantly share code, notes, and snippets.

@huskercane
Created July 29, 2014 15:17
Show Gist options
  • Select an option

  • Save huskercane/236aee49c75f3d1eb7a8 to your computer and use it in GitHub Desktop.

Select an option

Save huskercane/236aee49c75f3d1eb7a8 to your computer and use it in GitHub Desktop.
A sample rest client in clojure
(ns restclient.core
(:require [clj-http.client :as client]))
(defn get_tasks_json []
;; (client/get "https://172.21.0.32/admin/resources/backup"
;; {:basic-auth ["admin" "admin"]
(client/get "https://172.16.65.143/resources/tasks"
{:basic-auth ["cbadmin" "cbadmin"]
:content-type :json
:headers {"X-IBM-Workload-Deployer-API-Version" "3.1"}
:insecure? true
:as :json
:accept :json})
)
(defn get_jobs_json []
(client/get "https://172.21.0.32/admin/resources/jobs"
{:basic-auth ["admin" "admin"]
:content-type :json
:headers {"X-IBM-Workload-Deployer-API-Version" "3.1"}
:insecure? true
:as :json
:accept :json})
)
(defn filter_task [job_list]
(loop [my_list [] jl job_list]
(if (seq jl)
(if (.equals (:event (first jl)) "backup-requested" )
;;(println (first jl))
;;(println (:id (first jl)) )
(let [jj (conj my_list (:id (first jl)))]
(recur jj (rest jl)) ;;end recur
);;end let
);;end if
my_list;;return value on else
);;end if
) ;;end loop
) ;;end function
(defn filter_job [job_list]
(loop [my_list [] jl job_list]
(if (seq jl)
(if (and (.equals (:state (first jl)) "pending") (.equals (:action (first jl)) "backupjob" ) )
;;(println (first jl))
;;(println (:id (first jl)) )
(let [jj (conj my_list (:id (first jl)))]
(recur jj (rest jl)) ;;end recur, add id to return list
);;end let
(recur my_list (rest jl)) ;;end recur, dont process anything
);;end if
my_list;;return value on else
) ;;not done, end if
) ;;end loop
) ;;end function
(defn -main
"I don't do a whole lot."
[& args]
(println "Hello, World!")
(let [jobs (get_jobs_json)]
;;(println (jobs :body))
;;(println (first (jobs :body)))
(println (filter_job (jobs :body)))
(println (filter_task ((get_tasks_json) :body)))
)
;;(let [jobs (get_jobs_json)]
;;(doseq [k (keys jobs) v (vals jobs)] (println (str k " " v)))
;;)
)
;(defn jobs []
; (println (get_jobs_json))
; (doseq [k (keys db) v (vals db)] (println (str k " " v)))
; )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment