Skip to content

Instantly share code, notes, and snippets.

@wilkes
Created November 26, 2008 19:59
Show Gist options
  • Save wilkes/29541 to your computer and use it in GitHub Desktop.
Save wilkes/29541 to your computer and use it in GitHub Desktop.
(ns rets
(:import (org.apache.http.client HttpClient)
(org.apache.http.client.methods HttpGet HttpPost)
(org.apache.http.impl.client DefaultHttpClient)
(org.apache.http.util EntityUtils)))
(use '[clojure.contrib.str-utils])
(def base-url "http://rets.realtracs.net:6103")
(def login-path "/rets/login")
(def logout-path "/rets/logout")
(def login-url (str base-url login-path))
(def logout-url (str base-url logout-path))
(def default-auth-query { :username "parkrets", :password "*******" })
(def default-query { :querytype "DMQL", :standardnames "0", :count "0", :format "COMPACT" })
(defn to-string [response]
(EntityUtils/toString (.getEntity response)))
(defn- authenticate
"Perform authtication of web service"
[]
(to-string (.execute (DefaultHttpClient.) (HttpPost. login-url)))))
(defn- build-request
[request]
(doto request
(.addHeader "Content-type" "application/xml")))
(defn- do-request
"Perform requests to get XML"
[request]
(to-string (.execute (DefaultHttpClient.) (build-request request))))
(defn- map->querystring
"Builds a string in the form of querytype=DMQL&standardnames=0&count=0"
[m]
(let [join-pair (fn [[k v]]
(str-join "=" [(.toUpperCase (name k)) v]))]
(str-join "&"
(reduce (fn [acc pair]
(conj acc (join-pair pair))) [] m))))
(defn- build-url
"Build url"
[query]
(let [url (str base-url "?" (map->querystring (conj default-query query)))]
(HttpGet. url)))
;; Public API
(defn property
"Get the RETS information for a particular MSL entry"
[mlsnum]
(do-request (build-url { :searchtype "Property", :class "RES", :mlsnum mlsnum })))
(defn query
"Query the RETS service"
[query]
(do-request (build-request (query))))
;; Usage
(authenticate)
(property "100298")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment