Permalink
Browse files

Return reasonable status codes.

  • Loading branch information...
1 parent a82c611 commit 8795dc57dd3ea140bf6eb9710b6ba768bbf320be @Raynes committed Jan 5, 2012
Showing with 61 additions and 52 deletions.
  1. +17 −2 src/refheap/models/api.clj
  2. +44 −50 src/refheap/views/api.clj
@@ -1,6 +1,7 @@
(ns refheap.models.api
(:require [somnium.congomongo :as mongo]
- [refheap.models.users :as users])
+ [refheap.models.users :as users]
+ [noir.response :as response])
(:import java.util.UUID))
(defn gen-token
@@ -51,4 +52,18 @@
(case s
"true" true
"false" false
- false))
+ false))
+
+(defn add-status [status resp]
+ (assoc resp :status status))
+
+(defn error [msg] {:error msg})
+
+(defn response [type & [data]]
+ (case type
+ :bad (add-status 400 (response/json (error data)))
+ :unprocessable (add-status 422 (response/json (error data)))
+ :created (add-status 201 (response/json data))
+ :no-content {:status 204}
+ :not-found (add-status 404 (response/json data))
+ :ok (response/json data)))
@@ -2,8 +2,7 @@
(:use [noir.core :only [defpage]]
[refheap.views.common :only [layout]]
[hiccup.page-helpers :only [link-to]])
- (:require [noir.response :as response]
- [noir.session :as session]
+ (:require [noir.session :as session]
[refheap.models.api :as api]
[refheap.models.paste :as paste]
[refheap.models.users :as users]))
@@ -37,57 +36,52 @@
(defpage [:post "/api/paste"] {:keys [private contents language username token]
:or {private "false"}}
- (response/json
- (let [user (api/validate-user username token)]
- (if (string? user)
- {:error user}
- (let [paste (paste/paste language contents (api/string->bool private) user)]
- (if (string? paste)
- {:error paste}
- (api/process-paste paste)))))))
+ (let [user (api/validate-user username token)]
+ (if (string? user)
+ (api/response :unprocessable user)
+ (let [paste (paste/paste language contents (api/string->bool private) user)]
+ (if (string? paste)
+ (api/response :bad paste)
+ (api/response :created (api/process-paste paste)))))))
(defpage [:post "/api/paste/:id"] {:keys [id private contents language username token]}
- (response/json
- (if-let [paste (paste/get-paste id)]
- (let [user (api/validate-user username token)]
- (cond
- (string? user) {:error "Username or token is incorrect."}
- (nil? (:user paste)) {:error "You can't edit anonymous pastes."}
- (nil? user) {:error "You must be authenticated to edit pastes."}
- (not= (:id user) (:user paste)) {:error "You can only edit pastes that you own."}
- :else (let [paste (paste/update-paste paste
- (or language (:language paste))
- (or contents (:raw-contents paste))
- (if (nil? private)
- (:private paste)
- (api/string->bool private))
- user)]
- (if (string? paste)
- {:error paste}
- (api/process-paste paste)))))
- {:error "Paste does not exist."})))
+ (if-let [paste (paste/get-paste id)]
+ (let [user (api/validate-user username token)]
+ (cond
+ (string? user) (api/response :unprocessable user)
+ (nil? (:user paste)) (api/response :unprocessable "You can't edit anonymous pastes.")
+ (nil? user) (api/response :unprocessable "You must be authenticated to edit pastes.")
+ (not= (:id user) (:user paste)) (api/response :unprocessable "You can only edit pastes that you own.")
+ :else (let [paste (paste/update-paste paste
+ (or language (:language paste))
+ (or contents (:raw-contents paste))
+ (if (nil? private)
+ (:private paste)
+ (api/string->bool private))
+ user)]
+ (if (string? paste)
+ (api/response :bad paste)
+ (api/response :ok (api/process-paste paste))))))
+ (api/response :not-found "Paste does not exist.")))
(defpage [:delete "/api/paste/:id"] {:keys [id username token]}
- (let [resp (if-let [paste (paste/get-paste id)]
- (let [user (api/validate-user username token)]
- (cond
- (string? user) {:error user}
- (nil? (:user paste)) {:error "You can't delete anonymous pastes."}
- (nil? user) {:error "You must be authenticated to delete pastes."}
- (not= (:id user) (:user paste)) {:error "You can only delete pastes that you own."}
- :else (paste/delete-paste id)))
- {:error "Paste doesn't exist."})]
- (if (map? resp)
- (response/json resp)
- (response/empty))))
+ (if-let [paste (paste/get-paste id)]
+ (let [user (api/validate-user username token)]
+ (cond
+ (string? user) (api/response :unprocessable user)
+ (nil? (:user paste)) (api/response :unprocessable "You can't delete anonymous pastes.")
+ (nil? user) (api/response :unprocessable "You must be authenticated to delete pastes.")
+ (not= (:id user) (:user paste))
+ (api/response :unprocessable "You can only delete pastes that you own.")
+ :else (api/response :no-content (paste/delete-paste id))))
+ (api/response :not-found "Paste doesn't exist.")))
(defpage "/api/paste/:id" {:keys [id username token]}
- (response/json
- (if-let [paste (paste/get-paste id)]
- (let [user (api/validate-user username token)]
- (cond
- (string? user) {:error user}
- (and (:private paste) (not= (:id user) (:user paste)))
- {:error "You can't see private pastes that you don't own."}
- :else (api/process-paste paste)))
- {:error "Paste does not exist."})))
+ (if-let [paste (paste/get-paste id)]
+ (let [user (api/validate-user username token)]
+ (cond
+ (string? user) (api/response :unprocessable user)
+ (and (:private paste) (not= (:id user) (:user paste)))
+ (api/response :unprocessable "You can't see private pastes that you don't own.")
+ :else (api/response :ok (api/process-paste paste))))
+ (api/response :not-found "Paste does not exist.")))

0 comments on commit 8795dc5

Please sign in to comment.