Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi! I cleaned up your code for you! #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ For real-world code using this library, see the `clj-mql` project:

<https://github.com/rnewman/clj-mql/tree/master>


# Building with Ant #

Invoke `ant`, optionally passing `-Dclojure.jar="..."` and `-Dclojure.contrib.jar="..."`.
Expand Down Expand Up @@ -90,7 +90,7 @@ parameters, as expected by most HTTP servers. For example:
=>
"foo=bar&baz=noo&baz=5&baz=true&top=%7B%3Ax+5%2C+%3Ay+7%7D"


# Examples #

(:content (http/get (java.net.URI. "http://example.com") :as :string))
Expand All @@ -99,7 +99,7 @@ parameters, as expected by most HTTP servers. For example:

(select-keys
(http/get "http://clojure.org/api" :as :stream) [:code :reason :content])
=>
=>
{:content #<EofSensorInputStream org.apache.http.conn.EofSensorInputStream@4ba57633>,
:reason "OK",
:code 200}
Expand Down Expand Up @@ -128,7 +128,7 @@ parameters, as expected by most HTTP servers. For example:


# Keyword parameters #

You can use `:query`, `:headers`, `:parameters`, `:as`, and `:headers-as`.

The first three are associative. `:as` can be:
Expand Down Expand Up @@ -167,7 +167,7 @@ You can pass a parameter map to the HTTP functions. This is used to set various
options on the HTTP client.

The keys are long-winded Java constants, but the capability is very useful
(e.g., for proxying). See
(e.g., for proxying). See

<http://hc.apache.org/httpcomponents-client-4.0.1/httpclient/apidocs/org/apache/http/client/params/AllClientPNames.html>

Expand Down
14 changes: 7 additions & 7 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<!-- Override these with -Dclojure.jar=... in your Ant invocation. -->
<property name="clojure.jar" location="/opt/clojure/clojure.jar"/>
<property name="clojure.contrib.jar" location="/opt/clojure-contrib/clojure-contrib.jar"/>

<available property="hasclojure" file="${clojure.jar}"/>

<!-- Library. -->
<property name="src" location="src"/>
<property name="lib" location="lib"/>
<property name="build" location="classes"/>
<property name="jarfile" location="clj-apache-http.jar"/>
<property name="deploy" location="deploy"/>

<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
Expand All @@ -32,16 +32,16 @@
<classpath>
<path location="${src}"/>
<path location="${build}"/>

<path location="${lib}/commons-logging-1.1.1.jar"/>
<path location="${lib}/httpclient-4.0.1.jar"/>
<path location="${lib}/httpcore-4.0.1.jar"/>
<path location="${lib}/httpmime-4.0.1.jar"/>

<path location="${clojure.jar}"/>
<path location="${clojure.contrib.jar}"/>
</classpath>

<sysproperty key="clojure.compile.path" value="${build}"/>
<arg value="com.twinql.clojure.http"/>
</java>
Expand All @@ -56,7 +56,7 @@
</manifest>
</jar>
</target>

<target name="deploy" description="Copy appropriate jar files to one place."
depends="jar">
<copy todir="${deploy}" verbose="true" file="${jarfile}"/>
Expand Down
48 changes: 24 additions & 24 deletions src/com/twinql/clojure/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(:require
[clojure.contrib.io :as io]
[clojure.contrib.json :as json])
(:import
(:import
(java.lang Exception)
(java.net URI)
(java.io InputStream)
Expand Down Expand Up @@ -78,15 +78,15 @@
(if (instance? clojure.lang.Named x)
(name x)
(str x)))

(defn- map->name-value-pairs
"Take an associative structure and return a sequence of BasicNameValuePairs.
Any associated value that is sequential will appear multiple times in the output, so that

{:foo [\"bar\" \"baz\"]}

will produce pairs that encode to

?foo=bar&foo=baz"
[q]
(mapcat
Expand Down Expand Up @@ -168,10 +168,10 @@

(defmethod headers-as :header-seq [#^HeaderIterator headers as]
(iterator-seq headers))

(defmethod headers-as :seq [#^HeaderIterator headers as]
(map header-pair (iterator-seq headers)))

(defmethod headers-as nil [#^HeaderIterator headers as]
(headers-as headers :seq))

Expand Down Expand Up @@ -246,23 +246,23 @@
;; The HTTP params go away in 4.1.
(SingleClientConnManager. (BasicHttpParams.) registry)))

(defn #^BasicHttpParams connection-limits
(defn #^BasicHttpParams connection-limits
"Return HTTP Parameters for the provided connection limit."
[#^Integer max-total-connections]
(doto (BasicHttpParams.)
(.setParameter ConnManagerPNames/MAX_TOTAL_CONNECTIONS max-total-connections)))

(defn #^ClientConnectionManager thread-safe-connection-manager
"Produce a new ThreadSafeClientConnManager with http and https, or
the provided registry."
([]
(thread-safe-connection-manager (scheme-registry true)))

([#^SchemeRegistry registry]
;; The HTTP params go away in 4.1.
;; BasicHttpParams isn't thread-safe...!
(thread-safe-connection-manager registry (BasicHttpParams.)))

([#^SchemeRegistry registry #^HttpParams params]
(ThreadSafeClientConnManager. params registry)))

Expand Down Expand Up @@ -335,12 +335,12 @@
(if (= "https" (.getScheme u))
443
80))]

(when-not (.getAuthScheme auth-state)
(.setCredentials c-p (AuthScope. target-host target-port) c)
(.setAuthScheme auth-state (BasicScheme.))
(.setCredentials auth-state c)))))]

(.addRequestInterceptor client prx 0)))))


