module Fedora.Copr
(
module C
, CoprConfig (..)
, Username
, ProjectName
, defaultConfig
, withConfig
, addBuild
, buildStatus
, coprs
, new
) where
import Fedora.Copr.CoprBuild as C (CoprBuild (), CoprBuildResponse ())
import Fedora.Copr.CoprProject as C (CoprProject (), NewCoprResponse ())
import Fedora.Copr.CoprStatus as C (CoprStatusResponse ())
import Fedora.Copr.ListCoprs as C (Coprs ())
import Data.Aeson
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as LS
import Data.Monoid ((<>))
import Network.Http.Client
import OpenSSL (withOpenSSL)
import qualified System.IO.Streams.ByteString as SBS
data CoprConfig = CoprConfig {
domain :: Hostname
, port :: Port
, ssl :: Bool
, login :: S.ByteString
, token :: S.ByteString
} deriving (Eq, Show)
type Username = S.ByteString
type ProjectName = S.ByteString
defaultConfig :: CoprConfig
defaultConfig = CoprConfig {
domain = "copr.fedoraproject.org"
, port = 80
, ssl = False
, login = ""
, token = ""
}
withConfig :: CoprConfig -> (CoprConfig -> IO a) -> IO a
withConfig = flip id
prepareRequest :: CoprConfig -> Method -> S.ByteString -> RequestBuilder ()
prepareRequest c m url = do
http m url
setAccept "application/json"
setContentType "application/json"
setAuthorizationBasic (login c) (token c)
finishRequest :: FromJSON a => Connection -> IO a
finishRequest cnx = do
x <- receiveResponse cnx jsonHandler
closeConnection cnx
return x
apiGet :: FromJSON a => S.ByteString -> CoprConfig -> IO a
apiGet url c = withOpenSSL $ do
cnx <- if ssl c
then do
ctx <- baselineContextSSL
openConnectionSSL ctx (domain c) (port c)
else
openConnection (domain c) (port c)
q <- buildRequest $ prepareRequest c GET url
sendRequest cnx q emptyBody
finishRequest cnx
apiPost :: (ToJSON a, FromJSON b) => S.ByteString -> a -> CoprConfig -> IO b
apiPost url d c = withOpenSSL $ do
cnx <- if ssl c
then do
ctx <- baselineContextSSL
openConnectionSSL ctx (domain c) (port c)
else
openConnection (domain c) (port c)
q <- buildRequest $ do
prepareRequest c POST url
setContentLength $ LS.length (encode d)
body <- SBS.fromLazyByteString (encode d)
sendRequest cnx q (inputStreamBody body)
finishRequest cnx
coprs :: Username
-> CoprConfig
-> IO Coprs
coprs u = apiGet ("/api/coprs/" <> u <> "/")
new :: Username
-> CoprProject
-> CoprConfig
-> IO NewCoprResponse
new u = apiPost ("/api/coprs/" <> u <> "/new/")
addBuild :: Username
-> ProjectName
-> CoprBuild
-> CoprConfig
-> IO CoprBuildResponse
addBuild u p = apiPost ("/api/coprs/" <> u <> "/" <> p <> "/new_build/")
buildStatus :: Int
-> CoprConfig
-> IO CoprStatusResponse
buildStatus i = apiGet ("/api/coprs/build_status/" <> C8.pack (show i) <> "/")