{-# LANGUAGE DataKinds #-}
module GitHub.Tools.Requests where

import           Control.Monad.Catch (throwM)
import           Data.Aeson          (FromJSON)
import qualified GitHub
import           Network.HTTP.Client (Manager)


request
  :: FromJSON a
  => Maybe GitHub.Auth
  -> Manager
  -> GitHub.Request 'GitHub.RO a
  -> IO a
request :: Maybe Auth -> Manager -> Request 'RO a -> IO a
request Maybe Auth
auth Manager
mgr Request 'RO a
req = do
  Either Error a
response <- IO (Either Error a)
executeRequest
  case Either Error a
response of
    Left  Error
err -> Error -> IO a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM Error
err
    Right a
res -> a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res

  where
    executeRequest :: IO (Either Error a)
executeRequest =
      case Maybe Auth
auth of
        Maybe Auth
Nothing -> Manager -> Request 'RO a -> IO (Either Error a)
forall (mt :: MediaType *) a.
ParseResponse mt a =>
Manager -> GenRequest mt 'RO a -> IO (Either Error a)
GitHub.executeRequestWithMgr' Manager
mgr Request 'RO a
req
        Just Auth
tk -> Manager -> Auth -> Request 'RO a -> IO (Either Error a)
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
Manager -> am -> GenRequest mt rw a -> IO (Either Error a)
GitHub.executeRequestWithMgr Manager
mgr Auth
tk Request 'RO a
req