module SimpleRequestion where



-- ### Simple tests ###

myApiKey = "b25b959554ed76058ac220b7b2e0a026" -- This is the demo key

topTracks   = "http://ws.audioscrobbler.com/2.0/?method=geo.gettoptracks&country=spain&api_key=" ++ myApiKey ++ "&format=json"
topArtists  = "http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&api_key=" ++ myApiKey ++ "&format=json"
getevents page = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&country=uk&location=london&api_key=" ++ myApiKey ++ "&format=json&page=" ++ show page
albumgetinfo = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=Cher&album=Believe&format=json"
albumsearch = "http://ws.audioscrobbler.com/2.0/?method=album.search&album=believe&api_key=b25b959554ed76058ac220b7b2e0a026&format=json"

toVal :: Partial String String JSValue
toVal = Partial (runGetJSON . readJSValue)

runJSPartial pa s = apply (pa . toVal) s

testDecode :: String -> ValTo a -> IO a
testDecode url pa = do
    (_,js) <- curlGetString url []
    case runValTo js of
        Right t -> return t
        Left e -> error ("poc: " ++ e)

testTopArtists = testDecode topArtists :: IO TopArtists
testTopTracks = testDecode topTracks :: IO TopTracks

testGetEvents :: Int -> IO ()
testGetEvents page = do
    (_,js) <- curlGetString (getevents page) []
    case safeDecode js of
        Ok evs -> if moreEvents evs then testGetEvents (page+1) else print page
        Error e -> error ("poc: " ++ e)

fetchJSON url = do
    (_,js) <- curlGetString url []
    let (Right jsval) = runGetJSON readJSValue js
    return jsval

prettyFetchJSON url = print . pp_value =<< fetchJSON url