quick macro for creating and running xmlrpc clients

Jonathan Raphaelson
2010-07-08T04:52:43Z

rpcmacro.clj

| (ns rpcmacro
| (:import [java.net URL])
| (:import [org.apache.xmlrpc XmlRpcException])
| (:import [org.apache.xmlrpc.client XmlRpcClient XmlRpcClientConfigImpl XmlRpcCommonsTransportFactory XmlRpcSunHttpTransport XmlRpcSunHttpTransportFactory])
| (:import [org.apache.commons.httpclient HttpClient HttpState])
| (:use [clojure.contrib.pprint]))
|
| (defn mk-client [url]
| (let [config (XmlRpcClientConfigImpl.)
| client (XmlRpcClient.)
| httpcl (HttpClient.)
| factory (XmlRpcCommonsTransportFactory. client)]
| (.setServerURL config (URL. url))
| (.setConfig client config)
| (.setHttpClient factory httpcl)
| (.setTransportFactory client factory)
| client))
|
| (defmacro defrpc
| ([fname rname]
| `(defn ~fname [client#]
| (.execute client# ~rname [])))
| ([fname rname params]
| `(defn ~fname [client# & args#]
| (let [names# (map (fn [s#] (name s#)) ~params)]
| (println client# ~rname [(zipmap names# args#)])
| (.execute client# ~rname [(zipmap names# args#)])))))
|
| ; examples: each defrpc call creates a function which calls the
| ; named method and passes any given arguments along to the xmlrpc server.
|
| (def client (mk-client "https://landfill.bugzilla.org/bugzilla-3.4-branch/xmlrpc.cgi"))
|
| (defrpc get-version "Bugzilla.version")
| (defrpc login "User.login" ['login 'password])
| (defrpc logout "User.logout")
| (defrpc create-bug "Bug.create" ['product 'component 'version 'op-sys 'platform 'summary 'description])
|
| (get-version client)
| ; => bugzilla version string
|
| (create-bug client "FoodReplicator" "Salt" "1.0" "All" "Linux" "Summary" "Description")
| ; => created bug