module GitHub.REST
(
MonadGitHubREST(..)
, GitHubT
, GitHubState(..)
, runGitHubT
, Token(..)
, GHEndpoint(..)
, GitHubData
, EndpointVals
, KeyValue(..)
, githubTry
, githubTry'
, (.:)
, StdMethod(..)
) where
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Data.Aeson (FromJSON, Value(..), decode, withObject)
import Data.Aeson.Types (parseEither, parseField)
import qualified Data.ByteString.Lazy as ByteStringL
import Data.Text (Text)
import Network.HTTP.Client
(HttpException(..), HttpExceptionContent(..), Response(..))
import Network.HTTP.Types (Status, StdMethod(..), status422)
import UnliftIO.Exception (handleJust)
import GitHub.REST.Auth
import GitHub.REST.Endpoint
import GitHub.REST.KeyValue
import GitHub.REST.Monad
githubTry :: MonadUnliftIO m => m a -> m (Either Value a)
githubTry = githubTry' status422
githubTry' :: MonadUnliftIO m => Status -> m a -> m (Either Value a)
githubTry' status = handleJust statusException (return . Left) . fmap Right
where
statusException (HttpExceptionRequest _ (StatusCodeException r body))
| responseStatus r == status = decode $ ByteStringL.fromStrict body
statusException _ = Nothing
(.:) :: FromJSON a => Value -> Text -> a
(.:) v key = either error id $ parseEither parseObject v
where
parseObject = withObject "parseObject" (`parseField` key)