Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the /secured/preferences endpoint.

  • Loading branch information...
commit 3786191dbf02a7ff68c13253b7ebb7ea09aeb14e 1 parent 3bd588e
@johnworth johnworth authored
View
32 README.markdown
@@ -2614,6 +2614,38 @@ $ curl -XDELETE http://by-tor:8888/secured/sessions?proxyToken=$(cas-ticket) | p
An attempt to remove session data that doesn't already exist will be silently
ignored.
+### Saving User Preferences
+
+Secured Endpoint: POST /secured/preferences
+
+This service can be used to save arbitrary user preferences. The POST
+body is stored without modification and can be retrieved by sending a GET
+request to the same URL.
+
+Example:
+
+```
+$ curl -sd data http://by-tor:8888/secured/preferences?proxyToken=$(cas-ticket)
+data
+```
+
+### Removing User Preferences
+
+Secured Endpoint: DELETE /secured/preferences
+
+This service can be used to remove a user's preferences.
+
+Example:
+
+```
+$ curl -X DELETE http://by-tor:8888/secured/preferences?proxyToken=$(cas-ticket)
+{
+ "success" : true
+}
+```
+
+An attempt to remove preference data that doesn't already exist will be silently ignored.
+
### Listing Collaborators
Secured Endpoint: GET /secured/collaborators
View
4 src/donkey/config.clj
@@ -155,6 +155,10 @@
"The bucket in Riak to retrieve user sessions from."
"donkey.sessions.bucket")
+(STR riak-prefs-bucket
+ "The bucket in Riak to retrieve user preferences from."
+ "donkey.preferences.bucket")
+
(STR userinfo-base-url
"The base URL for the user info API."
"donkey.userinfo.base-url")
View
10 src/donkey/core.clj
@@ -9,6 +9,7 @@
[donkey.user-attributes]
[donkey.user-info]
[donkey.user-sessions]
+ [donkey.user-prefs]
[ring.middleware keyword-params nested-params])
(:require [compojure.route :as route]
[compojure.handler :as handler]
@@ -90,6 +91,15 @@
(DELETE "/sessions" []
(trap #(remove-session)))
+ (GET "/preferences" []
+ (trap #(user-prefs)))
+
+ (POST "/preferences" [:as {body :body}]
+ (trap #(user-prefs (slurp body))))
+
+ (DELETE "/preferences" []
+ (trap #(remove-prefs)))
+
(GET "/user-search/:search-string" [search-string :as req]
(trap #(user-search search-string (get-in req [:headers "range"]))))
View
56 src/donkey/user_prefs.clj
@@ -0,0 +1,56 @@
+(ns donkey.user-prefs
+ (:use [slingshot.slingshot :only [try+ throw+]]
+ [clojure-commons.error-codes]
+ [donkey.config]
+ [donkey.service]
+ [donkey.user-attributes])
+ (:require [clj-http.client :as cl]
+ [clojure.string :as string]
+ [clojure.tools.logging :as log]
+ [clojure-commons.file-utils :as ft]))
+
+(defn- key-url
+ [user]
+ (str
+ (string/join "/"
+ (map ft/rm-last-slash [(riak-base-url) (riak-prefs-bucket) user]))
+ "?returnbody=true"))
+
+(defn- request-failed
+ "Throws an exception for a failed request."
+ [resp]
+ (throw+ {:error_code ERR_REQUEST_FAILED
+ :body (:body resp)}))
+
+(defn user-prefs
+ ([]
+ (let [user (:username current-user)]
+ (log/debug (str "user-prefs: GET " (key-url user)))
+ (let [resp (cl/get (key-url user) {:throw-exceptions false})]
+ (cond
+ (= 200 (:status resp)) (:body resp)
+ (= 404 (:status resp)) "{}"
+ :else (request-failed resp)))))
+
+ ([new-prefs]
+ (let [user (:username current-user)]
+ (log/debug (str "user-prefs: POST " (key-url user) " " new-prefs))
+ (let [resp (cl/post
+ (key-url user)
+ {:content-type :json :body new-prefs}
+ {:throw-exceptions false})]
+ (cond
+ (= 200 (:status resp)) (:body resp)
+ :else (request-failed resp))))))
+
+(defn remove-prefs
+ "Removes user session information from the Riak cluster."
+ []
+ (let [user (:username current-user)
+ url (key-url user)
+ _ (log/debug "user-prefs: DELETE" url)
+ resp (cl/delete url {:throw-exceptions false})
+ status (:status resp)]
+ (cond (= 404 (:status resp)) (success-response)
+ (<= 200 (:status resp) 299) (success-response)
+ :else (request-failed resp))))
Please sign in to comment.
Something went wrong with that request. Please try again.