module Patrol.Client where

import qualified Control.Monad.Catch as Catch
import qualified Control.Monad.IO.Class as IO
import qualified Data.Aeson as Aeson
import qualified Network.HTTP.Client as Client
import qualified Patrol.Exception.Problem as Problem
import qualified Patrol.Type.Dsn as Dsn
import qualified Patrol.Type.Event as Event
import qualified Patrol.Type.Response as Response

store ::
  (IO.MonadIO io, Catch.MonadThrow io) =>
  Client.Manager ->
  Dsn.Dsn ->
  Event.Event ->
  io Response.Response
store :: forall (io :: * -> *).
(MonadIO io, MonadThrow io) =>
Manager -> Dsn -> Event -> io Response
store Manager
manager Dsn
dsn Event
event = do
  Request
request <- forall (m :: * -> *). MonadThrow m => Dsn -> Event -> m Request
Event.intoRequest Dsn
dsn Event
event
  Response ByteString
response <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
IO.liftIO forall a b. (a -> b) -> a -> b
$ Request -> Manager -> IO (Response ByteString)
Client.httpLbs Request
request Manager
manager
  forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
Catch.throwM forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Problem
Problem.Problem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => a -> a -> a
mappend String
"invalid response body: ") forall (f :: * -> *) a. Applicative f => a -> f a
pure
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => ByteString -> Either String a
Aeson.eitherDecode
    forall a b. (a -> b) -> a -> b
$ forall body. Response body -> body
Client.responseBody Response ByteString
response