{-# LANGUAGE OverloadedStrings #-}

{- |
Copyright: (c) 2020 Jens Petersen
SPDX-License-Identifier: MIT
Maintainer: Jens Petersen <juhpetersen@gmail.com>

Fedora Bodhi REST client library
-}

module Fedora.Bodhi
  ( bodhiBuild
  , bodhiBuilds
  , bodhiComment
  , bodhiComments
  , bodhiCSRF
  , bodhiOverride
  , bodhiOverrides
  , bodhiOverrideDates
  , bodhiPackages
  , bodhiRelease
  , bodhiReleases
  , bodhiUpdate
  , bodhiUpdates
  , bodhiUser
  , bodhiUsers
  , lookupKey
  , lookupKey'
  , queryBodhi
  , makeKey
  , makeItem
  , maybeKey
  , Query
  , QueryItem
  ) where

import Data.Aeson.Types
import Data.Text (Text)
import Data.Time.LocalTime
import Network.HTTP.Query

server :: String
server :: String
server = "bodhi.fedoraproject.org"

-- | Returns build JSON for NVR
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/builds.html#service-0
bodhiBuild :: String -> IO Object
bodhiBuild :: String -> IO Object
bodhiBuild nvr :: String
nvr =
  Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] (String -> IO Object) -> String -> IO Object
forall a b. (a -> b) -> a -> b
$ "builds" String -> String -> String
+/+ String
nvr

-- | returns JSON list of builds
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/builds.html#service-1
bodhiBuilds :: Query -> IO [Object]
bodhiBuilds :: Query -> IO [Object]
bodhiBuilds params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "builds" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "builds/"

-- | Returns comment JSON for id
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/comments.html#service-0
bodhiComment :: String -> IO Object
bodhiComment :: String -> IO Object
bodhiComment cid :: String
cid =
  Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] (String -> IO Object) -> String -> IO Object
forall a b. (a -> b) -> a -> b
$ "comments" String -> String -> String
+/+ String
cid

-- | returns JSON list of comments
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/comments.html#service-1
bodhiComments :: Query -> IO [Object]
bodhiComments :: Query -> IO [Object]
bodhiComments params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "comments" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "comments/"

-- | Get CSRF token
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/csrf.html
bodhiCSRF :: IO (Maybe Text)
bodhiCSRF :: IO (Maybe Text)
bodhiCSRF =
  Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey "csrf_token" (Object -> Maybe Text) -> IO Object -> IO (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] "csrf"

-- | Returns override JSON for NVR
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/overrides.html#service-0
bodhiOverride :: String -> IO (Maybe Object)
bodhiOverride :: String -> IO (Maybe Object)
bodhiOverride nvr :: String
nvr =
  Text -> Object -> Maybe Object
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey "override" (Object -> Maybe Object) -> IO Object -> IO (Maybe Object)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] ("overrides" String -> String -> String
+/+ String
nvr)

-- | Returns override expiration and submission dates for NVR
bodhiOverrideDates :: String -> IO (Maybe (LocalTime,LocalTime))
bodhiOverrideDates :: String -> IO (Maybe (LocalTime, LocalTime))
bodhiOverrideDates nvr :: String
nvr = do
  Maybe Object
mobj <- String -> IO (Maybe Object)
bodhiOverride String
nvr
  case Maybe Object
mobj of
    Nothing -> do
      String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ "Override for " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
nvr String -> String -> String
forall a. [a] -> [a] -> [a]
++ " not found"
      Maybe (LocalTime, LocalTime) -> IO (Maybe (LocalTime, LocalTime))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (LocalTime, LocalTime)
forall a. Maybe a
Nothing
    Just obj :: Object
obj -> Maybe (LocalTime, LocalTime) -> IO (Maybe (LocalTime, LocalTime))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (LocalTime, LocalTime) -> IO (Maybe (LocalTime, LocalTime)))
-> Maybe (LocalTime, LocalTime)
-> IO (Maybe (LocalTime, LocalTime))
forall a b. (a -> b) -> a -> b
$ Object -> Maybe (LocalTime, LocalTime)
readDates Object
obj
  where
    readDates :: Object -> Maybe (LocalTime,LocalTime)
    readDates :: Object -> Maybe (LocalTime, LocalTime)
