{-# 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 :: [Char]
fedoraPDC = [Char]
"pdc.fedoraproject.org"

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

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

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

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

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

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

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

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

-- | Compose List
--
-- https://pdc.fedoraproject.org/rest_api/v1/composes/
pdcComposes :: String -> Maybe String -> Query -> IO Object
pdcComposes :: [Char] -> Maybe [Char] -> Query -> IO Object
pdcComposes [Char]
server Maybe [Char]
mcompose Query
params = do
  let path :: [Char]
path = [Char]
"composes" [Char] -> [Char] -> [Char]
+/+ forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"" (forall a. [a] -> [a] -> [a]
++ [Char]
"/") Maybe [Char]
mcompose
  [Char] -> [Char] -> Query -> IO Object
queryPDC [Char]
server [Char]
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 :: [Char] -> Query -> IO [Object]
pdcContentDeliveryContentFormats [Char]
server Query
params =
  forall a. FromJSON a => Text -> Object -> a
lookupKey' Text
"results" forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [Char] -> Query -> IO Object
queryPDC [Char]
server [Char]
"content-delivery-content-formats" Query
params

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

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

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

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

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

-- | Release List
--
-- https://pdc.fedoraproject.org/rest_api/v1/releases/
pdcReleases :: String -> Query -> IO Object
pdcReleases :: [Char] -> Query -> IO Object
pdcReleases [Char]
server =
  [Char] -> [Char] -> Query -> IO Object
queryPDC [Char]
server [Char]
"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 :: [Char] -> Query -> IO Object
pdcWhereToFileBugs [Char]
server =
  [Char] -> [Char] -> Query -> IO Object
queryPDC [Char]
server [Char]
"rpc/where-to-file-bugs"

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

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

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