Browse Source

API v1.2

master
Christian Mueller 12 years ago
parent
commit
6cc030e855
  1. 3
      API.md
  2. 27
      src/NoteHub/api.clj
  3. 12
      src/NoteHub/views/pages.clj
  4. 27
      test/NoteHub/test/api.clj

3
API.md

@ -1,9 +1,10 @@
# NoteHub API # NoteHub API
**Version 1.1, status: released.** **Version 1.2, status: released.**
## Changelog ## Changelog
- **V1.2**: Theme & fonts can be specified at the creation.
- **V1.1**: fields `publisher` and `title` in the response to the note retrieval. - **V1.1**: fields `publisher` and `title` in the response to the note retrieval.
- **V1.0**: initial release. - **V1.0**: initial release.

27
src/NoteHub/api.clj

@ -45,11 +45,13 @@
([success message & params] ([success message & params]
(assoc (create-response success) :message (apply format message params)))) (assoc (create-response success) :message (apply format message params))))
(defn- get-path [noteID & description] (defn- get-path [token & [description]]
(let [[year month day title] (split noteID #" ")] (if (= :url description)
(str domain "/" token)
(let [[year month day title] (split token #" ")]
(if description (if description
(str domain "/" (storage/create-short-url noteID {:year year :month month :day day :title title})) (str domain "/" (storage/create-short-url token {:year year :month month :day day :title title}))
(apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)]))))) (apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)]))))))
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")] (let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
(defn get-signature (defn get-signature
@ -66,15 +68,15 @@
{:note note {:note note
:title (derive-title note) :title (derive-title note)
:longURL (get-path noteID) :longURL (get-path noteID)
:shortURL (get-path noteID :short) :shortURL (get-path noteID :id)
:statistics (storage/get-note-statistics noteID) :statistics (storage/get-note-statistics noteID)
:status (create-response true) :status (create-response true)
:publisher (storage/get-publisher noteID)}) :publisher (storage/get-publisher noteID)})
(create-response false "noteID '%s' unknown" noteID))) (create-response false "noteID '%s' unknown" noteID)))
(defn post-note (defn post-note
([note pid signature] (post-note note pid signature nil)) ([note pid signature] (post-note note pid signature {}))
([note pid signature password] ([note pid signature opts]
;(log "post-note: %s" {:pid pid :signature signature :password password :note note}) ;(log "post-note: %s" {:pid pid :signature signature :password password :note note})
(let [errors (filter identity (let [errors (filter identity
[(when-not (storage/valid-publisher? pid) "pid invalid") [(when-not (storage/valid-publisher? pid) "pid invalid")
@ -82,7 +84,9 @@
"signature invalid") "signature invalid")
(when (blank? note) "note is empty")])] (when (blank? note) "note is empty")])]
(if (empty? errors) (if (empty? errors)
(let [[year month day] (get-date) (let [[year month day] (map str (get-date))
password (opts :password)
params (opts :params {})
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
(-> note split-lines first (sreplace " " "-") lower-case)) (-> note split-lines first (sreplace " " "-") lower-case))
trim (fn [s] (apply str (drop-while #(= \- %) s))) trim (fn [s] (apply str (drop-while #(= \- %) s)))
@ -94,12 +98,13 @@
(cons proposed-title (cons proposed-title
(map #(str proposed-title "-" (+ 2 %)) (range))))) (map #(str proposed-title "-" (+ 2 %)) (range)))))
noteID (build-key date title) noteID (build-key date title)
short-url (storage/create-short-url noteID {:year year :month month :day day :title title})] new-params (assoc params :year year :month month :day day :title title)
short-url (storage/create-short-url noteID new-params)]
(do (do
(storage/add-note noteID note pid password) (storage/add-note noteID note pid password)
{:noteID noteID {:noteID noteID
:longURL (get-path noteID) :longURL (get-path noteID)
:shortURL (get-path noteID :short) :shortURL (get-path short-url :url)
:status (create-response true)})) :status (create-response true)}))
{:status (create-response false (first errors))})))) {:status (create-response false (first errors))}))))
@ -116,6 +121,6 @@
(do (do
(storage/edit-note noteID note) (storage/edit-note noteID note)
{:longURL (get-path noteID) {:longURL (get-path noteID)
:shortURL (get-path noteID :short) :shortURL (get-path noteID :id)
:status (create-response true)}) :status (create-response true)})
{:status (create-response false (first errors))}))) {:status (create-response false (first errors))})))

12
src/NoteHub/views/pages.clj

@ -158,7 +158,7 @@
(let [pid "NoteHub" (let [pid "NoteHub"
psk (storage/get-psk pid)] psk (storage/get-psk pid)]
(if (storage/valid-publisher? pid) (if (storage/valid-publisher? pid)
(let [resp (api/post-note note pid (api/get-signature pid psk note) password)] (let [resp (api/post-note note pid (api/get-signature pid psk note) {:password password})]
(if (and (if (and
(storage/invalidate-session session) (storage/invalidate-session session)
(get-in resp [:status :success])) (get-in resp [:status :success]))
@ -188,8 +188,14 @@
(defpage [:get "/api/note"] {:keys [version noteID]} (defpage [:get "/api/note"] {:keys [version noteID]}
(generate-string (api/get-note noteID))) (generate-string (api/get-note noteID)))
(defpage [:post "/api/note"] {:keys [version note pid signature password]} (defpage [:post "/api/note"] {:keys [version note pid signature password] :as params}
(generate-string (api/post-note note pid signature password))) (generate-string
(api/post-note
note
pid
signature
{:params (dissoc params :version :note :pid :signature :password)
:password password})))
(defpage [:put "/api/note"] {:keys [version noteID note pid signature password]} (defpage [:put "/api/note"] {:keys [version noteID note pid signature password]}
(generate-string (api/update-note noteID note pid signature password))) (generate-string (api/update-note noteID note pid signature password)))

27
test/NoteHub/test/api.clj

@ -73,7 +73,7 @@
(isnt (:success (:status response))) (isnt (:success (:status response)))
(is (= (:message (:status response)) "pid invalid")))) (is (= (:message (:status response)) "pid invalid"))))
(testing "note update" (testing "note update"
(let [post-response (post-note note pid (get-signature pid psk note) "passwd") (let [post-response (post-note note pid (get-signature pid psk note) {:password "passwd"})
note-id (:noteID post-response) note-id (:noteID post-response)
new-note "a new note!"] new-note "a new note!"]
(is (:success (:status post-response))) (is (:success (:status post-response)))
@ -112,7 +112,30 @@
(storage/delete-note noteID) (storage/delete-note noteID)
(not (storage/note-exists? noteID))))))) (not (storage/note-exists? noteID)))))))
(deftest note-update
(deftest api-note-creation-with-params
(testing "Note creation with params"
(let [response (send-request [:post "/api/note"]
{:note note
:pid pid
:signature (get-signature pid psk note)
:version "1.0"
:theme "dark"
:text-font "Helvetica"})
body (parse-string (:body response))
noteID (body "noteID")]
(let [url ((:headers
(send-request
(str "/"
(last (clojure.string/split
((parse-string (:body response)) "shortURL") #"/"))))) "Location")]
(substring? "theme=dark" url)
(substring? "text-font=Felvetica" url))
(is (do
(storage/delete-note noteID)
(not (storage/note-exists? noteID)))))))
(deftest api-note-update
(let [response (send-request [:post "/api/note"] (let [response (send-request [:post "/api/note"]
{:note note {:note note
:pid pid :pid pid

Loading…
Cancel
Save