Commits

Edoardo Tosca committed 6a3df36

added some refactoring and a first unit test

Comments (0)

Files changed (9)

-(defproject clj-grooveshark "0.1.0-SNAPSHOT"
+(defproject clj-grooveshark "0.1.1-SNAPSHOT"
   :description "Clojure bindings for Grooveshark API"
   :url "http://example.com/FIXME"
   :license {:name "Eclipse Public License"

src/clj_grooveshark/album.clj

 (defn get-does-album-exists? [album-id]
   "Checks if a given album id exists"
   (let [response (execute (a-query "getDoesAlbumExist", {:albumID album-id}))]
-    (get response "result")
+    (:result response)
     )
   )

src/clj_grooveshark/artist.clj

   "Retrieve albums of an artist, given an artist id"
   [artist-id]
   (let [response (execute (a-query "getArtistAlbums" {:artistID artist-id}))]
-    (if (contains? response "errors")
-      (get response "errors")
-      (get (get response "result") "albums")
+    (if (contains? response :errors)
+      (:errors response)
+      (:albums (:result response))
       )
     )
   )
   "Checks if a given artist id exists"
   [artist-id]
   (let [response (execute (a-query "getDoesArtistExist", {:artistID artist-id}))]
-    (get response "result")
+    (:result response)
     )
   )
 
   (execute (a-query "getArtistsInfo" {:artistIDs  (concat (list artist-id) more)}))
   )
 
+(defn get-artists-info2
+  "Returns information for the given list of artist ids."
+  [artist-id & more]
+  (exec plain (a-query2 "getArtistsInfo" {:artistIDs  (concat (list artist-id) more)}))
+  )
+
+
+
+
 

src/clj_grooveshark/core.clj

     )
   )
 
+
 ;TO REFACTOR!
 (defn execute-basic [query]
   (client/get (:url configuration) {:body query} {:as :json})
   )
 
 (defn execute [query]
-  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (toHexString (sign (:secret configuration) query))) {:body query} {:as :json})))
+;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
+  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
   )
 
 (defn execute-secure [query]
-;  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (sign (:secret configuration) query))) {:body query} {:as :json})))
-  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (sign (:secret configuration) query))) {:body query} {:as :json})))
+;  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
+  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
+  )
+
+
+(defn exec
+  [url query]
+  (json/read-str (:body (client/get (str url "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
+  )
+
+(def secure
+  (:secure-url configuration)
+  )
+
+(def plain
+  (:url configuration)
   )
 
 
 
 
+
+

src/clj_grooveshark/crypto.clj

 (defn secretKeyInst [key mac]
   (SecretKeySpec. (.getBytes key) (.getAlgorithm mac)))
 
-(defn sign
+(defn create-signature
   "Returns the signature of a string with a given
     key, using a MD5 HMAC."
   [key string]
   [bytes]
   (apply str (map toString bytes)))
 
-(defn sign-as-string [key string]
-  (toHexString (sign key string))
+
+(defn sign
+  "Sign a given querystring with the given key"
+  [key string]
+  (toHexString (create-signature key string))
   )

src/clj_grooveshark/playlist.clj

 
 (defn create-playlist
   "Create a new playlist, optionally adding songs to it"
-  [playlist-name session-id song-id & more]
-  (execute-secure (a-query2 "createPlaylist" {:name playlist-name :songIDs (concat (list song-id) more)} {:sessionID session-id}))
+  ([playlist-name session-id song-id & more]
+  (execute-secure (a-query2 "createPlaylist" {:name playlist-name :songIDs (concat (list song-id) more)} {:sessionID session-id})))
+  ([playlist-name session-id song-ids]
+  (execute-secure (a-query2 "createPlaylist" {:name playlist-name :songIDs song-ids} {:sessionID session-id})))
   )
 
 (defn set-playlist-songs

src/clj_grooveshark/song.clj

   "Checks if a given song id exists"
   [song-id]
   (let [response (execute (a-query "getDoesSongExist", {:songID song-id}))]
-    (get response "result")
+    (:result response)
     )
   )
 
   ([] (execute (a-query2 "getPopularSongsToday" nil nil)))
   ([limit] (execute (a-query2 "getPopularSongsToday" {:limit  limit} nil)))
 
-  )
+  )

test/clj_grooveshark/artist_test.clj

+(ns clj-grooveshark.artist-test
+  (:require [clojure.data.json :as json])
+  (:require [clj-http.client :as client])
+  (:use clojure.test
+        clj-grooveshark.artist))
+
+
+
+(defn stringify [query]
+  {:body (json/write-str query)}
+  )
+
+(deftest should-get-artists-info
+  (let [query (stringify {"result" {"artists" [{"ArtistID" 1261,"ArtistName" "Mogwai","IsVerified" "1"}]}})]
+    (with-redefs [client/get (fn [url body json] query)]
+      (is (= {:result {:artists [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified "1"}]}} (get-artists-info 1261)))
+      )
+    )
+  )
+
+
+

test/clj_grooveshark/core_test.clj

 
 (deftest should-create-a-basic-query
   (testing "a basic query"
-    (is (= {"method" "pingService", "header" {"wsKey" "A-KEY"}} (jsonify (a-query2 "pingService"))))
+    (is (= {"header" {"wsKey" "devonlinux"}, "method" "pingService"} (jsonify (a-query2 "pingService"))))
     )
   )
 
 (deftest should-create-a-query-with-parameters
   (testing "a query with parameters"
-    (is (= {"method" "getDoesAlbumExist", "parameters" {"albumID" "1234"}, "header" {"wsKey" "A-KEY"}} (jsonify (a-query2 "getDoesAlbumExist", {:albumID "1234"}))))
+    (is (= {"header" {"wsKey" "devonlinux"}, "method" "getDoesAlbumExist", "parameters" {"albumID" "1234"}} (jsonify (a-query2 "getDoesAlbumExist", {:albumID "1234"}))))
     )
   )
 
 (deftest should-create-a-query-with-extra-headers
   (testing "a query with extra headers"
-    (is (= {"method" "logout", "header" {"wsKey" "A-KEY", "sessionID" "1234abcd"}} (jsonify (a-query2 "logout" nil , {:sessionID "1234abcd"}))))
+    (is (= {"method" "logout", "header" {"wsKey" "devonlinux", "sessionID" "1234abcd"}} (jsonify (a-query2 "logout" nil , {:sessionID "1234abcd"}))))
     )
   )