{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

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

Fedora PDC web api client library
-}

module Fedora.PDC
  ( fedoraPDC
  , pdcArches
  , pdcChangesets
  , pdcComponentBranches
  , pdcComponentBranchSLAs
  , pdcComponentRelationshipTypes
  , pdcComponentSLATypes
  , pdcComposes
  , pdcComposeImages
  , pdcComposeImageRttTests
  , pdcComposeRpms
  , pdcComposeTreeRttTests
  , pdcContentDeliveryContentFormats
  , pdcGlobalComponents
  , pdcImages
  , pdcModules
  , pdcProductVersions
  , pdcProducts
  , pdcReleases
  , pdcWhereToFileBugs
  , pdcRpms
  , queryPDC
  , lookupKey
  , lookupKey'
  , makeKey
  , makeItem
  , maybeKey
  , Query
  , QueryItem
  , getResultsList
  )
where

import Data.Aeson.Types
import Network.HTTP.Query

fedoraPDC :: String
fedoraPDC :: String
fedoraPDC = String
"pdc.fedoraproject.org"

-- | Arch List
--
-- https://pdc.fedoraproject.org/rest_api/v1/arches/
pdcArches :: String -> IO [Object]
pdcArches :: String -> IO [Object]
pdcArches String
server =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"arches" []

-- https://pdc.fedoraproject.org/rest_api/v1/auth/ *

-- https://pdc.fedoraproject.org/rest_api/v1/base-products/ no-op?

-- https://pdc.fedoraproject.org/rest_api/v1/bugzilla-components/ no-op?

-- | Changeset List
--
-- https://pdc.fedoraproject.org/rest_api/v1/changesets/
pdcChangesets :: String -> Query -> IO Object
pdcChangesets :: String -> Query -> IO Object
pdcChangesets String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"changesets"

-- | Sla To Component Branch List
--
-- https://pdc.fedoraproject.org/rest_api/v1/component-branch-slas/
pdcComponentBranchSLAs :: String -> Query -> IO Object
pdcComponentBranchSLAs :: String -> Query -> IO Object
pdcComponentBranchSLAs String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"component-branch-slas"

-- | Component Branch List
--
-- https://pdc.fedoraproject.org/rest_api/v1/component-branches/
pdcComponentBranches :: String -> Query -> IO Object
pdcComponentBranches :: String -> Query -> IO Object
pdcComponentBranches String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"component-branches"

-- | Release Component Relationship Type List
--
-- https://pdc.fedoraproject.org/rest_api/v1/component-relationship-types/
pdcComponentRelationshipTypes :: String -> IO [Object]
pdcComponentRelationshipTypes :: String -> IO [Object]
pdcComponentRelationshipTypes String
server =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"component-relationship-types" []

-- | Sla List
--
-- https://pdc.fedoraproject.org/rest_api/v1/component-sla-types/
pdcComponentSLATypes :: String -> Query -> IO [Object]
pdcComponentSLATypes :: String -> Query -> IO [Object]
pdcComponentSLATypes String
server Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"component-sla-types" Query
params

-- | Compose Image Rtt Test List
--
-- https://pdc.fedoraproject.org/rest_api/v1/compose-image-rtt-tests/ (all untested?)
pdcComposeImageRttTests :: String -> Query -> IO Object
pdcComposeImageRttTests :: String -> Query -> IO Object
pdcComposeImageRttTests String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"compose-image-rtt-tests"

-- | Compose Image Instance
--
-- https://pdc.fedoraproject.org/rest_api/v1/compose-images/ {compose_id}/
pdcComposeImages :: String -> String -> IO Object
pdcComposeImages :: String -> String -> IO Object
pdcComposeImages String
server String
compose =
  String -> String -> Query -> IO Object