Expand All @@ -361,7 +361,7 @@
filters
#^ClientConnectionManager
connection-manager]

(let [#^DefaultHttpClient http-client (if connection-manager
(DefaultHttpClient.
connection-manager
Expand All @@ -377,7 +377,7 @@
(when parameters
(doseq [[pname pval] parameters]
(.setParameter params pname pval)))

(let [#^HttpResponse http-response
;; Used for manipulation of the request prior to execution.
;; Allows things like Basic Auth.
Expand All @@ -389,15 +389,15 @@

;; Otherwise, be simple.
(.execute http-client http-verb))

#^StatusLine status-line (.getStatusLine http-response)
#^HttpEntity entity (.getEntity http-response)

response {:code (.getStatusCode status-line)
:reason (.getReasonPhrase status-line)
:content
(entity-as entity as (.getStatusCode status-line))

:entity entity
:client http-client
:response http-response
Expand All @@ -409,7 +409,7 @@
;; (.. http-client getConnectionManager closeExpiredConnections)
(when-not connection-manager
(.. http-client getConnectionManager shutdown))

response))))

(defn- adding-headers! [#^AbstractHttpMessage verb headers]
Expand All @@ -424,7 +424,7 @@
(let [#^URI u uri-parts]
(if (and query-parameters
(not (empty? query-parameters)))
(. URIUtils createURI
(. URIUtils createURI
(or (.getScheme u) "http")
(.getHost u)
(.getPort u)
Expand All @@ -434,7 +434,7 @@
(encode-query query-parameters))
(.getFragment u))
u))

(associative? uri-parts)
(let [{:keys [scheme host path port query fragment]} uri-parts]
(. URIUtils createURI
Expand All @@ -446,7 +446,7 @@
(encode-query query)
(encode-query query-parameters))
fragment))

(string? uri-parts)
(recur (URI. uri-parts) query-parameters)))

Expand All @@ -470,7 +470,7 @@
~'cookie-store
~'filters
~'connection-manager))))

;; For requests with bodies.
(defmacro def-http-body-verb [verb class]
`(defn ~verb
Expand Down Expand Up @@ -509,7 +509,7 @@ If only a query parameter map is provided, it is included in the body.")
~'cookie-store
~'filters
~'connection-manager)))))

(def-http-body-verb post HttpPost)
(def-http-body-verb put HttpPut)
(def-http-verb get HttpGet)
Expand Down Expand Up @@ -560,7 +560,7 @@ If only a query parameter map is provided, it is included in the body.")
:credential-charset org.apache.http.auth.params.AuthPNames/CREDENTIAL_CHARSET ; String.
:date-patterns org.apache.http.cookie.params.CookieSpecPNames/DATE_PATTERNS ; String.
}]

(defn map->params
"Put more pleasant names on the Apache constants."
[m]
Expand Down
8 changes: 4 additions & 4 deletions tests/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

(deftest test-resolve
(are [x y] (= x y)

(URI. "http://foo.com/bar?baz=5")
(http/resolve-uri "http://foo.com/bar" {:baz 5})

(URI. "http://foo.com/bar/?baz=1&noo=2&oz=bar#zam")
(http/resolve-uri "http://foo.com/bar/#zam" [[:baz 1] [:noo 2] ["oz" "bar"]])

(URI. "http://bar:8080/?foo=noo#zap")
(http/resolve-uri {:host "bar" :port 8080 :query {:foo :noo} :fragment "zap"} {})

(URI. "http://bar:8080/?foo=1&tar=bar")
(http/resolve-uri (URI. "http://bar:8080/?foo=1")
{:tar "bar"})))
Expand Down