Skip to content

Commit

Permalink
Recognize and apply :url connect option.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgrana committed Aug 1, 2010
1 parent 992936c commit e50e5cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/fleetdb/client.clj
@@ -1,6 +1,6 @@
(ns fleetdb.client
(:require (clj-json [core :as json]))
(:import (java.net Socket)
(:import (java.net Socket URI)
(java.io OutputStreamWriter BufferedWriter
InputStreamReader BufferedReader
Closeable)
Expand All @@ -26,21 +26,32 @@
(.close #^BufferedWriter (:writer client))
(.close #^Socket (:socket client)))

(defn- apply-url [url options]
(let [url-parsed (URI. url)]
(-> options
(dissoc :url)
(assoc :host (.getHost url-parsed))
(assoc :port (.getPort url-parsed))
(assoc :password (if-let [ui (.getUserInfo url-parsed)]
(second (re-find #":(.+)" ui)))))))

(defn connect [& [options]]
(let [host (get options :host "127.0.0.1")
port (get options :port 3400)
timeout (get options :timeout)
password (get options :password)
socket (Socket. #^String host #^Integer port)
writer (BufferedWriter. (OutputStreamWriter. (.getOutputStream socket)))
reader (BufferedReader. (InputStreamReader. (.getInputStream socket)))
attrs {:writer writer :reader reader :socket socket
:host host :port port :password password :timeout timeout}]
(when timeout
(.setSoTimeout socket (int (* timeout 1000))))
(when password
(doquery writer reader ["auth" password]))
(proxy [IFn ILookup Closeable] []
(invoke [q] (doquery writer reader q))
(valAt [k] (attrs k))
(close [] (close attrs)))))
(if-let [url (:url options)]
(connect (apply-url url options))
(let [host (get options :host "127.0.0.1")
port (get options :port 3400)
timeout (get options :timeout)
password (get options :password)
socket (Socket. #^String host #^Integer port)
writer (BufferedWriter. (OutputStreamWriter. (.getOutputStream socket)))
reader (BufferedReader. (InputStreamReader. (.getInputStream socket)))
attrs {:writer writer :reader reader :socket socket
:host host :port port :password password :timeout timeout}]
(when timeout
(.setSoTimeout socket (int (* timeout 1000))))
(when password
(doquery writer reader ["auth" password]))
(proxy [IFn ILookup Closeable] []
(invoke [q] (doquery writer reader q))
(valAt [k] (attrs k))
(close [] (close attrs))))))
4 changes: 4 additions & 0 deletions test/fleetdb/client_test.clj
Expand Up @@ -18,6 +18,10 @@
(with-client [client nil]
(assert= "pong" (client ["ping"]))))

(deftest "url options"
(with-client [client {:url "fleetdb://:pass@localhost:3401"}]
(assert= "pong" (client "ping"))))

(deftest "malformed query"
(with-client [client nil]
(assert-throws #"Malformed query"
Expand Down

0 comments on commit e50e5cd

Please sign in to comment.