2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-04-23 13:07:52 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-09-23 18:26:05 +02:00
|
|
|
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
import Control.Applicative
|
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.IO.Class
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-08-17 23:56:29 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text (Text)
|
|
|
|
import GHC.Generics
|
2015-09-28 18:07:12 +02:00
|
|
|
import Network.HTTP.Client (Manager, defaultManagerSettings,
|
|
|
|
newManager)
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
2015-08-17 23:56:29 +02:00
|
|
|
import Servant.API
|
|
|
|
import Servant.Client
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.IO as T
|
2015-04-23 13:07:52 +02:00
|
|
|
|
|
|
|
type HackageAPI =
|
2015-04-23 17:10:43 +02:00
|
|
|
"users" :> Get '[JSON] [UserSummary]
|
2015-04-23 13:07:52 +02:00
|
|
|
:<|> "user" :> Capture "username" Username :> Get '[JSON] UserDetailed
|
|
|
|
:<|> "packages" :> Get '[JSON] [Package]
|
|
|
|
|
|
|
|
type Username = Text
|
|
|
|
|
|
|
|
data UserSummary = UserSummary
|
|
|
|
{ summaryUsername :: Username
|
|
|
|
, summaryUserid :: Int
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance FromJSON UserSummary where
|
|
|
|
parseJSON (Object o) =
|
|
|
|
UserSummary <$> o .: "username"
|
|
|
|
<*> o .: "userid"
|
|
|
|
|
|
|
|
parseJSON _ = mzero
|
|
|
|
|
|
|
|
type Group = Text
|
|
|
|
|
|
|
|
data UserDetailed = UserDetailed
|
|
|
|
{ username :: Username
|
|
|
|
, userid :: Int
|
|
|
|
, groups :: [Group]
|
|
|
|
} deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance FromJSON UserDetailed
|
|
|
|
|
|
|
|
newtype Package = Package { packageName :: Text }
|
|
|
|
deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance FromJSON Package
|
|
|
|
|
|
|
|
hackageAPI :: Proxy HackageAPI
|
|
|
|
hackageAPI = Proxy
|
|
|
|
|
2015-09-28 18:07:12 +02:00
|
|
|
|
|
|
|
{-# NOINLINE manager #-}
|
|
|
|
manager :: Manager
|
|
|
|
manager = unsafePerformIO $ newManager defaultManagerSettings
|
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
getUsers :: ExceptT ServantError IO [UserSummary]
|
|
|
|
getUser :: Username -> ExceptT ServantError IO UserDetailed
|
|
|
|
getPackages :: ExceptT ServantError IO [Package]
|
2015-09-28 18:07:12 +02:00
|
|
|
getUsers :<|> getUser :<|> getPackages =
|
|
|
|
client hackageAPI (BaseUrl Http "hackage.haskell.org" 80 "") manager
|
2015-04-23 13:07:52 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = print =<< uselessNumbers
|
|
|
|
|
|
|
|
uselessNumbers :: IO (Either ServantError ())
|
2015-09-12 14:11:24 +02:00
|
|
|
uselessNumbers = runExceptT $ do
|
2015-05-08 20:47:19 +02:00
|
|
|
users <- getUsers
|
2015-04-23 13:07:52 +02:00
|
|
|
liftIO . putStrLn $ show (length users) ++ " users"
|
|
|
|
|
|
|
|
user <- liftIO $ do
|
|
|
|
putStrLn "Enter a valid hackage username"
|
|
|
|
T.getLine
|
2015-09-28 18:07:12 +02:00
|
|
|
userDetailed <- getUser user
|
2015-04-23 13:07:52 +02:00
|
|
|
liftIO . T.putStrLn $ user <> " maintains " <> T.pack (show (length $ groups userDetailed)) <> " packages"
|
2015-05-08 20:47:19 +02:00
|
|
|
|
|
|
|
packages <- getPackages
|
2015-04-23 13:07:52 +02:00
|
|
|
let monadPackages = filter (isMonadPackage . packageName) packages
|
|
|
|
liftIO . putStrLn $ show (length monadPackages) ++ " monad packages"
|
|
|
|
|
|
|
|
where isMonadPackage = T.isInfixOf "monad"
|