    {-
       OpenAPI Petstore

       This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\

       OpenAPI Version: 3.0.1
       OpenAPI Petstore API version: 1.0.0
       Generated by OpenAPI Generator (https://openapi-generator.tech)
    -}

    {-|
    Module : OpenAPIPetstore.Client
    -}

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE RecordWildCards #-}
    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE DeriveFunctor #-}
    {-# LANGUAGE DeriveFoldable #-}
    {-# LANGUAGE DeriveTraversable #-}
    {-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-}

    module OpenAPIPetstore.Client where

    import OpenAPIPetstore.Core
    import OpenAPIPetstore.Logging
    import OpenAPIPetstore.MimeTypes

    import qualified Control.Exception.Safe as E
    import qualified Control.Monad.IO.Class as P
    import qualified Control.Monad as P
    import qualified Data.Aeson.Types as A
    import qualified Data.ByteString as B
    import qualified Data.ByteString.Char8 as BC
    import qualified Data.ByteString.Lazy as BL
    import qualified Data.ByteString.Lazy.Char8 as BCL
    import qualified Data.Proxy as P (Proxy(..))
    import qualified Data.Text as T
    import qualified Data.Text.Encoding as T
    import qualified Network.HTTP.Client as NH
    import qualified Network.HTTP.Client.MultipartFormData as NH
    import qualified Network.HTTP.Types as NH
    import qualified Web.FormUrlEncoded as WH
    import qualified Web.HttpApiData as WH

    import Data.Function ((&))
    import Data.Monoid ((<>))
    import Data.Text (Text)
    import GHC.Exts (IsString(..))

    -- * Dispatch

    -- ** Lbs

    -- | send a request returning the raw http response
    dispatchLbs
      :: (Produces req accept, MimeType contentType)
      => NH.Manager -- ^ http-client Connection manager
      -> OpenAPIPetstoreConfig -- ^ config
      -> OpenAPIPetstoreRequest req contentType res accept -- ^ request
      -> IO (NH.Response BCL.ByteString) -- ^ response
    dispatchLbs :: forall req accept contentType res.
    (Produces req accept, MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchLbs Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request  = do
      InitRequest req contentType res accept
    initReq <- OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (InitRequest req contentType res accept)
    forall accept contentType req res.
    (MimeType accept, MimeType contentType) =>
    OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (InitRequest req contentType res accept)
    _toInitRequest OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request
      Manager
    -> OpenAPIPetstoreConfig
    -> InitRequest req contentType res accept
    -> IO (Response ByteString)
    forall req contentType res accept.
    Manager
    -> OpenAPIPetstoreConfig
    -> InitRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchInitUnsafe Manager
    manager OpenAPIPetstoreConfig
    config InitRequest req contentType res accept
    initReq

    -- ** Mime

    -- | pair of decoded http body and http response
    data MimeResult res =
      MimeResult { forall res. MimeResult res -> Either MimeError res
    mimeResult :: Either MimeError res -- ^ decoded http body
                 , forall res. MimeResult res -> Response ByteString
    mimeResultResponse :: NH.Response BCL.ByteString -- ^ http response
                 }
      deriving (Int -> MimeResult res -> ShowS
    [MimeResult res] -> ShowS
    MimeResult res -> String
    (Int -> MimeResult res -> ShowS)
    -> (MimeResult res -> String)
    -> ([MimeResult res] -> ShowS)
    -> Show (MimeResult res)
    forall res. Show res => Int -> MimeResult res -> ShowS
    forall res. Show res => [MimeResult res] -> ShowS
    forall res. Show res => MimeResult res -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [MimeResult res] -> ShowS
    $cshowList :: forall res. Show res => [MimeResult res] -> ShowS
    show :: MimeResult res -> String
    $cshow :: forall res. Show res => MimeResult res -> String
    showsPrec :: Int -> MimeResult res -> ShowS
    $cshowsPrec :: forall res. Show res => Int -> MimeResult res -> ShowS
    Show, (forall a b. (a -> b) -> MimeResult a -> MimeResult b)
    -> (forall a b. a -> MimeResult b -> MimeResult a)
    -> Functor MimeResult
    forall a b. a -> MimeResult b -> MimeResult a
    forall a b. (a -> b) -> MimeResult a -> MimeResult b
    forall (f :: * -> *).
    (forall a b. (a -> b) -> f a -> f b)
    -> (forall a b. a -> f b -> f a) -> Functor f
    <$ :: forall a b. a -> MimeResult b -> MimeResult a
    $c<$ :: forall a b. a -> MimeResult b -> MimeResult a
    fmap :: forall a b. (a -> b) -> MimeResult a -> MimeResult b
    $cfmap :: forall a b. (a -> b) -> MimeResult a -> MimeResult b
    Functor, (forall m. Monoid m => MimeResult m -> m)
    -> (forall m a. Monoid m => (a -> m) -> MimeResult a -> m)
    -> (forall m a. Monoid m => (a -> m) -> MimeResult a -> m)
    -> (forall a b. (a -> b -> b) -> b -> MimeResult a -> b)
    -> (forall a b. (a -> b -> b) -> b -> MimeResult a -> b)
    -> (forall b a. (b -> a -> b) -> b -> MimeResult a -> b)
    -> (forall b a. (b -> a -> b) -> b -> MimeResult a -> b)
    -> (forall a. (a -> a -> a) -> MimeResult a -> a)
    -> (forall a. (a -> a -> a) -> MimeResult a -> a)
    -> (forall a. MimeResult a -> [a])
    -> (forall a. MimeResult a -> Bool)
    -> (forall a. MimeResult a -> Int)
    -> (forall a. Eq a => a -> MimeResult a -> Bool)
    -> (forall a. Ord a => MimeResult a -> a)
    -> (forall a. Ord a => MimeResult a -> a)
    -> (forall a. Num a => MimeResult a -> a)
    -> (forall a. Num a => MimeResult a -> a)
    -> Foldable MimeResult
    forall a. Eq a => a -> MimeResult a -> Bool
    forall a. Num a => MimeResult a -> a
    forall a. Ord a => MimeResult a -> a
    forall m. Monoid m => MimeResult m -> m
    forall a. MimeResult a -> Bool
    forall a. MimeResult a -> Int
    forall a. MimeResult a -> [a]
    forall a. (a -> a -> a) -> MimeResult a -> a
    forall m a. Monoid m => (a -> m) -> MimeResult a -> m
    forall b a. (b -> a -> b) -> b -> MimeResult a -> b
    forall a b. (a -> b -> b) -> b -> MimeResult a -> b
    forall (t :: * -> *).
    (forall m. Monoid m => t m -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall m a. Monoid m => (a -> m) -> t a -> m)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall a b. (a -> b -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall b a. (b -> a -> b) -> b -> t a -> b)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. (a -> a -> a) -> t a -> a)
    -> (forall a. t a -> [a])
    -> (forall a. t a -> Bool)
    -> (forall a. t a -> Int)
    -> (forall a. Eq a => a -> t a -> Bool)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Ord a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> (forall a. Num a => t a -> a)
    -> Foldable t
    product :: forall a. Num a => MimeResult a -> a
    $cproduct :: forall a. Num a => MimeResult a -> a
    sum :: forall a. Num a => MimeResult a -> a
    $csum :: forall a. Num a => MimeResult a -> a
    minimum :: forall a. Ord a => MimeResult a -> a
    $cminimum :: forall a. Ord a => MimeResult a -> a
    maximum :: forall a. Ord a => MimeResult a -> a
    $cmaximum :: forall a. Ord a => MimeResult a -> a
    elem :: forall a. Eq a => a -> MimeResult a -> Bool
    $celem :: forall a. Eq a => a -> MimeResult a -> Bool
    length :: forall a. MimeResult a -> Int
    $clength :: forall a. MimeResult a -> Int
    null :: forall a. MimeResult a -> Bool
    $cnull :: forall a. MimeResult a -> Bool
    toList :: forall a. MimeResult a -> [a]
    $ctoList :: forall a. MimeResult a -> [a]
    foldl1 :: forall a. (a -> a -> a) -> MimeResult a -> a
    $cfoldl1 :: forall a. (a -> a -> a) -> MimeResult a -> a
    foldr1 :: forall a. (a -> a -> a) -> MimeResult a -> a
    $cfoldr1 :: forall a. (a -> a -> a) -> MimeResult a -> a
    foldl' :: forall b a. (b -> a -> b) -> b -> MimeResult a -> b
    $cfoldl' :: forall b a. (b -> a -> b) -> b -> MimeResult a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> MimeResult a -> b
    $cfoldl :: forall b a. (b -> a -> b) -> b -> MimeResult a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> MimeResult a -> b
    $cfoldr' :: forall a b. (a -> b -> b) -> b -> MimeResult a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> MimeResult a -> b
    $cfoldr :: forall a b. (a -> b -> b) -> b -> MimeResult a -> b
    foldMap' :: forall m a. Monoid m => (a -> m) -> MimeResult a -> m
    $cfoldMap' :: forall m a. Monoid m => (a -> m) -> MimeResult a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> MimeResult a -> m
    $cfoldMap :: forall m a. Monoid m => (a -> m) -> MimeResult a -> m
    fold :: forall m. Monoid m => MimeResult m -> m
    $cfold :: forall m. Monoid m => MimeResult m -> m
    Foldable, Functor MimeResult
    Foldable MimeResult
    Functor MimeResult
    -> Foldable MimeResult
    -> (forall (f :: * -> *) a b.
        Applicative f =>
        (a -> f b) -> MimeResult a -> f (MimeResult b))
    -> (forall (f :: * -> *) a.
        Applicative f =>
        MimeResult (f a) -> f (MimeResult a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> MimeResult a -> m (MimeResult b))
    -> (forall (m :: * -> *) a.
        Monad m =>
        MimeResult (m a) -> m (MimeResult a))
    -> Traversable MimeResult
    forall (t :: * -> *).
    Functor t
    -> Foldable t
    -> (forall (f :: * -> *) a b.
        Applicative f =>
        (a -> f b) -> t a -> f (t b))
    -> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
    -> (forall (m :: * -> *) a b.
        Monad m =>
        (a -> m b) -> t a -> m (t b))
    -> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
    -> Traversable t
    forall (m :: * -> *) a.
    Monad m =>
    MimeResult (m a) -> m (MimeResult a)
    forall (f :: * -> *) a.
    Applicative f =>
    MimeResult (f a) -> f (MimeResult a)
    forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MimeResult a -> m (MimeResult b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> MimeResult a -> f (MimeResult b)
    sequence :: forall (m :: * -> *) a.
    Monad m =>
    MimeResult (m a) -> m (MimeResult a)
    $csequence :: forall (m :: * -> *) a.
    Monad m =>
    MimeResult (m a) -> m (MimeResult a)
    mapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MimeResult a -> m (MimeResult b)
    $cmapM :: forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MimeResult a -> m (MimeResult b)
    sequenceA :: forall (f :: * -> *) a.
    Applicative f =>
    MimeResult (f a) -> f (MimeResult a)
    $csequenceA :: forall (f :: * -> *) a.
    Applicative f =>
    MimeResult (f a) -> f (MimeResult a)
    traverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> MimeResult a -> f (MimeResult b)
    $ctraverse :: forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> MimeResult a -> f (MimeResult b)
    Traversable)

    -- | pair of unrender/parser error and http response
    data MimeError =
      MimeError {
        MimeError -> String
    mimeError :: String -- ^ unrender/parser error
      , MimeError -> Response ByteString
    mimeErrorResponse :: NH.Response BCL.ByteString -- ^ http response
      } deriving (Int -> MimeError -> ShowS
    [MimeError] -> ShowS
    MimeError -> String
    (Int -> MimeError -> ShowS)
    -> (MimeError -> String)
    -> ([MimeError] -> ShowS)
    -> Show MimeError
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [MimeError] -> ShowS
    $cshowList :: [MimeError] -> ShowS
    show :: MimeError -> String
    $cshow :: MimeError -> String
    showsPrec :: Int -> MimeError -> ShowS
    $cshowsPrec :: Int -> MimeError -> ShowS
    Show)

    -- | send a request returning the 'MimeResult'
    dispatchMime
      :: forall req contentType res accept. (Produces req accept, MimeUnrender accept res, MimeType contentType)
      => NH.Manager -- ^ http-client Connection manager
      -> OpenAPIPetstoreConfig -- ^ config
      -> OpenAPIPetstoreRequest req contentType res accept -- ^ request
      -> IO (MimeResult res) -- ^ response
    dispatchMime :: forall req contentType res accept.
    (Produces req accept, MimeUnrender accept res,
     MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (MimeResult res)
    dispatchMime Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request = do
      Response ByteString
    httpResponse <- Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (Response ByteString)
    forall req accept contentType res.
    (Produces req accept, MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchLbs Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request
      let statusCode :: Int
    statusCode = Status -> Int
    NH.statusCode (Status -> Int)
    -> (Response ByteString -> Status) -> Response ByteString -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Response ByteString -> Status
    forall body. Response body -> Status
    NH.responseStatus (Response ByteString -> Int) -> Response ByteString -> Int
    forall a b. (a -> b) -> a -> b
    $ Response ByteString
    httpResponse
      Either MimeError res
    parsedResult <-
        Text -> OpenAPIPetstoreConfig -> LogExec IO (Either MimeError res)
    forall (m :: * -> *) a.
    (MonadCatch m, MonadIO m) =>
    Text -> OpenAPIPetstoreConfig -> LogExec m a
    runConfigLogWithExceptions Text
    "Client" OpenAPIPetstoreConfig
    config LogExec IO (Either MimeError res)
    -> LogExec IO (Either MimeError res)
    forall a b. (a -> b) -> a -> b
    $
        do if (Int
    statusCode Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    400 Bool -> Bool -> Bool
    && Int
    statusCode Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    600)
             then do
               let s :: String
    s = String
    "error statusCode: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> String
    forall a. Show a => a -> String
    show Int
    statusCode
               Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    "Client" LogLevel
    levelError (String -> Text
    T.pack String
    s)
               Either MimeError res -> KatipT IO (Either MimeError res)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (MimeError -> Either MimeError res
    forall a b. a -> Either a b
    Left (String -> Response ByteString -> MimeError
    MimeError String
    s Response ByteString
    httpResponse))
             else case Proxy accept -> ByteString -> Either String res
    forall mtype o.
    MimeUnrender mtype o =>
    Proxy mtype -> ByteString -> Either String o
    mimeUnrender (Proxy accept
    forall {k} (t :: k). Proxy t
    P.Proxy :: P.Proxy accept) (Response ByteString -> ByteString
    forall body. Response body -> body
    NH.responseBody Response ByteString
    httpResponse) of
               Left String
    s -> do
                 Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    "Client" LogLevel
    levelError (String -> Text
    T.pack String
    s)
                 Either MimeError res -> KatipT IO (Either MimeError res)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (MimeError -> Either MimeError res
    forall a b. a -> Either a b
    Left (String -> Response ByteString -> MimeError
    MimeError String
    s Response ByteString
    httpResponse))
               Right res
    r -> Either MimeError res -> KatipT IO (Either MimeError res)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (res -> Either MimeError res
    forall a b. b -> Either a b
    Right res
    r)
      MimeResult res -> IO (MimeResult res)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either MimeError res -> Response ByteString -> MimeResult res
    forall res.
    Either MimeError res -> Response ByteString -> MimeResult res
    MimeResult Either MimeError res
    parsedResult Response ByteString
    httpResponse)

    -- | like 'dispatchMime', but only returns the decoded http body
    dispatchMime'
      :: (Produces req accept, MimeUnrender accept res, MimeType contentType)
      => NH.Manager -- ^ http-client Connection manager
      -> OpenAPIPetstoreConfig -- ^ config
      -> OpenAPIPetstoreRequest req contentType res accept -- ^ request
      -> IO (Either MimeError res) -- ^ response
    dispatchMime' :: forall req accept res contentType.
    (Produces req accept, MimeUnrender accept res,
     MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (Either MimeError res)
    dispatchMime' Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request  = do
        MimeResult Either MimeError res
    parsedResult Response ByteString
    _ <- Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (MimeResult res)
    forall req contentType res accept.
    (Produces req accept, MimeUnrender accept res,
     MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (MimeResult res)
    dispatchMime Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request
        Either MimeError res -> IO (Either MimeError res)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Either MimeError res
    parsedResult

    -- ** Unsafe

    -- | like 'dispatchReqLbs', but does not validate the operation is a 'Producer' of the "accept" 'MimeType'.  (Useful if the server's response is undocumented)
    dispatchLbsUnsafe
      :: (MimeType accept, MimeType contentType)
      => NH.Manager -- ^ http-client Connection manager
      -> OpenAPIPetstoreConfig -- ^ config
      -> OpenAPIPetstoreRequest req contentType res accept -- ^ request
      -> IO (NH.Response BCL.ByteString) -- ^ response
    dispatchLbsUnsafe :: forall accept contentType req res.
    (MimeType accept, MimeType contentType) =>
    Manager
    -> OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchLbsUnsafe Manager
    manager OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request  = do
      InitRequest req contentType res accept
    initReq <- OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (InitRequest req contentType res accept)
    forall accept contentType req res.
    (MimeType accept, MimeType contentType) =>
    OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (InitRequest req contentType res accept)
    _toInitRequest OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    request
      Manager
    -> OpenAPIPetstoreConfig
    -> InitRequest req contentType res accept
    -> IO (Response ByteString)
    forall req contentType res accept.
    Manager
    -> OpenAPIPetstoreConfig
    -> InitRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchInitUnsafe Manager
    manager OpenAPIPetstoreConfig
    config InitRequest req contentType res accept
    initReq

    -- | dispatch an InitRequest
    dispatchInitUnsafe
      :: NH.Manager -- ^ http-client Connection manager
      -> OpenAPIPetstoreConfig -- ^ config
      -> InitRequest req contentType res accept -- ^ init request
      -> IO (NH.Response BCL.ByteString) -- ^ response
    dispatchInitUnsafe :: forall req contentType res accept.
    Manager
    -> OpenAPIPetstoreConfig
    -> InitRequest req contentType res accept
    -> IO (Response ByteString)
    dispatchInitUnsafe Manager
    manager OpenAPIPetstoreConfig
    config (InitRequest Request
    req) = do
      Text -> OpenAPIPetstoreConfig -> LogExec IO (Response ByteString)
    forall (m :: * -> *) a.
    (MonadCatch m, MonadIO m) =>
    Text -> OpenAPIPetstoreConfig -> LogExec m a
    runConfigLogWithExceptions Text
    src OpenAPIPetstoreConfig
    config LogExec IO (Response ByteString)
    -> LogExec IO (Response ByteString)
    forall a b. (a -> b) -> a -> b
    $
        do Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    src LogLevel
    levelInfo Text
    requestLogMsg
           Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    src LogLevel
    levelDebug Text
    requestDbgLogMsg
           Response ByteString
    res <- IO (Response ByteString) -> KatipT IO (Response ByteString)
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    P.liftIO (IO (Response ByteString) -> KatipT IO (Response ByteString))
    -> IO (Response ByteString) -> KatipT IO (Response ByteString)
    forall a b. (a -> b) -> a -> b
    $ Request -> Manager -> IO (Response ByteString)
    NH.httpLbs Request
    req Manager
    manager
           Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    src LogLevel
    levelInfo (Response ByteString -> Text
    forall {body}. Response body -> Text
    responseLogMsg Response ByteString
    res)
           Text -> LogLevel -> Text -> KatipT IO ()
    forall (m :: * -> *).
    (Applicative m, Katip m) =>
    Text -> LogLevel -> Text -> m ()
    _log Text
    src LogLevel
    levelDebug ((String -> Text
    T.pack (String -> Text)
    -> (Response ByteString -> String) -> Response ByteString -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Response ByteString -> String
    forall a. Show a => a -> String
    show) Response ByteString
    res)
           Response ByteString -> KatipT IO (Response ByteString)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Response ByteString
    res
      where
        src :: Text
    src = Text
    "Client"
        endpoint :: Text
    endpoint =
          String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $
          Method -> String
    BC.unpack (Method -> String) -> Method -> String
    forall a b. (a -> b) -> a -> b
    $
          Request -> Method
    NH.method Request
    req Method -> Method -> Method
    forall a. Semigroup a => a -> a -> a
    <> Method
    " " Method -> Method -> Method
    forall a. Semigroup a => a -> a -> a
    <> Request -> Method
    NH.host Request
    req Method -> Method -> Method
    forall a. Semigroup a => a -> a -> a
    <> Request -> Method
    NH.path Request
    req Method -> Method -> Method
    forall a. Semigroup a => a -> a -> a
    <> Request -> Method
    NH.queryString Request
    req
        requestLogMsg :: Text
    requestLogMsg = Text
    "REQ:" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    endpoint
        requestDbgLogMsg :: Text
    requestDbgLogMsg =
          Text
    "Headers=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> (String -> Text
    T.pack (String -> Text)
    -> (RequestHeaders -> String) -> RequestHeaders -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RequestHeaders -> String
    forall a. Show a => a -> String
    show) (Request -> RequestHeaders
    NH.requestHeaders Request
    req) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    " Body=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
          (case Request -> RequestBody
    NH.requestBody Request
    req of
             NH.RequestBodyLBS ByteString
    xs -> Method -> Text
    T.decodeUtf8 (ByteString -> Method
    BL.toStrict ByteString
    xs)
             RequestBody
    _ -> Text
    "<RequestBody>")
        responseStatusCode :: Response body -> Text
    responseStatusCode = (String -> Text
    T.pack (String -> Text) -> (Int -> String) -> Int -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> String
    forall a. Show a => a -> String
    show) (Int -> Text) -> (Response body -> Int) -> Response body -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Status -> Int
    NH.statusCode (Status -> Int)
    -> (Response body -> Status) -> Response body -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Response body -> Status
    forall body. Response body -> Status
    NH.responseStatus
        responseLogMsg :: Response body -> Text
    responseLogMsg Response body
    res =
          Text
    "RES:statusCode=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Response body -> Text
    forall {body}. Response body -> Text
    responseStatusCode Response body
    res Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    " (" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    endpoint Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ")"

    -- * InitRequest

    -- | wraps an http-client 'Request' with request/response type parameters
    newtype InitRequest req contentType res accept = InitRequest
      { forall req contentType res accept.
    InitRequest req contentType res accept -> Request
    unInitRequest :: NH.Request
      } deriving (Int -> InitRequest req contentType res accept -> ShowS
    [InitRequest req contentType res accept] -> ShowS
    InitRequest req contentType res accept -> String
    (Int -> InitRequest req contentType res accept -> ShowS)
    -> (InitRequest req contentType res accept -> String)
    -> ([InitRequest req contentType res accept] -> ShowS)
    -> Show (InitRequest req contentType res accept)
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    forall req contentType res accept.
    Int -> InitRequest req contentType res accept -> ShowS
    forall req contentType res accept.
    [InitRequest req contentType res accept] -> ShowS
    forall req contentType res accept.
    InitRequest req contentType res accept -> String
    showList :: [InitRequest req contentType res accept] -> ShowS
    $cshowList :: forall req contentType res accept.
    [InitRequest req contentType res accept] -> ShowS
    show :: InitRequest req contentType res accept -> String
    $cshow :: forall req contentType res accept.
    InitRequest req contentType res accept -> String
    showsPrec :: Int -> InitRequest req contentType res accept -> ShowS
    $cshowsPrec :: forall req contentType res accept.
    Int -> InitRequest req contentType res accept -> ShowS
    Show)

    -- |  Build an http-client 'Request' record from the supplied config and request
    _toInitRequest
      :: (MimeType accept, MimeType contentType)
      => OpenAPIPetstoreConfig -- ^ config
      -> OpenAPIPetstoreRequest req contentType res accept -- ^ request
      -> IO (InitRequest req contentType res accept) -- ^ initialized request
    _toInitRequest :: forall accept contentType req res.
    (MimeType accept, MimeType contentType) =>
    OpenAPIPetstoreConfig
    -> OpenAPIPetstoreRequest req contentType res accept
    -> IO (InitRequest req contentType res accept)
    _toInitRequest OpenAPIPetstoreConfig
    config OpenAPIPetstoreRequest req contentType res accept
    req0  =
      Text
    -> OpenAPIPetstoreConfig
    -> LogExec IO (InitRequest req contentType res accept)
    forall (m :: * -> *) a.
    (MonadCatch m, MonadIO m) =>
    Text -> OpenAPIPetstoreConfig -> LogExec m a
    runConfigLogWithExceptions Text
    "Client" OpenAPIPetstoreConfig
    config LogExec IO (InitRequest req contentType res accept)
    -> LogExec IO (InitRequest req contentType res accept)
    forall a b. (a -> b) -> a -> b
    $ do
        Request
    parsedReq <- IO Request -> KatipT IO Request
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    P.liftIO (IO Request -> KatipT IO Request)
    -> IO Request -> KatipT IO Request
    forall a b. (a -> b) -> a -> b
    $ String -> IO Request
    forall (m :: * -> *). MonadThrow m => String -> m Request
    NH.parseRequest (String -> IO Request) -> String -> IO Request
    forall a b. (a -> b) -> a -> b
    $ ByteString -> String
    BCL.unpack (ByteString -> String) -> ByteString -> String
    forall a b. (a -> b) -> a -> b
    $ ByteString -> ByteString -> ByteString
    BCL.append (OpenAPIPetstoreConfig -> ByteString
    configHost OpenAPIPetstoreConfig
    config) ([ByteString] -> ByteString
    BCL.concat (OpenAPIPetstoreRequest req contentType res accept -> [ByteString]
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept -> [ByteString]
    rUrlPath OpenAPIPetstoreRequest req contentType res accept
    req0))
        OpenAPIPetstoreRequest req contentType res accept
    req1 <- IO (OpenAPIPetstoreRequest req contentType res accept)
    -> KatipT IO (OpenAPIPetstoreRequest req contentType res accept)
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    P.liftIO (IO (OpenAPIPetstoreRequest req contentType res accept)
     -> KatipT IO (OpenAPIPetstoreRequest req contentType res accept))
    -> IO (OpenAPIPetstoreRequest req contentType res accept)
    -> KatipT IO (OpenAPIPetstoreRequest req contentType res accept)
    forall a b. (a -> b) -> a -> b
    $ OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreConfig
    -> IO (OpenAPIPetstoreRequest req contentType res accept)
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreConfig
    -> IO (OpenAPIPetstoreRequest req contentType res accept)
    _applyAuthMethods OpenAPIPetstoreRequest req contentType res accept
    req0 OpenAPIPetstoreConfig
    config
        Bool -> KatipT IO () -> KatipT IO ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    P.when
            (OpenAPIPetstoreConfig -> Bool
    configValidateAuthMethods OpenAPIPetstoreConfig
    config Bool -> Bool -> Bool
    && (Bool -> Bool
    not (Bool -> Bool)
    -> (OpenAPIPetstoreRequest req contentType res accept -> Bool)
    -> OpenAPIPetstoreRequest req contentType res accept
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [TypeRep] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([TypeRep] -> Bool)
    -> (OpenAPIPetstoreRequest req contentType res accept -> [TypeRep])
    -> OpenAPIPetstoreRequest req contentType res accept
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . OpenAPIPetstoreRequest req contentType res accept -> [TypeRep]
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept -> [TypeRep]
    rAuthTypes) OpenAPIPetstoreRequest req contentType res accept
    req1)
            (AuthMethodException -> KatipT IO ()
    forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
    E.throw (AuthMethodException -> KatipT IO ())
    -> AuthMethodException -> KatipT IO ()
    forall a b. (a -> b) -> a -> b
    $ String -> AuthMethodException
    AuthMethodException (String -> AuthMethodException) -> String -> AuthMethodException
    forall a b. (a -> b) -> a -> b
    $ String
    "AuthMethod not configured: " String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> (TypeRep -> String
    forall a. Show a => a -> String
    show (TypeRep -> String)
    -> (OpenAPIPetstoreRequest req contentType res accept -> TypeRep)
    -> OpenAPIPetstoreRequest req contentType res accept
    -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [TypeRep] -> TypeRep
    forall a. [a] -> a
    head ([TypeRep] -> TypeRep)
    -> (OpenAPIPetstoreRequest req contentType res accept -> [TypeRep])
    -> OpenAPIPetstoreRequest req contentType res accept
    -> TypeRep
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . OpenAPIPetstoreRequest req contentType res accept -> [TypeRep]
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept -> [TypeRep]
    rAuthTypes) OpenAPIPetstoreRequest req contentType res accept
    req1)
        let req2 :: OpenAPIPetstoreRequest req contentType res accept
    req2 = OpenAPIPetstoreRequest req contentType res accept
    req1 OpenAPIPetstoreRequest req contentType res accept
    -> (OpenAPIPetstoreRequest req contentType res accept
        -> OpenAPIPetstoreRequest req contentType res accept)
    -> OpenAPIPetstoreRequest req contentType res accept
    forall a b. a -> (a -> b) -> b
    & OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreRequest req contentType res accept
    forall req contentType res accept.
    MimeType contentType =>
    OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreRequest req contentType res accept
    _setContentTypeHeader OpenAPIPetstoreRequest req contentType res accept
    -> (OpenAPIPetstoreRequest req contentType res accept
        -> OpenAPIPetstoreRequest req contentType res accept)
    -> OpenAPIPetstoreRequest req contentType res accept
    forall a b. a -> (a -> b) -> b
    & OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreRequest req contentType res accept
    forall req contentType res accept.
    MimeType accept =>
    OpenAPIPetstoreRequest req contentType res accept
    -> OpenAPIPetstoreRequest req contentType res accept
    _setAcceptHeader
            params :: Params
    params = OpenAPIPetstoreRequest req contentType res accept -> Params
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept -> Params
    rParams OpenAPIPetstoreRequest req contentType res accept
    req2
            reqHeaders :: RequestHeaders
    reqHeaders = (HeaderName
    "User-Agent", Text -> Method
    forall a. ToHttpApiData a => a -> Method
    WH.toHeader (OpenAPIPetstoreConfig -> Text
    configUserAgent OpenAPIPetstoreConfig
    config)) (HeaderName, Method) -> RequestHeaders -> RequestHeaders
    forall a. a -> [a] -> [a]
    : Params -> RequestHeaders
    paramsHeaders Params
    params
            reqQuery :: Method
    reqQuery = let query :: Query
    query = Params -> Query
    paramsQuery Params
    params
                           queryExtraUnreserved :: Method
    queryExtraUnreserved = OpenAPIPetstoreConfig -> Method
    configQueryExtraUnreserved OpenAPIPetstoreConfig
    config
                       in if Method -> Bool
    B.null Method
    queryExtraUnreserved
                            then Bool -> Query -> Method
    NH.renderQuery Bool
    True Query
    query
                            else Bool -> PartialEscapeQuery -> Method
    NH.renderQueryPartialEscape Bool
    True (Method -> Query -> PartialEscapeQuery
    toPartialEscapeQuery Method
    queryExtraUnreserved Query
    query)
            pReq :: Request
    pReq = Request
    parsedReq { method :: Method
    NH.method = OpenAPIPetstoreRequest req contentType res accept -> Method
    forall req contentType res accept.
    OpenAPIPetstoreRequest req contentType res accept -> Method
    rMethod OpenAPIPetstoreRequest req contentType res accept
    req2
                            , requestHeaders :: RequestHeaders
    NH.requestHeaders = RequestHeaders
    reqHeaders
                            , queryString :: Method
    NH.queryString = Method
    reqQuery
                            }
        Request
    outReq <- case Params -> ParamBody
    paramsBody Params
    params of
            ParamBody
    ParamBodyNone -> Request -> KatipT IO Request
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Request
    pReq { requestBody :: RequestBody
    NH.requestBody = RequestBody
    forall a. Monoid a => a
    mempty })
            ParamBodyB Method
    bs -> Request -> KatipT IO Request
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Request
    pReq { requestBody :: RequestBody
    NH.requestBody = Method -> RequestBody
    NH.RequestBodyBS Method
    bs })
            ParamBodyBL ByteString
    bl -> Request -> KatipT IO Request
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Request
    pReq { requestBody :: RequestBody
    NH.requestBody = ByteString -> RequestBody
    NH.RequestBodyLBS ByteString
    bl })
            ParamBodyFormUrlEncoded Form
    form -> Request -> KatipT IO Request
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Request
    pReq { requestBody :: RequestBody
    NH.requestBody = ByteString -> RequestBody
    NH.RequestBodyLBS (Form -> ByteString
    WH.urlEncodeForm Form
    form) })
            ParamBodyMultipartFormData [Part]
    parts -> [Part] -> Request -> KatipT IO Request
    forall (m :: * -> *). MonadIO m => [Part] -> Request -> m Request
    NH.formDataBody [Part]
    parts Request
    pReq

        InitRequest req contentType res accept
    -> KatipT IO (InitRequest req contentType res accept)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Request -> InitRequest req contentType res accept
    forall req contentType res accept.
    Request -> InitRequest req contentType res accept
    InitRequest Request
    outReq)

    -- | modify the underlying Request
    modifyInitRequest :: InitRequest req contentType res accept -> (NH.Request -> NH.Request) -> InitRequest req contentType res accept
    modifyInitRequest :: forall req contentType res accept.
    InitRequest req contentType res accept
    -> (Request -> Request) -> InitRequest req contentType res accept
    modifyInitRequest (InitRequest Request
    req) Request -> Request
    f = Request -> InitRequest req contentType res accept
    forall req contentType res accept.
    Request -> InitRequest req contentType res accept
    InitRequest (Request -> Request
    f Request
    req)

    -- | modify the underlying Request (monadic)
    modifyInitRequestM :: Monad m => InitRequest req contentType res accept -> (NH.Request -> m NH.Request) -> m (InitRequest req contentType res accept)
    modifyInitRequestM :: forall (m :: * -> *) req contentType res accept.
    Monad m =>
    InitRequest req contentType res accept
    -> (Request -> m Request)
    -> m (InitRequest req contentType res accept)
    modifyInitRequestM (InitRequest Request
    req) Request -> m Request
    f = (Request -> InitRequest req contentType res accept)
    -> m Request -> m (InitRequest req contentType res accept)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Request -> InitRequest req contentType res accept
    forall req contentType res accept.
    Request -> InitRequest req contentType res accept
    InitRequest (Request -> m Request
    f Request
    req)

    -- ** Logging

    -- | Run a block using the configured logger instance
    runConfigLog
      :: P.MonadIO m
      => OpenAPIPetstoreConfig -> LogExec m a
    runConfigLog :: forall (m :: * -> *) a.
    MonadIO m =>
    OpenAPIPetstoreConfig -> LogExec m a
    runConfigLog OpenAPIPetstoreConfig
    config = OpenAPIPetstoreConfig -> LogExecWithContext
    configLogExecWithContext OpenAPIPetstoreConfig
    config (OpenAPIPetstoreConfig -> LogContext
    configLogContext OpenAPIPetstoreConfig
    config)

    -- | Run a block using the configured logger instance (logs exceptions)
    runConfigLogWithExceptions
      :: (E.MonadCatch m, P.MonadIO m)
      => T.Text -> OpenAPIPetstoreConfig -> LogExec m a
    runConfigLogWithExceptions :: forall (m :: * -> *) a.
    (MonadCatch m, MonadIO m) =>
    Text -> OpenAPIPetstoreConfig -> LogExec m a
    runConfigLogWithExceptions Text
    src OpenAPIPetstoreConfig
    config = OpenAPIPetstoreConfig -> LogExec m a
    forall (m :: * -> *) a.
    MonadIO m =>
    OpenAPIPetstoreConfig -> LogExec m a
    runConfigLog OpenAPIPetstoreConfig
    config LogExec m a -> (KatipT m a -> KatipT m a) -> LogExec m a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> KatipT m a -> KatipT m a
    forall (m :: * -> *) a.
    (Katip m, MonadCatch m, Applicative m) =>
    Text -> m a -> m a
    logExceptions Text
    src