queryPDC String
server (String
"compose-images" String -> String -> String
+/+ String
compose String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/") []

-- | Compose Rpm List (seems heavy)
--
-- https://pdc.fedoraproject.org/rest_api/v1/compose-rpms/ {compose_id}/
pdcComposeRpms :: String -> String -> IO Object
pdcComposeRpms :: String -> String -> IO Object
pdcComposeRpms String
server String
compose =
  String -> String -> Query -> IO Object
queryPDC String
server (String
"compose-rpms" String -> String -> String
+/+ String
compose String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/") []

-- | Compose Tree Rtt Test List
--
-- https://pdc.fedoraproject.org/rest_api/v1/compose-tree-rtt-tests/
pdcComposeTreeRttTests :: String -> Query -> IO Object
pdcComposeTreeRttTests :: String -> Query -> IO Object
pdcComposeTreeRttTests String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"compose-tree-rtt-tests"

-- | Compose List
--
-- https://pdc.fedoraproject.org/rest_api/v1/composes/
pdcComposes :: String -> Maybe String -> Query -> IO Object
pdcComposes :: String -> Maybe String -> Query -> IO Object
pdcComposes String
server Maybe String
mcompose Query
params = do
  let path :: String
path = String
"composes" String -> String -> String
+/+ String -> (String -> String) -> Maybe String -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/") Maybe String
mcompose
  String -> String -> Query -> IO Object
queryPDC String
server String
path Query
params

-- https://pdc.fedoraproject.org/rest_api/v1/composes/%7Bcompose_id%7D/rpm-mapping/%7Bpackage%7D/

-- | Content Format List
--
-- https://pdc.fedoraproject.org/rest_api/v1/content-delivery-content-formats/
pdcContentDeliveryContentFormats :: String -> Query -> IO [Object]
pdcContentDeliveryContentFormats :: String -> Query -> IO [Object]
pdcContentDeliveryContentFormats String
server Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"content-delivery-content-formats" Query
params

-- | Global Component List
--
-- https://pdc.fedoraproject.org/rest_api/v1/global-components/
pdcGlobalComponents :: String -> Query -> IO Object
pdcGlobalComponents :: String -> Query -> IO Object
pdcGlobalComponents String
server =
   String -> String -> Query -> IO Object
queryPDC String
server String
"global-components"

-- | Image List
--
-- https://pdc.fedoraproject.org/rest_api/v1/images/
pdcImages :: String -> Query -> IO Object
pdcImages :: String -> Query -> IO Object
pdcImages String
server =
   String -> String -> Query -> IO Object
queryPDC String
server String
"images"

-- | Module List
--
-- https://pdc.fedoraproject.org/rest_api/v1/modules/
pdcModules :: String -> Query -> IO Object
pdcModules :: String -> Query -> IO Object
pdcModules String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"modules"

-- | Product Version List
--
-- https://pdc.fedoraproject.org/rest_api/v1/product-versions/
pdcProductVersions :: String -> Query -> IO [Object]
pdcProductVersions :: String -> Query -> IO [Object]
pdcProductVersions String
server Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"product-versions" Query
params

-- | Product List
--
-- https://pdc.fedoraproject.org/rest_api/v1/products/
pdcProducts :: String -> Query -> IO [Object]
pdcProducts :: String -> Query -> IO [Object]
pdcProducts String
server Query
params =
  Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" (Object -> [Object]) -> IO Object -> IO [Object]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Query -> IO Object
queryPDC String
server String
"products" Query
params

-- | Release List
--
-- https://pdc.fedoraproject.org/rest_api/v1/releases/
pdcReleases :: String -> Query -> IO Object
pdcReleases :: String -> Query -> IO Object
pdcReleases String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"releases"

-- | Filter Bugzilla Products And Components List
--
-- https://pdc.fedoraproject.org/rest_api/v1/rpc/where-to-file-bugs/
pdcWhereToFileBugs :: String -> Query -> IO Object
pdcWhereToFileBugs :: String -> Query -> IO Object
pdcWhereToFileBugs String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"rpc/where-to-file-bugs"

-- | Rpm List
--
-- https://pdc.fedoraproject.org/rest_api/v1/rpms/
pdcRpms :: String -> Query -> IO Object
pdcRpms :: String -> Query -> IO Object
pdcRpms String
server =
  String -> String -> Query -> IO Object
queryPDC String
server String
"rpms"

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

-- | Get results key from a response object
getResultsList :: Object -> [Object]
getResultsList :: Object -> [Object]
getResultsList = Text -> Object -> [Object]
forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results"