{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | This module contains the internal gerrit REST client
module Gerrit.Client
  ( GerritClient (baseUrl),
    withClient,
    gerritGet,
    gerritPost,
    getClient,
  )
where

import Data.Aeson (FromJSON, ToJSON, eitherDecode, encode)
import qualified Data.ByteString.Lazy as BSL
import Data.Text (Text, unpack)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Network.HTTP.Client
import Network.HTTP.Client.OpenSSL (newOpenSSLManager, withOpenSSL)

-- | The GerritClient record, use 'withClient' to create
data GerritClient = GerritClient
  { GerritClient -> Text
baseUrl :: Text,
    GerritClient -> Manager
manager :: Manager,
    GerritClient -> Maybe (Text, Text)
auth :: Maybe (Text, Text)
  }

-- | Need to be call through withOpenSSL
getClient :: Text -> Maybe (Text, Text) -> IO GerritClient
getClient :: Text -> Maybe (Text, Text) -> IO GerritClient
getClient Text
url Maybe (Text, Text)
auth = do
  let baseUrl :: Text
baseUrl = (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
url Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/"
  Manager
manager <- IO Manager
forall (m :: * -> *). MonadIO m => m Manager
newOpenSSLManager
  GerritClient -> IO GerritClient
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GerritClient -> IO GerritClient)
-> GerritClient -> IO GerritClient
forall a b. (a -> b) -> a -> b
$ GerritClient :: Text -> Manager -> Maybe (Text, Text) -> GerritClient
GerritClient {Maybe (Text, Text)
Text
Manager
manager :: Manager
baseUrl :: Text
auth :: Maybe (Text, Text)
auth :: Maybe (Text, Text)
manager :: Manager
baseUrl :: Text
..}

-- | Create the 'GerritClient'
withClient ::
  -- | The gerrit api url
  Text ->
  -- | Credentials (login, password) [Optional]
  Maybe (Text, Text) ->
  -- | The callback
  (GerritClient -> IO a) ->
  -- | withClient performs the IO
  IO a
withClient :: Text -> Maybe (Text, Text) -> (GerritClient -> IO a) -> IO a
withClient Text
url Maybe (Text, Text)
creds GerritClient -> IO a
callBack = IO a -> IO a
forall a. IO a -> IO a
withOpenSSL (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
  GerritClient
client <- Text -> Maybe (Text, Text) -> IO GerritClient
getClient Text
url Maybe (Text, Text)
creds
  GerritClient -> IO a
callBack GerritClient
client

gerritDecode :: (FromJSON a, Applicative f) => Response BSL.ByteString -> f a
gerritDecode :: Response ByteString -> f a
gerritDecode Response ByteString
response = case ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> Either String a) -> ByteString -> Either String a
forall a b. (a -> b) -> a -> b
$ Int64 -> ByteString -> ByteString
BSL.drop Int64
5 (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ Response ByteString -> ByteString
forall body. Response body -> body
responseBody Response ByteString
response of
  Left String
err -> String -> f a
forall a. HasCallStack => String -> a
error (String -> f a) -> String -> f a
forall a b. (a -> b) -> a -> b
$ String
"Decoding of " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show (Response ByteString -> ByteString
forall body. Response body -> body
responseBody Response ByteString
response) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" failed with: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
err
  Right a
a -> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a

gerritPost :: (ToJSON a, FromJSON b) => Text -> a -> GerritClient -> IO b
gerritPost :: Text -> a -> GerritClient -> IO b
gerritPost Text
path a
postData GerritClient {Maybe (Text, Text)
Text
Manager
auth :: Maybe (Text, Text)
manager :: Manager
baseUrl :: Text
auth :: GerritClient -> Maybe (Text, Text)
manager :: GerritClient -> Manager
baseUrl :: GerritClient -> Text
..} =
  do
    Request
initRequest <- case Maybe (Text, Text)
auth of
      Just (Text
user, Text
pass) ->
        ByteString -> ByteString -> Request -> Request
applyBasicAuth (Text -> ByteString
T.encodeUtf8 Text
user) (Text -> ByteString
T.encodeUtf8 Text
pass)
          (Request -> Request) -> IO Request -> IO Request
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseUrlThrow (Text -> String
unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text
baseUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"a/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
path)
      Maybe (Text, Text)
Nothing -> String -> IO Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseUrlThrow (Text -> String
unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text
baseUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
path)
    let request :: Request
request =
          Request
initRequest
            { method :: ByteString
method = ByteString
"POST",
              requestHeaders :: RequestHeaders
requestHeaders = Request -> RequestHeaders
requestHeaders Request
initRequest RequestHeaders -> RequestHeaders -> RequestHeaders
forall a. Semigroup a => a -> a -> a
<> [(HeaderName
"Content-Type", ByteString
"application/json; charset=UTF-8")],
              requestBody :: RequestBody
requestBody = ByteString -> RequestBody
RequestBodyLBS (ByteString -> RequestBody) -> ByteString -> RequestBody
forall a b. (a -> b) -> a -> b
$ a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
postData
            }
    Response ByteString
response <- Request -> Manager -> IO (Response ByteString)
httpLbs Request
request Manager
manager
    Response ByteString -> IO b
forall a (f :: * -> *).
(FromJSON a, Applicative f) =>
Response ByteString -> f a
gerritDecode Response ByteString
response

gerritGet :: (FromJSON a) => Text -> GerritClient -> IO a
gerritGet :: Text -> GerritClient -> IO a
gerritGet Text
path GerritClient {Maybe (Text, Text)
Text
Manager
auth :: Maybe (Text, Text)
manager :: Manager
baseUrl :: Text
auth :: GerritClient -> Maybe (Text, Text)
manager :: GerritClient -> Manager
baseUrl :: GerritClient -> Text
..} =
  do
    let url :: Text
url = Text
baseUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
path
    Request
request <- String -> IO Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseUrlThrow (Text -> String
unpack Text
url)
    Response ByteString
response <- Request -> Manager -> IO (Response ByteString)
httpLbs Request
request Manager
manager
    Response ByteString -> IO a
forall a (f :: * -> *).
(FromJSON a, Applicative f) =>
Response ByteString -> f a
gerritDecode Response ByteString
response