From a431d571ce210029091f79237af4dd655c4c2996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Sat, 4 Oct 2014 09:55:18 +0200 Subject: [PATCH] notehub API made hostname-URL independent --- API.md | 2 +- src/notehub/api.clj | 32 +++++++++++++------------------- src/notehub/handler.clj | 8 ++++++-- test/notehub/test/api.clj | 5 ++--- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/API.md b/API.md index 2624631..81971a1 100644 --- a/API.md +++ b/API.md @@ -18,7 +18,7 @@ A PID is a string chosen by the publisher and cannot be longer than 16 character All API requests must be issued with one special parameter `version` denoting the expected version of the API as a string, e.g. `1.0` (see examples below). You should always put the version of this document as a `version` parameter. -Once you obtained your PSK, you can test the API [here](http://www.notehub.org/api-test.html). +Once you obtained your PSK, you can test the API [here](/api-test.html). ## NoteHub API Access Request To register as a publisher and gain access to NoteHub API, please send an email with the following information about you: the desired PID, your contact information, a short description of what you want to do and an URL of the resource where the API will be used or its website. diff --git a/src/notehub/api.clj b/src/notehub/api.clj index 752a60b..17e950c 100644 --- a/src/notehub/api.clj +++ b/src/notehub/api.clj @@ -13,12 +13,6 @@ (def version "1.4") -(def domain - (get-setting - (if (get-setting :dev-mode) - :dev-domain - :prod-domain))) - (defn log "Logs args to the server stdout" [string & args] @@ -51,13 +45,13 @@ ([success message & params] (assoc (create-response success) :message (apply format message params)))) -(defn- get-path [token & [description]] +(defn- get-path [host-url token & [description]] (if (= :url description) - (str domain "/" token) + (str host-url "/" token) (let [[year month day title] (split token #"/")] (if description - (str domain "/" (storage/create-short-url token {:year year :month month :day day :title title})) - (str domain (url year month day title)))))) + (str host-url "/" (storage/create-short-url token {:year year :month month :day day :title title})) + (str host-url (url year month day title)))))) (defn version-manager [f params] (if-let [req-version (:version params)] @@ -68,21 +62,21 @@ (f params))) {:status (create-response false "API version expected")})) -(defn get-note [{:keys [noteID]}] +(defn get-note [{:keys [noteID hostURL]}] (if (storage/note-exists? noteID) (let [note (storage/get-note noteID)] (storage/increment-note-view noteID) {:note note :title (derive-title note) - :longURL (get-path noteID) - :shortURL (get-path noteID :id) + :longURL (get-path hostURL noteID) + :shortURL (get-path hostURL noteID :id) :statistics (storage/get-note-statistics noteID) :status (create-response true) :publisher (storage/get-publisher noteID)}) {:status (create-response false "noteID '%s' unknown" noteID)})) (defn post-note - [{:keys [note pid signature password] :as params}] + [{:keys [note pid signature password hostURL] :as params}] ;(log "post-note: %s" {:pid pid :signature signature :password password :note note}) (let [errors (filter identity [(when-not (storage/valid-publisher? pid) "pid invalid") @@ -101,8 +95,8 @@ (map #(str proposed-title "-" (+ 2 %)) (range))))) noteID (build-key year month day title) new-params (assoc params :year year :month month :day day :title title) - short-url (get-path (storage/create-short-url noteID new-params) :url) - long-url (get-path noteID)] + short-url (get-path hostURL (storage/create-short-url noteID new-params) :url) + long-url (get-path hostURL noteID)] (do (storage/add-note noteID note pid password) {:noteID noteID @@ -112,7 +106,7 @@ {:status (create-response false (first errors))}))) -(defn update-note [{:keys [noteID note pid signature password]}] +(defn update-note [{:keys [noteID note pid signature password hostURL]}] ;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note}) (let [errors (filter identity [(when-not (storage/valid-publisher? pid) "pid invalid") @@ -123,7 +117,7 @@ (if (empty? errors) (do (storage/edit-note noteID note) - {:longURL (get-path noteID) - :shortURL (get-path noteID :id) + {:longURL (get-path hostURL noteID) + :shortURL (get-path hostURL noteID :id) :status (create-response true)}) {:status (create-response false (first errors))}))) diff --git a/src/notehub/handler.clj b/src/notehub/handler.clj index ff72417..5b691cb 100644 --- a/src/notehub/handler.clj +++ b/src/notehub/handler.clj @@ -143,13 +143,17 @@ (def app (let [handler (handler/site app-routes)] (fn [request] - (if (get-setting :dev-mode) + (let [{:keys [scheme server-name server-port]} request + hostURL (str (name scheme) "://" server-name + (when (not= 80 server-port) (str ":" server-port))) + request (assoc-in request [:params :hostURL] hostURL)] + (if (get-setting :dev-mode) (handler request) (try (handler request) (catch Exception e (do ;TODO (log e) - (response 500)))))))) + (response 500))))))))) (defn -main [& [port]] (jetty/run-jetty #'app diff --git a/test/notehub/test/api.clj b/test/notehub/test/api.clj index f2b6788..5f7310f 100644 --- a/test/notehub/test/api.clj +++ b/test/notehub/test/api.clj @@ -11,7 +11,7 @@ (def pid2 "somePlugin2") (def note-title (let [[y m d] (get-date)] (apply str (interpose "/" [y m d "hello-world"])))) -(def note-url (str (apply str domain "/" (interpose "/" (get-date))) "/hello-world")) +(def note-url (str (apply str "/" (interpose "/" (get-date))) "/hello-world")) (defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b)))) (defmacro isnt [arg] `(is (not ~arg))) @@ -58,8 +58,7 @@ (is (storage/note-exists? (:noteID post-response))) (let [su (last (clojure.string/split (:shortURL get-response) #"/"))] (is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su))))) - (let [resp (send-request - (clojure.string/replace (:shortURL get-response) domain "")) + (let [resp (send-request (:shortURL get-response)) resp (send-request ((:headers resp) "Location"))] (is (substring? "hello world"(:body resp)))) (is (= (:publisher get-response) pid))