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. 8
      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 @@ -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).
## <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.

32
src/notehub/api.clj

@ -13,12 +13,6 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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))})))

8
src/notehub/handler.clj

@ -143,13 +143,17 @@ @@ -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

5
test/notehub/test/api.clj

@ -11,7 +11,7 @@ @@ -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 @@ @@ -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))

Loading…
Cancel
Save