module GH
( get
, getEach
, getEach_
, getAll
, post
, put
, delete
, module GH.Request
) where
import Control.Monad (void)
import Data.Aeson (FromJSON(..), ToJSON)
import Data.List (isInfixOf)
import Data.Monoid ((<>))
import GH.Request
get :: FromJSON a => Path -> IO a
get path = requestJSON path id
getEach :: FromJSON a => Path -> (a -> IO b) -> IO [b]
getEach path f = go 1
where
go n = do
page <- get $ appendPage n
if null page
then return []
else (<>) <$> mapM f page <*> go (n + 1)
appendPage :: Int -> String
appendPage n
| "?" `isInfixOf` path = path <> "&page=" <> show n
| otherwise = path <> "?page=" <> show n
getEach_ :: FromJSON a => Path -> (a -> IO b) -> IO ()
getEach_ path = void . getEach path
getAll :: FromJSON a => Path -> IO [a]
getAll path = getEach path (return . id)
post :: (ToJSON a, FromJSON b) => Path -> a -> IO b
post path body = requestJSON path $ setMethod "POST" . setBody body
put :: (ToJSON a, FromJSON b) => Path -> a -> IO b
put path body = requestJSON path $ setMethod "POST" . setBody body
delete :: Path -> IO ()
delete path = requestJSON path $ setMethod "DELETE"