Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
Created March 1, 2015 17:28
Show Gist options
  • Select an option

  • Save andrewrlee/b32d2bddcc72ece5fbfd to your computer and use it in GitHub Desktop.

Select an option

Save andrewrlee/b32d2bddcc72ece5fbfd to your computer and use it in GitHub Desktop.
Sample clojure thrift client
(ns test-client.core
(:gen-class)
(:require [thrift-clj.core :as thrift]
[environ.core :refer [env]]))
(thrift/import
(:types [github.thrift.mongo.core.api Commit]
[github.thrift.mongo.core.api Push])
(:clients github.thrift.mongo.core.api.PushService))
(defn info
"logs a series of statements"
[& args] (println (str "INFO > " (java.util.Date.) " > " (apply str args))))
(defn- connect []
(let [host-port [ (env :host "localhost") (env :port 8080)]]
(info "connecting on " host-port)
(thrift/connect! PushService (thrift/framed host-port :protocol :compact))))
(defn get-total-number-of-events
[] (try
(with-open [c (connect)]
(PushService/getTotalNumberOfPushes c))
(catch Exception e (info "Not Healthy!, message: " (.getMessage e)))))
(defn query
[term] (try
(with-open [c (connect)]
(PushService/getPushes c term))
(catch Exception e (info "Not Healthy!, message: " (.getMessage e)))))
(defn server-healthy?
"Pings the server and returns \"Healthy!\" if healthy or \"Not Healthy!\" and a message if otherwise"
[] (try
(with-open [c (connect)]
(let [response (PushService/ping c)
healthy? (= response "pong!")]
(if healthy? (info "Healthy!") (info "Not Healthy!, response:" response))
healthy?))
(catch Exception e (info "Not Healthy!, message: " (.getMessage e)))))
(defn -main [& args]
(if (server-healthy?)
(do (println "There are " (get-total-number-of-events) " items")
(doseq [term args] (println "searching for " term) (clojure.pprint/pprint (query term))))))
(defproject test-client "0.1.0-SNAPSHOT"
:description "A dummy client for the thrift server"
:url "https://github.com/plasma147/github-thrift-client-server"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[environ "1.0.0"]
[thrift-clj "0.2.1"]
[ch.qos.logback/logback-classic "1.0.13"]
[api "0.2.0-SNAPSHOT"]]
:plugins [[lein-environ "1.0.0"]]
:main ^:skip-aot test-client.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment