{-# LANGUAGE CPP #-}

{- |
Copyright: (c) 2020 Jens Petersen
SPDX-License-Identifier: GPL-2.0-only
Maintainer: Jens Petersen <petersen@redhat.com>

Copr REST client library
-}

module Web.Fedora.Copr.API
  ( coprGetProject
  , coprGetProjectsList
  , coprSearchProjects
  , coprGetBuild
  , coprGetBuildSourceChroot
  , coprGetBuildSourceConfig
  , coprGetBuildPackageList
  , coprGetBuildChroot
  , coprGetBuildChrootList
  , coprGetBuildChrootConfig
  , coprMockChrootList
  , coprGetPackage
  , coprGetPackageList
  , coprGetProjectChroot
  , coprGetProjectChrootBuildConfig
  , queryCopr
  , maybeKey
  , makeKey
  , makeItem
  , lookupKey
  , lookupKey'
  )
where

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

-- # Projects

-- | List project details
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/project.py#_9
coprGetProject :: String -> String -> String -> IO Object
coprGetProject :: String -> String -> String -> IO Object
coprGetProject String
server String
owner String
project = do
  let path :: String
path = String
"project"
      params :: [QueryItem]
params = [String -> String -> QueryItem
makeItem String
"ownername" String
owner, String -> String -> QueryItem
makeItem String
"projectname" String
project]
  String -> String -> [QueryItem] -> IO Object
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- | List projects of owner
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/project.py#_26
coprGetProjectsList :: String -> String -> IO Value
-- FIXME limit=int or maybe pagination?
coprGetProjectsList :: String -> String -> IO Value
coprGetProjectsList String
server String
owner = do
  let path :: String
path = String
"project/list"
      params :: [QueryItem]
params = String -> String -> [QueryItem]
makeKey String
"ownername" String
owner
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- | search projects by query string
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/project.py#_43
coprSearchProjects :: String -> String -> IO Value
coprSearchProjects :: String -> String -> IO Value
coprSearchProjects String
server String
query = do
  let path :: String
path = String
"project/search"
      params :: [QueryItem]
params = String -> String -> [QueryItem]
makeKey String
"query" String
query
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- # Builds

-- | get build
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build.py#_10
coprGetBuild :: String -> Int -> IO Value
coprGetBuild :: String -> Int -> IO Value
coprGetBuild String
server Int
bid = do
  let path :: String
path = String
"build" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- | get srpm build
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build.py#_22
coprGetBuildSourceChroot :: String -> Int -> IO Value
coprGetBuildSourceChroot :: String -> Int -> IO Value
coprGetBuildSourceChroot String
server Int
bid = do
  let path :: String
path = String
"build/source-chroot" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- | get build source config
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build.py#_34
coprGetBuildSourceConfig :: String -> Int -> IO Value
coprGetBuildSourceConfig :: String -> Int -> IO Value
coprGetBuildSourceConfig String
server Int
bid = do
  let path :: String
path = String
"build/source-build-config" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- | get list of packages
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build.py#_46
coprGetBuildPackageList :: String -> Query -> IO Value
coprGetBuildPackageList :: String -> [QueryItem] -> IO Value
coprGetBuildPackageList String
server [QueryItem]
params = do
  let path :: String
path = String
"build/list"
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- # Build chroot

-- | get build chroot
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build_chroot.py#_8
coprGetBuildChroot :: String -> Int -> String -> IO Value
coprGetBuildChroot :: String -> Int -> String -> IO Value
coprGetBuildChroot String
server Int
bid String
chroot = do
  let path :: String
path = String
"build-chroot" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid String -> String -> String
+/+ String
chroot
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- | list of build chroots
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build_chroot.py#_25
coprGetBuildChrootList :: String -> Int -> IO Value
coprGetBuildChrootList :: String -> Int -> IO Value
coprGetBuildChrootList String
server Int
bid = do
  let path :: String
path = String
"build-chroot/list" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- | get build config for chroot
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/build_chroot.py#_44
coprGetBuildChrootConfig :: String -> Int -> String -> IO Value
coprGetBuildChrootConfig :: String -> Int -> String -> IO Value
coprGetBuildChrootConfig String
server Int
bid String
chroot = do
  let path :: String
path = String
"build-chroot/build-config" String -> String -> String
+/+ Int -> String
forall a. Show a => a -> String
show Int
bid String -> String -> String
+/+ String
chroot
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- # Mock chroot

-- | list of all available mock chroots
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/mock_chroot.py
coprMockChrootList :: String -> IO Value
coprMockChrootList :: String -> IO Value
coprMockChrootList String
server = do
  let path :: String
path = String
"mock-chroots/list"
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path []

-- # Package

-- | Get project package details
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/package.py#_9
coprGetPackage :: String -> String -> String -> String -> IO Value
coprGetPackage :: String -> String -> String -> String -> IO Value
coprGetPackage String
server String
owner String
project String
package = do
  let path :: String
path = String
"package"
      params :: [QueryItem]
params = [String -> String -> QueryItem
makeItem String
"ownername" String
owner,
                String -> String -> QueryItem
makeItem String
"projectname" String
project,
                String -> String -> QueryItem
makeItem String
"packagename" String
package]
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- | List project packages
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/package.py#_28
coprGetPackageList :: String -> String -> String -> IO Value
coprGetPackageList :: String -> String -> String -> IO Value
coprGetPackageList String
server String
owner String
project = do
  let path :: String
path = String
"package/list"
      params :: [QueryItem]
params = [String -> String -> QueryItem
makeItem String
"ownername" String
owner, String -> String -> QueryItem
makeItem String
"projectname" String
project]
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- # Project chroot

-- | get build chroot
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/project_chroot.py#_10
coprGetProjectChroot :: String -> String -> String -> String -> IO Value
coprGetProjectChroot :: String -> String -> String -> String -> IO Value
coprGetProjectChroot String
server String
owner String
project String
chroot = do
  let path :: String
path = String
"project-chroot"
      params :: [QueryItem]
params = [String -> String -> QueryItem
makeItem String
"ownername" String
owner,
                String -> String -> QueryItem
makeItem String
"projectname" String
project,
                String -> String -> QueryItem
makeItem String
"chrootname" String
chroot]
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params

-- | list of build chroots
--
-- https://pagure.io/copr/copr/blob/master/f/python/copr/v3/proxies/project_chroot.py#_29
coprGetProjectChrootBuildConfig :: String -> String -> String -> String -> IO Value
coprGetProjectChrootBuildConfig :: String -> String -> String -> String -> IO Value
coprGetProjectChrootBuildConfig String
server String
owner String
project String
chroot = do
  let path :: String
path = String
"project-chroot/build-config"
      params :: [QueryItem]
params = [String -> String -> QueryItem
makeItem String
"ownername" String
owner,
                String -> String -> QueryItem
makeItem String
"projectname" String
project,
                String -> String -> QueryItem
makeItem String
"chrootname" String
chroot]
  String -> String -> [QueryItem] -> IO Value
forall a. FromJSON a => String -> String -> [QueryItem] -> IO a
queryCopr String
server String
path [QueryItem]
params


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