Browse Source

notehub API made hostname-URL independent

master
Christian Müller 11 years ago
parent
commit
a431d571ce
  1. 2
      API.md
  2. 32
      src/notehub/api.clj
  3. 6
      src/notehub/handler.clj
  4. 5
      test/notehub/test/api.clj

2
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. 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).
## <a name="registration"></a>NoteHub API Access Request ## <a name="registration"></a>NoteHub API Access Request
To register as a publisher and gain access to NoteHub API, please <a href="mailto:notehub@icloud.com?subject=NoteHub API Access Request&body=Please add [a] desired PID as a 16 char string [b] your contact information, [c] short usage explanation and [d] the URL of the resource or it's website.">send</a> 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. To register as a publisher and gain access to NoteHub API, please <a href="mailto:notehub@icloud.com?subject=NoteHub API Access Request&body=Please add [a] desired PID as a 16 char string [b] your contact information, [c] short usage explanation and [d] the URL of the resource or it's website.">send</a> 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.

32
src/notehub/api.clj

@ -13,12 +13,6 @@
(def version "1.4") (def version "1.4")
(def domain
(get-setting
(if (get-setting :dev-mode)
:dev-domain
:prod-domain)))
(defn log (defn log
"Logs args to the server stdout" "Logs args to the server stdout"
[string & args] [string & args]
@ -51,13 +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 [token & [description]] (defn- get-path [host-url token & [description]]
(if (= :url description) (if (= :url description)
(str domain "/" token) (str host-url "/" token)
(let [[year month day title] (split token #"/")] (let [[year month day title] (split token #"/")]
(if description (if description
(str domain "/" (storage/create-short-url token {:year year :month month :day day :title title})) (str host-url "/" (storage/create-short-url token {:year year :month month :day day :title title}))
(str domain (url year month day title)))))) (str host-url (url year month day title))))))
(defn version-manager [f params] (defn version-manager [f params]
(if-let [req-version (:version params)] (if-let [req-version (:version params)]
@ -68,21 +62,21 @@
(f params))) (f params)))
{:status (create-response false "API version expected")})) {:status (create-response false "API version expected")}))
(defn get-note [{:keys [noteID]}] (defn get-note [{:keys [noteID hostURL]}]
(if (storage/note-exists? noteID) (if (storage/note-exists? noteID)
(let [note (storage/get-note noteID)] (let [note (storage/get-note noteID)]
(storage/increment-note-view noteID) (storage/increment-note-view noteID)
{:note note {:note note
:title (derive-title note) :title (derive-title note)
:longURL (get-path noteID) :longURL (get-path hostURL noteID)
:shortURL (get-path noteID :id) :shortURL (get-path hostURL 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)})
{:status (create-response false "noteID '%s' unknown" noteID)})) {:status (create-response false "noteID '%s' unknown" noteID)}))
(defn post-note (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}) ;(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")
@ -101,8 +95,8 @@
(map #(str proposed-title "-" (+ 2 %)) (range))))) (map #(str proposed-title "-" (+ 2 %)) (range)))))
noteID (build-key year month day title) noteID (build-key year month day title)
new-params (assoc params :year year :month month :day day :title 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) short-url (get-path hostURL (storage/create-short-url noteID new-params) :url)
long-url (get-path noteID)] long-url (get-path hostURL noteID)]
(do (do
(storage/add-note noteID note pid password) (storage/add-note noteID note pid password)
{:noteID noteID {:noteID noteID
@ -112,7 +106,7 @@
{:status (create-response false (first errors))}))) {: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}) ;(log "update-note: %s" {:pid pid :noteID noteID :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")
@ -123,7 +117,7 @@
(if (empty? errors) (if (empty? errors)
(do (do
(storage/edit-note noteID note) (storage/edit-note noteID note)
{:longURL (get-path noteID) {:longURL (get-path hostURL noteID)
:shortURL (get-path noteID :id) :shortURL (get-path hostURL noteID :id)
:status (create-response true)}) :status (create-response true)})
{:status (create-response false (first errors))}))) {:status (create-response false (first errors))})))

6
src/notehub/handler.clj

@ -143,13 +143,17 @@
(def app (def app
(let [handler (handler/site app-routes)] (let [handler (handler/site app-routes)]
(fn [request] (fn [request]
(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) (if (get-setting :dev-mode)
(handler request) (handler request)
(try (handler request) (try (handler request)
(catch Exception e (catch Exception e
(do (do
;TODO (log e) ;TODO (log e)
(response 500)))))))) (response 500)))))))))
(defn -main [& [port]] (defn -main [& [port]]
(jetty/run-jetty #'app (jetty/run-jetty #'app

5
test/notehub/test/api.clj

@ -11,7 +11,7 @@
(def pid2 "somePlugin2") (def pid2 "somePlugin2")
(def note-title (let [[y m d] (get-date)] (def note-title (let [[y m d] (get-date)]
(apply str (interpose "/" [y m d "hello-world"])))) (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)))) (defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(defmacro isnt [arg] `(is (not ~arg))) (defmacro isnt [arg] `(is (not ~arg)))
@ -58,8 +58,7 @@
(is (storage/note-exists? (:noteID post-response))) (is (storage/note-exists? (:noteID post-response)))
(let [su (last (clojure.string/split (:shortURL get-response) #"/"))] (let [su (last (clojure.string/split (:shortURL get-response) #"/"))]
(is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su))))) (is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su)))))
(let [resp (send-request (let [resp (send-request (:shortURL get-response))
(clojure.string/replace (:shortURL get-response) domain ""))
resp (send-request ((:headers resp) "Location"))] resp (send-request ((:headers resp) "Location"))]
(is (substring? "hello world"(:body resp)))) (is (substring? "hello world"(:body resp))))
(is (= (:publisher get-response) pid)) (is (= (:publisher get-response) pid))

Loading…
Cancel
Save