Commits

Sebastián Magrí committed 068777c

Implement scSimpleGet and scRecursiveGet for more idiomatic requests to the API, disallowing the usage of the internal scGet function.

Comments (0)

Files changed (7)

src/Network/SoundCloud.hs

 -}
 
 module Network.SoundCloud (
-  scGet,
+  scSimpleGet,
+  scRecursiveGet,
   scFetch,
   scResolve,
   scResourceType,
   scShowInfo
   ) where
 
-import Network.SoundCloud.Util (scGet, scFetch, scResolve, scResourceType)
+import Network.SoundCloud.Util (scSimpleGet, scRecursiveGet, scFetch, scResolve, scResourceType)
 
 import qualified Network.SoundCloud.App         as App
 import qualified Network.SoundCloud.Group       as Group
 
 -- | Show information about a resource given its public URL
 scShowInfo :: String -> IO ()
-scShowInfo url =
-    do rUrl <- scResolve url
-       scResourceShowInfo rUrl
+scShowInfo url = scResourceShowInfo =<< scResolve url

src/Network/SoundCloud/App.hs

 import GHC.Generics (Generic)
 import Text.Printf (printf)
 
-import Network.SoundCloud.Util (scGet, scResolve)
+import Network.SoundCloud.Util (scRecursiveGet, scResolve)
 
 -- | JSON representation of applications
 data JSON = JSON { id                     :: Int
 -- as in <http://soundcloud.com/apps/app_name>
 getJSON :: String -> IO (Maybe JSON)
 getJSON url =
-    do tUrl <- scResolve url
-       dat  <- scGet tUrl True
+    do dat <- scRecursiveGet =<< scResolve url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeJSON d

src/Network/SoundCloud/Group.hs

 import Text.Printf (printf)
 
 import qualified Network.SoundCloud.MiniUser as User
-import Network.SoundCloud.Util (scGet, scResolve)
+import Network.SoundCloud.Util (scRecursiveGet, scResolve)
 
 -- | Represents group's JSON as a record
 data JSON = JSON { id                     :: Int
 -- as in http://soundcloud.com/groups/group_name
 getJSON :: String -> IO (Maybe JSON)
 getJSON url =
-    do tUrl <- scResolve url
-       dat  <- scGet tUrl True
+    do dat  <- scRecursiveGet =<< scResolve url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeJSON d

src/Network/SoundCloud/Set.hs

 
 import qualified Network.SoundCloud.MiniUser as User
 import qualified Network.SoundCloud.Track as Track
-import Network.SoundCloud.Util (scGet, scResolve)
+import Network.SoundCloud.Util (scRecursiveGet, scResolve)
 
 -- | Represents Set JSON as a record
 data JSON = JSON { id                     :: Int
 -- as in <http://soundcloud.com/artist/set_title>
 getJSON :: String -> IO (Maybe JSON)
 getJSON url =
-    do tUrl <- scResolve url
-       dat  <- scGet tUrl True
+    do dat  <- scRecursiveGet =<< scResolve url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeJSON d

src/Network/SoundCloud/Track.hs

 import Prelude hiding (id)
 import Text.Printf (printf)
 
-import Network.SoundCloud.Util (scGet, scFetch, scResolve)
+import Network.SoundCloud.Util (scRecursiveGet, scFetch, scResolve)
 import Network.SoundCloud.Const (clientId, tracksURL)
 import qualified Network.SoundCloud.MiniUser as User
 import qualified Network.SoundCloud.Comment as Comment
 -- as in <http://soundcloud.com/artist/track_title>
 getJSON :: String -> IO (Maybe JSON)
 getJSON url =
-    do tUrl <- scResolve url
-       dat  <- scGet tUrl True
+    do dat  <- scRecursiveGet =<< scResolve url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeJSON d
 getComments :: Int -> IO (Maybe [Comment.JSON])
 getComments trackId =
     do let url = tracksURL ++ "/" ++ show trackId ++ "/comments.json?client_id=" ++ clientId
-       dat <- scGet url True
+       dat <- scRecursiveGet url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeComments d
 -- | Fetch a downloadable track
 fetch :: String -> String -> IO ()
 fetch trackUrl output =
-    do tUrl <- scResolve trackUrl
-       dat <- scGet tUrl True
+    do dat <- scRecursiveGet =<< scResolve trackUrl
        case dat of
          Nothing -> putStrLn "Unable to connect"
          Just d  ->

src/Network/SoundCloud/User.hs

 import GHC.Generics (Generic)
 import Text.Printf (printf)
 
-import Network.SoundCloud.Util (scGet, scResolve)
+import Network.SoundCloud.Util (scRecursiveGet, scResolve)
 
 -- | Record representation of a user's JSON
 data JSON = JSON { id                     :: Int
 -- as in <http://soundcloud.com/artist>
 getJSON :: String -> IO (Maybe JSON)
 getJSON url =
-    do tUrl <- scResolve url
-       dat  <- scGet tUrl True
+    do dat  <- scRecursiveGet =<< scResolve url
        case dat of
          Nothing -> return Nothing
          Just d  -> return $ decodeJSON d

src/Network/SoundCloud/Util.hs

    General functions used by other modules
 -}
 
-module Network.SoundCloud.Util where
+module Network.SoundCloud.Util (
+  scSimpleGet,
+  scRecursiveGet,
+  scFetch,
+  scResourceType,
+  scResolve
+  ) where
 
 import Data.List
 import Network.HTTP
 
 import Network.SoundCloud.Const
 
--- | Issue a @GET@ request to an URL given as first parameter
+-- | Issue a @GET@ request to an URL given as second parameter
 -- and returns the response body as a 'String' or 'Nothing'
 -- on failure.
 --
--- If the second argument is set to 'True', and a @3XX@
+-- If the first argument is set to 'True', and a @3XX@
 -- response code is found, a new request will be made
 -- to the @Location@ header of the response.
-scGet :: String -> Bool -> IO (Maybe String)
-scGet url followRedirections =
+scGet :: Bool -> String -> IO (Maybe String)
+scGet followRedirections url =
     do res <- simpleHTTP $ getRequest url
        case res of
          Left   _ -> return Nothing
                      Nothing       -> return Nothing
                      Just uri      ->
                          if followRedirections
-                         then scGet uri True
+                         then scRecursiveGet uri
                          else return $ Just uri
                _ -> return Nothing
 
+-- | Issue a @GET@ HTTP request to the passed URL returning
+-- @Nothing@ if a response code different than 2XX is found.
+scSimpleGet :: String -> IO (Maybe String)
+scSimpleGet = scGet False
+
+-- | Issue a @GET@ HTTP request to the passed URL recursing
+-- over redirections
+scRecursiveGet :: String -> IO (Maybe String)
+scRecursiveGet = scGet True
+
 -- | Given an URL as a first parameter, and a path as a second,
 -- issue a @GET@ request to the @URL@ and save the response body
 -- to a file at @path@.
 scFetch :: String -> String -> IO ()
 scFetch dUrl out =
-    do contents <- scGet dUrl True
+    do contents <- scRecursiveGet dUrl
        case contents of
          Nothing -> putStrLn "Could not fetch file contents."
          Just  c ->
 --     <http://api.soundcloud.com/tracks/track_id.json?client_id=foo>
 scResolve :: String -> IO String
 scResolve url =
-    do dat <- scGet resolveUrl False
+    do dat <- scSimpleGet resolveUrl
        case dat of
          Nothing        -> return ""
          Just d         -> return d