Permalink
Browse files

change api-key and wait-response

  • Loading branch information...
1 parent ac0ea6b commit eab39ced88d02cb9b53300ec68176cd29565cb93 @siscia committed May 25, 2012
Showing with 53 additions and 44 deletions.
  1. +30 −30 src/echonest_api/core.clj
  2. +23 −14 test/echonest_api/test/core.clj
View
@@ -29,59 +29,59 @@
(throw (Exception. (str "Error #: " code-echo ": " (-> body :response :status :message)))))))
(defn basic-query
- [category paramaters & {:keys [query wait-response] :or {wait-response @*wait-response*}}]
- (letfn [(inner [_]
- (let [url (str url-base category "/" paramaters "?")
- query (conj {"api_key" @*api-key*} query)]
- (with-open [client (client/create-client)]
- (let [response (client/GET client url :query query)]
- (client/await response)))))]
+ [category paramaters & {:keys [query wait-response] :or {wait-response *wait-response*}}]
+ (let [inner
+ #(let [url (str url-base category "/" paramaters "?")
+ query (conj {"api_key" *api-key*} query)]
+ (with-open [client (client/create-client)]
+ (let [response (client/GET client url :query query)]
+ (client/await response))))]
(if wait-response
- (inner nil)
- (send (agent nil) inner))))
+ (inner)
+ (future-call inner))))
-(defn upload-song [path-to-song & {:keys [query wait-response] :or {wait-response @*wait-response*}}]
- (letfn [(inner [_]
- (let [url (str url-base "track/upload?")
- queri (conj {"api_key" @*api-key*} query)]
- (with-open [client (client/create-client)]
- (let [response (client/POST client url :query queri :body (File. path-to-song) :headers {"Content-Type" "application/octet-stream"} :timeout -1)]
- (client/await response)))))]
+(defn upload-song [path-to-song & {:keys [query wait-response] :or {wait-response *wait-response*}}]
+ (let [inner
+ #(let [url (str url-base "track/upload?")
+ queri (conj {"api_key" *api-key*} query)]
+ (with-open [client (client/create-client)]
+ (let [response (client/POST client url :query queri :body (File. path-to-song) :headers {"Content-Type" "application/octet-stream"} :timeout -1)]
+ (client/await response))))]
(if wait-response
- (inner nil)
- (send (agent nil) inner))))
+ (inner)
+ (future-call inner))))
(defn- POST-REQUEST
"Utility to send a generic POST request DO NOT wait for the realization of the response"
- [url & {:keys [query body headers wait-response] :or {wait-response @*wait-response*}}]
- (letfn [(inner [_]
- (with-open [client (client/create-client)]
- (let [response (client/POST client url :query query :body body :headers headers)]
- (client/await response))))]
+ [url & {:keys [query body headers wait-response] :or {wait-response *wait-response*}}]
+ (let [inner
+ #(with-open [client (client/create-client)]
+ (let [response (client/POST client url :query query :body body :headers headers)]
+ (client/await response)))]
(if wait-response
- (inner nil)
- (send (agent nil) inner))))
+ (inner)
+ (future-call inner))))
-(defn analyze-track [{:keys [query wait-response] :or {wait-response @*wait-response*}}]
+(defn analyze-track [{:keys [query wait-response] :or {wait-response *wait-response*}}]
(POST-REQUEST (str url-base "track/analyze?")
- :query (conj {"api_key" @*api-key*} query)
+ :query (conj {"api_key" *api-key*} query)
:headers {"Content-Type" "multipart/form-data"}
:wait-response wait-response))
(defn create-catalog [name type & {:keys [query wait-response]}]
(POST-REQUEST (str url-base "catalog/create")
- :query (conj query {:name name :type type :api_key @*api-key*})
+ :query (conj query {:name name :type type :api_key *api-key*})
:headers {"Content-Type" "multipart/form-data"} :wait-response wait-response))
(defn update-catalog [id data & {:keys [query wait-response]}]
"Easiest way data = (slurp \"path/of/the/json.json\") "
(POST-REQUEST (str url-base "catalog/update")
- :body {:id id :api_key @*api-key* :data data}
+ :body {:id id :api_key *api-key* :data data}
:query query
:wait-response wait-response))
(defn delete-catalog [id & {:keys [query wait-response]}]
(POST-REQUEST (str url-base "catalog/delete")
- :body {:id id :api_key @*api-key*}
+ :body {:id id :api_key *api-key*}
:query query
:wait-response wait-response))
@@ -8,33 +8,42 @@
(fact ;;Just make sure everything is working
(+ 1 1) => 2)
+(fact
+ *api-key* => "N6E4NIOVYMTHNDM8J"
+ (binding [*api-key* 3] *api-key*) => 3
+ (do (set-api-key! "api-key") *api-key*) => "api-key"
+ (do (set-api-key! "N6E4NIOVYMTHNDM8J"))
+ *api-key* => "N6E4NIOVYMTHNDM8J")
+
-;; (fact
-;; (with-open [client (client/create-client)]
-;; (let [resp (upload-song "/home/simo/Music/London Calling - The Clash.mp3" :query {:filetype "mp3"})]
-;; (read-json (client/string resp)))) => (read-json "{\"response\": {\"status\": {\"version\": \"4.2\", \"code\": 0, \"message\": \"Success\"}, \"track\": {\"status\": \"complete\", \"song_id\": \"SOZITHT12812D029DA\", \"audio_md5\": \"fa78064e502aa893429ffcd79470f822\", \"artist\": \"The Clash\", \"samplerate\": 44100, \"title\": \"London Calling\", \"analyzer_version\": \"3.1.0_beta_5\", \"release\": \"Best Of Rock\", \"artist_id\": \"AR13CXU1187B9AD30A\", \"bitrate\": 128, \"id\": \"TRUPOEL134CE9FEF25\", \"md5\": \"edf887ed75efa69d610ba621bb6483c0\"}}}"))
+(fact
+ *wait-response* => true
+ (binding [*wait-response* 3] *wait-response*) => 3
+ (do (set-wait-policy! "wait-policy") *wait-response*) => "wait-policy"
+ (do (set-wait-policy! true))
+ *wait-response* => true)
(fact
- (:url (search "artist" "biographies" :query {:name "rihanna"})) =>
- (str "http://developer.echonest.com/api/v4/artist/biographies?name=rihanna&api_key=" @*api-key*))
+ (:url (basic-query "artist" "biographies" :query {:name "rihanna"})) =>
+ (str "http://developer.echonest.com/api/v4/artist/biographies?name=rihanna&api_key=" *api-key*))
(fact
- (-> (analyze-response (search "artist" "profile" :query {:name "The beatles"})) :response :artist :id) => "AR6XZ861187FB4CECD")
+ (-> (analyze-response (basic-query "artist" "profile" :query {:name "The beatles"})) :response :artist :id) => "AR6XZ861187FB4CECD")
(fact
- (-> (analyze-response (search "artist" "twitter" :query {:name "The beatles"})) :response :artist :twitter) => "thebeatles")
+ (-> (analyze-response (basic-query "artist" "twitter" :query {:name "The beatles"})) :response :artist :twitter) => "thebeatles")
(fact
- (let [urls (-> (analyze-response (search "artist" "urls" :query {:name "The beatles"})) :response :urls)]
+ (let [urls (-> (analyze-response (basic-query "artist" "urls" :query {:name "The beatles"})) :response :urls)]
(:itunes_url urls) => "http://itunes.com/TheBeatles"
(:wikipedia_url urls) => "http://en.wikipedia.org/wiki/The_Beatles"))
(fact
- (let [song (-> (analyze-response (search "song" "search" :query {:title "I love the way you lie" :artist "rihanna"})) :response :songs (nth 0))]
- (:artist_id song) => "ARYGAUS13442810319"
- (:id song) => "SOGGYLD13521E6CAAA"
- (:artist_name song) => "emminem & rihanna"
- (:title song) => "I Love the Way You Lie"))
+ (let [song (-> (analyze-response (basic-query "song" "search" :query {:title "Hate that I love you" :artist "rihanna"})) :response :songs (nth 0))]
+ (:artist_id song) => "ARKU3Z61187FB51DCA"
+ (:id song) => "SOYLDAZ13773AA9C4A"
+ (:artist_name song) => "Rihanna"
+ (:title song) => "Hate That I Love You"))

0 comments on commit eab39ce

Please sign in to comment.