readDates =
      (Object -> Parser (LocalTime, LocalTime))
-> Object -> Maybe (LocalTime, LocalTime)
forall a b. (a -> Parser b) -> a -> Maybe b
parseMaybe ((Object -> Parser (LocalTime, LocalTime))
 -> Object -> Maybe (LocalTime, LocalTime))
-> (Object -> Parser (LocalTime, LocalTime))
-> Object
-> Maybe (LocalTime, LocalTime)
forall a b. (a -> b) -> a -> b
$ \obj :: Object
obj -> do
        LocalTime
expire <- Object
obj Object -> Text -> Parser LocalTime
forall a. FromJSON a => Object -> Text -> Parser a
.: "expiration_date"
        LocalTime
submit <- Object
obj Object -> Text -> Parser LocalTime
forall a. FromJSON a => Object -> Text -> Parser a
.: "submission_date"
        (LocalTime, LocalTime) -> Parser (LocalTime, LocalTime)
forall (m :: * -> *) a. Monad m => a -> m a
return (LocalTime
expire,LocalTime
submit)

-- | returns JSON list of overrides
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/overrides.html#service-1
bodhiOverrides :: Query -> IO [Object]
bodhiOverrides :: Query -> IO [Object]
bodhiOverrides params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "overrides" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "overrides/"

-- | Packages query
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/packages.html#service-0
bodhiPackages :: Query -> IO [Object]
bodhiPackages :: Query -> IO [Object]
bodhiPackages params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "packages" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "packages/"

-- | read releases metadata from Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/releases.html#service-0
bodhiRelease :: String -> IO Object
bodhiRelease :: String -> IO Object
bodhiRelease rel :: String
rel =
  Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] (String -> IO Object) -> String -> IO Object
forall a b. (a -> b) -> a -> b
$ "releases" String -> String -> String
+/+ String
rel

-- | read releases metadata from Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/releases.html#service-1
bodhiReleases :: Query -> IO [Object]
-- FIXME handle errors:
-- fromList [("status",String "error"),("errors",Array [Object (fromList [("location",String "body"),("name",String "name"),("description",String "No such release")])])]
bodhiReleases :: Query -> IO [Object]
bodhiReleases params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "releases" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "releases/"

-- | read an update from Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/updates.html#service-0
bodhiUpdate :: String -> IO (Maybe Object)
bodhiUpdate :: String -> IO (Maybe Object)
bodhiUpdate update :: String
update =
  Text -> Object -> Maybe Object
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey "update" (Object -> Maybe Object) -> IO Object -> IO (Maybe Object)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] ("updates" String -> String -> String
+/+ String
update)

-- | search for updates on Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/updates.html#service-2
bodhiUpdates :: Query -> IO [Object]
bodhiUpdates :: Query -> IO [Object]
bodhiUpdates params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "updates" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "updates/"

-- | user info from Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/users.html#service-0
bodhiUser :: String -> IO Object
bodhiUser :: String -> IO Object
bodhiUser user :: String
user =
  Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi [] (String -> IO Object) -> String -> IO Object
forall a b. (a -> b) -> a -> b
$ "users" String -> String -> String
+/+ String
user

-- | list users from Bodhi
--
-- https://bodhi.fedoraproject.org/docs/server_api/rest/users.html#service-1
bodhiUsers :: Query -> IO [Object]
bodhiUsers :: Query -> IO [Object]
bodhiUsers params :: Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' "users" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query -> String -> IO Object
forall a. FromJSON a => Query -> String -> IO a
queryBodhi Query
params "users/"

-- | low-level query
queryBodhi :: FromJSON a => Query -> String -> IO a
queryBodhi :: Query -> String -> IO a
queryBodhi params :: Query
params path :: String
path =
  let url :: String
url = "https://" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
server String -> String -> String
+/+ String
path
  in String -> Query -> IO a
forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
String -> Query -> m a
webAPIQuery String
url Query
params