Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Expand entity-as from 2 to 3 args; the third is the status code.

This change makes it possible to write entity-as methods for services
which return, e.g., XML on success, but plain text (or HTML) on failure.
No more exceptions thrown before you get to check the status code.
  • Loading branch information...
commit 7187a9e1cc93884525f288f32c4841c3048f0c46 1 parent 595105a
@rnewman authored
Showing with 69 additions and 52 deletions.
  1. +69 −52 src/com/twinql/clojure/http.clj
View
121 src/com/twinql/clojure/http.clj
@@ -162,19 +162,23 @@
(header-element-map (iterator-seq headers)))
;;; Entity processors.
-(defmulti entity-as (fn [entity as] as))
+;;; None of these care about the third argument, which is the status
+;;; code.
+(defmulti entity-as (fn [entity as & args] as))
-(defmethod entity-as :identity [entity as] entity)
+(defmethod entity-as :identity
+ [entity as & args] entity)
-(defmethod entity-as nil [entity as] entity)
+(defmethod entity-as nil
+ [entity as & args] entity)
-(defmethod entity-as :stream [#^HttpEntity entity as]
+(defmethod entity-as :stream [#^HttpEntity entity as & args]
(.getContent entity))
-(defmethod entity-as :reader [#^HttpEntity entity as]
+(defmethod entity-as :reader [#^HttpEntity entity as & args]
(duck/reader (.getContent entity)))
-(defmethod entity-as :string [#^HttpEntity entity as]
+(defmethod entity-as :string [#^HttpEntity entity as & args]
(duck/slurp* (.getContent entity)))
(defn preemptive-basic-auth-filter
@@ -252,7 +256,7 @@
#^CookieStore cookie-store
filters]
- (let [#^DefaultHttpClient http-client (new DefaultHttpClient)
+ (let [#^DefaultHttpClient http-client (DefaultHttpClient.)
params (.getParams http-client)]
(when cookie-store
@@ -280,7 +284,16 @@
response {:code (.getStatusCode status-line)
:reason (.getReasonPhrase status-line)
- :content (entity-as entity as)
+ :content
+
+ ;; Just while we go through the transition
+ ;; period from 2-arg to 3-arg..
+ (try
+ (entity-as entity as (.getStatusCode status-line))
+ (catch java.lang.IllegalArgumentException e
+ ;; Wrong number of args.
+ (entity-as entity as)))
+
:entity entity
:client http-client
:response http-response
@@ -289,7 +302,7 @@
h-as)}]
;; I don't know if it's actually a good thing to do this.
- ;(.. http-client getConnectionManager closeExpiredConnections)
+ ;; (.. http-client getConnectionManager closeExpiredConnections)
(.. http-client getConnectionManager shutdown)
response))))
@@ -398,46 +411,50 @@ If only a query parameter map is provided, it is included in the body.")
(let [{:keys [host port scheme]} (apply hash-map args)]
(HttpHost. host (ensure-int port) scheme)))
-(defn map->params
- "Put more pleasant names on the Apache constants."
- [m]
- ;; In Clojure a1397390d8b3b63f2039359520629d87b152d717, rename-keys is buggy.
- (rename-keys
- m
- {:default-headers org.apache.http.client.params.ClientPNames/DEFAULT_HEADERS ; Collection of Headers.
- :default-host org.apache.http.client.params.ClientPNames/DEFAULT_HOST ; HttpHost.
- :default-proxy org.apache.http.conn.params.ConnRoutePNames/DEFAULT_PROXY ; HttpHost.
- :virtual-host org.apache.http.client.params.ClientPNames/VIRTUAL_HOST ; HttpHost.
- :forced-route org.apache.http.conn.params.ConnRoutePNames/FORCED_ROUTE ; HttpRoute.
- :max-total-connections org.apache.http.conn.params.ConnManagerPNames/MAX_TOTAL_CONNECTIONS ; ConnPerRoute.
- :local-address org.apache.http.conn.params.ConnRoutePNames/LOCAL_ADDRESS ; InetAddress.
- :protocol-version org.apache.http.params.CoreProtocolPNames/PROTOCOL_VERSION ; ProtocolVersion.
- :max-status-line-garbage org.apache.http.conn.params.ConnConnectionPNames/MAX_STATUS_LINE_GARBAGE ; Integer.
- :max-connections-per-route org.apache.http.conn.params.ConnManagerPNames/MAX_CONNECTIONS_PER_ROUTE ; Integer.
- :connection-timeout org.apache.http.params.CoreConnectionPNames/CONNECTION_TIMEOUT ; Integer.
- :max-header-count org.apache.http.params.CoreConnectionPNames/MAX_HEADER_COUNT ; Integer.
- :max-line-length org.apache.http.params.CoreConnectionPNames/MAX_LINE_LENGTH ; Integer.
- :so-linger org.apache.http.params.CoreConnectionPNames/SO_LINGER ; Integer.
- :so-timeout org.apache.http.params.CoreConnectionPNames/SO_TIMEOUT ; Integer.
- :socket-buffer-size org.apache.http.params.CoreConnectionPNames/SOCKET_BUFFER_SIZE ; Integer.
- :wait-for-continue org.apache.http.params.CoreProtocolPNames/WAIT_FOR_CONTINUE ; Integer.
- :max-redirects org.apache.http.client.params.ClientPNames/MAX_REDIRECTS ; Integer.
- :timeout org.apache.http.conn.params.ConnManagerPNames/TIMEOUT ; Long.
- :stale-connection-check org.apache.http.params.CoreConnectionPNames/STALE_CONNECTION_CHECK ; Boolean.
- :tcp-nodelay org.apache.http.params.CoreConnectionPNames/TCP_NODELAY ; Boolean.
- :strict-transfer-encoding org.apache.http.params.CoreProtocolPNames/STRICT_TRANSFER_ENCODING ; Boolean.
- :use-expect-continue org.apache.http.params.CoreProtocolPNames/USE_EXPECT_CONTINUE ; Boolean.
- :handle-authentication org.apache.http.client.params.ClientPNames/HANDLE_AUTHENTICATION ; Boolean.
- :handle-redirects org.apache.http.client.params.ClientPNames/HANDLE_REDIRECTS ; Boolean.
- :reject-relative-redirect org.apache.http.client.params.ClientPNames/REJECT_RELATIVE_REDIRECT ; Boolean.
- :allow-circular-redirects org.apache.http.client.params.ClientPNames/ALLOW_CIRCULAR_REDIRECTS ; Boolean.
- :single-cookie-header org.apache.http.cookie.params.CookieSpecPNames/SINGLE_COOKIE_HEADER ; Boolean.
- :connection-manager-factory-class-name org.apache.http.client.params.ClientPNames/CONNECTION_MANAGER_FACTORY_CLASS_NAME ; String.
- :http-content-charset org.apache.http.params.CoreProtocolPNames/HTTP_CONTENT_CHARSET ; String.
- :http-element-charset org.apache.http.params.CoreProtocolPNames/HTTP_ELEMENT_CHARSET ; String.
- :origin-server org.apache.http.params.CoreProtocolPNames/ORIGIN_SERVER ; String.
- :user-agent org.apache.http.params.CoreProtocolPNames/USER_AGENT ; String.
- :cookie-policy org.apache.http.client.params.ClientPNames/COOKIE_POLICY ; String.
- :credential-charset org.apache.http.auth.params.AuthPNames/CREDENTIAL_CHARSET ; String.
- :date-patterns org.apache.http.cookie.params.CookieSpecPNames/DATE_PATTERNS ; String.
- }))
+
+(let [rename-to
+ {:default-headers org.apache.http.client.params.ClientPNames/DEFAULT_HEADERS ; Collection of Headers.
+ :default-host org.apache.http.client.params.ClientPNames/DEFAULT_HOST ; HttpHost.
+ :default-proxy org.apache.http.conn.params.ConnRoutePNames/DEFAULT_PROXY ; HttpHost.
+ :virtual-host org.apache.http.client.params.ClientPNames/VIRTUAL_HOST ; HttpHost.
+ :forced-route org.apache.http.conn.params.ConnRoutePNames/FORCED_ROUTE ; HttpRoute.
+ :max-total-connections org.apache.http.conn.params.ConnManagerPNames/MAX_TOTAL_CONNECTIONS ; ConnPerRoute.
+ :local-address org.apache.http.conn.params.ConnRoutePNames/LOCAL_ADDRESS ; InetAddress.
+ :protocol-version org.apache.http.params.CoreProtocolPNames/PROTOCOL_VERSION ; ProtocolVersion.
+ :max-status-line-garbage org.apache.http.conn.params.ConnConnectionPNames/MAX_STATUS_LINE_GARBAGE ; Integer.
+ :max-connections-per-route org.apache.http.conn.params.ConnManagerPNames/MAX_CONNECTIONS_PER_ROUTE ; Integer.
+ :connection-timeout org.apache.http.params.CoreConnectionPNames/CONNECTION_TIMEOUT ; Integer.
+ :max-header-count org.apache.http.params.CoreConnectionPNames/MAX_HEADER_COUNT ; Integer.
+ :max-line-length org.apache.http.params.CoreConnectionPNames/MAX_LINE_LENGTH ; Integer.
+ :so-linger org.apache.http.params.CoreConnectionPNames/SO_LINGER ; Integer.
+ :so-timeout org.apache.http.params.CoreConnectionPNames/SO_TIMEOUT ; Integer.
+ :socket-buffer-size org.apache.http.params.CoreConnectionPNames/SOCKET_BUFFER_SIZE ; Integer.
+ :wait-for-continue org.apache.http.params.CoreProtocolPNames/WAIT_FOR_CONTINUE ; Integer.
+ :max-redirects org.apache.http.client.params.ClientPNames/MAX_REDIRECTS ; Integer.
+ :timeout org.apache.http.conn.params.ConnManagerPNames/TIMEOUT ; Long.
+ :stale-connection-check org.apache.http.params.CoreConnectionPNames/STALE_CONNECTION_CHECK ; Boolean.
+ :tcp-nodelay org.apache.http.params.CoreConnectionPNames/TCP_NODELAY ; Boolean.
+ :strict-transfer-encoding org.apache.http.params.CoreProtocolPNames/STRICT_TRANSFER_ENCODING ; Boolean.
+ :use-expect-continue org.apache.http.params.CoreProtocolPNames/USE_EXPECT_CONTINUE ; Boolean.
+ :handle-authentication org.apache.http.client.params.ClientPNames/HANDLE_AUTHENTICATION ; Boolean.
+ :handle-redirects org.apache.http.client.params.ClientPNames/HANDLE_REDIRECTS ; Boolean.
+ :reject-relative-redirect org.apache.http.client.params.ClientPNames/REJECT_RELATIVE_REDIRECT ; Boolean.
+ :allow-circular-redirects org.apache.http.client.params.ClientPNames/ALLOW_CIRCULAR_REDIRECTS ; Boolean.
+ :single-cookie-header org.apache.http.cookie.params.CookieSpecPNames/SINGLE_COOKIE_HEADER ; Boolean.
+ :connection-manager-factory-class-name org.apache.http.client.params.ClientPNames/CONNECTION_MANAGER_FACTORY_CLASS_NAME ; String.
+ :http-content-charset org.apache.http.params.CoreProtocolPNames/HTTP_CONTENT_CHARSET ; String.
+ :http-element-charset org.apache.http.params.CoreProtocolPNames/HTTP_ELEMENT_CHARSET ; String.
+ :origin-server org.apache.http.params.CoreProtocolPNames/ORIGIN_SERVER ; String.
+ :user-agent org.apache.http.params.CoreProtocolPNames/USER_AGENT ; String.
+ :cookie-policy org.apache.http.client.params.ClientPNames/COOKIE_POLICY ; String.
+ :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]
+ (rename-keys
+ m
+ rename-to)))
+
Please sign in to comment.
Something went wrong with that request. Please try again.