Skip to content

Instantly share code, notes, and snippets.

@lygaret
Created July 8, 2010 04:52
Show Gist options
  • Save lygaret/467649 to your computer and use it in GitHub Desktop.
Save lygaret/467649 to your computer and use it in GitHub Desktop.
quick macro for creating and running xmlrpc clients
(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment