Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Further cleanup.

clj-oauth2 restored temporarily until friend migration complete....
  • Loading branch information...
commit 2dd53db2c448f61ff4fc989ffca39366ad04cb7d 1 parent 67abee2
authored March 30, 2013
1  .gitignore
@@ -4,3 +4,4 @@ lib
4 4
 classes
5 5
 target
6 6
 .lein-deps-sum
  7
+.lein-repl-history
6  project.clj
... ...
@@ -1,6 +1,9 @@
1 1
 (defproject net.likestream/clj-facebook-graph "0.5.0-SNAPSHOT"
  2
+
2 3
   :description "A Clojure client for the Facebook Graph API"
3  
-  :url "http://github.com/FreeAgent/clj-facebook-graph/"
  4
+
  5
+  :url "http://github.com/LikeStream/clj-facebook-graph/"
  6
+
4 7
   :license {:name "Eclipse Public License - v 1.0"
5 8
             :url "http://www.eclipse.org/legal/epl-v10.html"
6 9
             :distribution :repo
@@ -11,6 +14,7 @@
11 14
                  [ring/ring-core "1.1.8"]
12 15
                  [clj-http "0.7.0"]
13 16
                  [slingshot "0.10.3"]
  17
+                 [bwhmather/clj-oauth2 "0.5.1"]
14 18
                  ]
15 19
 
16 20
   :profiles {:dev {:dependencies [[ring/ring-devel "1.1.8"]
49  src/clj_facebook_graph/auth.clj
... ...
@@ -1,9 +1,7 @@
1 1
 (ns clj-facebook-graph.auth
2  
-  (:use [clj-facebook-graph.helper :only [facebook-base-url facebook-fql-base-url]] 
3  
-        ;; [clojure.data.json :only [read-json]]
4  
-        )
5  
-  (:require ;; [clj-oauth2.client :as oauth2]
  2
+  (:require [clj-facebook-graph.helper :refer [facebook-base-url facebook-fql-base-url]]
6 3
             [clojure.string :as str]
  4
+            [clj-oauth2.client :as oauth2]
7 5
             [cheshire.core :as json])
8 6
   (:import [org.apache.commons.codec.binary Base64]
9 7
            [javax.crypto Mac]
@@ -14,13 +12,13 @@
14 12
    :authorization-uri "https://graph.facebook.com/oauth/authorize"
15 13
    :access-token-uri "https://graph.facebook.com/oauth/access_token"})
16 14
 
17  
-;; (defn get-access-token
18  
-;;   "Fetches the access token using clj-oauth2/client."
19  
-;;   [facebook-app-info params & [auth-req]]
20  
-;;   (:access-token (oauth2/get-access-token
21  
-;;                   (merge facebook-oauth2-endpoint facebook-app-info)
22  
-;;                   params
23  
-;;                   auth-req)))
  15
+(defn get-access-token
  16
+   "Fetches the access token using clj-oauth2/client."
  17
+   [facebook-app-info params & [auth-req]]
  18
+   (:access-token (oauth2/get-access-token
  19
+                   (merge facebook-oauth2-endpoint facebook-app-info)
  20
+                   params
  21
+                   auth-req)))
24 22
 
25 23
 
26 24
 ;; (defn make-auth-request [facebook-app-info]
@@ -37,11 +35,13 @@
37 35
   `(binding [*facebook-auth* ~facebook-auth]
38 36
      ~@body))
39 37
 
40  
-(defn- oauth2-access-token []
  38
+(defn- oauth2-access-token 
  39
+  []
41 40
   (assoc *facebook-auth*
42 41
     :query-param (:access-query-param facebook-oauth2-endpoint) :token-type "draft-10"))
43 42
 
44  
-(defn has-facebook-auth[]
  43
+(defn has-facebook-auth
  44
+  []
45 45
   "Returns whether there is an existing binding for facebook-auth"
46 46
   ((complement nil?) *facebook-auth*))
47 47
 
@@ -77,7 +77,12 @@
77 77
 (defn strtr
78 78
   "My take on PHP's strtr function."
79 79
   [value from to]
80  
-  ((apply comp (map (fn [a b] #(.replace % a b)) from to))
  80
+  ((apply comp 
  81
+          (map (fn 
  82
+                 [a b] 
  83
+                 #(.replace % a b))
  84
+               from
  85
+               to))
81 86
    value))
82 87
 
83 88
 (defn decode-signed-request
@@ -86,21 +91,21 @@
86 91
   [signed-request key]
87 92
   (when (and signed-request key
88 93
              (re-matches #"^[^\.]+\.[^\.]+$" signed-request))
89  
-    (let [[signiture payload] (str/split signed-request #"\.")
90  
-          signiture (str (strtr signiture "-_" "+/") "=")]
91  
-      (when (= signiture (hmac-sha-256 key payload))
  94
+    (let [[signature payload] (str/split signed-request #"\.")
  95
+          signature           (str (strtr signature "-_" "+/") "=")]
  96
+      (when (= signature (hmac-sha-256 key payload))
92 97
         (json/parse-string (base64-decode payload)
93 98
                            true)))))        
94 99
 
95  
-;;        (read-json (base64-decode payload))))))
96  
-
97  
-(defn extract-facebook-auth [session]
  100
+(defn extract-facebook-auth 
  101
+  [session]
98 102
   (:facebook-auth (val session)))
99 103
 
100 104
 (defn facebook-auth-user
101 105
   "Get the basic facebook user data for the given facebook-auth (access_token)."
102 106
   [client-get facebook-auth]
103  
-  (with-facebook-auth facebook-auth (client-get [:me] {:extract :body})))
  107
+  (with-facebook-auth facebook-auth 
  108
+    (client-get [:me] {:extract :body})))
104 109
 
105 110
 (defn facebook-auth-by-name
106 111
   "Take all sessions from the session-store and extracts the facebook-auth
@@ -108,7 +113,7 @@
108 113
    associated with his current facebook-auth (access-token)."
109 114
   [client-get session-store]
110 115
   (first (map #(let [facebook-auth (extract-facebook-auth %)
111  
-                     user-name (:name (facebook-auth-user client-get facebook-auth))]
  116
+                     user-name     (:name (facebook-auth-user client-get facebook-auth))]
112 117
                  (identity {user-name
113 118
                             facebook-auth}))
114 119
               session-store)))
71  src/clj_facebook_graph/client.clj
@@ -7,49 +7,40 @@
7 7
 ; You must not remove this notice, or any other, from this software.
8 8
 
9 9
 (ns clj-facebook-graph.client
10  
-  "A client for the Facebook Graph API based on clj-http and clj-oauth2."
11  
-  (:use [clj-facebook-graph.helper :only [wrap-exceptions facebook-base-url facebook-fql-base-url]]
12  
-        [clj-facebook-graph.auth :only [wrap-facebook-access-token]]
13  
-        [clj-facebook-graph.error-handling :only [wrap-facebook-exceptions]])
14  
-  (:require [clj-http.client :as client]
15  
-            [clj-facebook-graph.auth :as auth])
  10
+  "A client for the Facebook Graph API using clj-http"
  11
+  (:require [clj-http.client :as http]
  12
+            [clj-http.core :as http-core :only [request]]
  13
+            [clj-facebook-graph.helper :refer [wrap-exceptions facebook-base-url facebook-fql-base-url]]
  14
+            [clj-facebook-graph.error-handling :refer [wrap-facebook-exceptions]])
16 15
   (:refer-clojure :exclude [get]))
17 16
 
18  
-
19  
-(defn wrap-facebook-url-builder [client]
  17
+(defn wrap-facebook-url-builder 
20 18
   "Offers some convenience by assemble a Facebook Graph API URL from a vector of keywords or strings.
21  
-  Instead of defining the whole Facebook Graph API URL like this (client/get \"https://graph.facebook.com/me/friends\") you can
22  
-  simple write (client/get [:me :friends]) (you can also write [\"me\" \"friends\"]). It's flexible thanks to the homogeneity
  19
+  Instead of defining the whole Facebook Graph API URL like this (http/get \"https://graph.facebook.com/me/friends\") you can
  20
+  simple write (http/get [:me :friends]) (you can also write [\"me\" \"friends\"]). It's flexible thanks to the homogeneity
23 21
   of the Facebook Graph API. When you have more than an id (here \"me\") and a connection type
24 22
   (here \"friends\"), you can also provide three or more
25 23
   keywords (or strings) like in the case of 'https://graph.facebook.com/me/videos/uploaded' for example."
  24
+  [client]
26 25
   (fn [req]
27 26
     (let [{:keys [url]} req]    
28 27
       (if (vector? url)
29  
-        (let [url-parts-as-str (map #(if (keyword? %) (name %) (str %)) url)
30  
-              url (apply str (interpose "/" (conj url-parts-as-str facebook-base-url)))]
  28
+        (let [url-parts-as-str (map #(if (keyword? %) 
  29
+                                       (name %) 
  30
+                                       (str %))
  31
+                                    url)
  32
+              url              (apply str 
  33
+                                      (interpose "/" (conj url-parts-as-str 
  34
+                                                           facebook-base-url)))]
31 35
           (client (assoc req :url url)))
32 36
         (client req)))))
33 37
 
34  
-;; Use Cheshire support within clj-http...
35  
-;; (defn wrap-json-response-conversion [client]
36  
-;;   "Automatically transforms the body of a response of a Facebook Graph API request from JSON to a Clojure
37  
-;;    data structure through the use of clojure.data.json. It checks if the header Content-Type
38  
-;;    is 'text/javascript' which the Facebook Graph API returns in the case of a JSON response."
39  
-;;   (fn [req]
40  
-;;     (let [{:keys [headers] :as resp} (client req)
41  
-;;           content-type (headers "content-type")]
42  
-;;       (if (and content-type
43  
-;;                (or
44  
-;;                 (.startsWith content-type "text/javascript")
45  
-;;                 (.startsWith content-type "application/json")))
46  
-;;         (assoc resp :body (read-json (:body resp)))
47  
-;;         resp))))
48  
-(defn has-facebook-auth[]
49  
-  "Returns whether there is an existing binding for facebook-auth"
50  
-  (auth/has-facebook-auth))
  38
+;; (defn has-facebook-auth
  39
+;;   "Returns whether there is an existing binding for facebook-auth"
  40
+;;   []
  41
+;;   (auth/has-facebook-auth))
51 42
 
52  
-(defn wrap-facebook-data-extractor [client]
  43
+(defn wrap-facebook-data-extractor 
53 44
   "The Facebook Graph API mostly returns a JSON document in the form like this one:
54 45
    {
55 46
       \"data\": [...]
@@ -63,10 +54,11 @@
63 54
   automatically triggers the pagination as you walk through the seq.
64 55
   This Ring-style middleware also supports to simply extract the body part of the request
65 56
   (':extract :body'). "
  57
+  [client]
66 58
   (fn [req]
67 59
     (let [{:keys [extract paging]} req
68  
-          response (client req)
69  
-          body (:body response)]
  60
+          response                 (client req)
  61
+          body                     (:body response)]
70 62
       (if extract
71 63
         (if (= :body extract)
72 64
           body
@@ -80,7 +72,8 @@
80 72
               extraction)))
81 73
         response))))
82 74
 
83  
-(defn wrap-fql [client]
  75
+(defn wrap-fql 
  76
+  [client]
84 77
   (fn [req]
85 78
     (let [{:keys [url fql]} req]
86 79
       (if (and (= url :fql))
@@ -101,18 +94,17 @@
101 94
          wrap-facebook-exceptions
102 95
          wrap-exceptions
103 96
          wrap-request-fn
104  
-         ;; wrap-oauth2
105  
-         ;; wrap-facebook-access-token
106  
-         ;; wrap-json-response-conversion
107 97
          wrap-facebook-url-builder
108 98
          wrap-facebook-data-extractor
109 99
          wrap-fql
110 100
          ))
111  
-  ([request] (wrap-request request client/wrap-request)))
  101
+  ([request] 
  102
+     (wrap-request request 
  103
+                   http/wrap-request)))
112 104
 
113 105
 (def
114 106
   request
115  
-  (wrap-request #'clj-http.core/request))
  107
+  (wrap-request #'http-core/request))
116 108
 
117 109
 (defn get
118 110
   "Like #'request, but sets the :method and :url as appropriate."
@@ -122,4 +114,5 @@
122 114
 (defn post
123 115
   "Like #'request, but sets the :method and :url as appropriate."
124 116
   [url & [req]]
125  
-  (request (merge req {:as :json :method :post :url url})))
  117
+  (request (merge req 
  118
+                  {:as :json :method :post :url url})))
28  src/clj_facebook_graph/error_handling.clj
@@ -17,13 +17,13 @@
17 17
  ^{:doc "A map with some interesting Facebook errors that are
18 18
  assigned to a keyword to identify the error easier. At the moment the
19 19
  map only include two errors which are relevant for authentication and authorisation."}
20  
- facebook-errors {
21  
-                  :OAuthException {"Error validating access token"
22  
-                                   :invalid-access-token
23  
-                                   "An access token is required to request this resource."
24  
-                                   :access-token-required
25  
-                                   }
26  
-                  })
  20
+ facebook-errors 
  21
+ {:OAuthException {"Error validating access token"
  22
+                   :invalid-access-token
  23
+                   "An access token is required to request this resource."
  24
+                   :access-token-required
  25
+                   }
  26
+  })
27 27
 
28 28
 (defn identify-facebook-error
29 29
   "Tries to identify the Facebook error in the response with the help
@@ -33,12 +33,14 @@
33 33
    error is returned in a form like this one:
34 34
 
35 35
    {:error [:OAuthException :invalid-access-token] :message \"Error validation access token\"}"
36  
-  [response] (let [{:keys [type message]} (get-in response [:body :error])
37  
-                   error-type (keyword type)]
38  
-               {:error
39  
-                [error-type (if-let [error (get-in facebook-errors [error-type message])]
40  
-                              error :unknown)]
41  
-                :message message}))
  36
+  [response] 
  37
+  (let [{:keys [type message]} (get-in response [:body :error])
  38
+        error-type             (keyword type)
  39
+        error-categorized      (if-let [error (get-in facebook-errors [error-type message])]
  40
+                                 error 
  41
+                                 :unknown)]
  42
+    {:error [error-type error-categorized]
  43
+     :message message}))
42 44
 
43 45
 (defn wrap-facebook-exceptions
44 46
   "The ring-style middleware to detect Facebook errors in the response of
28  src/clj_facebook_graph/helper.clj
@@ -9,14 +9,14 @@
9 9
 (ns clj-facebook-graph.helper
10 10
   "Some helper functions."
11 11
   (:use ;; [clojure.data.json :only [read-json read-json-from Read-JSON-From]]
12  
-        [clojure.java.io :only [reader]]
  12
+        ;; [clojure.java.io :only [reader]]
13 13
         [clj-http.client :only [unexceptional-status?]]
14 14
         [clj-facebook-graph.uri :only [make-uri]]
15 15
         [clojure.string :only [blank?]]
16 16
         ring.middleware.params
17  
-        [slingshot.slingshot :only [throw+ try+]])
18  
-  (:import
19  
-   (java.io PushbackReader ByteArrayInputStream InputStreamReader)))
  17
+        [slingshot.slingshot :only [throw+ try+]]))
  18
+  ;; (:import
  19
+  ;;  (java.io PushbackReader ByteArrayInputStream InputStreamReader)))
20 20
 
21 21
 (def facebook-base-url "https://graph.facebook.com")
22 22
 
@@ -29,8 +29,9 @@
29 29
 ;;                                       (ByteArrayInputStream. input)))
30 30
 ;;                     keywordize? eof-error? eof-value)))
31 31
 
32  
-(defn build-url [request]
  32
+(defn build-url 
33 33
   "Builds a URL string which corresponds to the information of the request."
  34
+  [request]
34 35
   (let [{:keys [server-port server-name uri query-params scheme]} request]
35 36
     (str (make-uri {:scheme (name scheme)
36 37
                     :host server-name
@@ -38,19 +39,22 @@
38 39
                     :path uri
39 40
                     :query query-params}))))
40 41
 
41  
-(defn wrap-exceptions [client]
  42
+(defn wrap-exceptions 
42 43
   "An alternative Ring-style middleware to the #'clj-http.core/wrap-exceptions. This
43 44
    one also extracts the body from the http response, which is very helpful to see
44 45
    the error description Facebook has returned as JSON document."
  46
+  [client]
45 47
   (fn [req]
46 48
     (let [{:keys [status body] :as resp} (client req)]
47  
-      (if (or (not (clojure.core/get req :throw-exceptions true))
  49
+      (if (or (not (clojure.core/get req 
  50
+                                     :throw-exceptions 
  51
+                                     true))
48 52
               (unexceptional-status? status))
49 53
         resp
50 54
         (throw+ (str "Status: " status " body: " (slurp body)))))))
51 55
 
52  
-(defn wrap-print-request-map [client]
53  
-  "Simply prints the request map to *out*."
54  
-  (fn [req]
55  
-    (println req)
56  
-    (client req)))
  56
+;; (defn wrap-print-request-map [client]
  57
+;;   "Simply prints the request map to *out*."
  58
+;;   (fn [req]
  59
+;;     (println req)
  60
+;;     (client req)))
119  src/clj_facebook_graph/ring_middleware.clj
... ...
@@ -1,119 +0,0 @@
1  
-; Copyright (c) Maximilian Weber. All rights reserved.
2  
-; The use and distribution terms for this software are covered by the
3  
-; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4  
-; which can be found in the file epl-v10.html at the root of this distribution.
5  
-; By using this software in any fashion, you are agreeing to be bound by
6  
-; the terms of this license.
7  
-; You must not remove this notice, or any other, from this software.
8  
-
9  
-(ns clj-facebook-graph.ring-middleware
10  
-  "Middleware for Ring to realize a simple Facebook authentication."
11  
-  (:use [clj-facebook-graph.helper :only [build-url]]
12  
-        [clj-facebook-graph.auth :only [with-facebook-auth]]
13  
-        [ring.util.response :only [redirect]]
14  
-        [ring.middleware.keyword-params]
15  
-        [slingshot.slingshot :only [throw+ try+]])
16  
-;;  (:import clj_facebook_graph.FacebookGraphException)
17  
-  )
18  
-
19  
-(defn add-facebook-auth
20  
-  "Adds the facebook-auth information (includes the access token) to the ring session."
21  
-  [session access-token]
22  
-  (assoc session :facebook-auth {:access-token access-token}))
23  
-
24  
-;; (defn wrap-facebook-extract-callback-code
25  
-;;   "Extracts the code from the request which the Facebook Graph API appends as query
26  
-;;    parameter in the case of an successful authentication to the redirect_uri. It's
27  
-;;    flexible so that every URL and not only the login URL of your web application
28  
-;;    can be the redirect_uri of the Facebook authentication process.
29  
-;;    A redirect is triggered to the redirect_uri with out the code query parameter
30  
-;;    to get rid of the 'code' parameter in the web browser of your user, thereby the
31  
-;;    access token is associated with the user's session.
32  
-;;    An example:
33  
-
34  
-;;    You have a path in your web application like:
35  
-;;    http://www.yourwebapp.com/albums/1511
36  
-
37  
-;;    which displays the albums of a Facebook friend with the id 1511 of your user
38  
-;;    (also a Facebook user). For this reason your application needs a Facebook access token
39  
-;;    on behalf of your user to get access to the albums of the user's friend.
40  
-;;    After the user has successfully logged into Facebook, Facebook redirects the user back
41  
-;;    to your specified redirect_uri (in this case 'http://www.yourwebapp.com/albums/1511')
42  
-;;    with a query parameter 'code' appended. This code can be used by your web application to
43  
-;;    get an Facebook access token. So here the web browser of your user comes back
44  
-;;    from Facebook to the following URL of your application:
45  
-
46  
-;;    http://www.yourwebapp.com/albums/1511?code=123-example-code
47  
-
48  
-;;    This middleware extracts the code and use it to receive an access token from Facebook. Then
49  
-;;    it triggers a redirect to:
50  
-
51  
-;;    http://www.yourwebapp.com/albums/1511
52  
-
53  
-;;    To get rid of the 'code' query parameter in the browser of your user. In the same step the
54  
-;;    received access token is associated with the user's session in your web application.
55  
-
56  
-;;    Pay attention that you have specified the correct :redirect-uri in your facebook-app-info
57  
-;;    otherwise this middleware can not detect if the request is a redirect for the Facebook
58  
-;;    authentication or just a request with a code query parameter.
59  
-;;    "
60  
-;;   [handler facebook-app-info callback-handler]
61  
-;;   (fn [request]
62  
-;;     (let [code (get-in request [:params "code"])
63  
-;;           callback-path (.getPath (java.net.URI. (:redirect-uri facebook-app-info)))]
64  
-;;       (if (and code (= callback-path (:uri request)))
65  
-;;         (let [params (:params ((wrap-keyword-params identity) request))
66  
-;;               access-token (get-access-token facebook-app-info
67  
-;;                                              params
68  
-;;                                              (get-in request [:session :facebook-auth-request]))
69  
-;;               session (add-facebook-auth (:session request) access-token)
70  
-;;               session (dissoc session :facebook-auth-request)
71  
-;;               return-to (:return-to session)
72  
-;;               session (dissoc session :return-to)]
73  
-;;           (if return-to
74  
-;;             (assoc (redirect return-to) :session session)
75  
-;;             (callback-handler (assoc request :session session))))
76  
-;;         (handler request)))))
77  
-
78  
-;; (defn wrap-facebook-access-token-required
79  
-;;   "If the middleware for Facebook access through clj-http throws a FacebookGraphException
80  
-;;    which was caused by an OAuthException error then this peace of Ring middleware triggers a
81  
-;;    redirect to the Facebook authentication page. The following OAuthException errors are
82  
-;;    handled at the moment:
83  
-;;      - :invalid-access-token - The access token which is used by your application is invalid,
84  
-;;                                mostly it is expired.
85  
-;;      - :access-token-required - At the moment your application has not been using an access
86  
-;;                                at all to do Facebook Graph API requests, so your user have to
87  
-;;                                do a Facebook login first. 
88  
-;;    See #'wrap-facebook-extract-callback-code for an example request flow.
89  
-;;    If you need a Facebook at any point of the Ring request processing, you can throw
90  
-;;    a FacebookGraphException with the following error: {:error :facebook-login-required}."
91  
-;;   [handler facebook-app-info]
92  
-;;   (let [auth-errors #{[:OAuthException :invalid-access-token]
93  
-;;                       [:OAuthException :access-token-required]}]
94  
-;;     (fn [request]
95  
-;;       (try+
96  
-;;         (handler request)
97  
-;;         (catch Object e
98  
-;;           (if (let [error (:error @e)] (or (auth-errors error)
99  
-;;                                            (= :facebook-login-required error)))
100  
-;;             (let [session (if (= :get (:request-method request))
101  
-;;                             (assoc (:session request) :return-to (build-url request))
102  
-;;                             (:session request))
103  
-;;                   redirect-uri (:uri (make-auth-request facebook-app-info))]
104  
-;;               ;; we might want to pass a second argument to make-auth-request here to prevent CSRF.
105  
-;;               ;; this would have to be stored in the session (see wrap-facebook-extract-callback-code)
106  
-;;               (assoc (redirect redirect-uri)
107  
-;;                 :session session))
108  
-;;             (throw+ e)))))))
109  
-
110  
-;; (defn wrap-facebook-auth [handler facebook-app-info login-path]
111  
-;;   "Binds the facebook-auth (access-token) information to the thread bounded *facebook-auth*
112  
-;;    variable (by using with-facebook-auth) so it can be used by the Ring-style middleware
113  
-;;    for clj-http to access the Facebook Graph API."
114  
-;;   (fn [request]
115  
-;;     (if (and (= :get (:request-method request)) (= login-path (:uri request)))
116  
-;;       (redirect (:uri (make-auth-request facebook-app-info)))
117  
-;;       (if-let [facebook-auth (get-in request [:session :facebook-auth])]
118  
-;;         (with-facebook-auth facebook-auth (handler request))
119  
-;;         (handler request)))))
78  src/clj_facebook_graph/uri.clj
... ...
@@ -1,11 +1,12 @@
1 1
 (ns clj-facebook-graph.uri
2  
-  (:require [clojure.string :as str]
3  
-            [clj-http.client :only parse-url :as http])
4  
-  (:import [java.net URI URLEncoder URLDecoder]))
  2
+  (:require [clojure.string :as str])
  3
+            ;; [clj-http.client :only parse-url :as http])
  4
+  (:import [java.net URI URLEncoder]))
5 5
 
6  
-(def url-encode)
  6
+(declare url-encode)
7 7
 
8  
-(defn form-url-encode [arg]
  8
+(defn form-url-encode 
  9
+  [arg]
9 10
   (str/join \& (map (fn [[k v]]
10 11
                       (if (vector? v)
11 12
                         (form-url-encode (map (fn [v] [k v]) v))
@@ -17,53 +18,58 @@
17 18
 ;; taken from https://github.com/marktriggs/clojure-http-client/blob/master/src/clojure/http/client.clj
18 19
 (defn url-encode
19 20
   "Wrapper around java.net.URLEncoder returning an UTF-8 URL encoded
20  
-representation of argument, either a string or map."
  21
+   representation of argument, either a string or map."
21 22
   [arg]
22 23
   (if (map? arg)
23 24
     (form-url-encode arg)
24 25
     (URLEncoder/encode (str arg) "UTF-8")))
25 26
 
26  
-(defn url-decode [str]
27  
-  (URLDecoder/decode str "UTF-8"))
  27
+;; (defn url-decode [str]
  28
+;;   (URLDecoder/decode str "UTF-8"))
28 29
 
29  
-(defn form-url-decode [str]
30  
-  (into {}
31  
-        (map (fn [p] (vector (keyword (first p)) (second p)))
32  
-             (map (fn [s] (map url-decode (str/split s #"=")))
33  
-                  (str/split str #"&")))))
  30
+;; (defn form-url-decode [str]
  31
+;;   (into {}
  32
+;;         (map (fn [p] (vector (keyword (first p)) (second p)))
  33
+;;              (map (fn [s] (map url-decode (str/split s #"=")))
  34
+;;                   (str/split str #"&")))))
34 35
 
35  
-(defmacro uri-as-map [uri]
36  
-  `(hash-map
37  
-    ~@(mapcat
38  
-       (fn [[key getter]]
39  
-         `(~key (. ~uri ~getter)))
40  
-       '((:scheme getScheme)
41  
-         (:user-info getUserInfo)
42  
-         (:host getHost)
43  
-         (:port getPort)
44  
-         (:path getPath)
45  
-         (:query getQuery)
46  
-         (:fragment getFragment)))))
  36
+;; (defmacro uri-as-map [uri]
  37
+;;   `(hash-map
  38
+;;     ~@(mapcat
  39
+;;        (fn [[key getter]]
  40
+;;          `(~key (. ~uri ~getter)))
  41
+;;        '((:scheme getScheme)
  42
+;;          (:user-info getUserInfo)
  43
+;;          (:host getHost)
  44
+;;          (:port getPort)
  45
+;;          (:path getPath)
  46
+;;          (:query getQuery)
  47
+;;          (:fragment getFragment)))))
47 48
 
48  
-(defn parse-uri
49  
-  ([uri] (parse-uri uri true))
50  
-  ([uri form-url-decode-query?]
51  
-     (let [uri (uri-as-map (URI. uri))]
52  
-       (if (and form-url-decode-query? (:query uri))
53  
-         (assoc uri :query (form-url-decode (:query uri)))
54  
-         uri))))
  49
+;; (defn parse-uri
  50
+;;   ([uri] 
  51
+;;      (parse-uri uri true))
  52
+;;   ([uri form-url-decode-query?]
  53
+;;      (let [uri (uri-as-map (URI. uri))]
  54
+;;        (if (and form-url-decode-query? 
  55
+;;                 (:query uri))
  56
+;;          (assoc uri :query (form-url-decode (:query uri)))
  57
+;;          uri))))
55 58
 
56  
-(defn format* [str a b]
  59
+(defn format* 
  60
+  [str a b]
57 61
   (if b (format str a b) a))
58 62
 
59  
-(defn make-uri [{:keys [scheme user-info host port path fragment query]}]
  63
+(defn make-uri 
  64
+  [{:keys [scheme user-info host port path fragment query]}]
60 65
   (let [uri (format* "%2$s://%1$s" "" scheme)
61 66
         uri (format* "%s%s@" uri user-info)
62 67
         uri (format* "%s%s" uri host)
63  
-        uri (format* "%s:%d" uri (and (not (= -1 port)) port))
  68
+        uri (format* "%s:%d" uri (and (not (= -1 port)) 
  69
+                                      port))
64 70
         uri (format* "%s%s" uri path)
65 71
         uri (format* "%s?%s" uri (and query (if (map? query)
66 72
                                               (url-encode query)
67 73
                                               query)))
68 74
         uri (format* "%s#%s" uri fragment)]
69  
-    (URI. uri)))
  75
+    (URI. uri)))

0 notes on commit 2dd53db

Please sign in to comment.
Something went wrong with that request. Please try again.