servant-client-0.20.3.0: Automatic derivation of querying functions for servant

Safe Haskell | None
Language | Haskell2010

Servant.Client.Internal.HttpClient.Streaming

Synopsis

- client :: HasClient ClientM api => Proxy api -> Client ClientM api
- newtype ClientM a = ClientM {
  - unClientM :: ReaderT ClientEnv (ExceptT ClientError (Codensity IO)) a
  }
- runClientM :: NFData a => ClientM a -> ClientEnv -> IO (Either ClientError a)
- hoistClient :: HasClient ClientM api => Proxy api -> (forall a. m a -> n a) -> Client m api -> Client n api
- withClientM :: ClientM a -> ClientEnv -> (Either ClientError a -> IO b) -> IO b
- performRequest :: Maybe [Status] -> Request -> ClientM Response
- performWithStreamingRequest :: Request -> (StreamingResponse -> IO a) -> ClientM a
- data ClientEnv = ClientEnv {
  - manager :: Manager
  - baseUrl :: BaseUrl
  - cookieJar :: Maybe (TVar CookieJar)
  - makeClientRequest :: BaseUrl -> Request -> IO Request
  - middleware :: ClientMiddleware
  }
- mkClientEnv :: Manager -> BaseUrl -> ClientEnv
- clientResponseToResponse :: (a -> b) -> Response a -> ResponseF b
- defaultMakeClientRequest :: Applicative f => BaseUrl -> Request -> f Request
- catchConnectionError :: IO a -> IO (Either ClientError a)

# Documentation

client :: HasClient ClientM api => Proxy api -> Client ClientM api Source #

Generates a set of client functions for an API.

Example:

    type API = Capture "no" Int :> Get '[JSON] Int
           :<|> Get '[JSON] [Bool]

    api :: Proxy API
    api = Proxy

    getInt :: Int -> ClientM Int
    getBools :: ClientM [Bool]
    getInt :<|> getBools = client api

newtype ClientM a Source #

`ClientM` is the monad in which client functions run. Contains the `Manager` and `BaseUrl` used for requests in the reader environment.

Constructors

ClientM |

Fields

- unClientM :: ReaderT ClientEnv (ExceptT ClientError (Codensity IO)) a

#### Instances

Instances details

MonadIO ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods liftIO :: IO a -> ClientM a Source #
Applicative ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods pure :: a -> ClientM a Source # (<*>) :: ClientM (a -> b) -> ClientM a -> ClientM b Source # liftA2 :: (a -> b -> c) -> ClientM a -> ClientM b -> ClientM c Source # (*>) :: ClientM a -> ClientM b -> ClientM b Source # (<*) :: ClientM a -> ClientM b -> ClientM a Source #
Functor ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods fmap :: (a -> b) -> ClientM a -> ClientM b Source # (<$) :: a -> ClientM b -> ClientM a Source #
Monad ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods (>>=) :: ClientM a -> (a -> ClientM b) -> ClientM b Source # (>>) :: ClientM a -> ClientM b -> ClientM b Source # return :: a -> ClientM a Source #
Alt ClientM Source # | Try clients in order, last error is preserved.
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods (<!>) :: ClientM a -> ClientM a -> ClientM a Source # some :: Applicative ClientM => ClientM a -> ClientM [a] Source # many :: Applicative ClientM => ClientM a -> ClientM [a] Source #
RunClient ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods runRequestAcceptStatus :: Maybe [Status] -> Request -> ClientM Response Source # throwClientError :: ClientError -> ClientM a Source #
RunStreamingClient ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods withStreamingRequest :: Request -> (StreamingResponse -> IO a) -> ClientM a Source #
MonadError ClientError ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods throwError :: ClientError -> ClientM a Source # catchError :: ClientM a -> (ClientError -> ClientM a) -> ClientM a Source #
MonadReader ClientEnv ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods ask :: ClientM ClientEnv Source # local :: (ClientEnv -> ClientEnv) -> ClientM a -> ClientM a Source # reader :: (ClientEnv -> a) -> ClientM a Source #
MonadBase IO ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods liftBase :: IO α -> ClientM α Source #
Generic (ClientM a) Source # |

Instance details

Defined in Servant.Client.Internal.HttpClient.Streaming

Associated Types

type Rep (ClientM a) |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming type Rep (ClientM a) = Rep (ReaderT ClientEnv (ExceptT ClientError (Codensity IO)) a)

Methods

from :: ClientM a -> Rep (ClientM a) x Source #

to :: Rep (ClientM a) x -> ClientM a Source #

type Rep (ClientM a) Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming type Rep (ClientM a) = Rep (ReaderT ClientEnv (ExceptT ClientError (Codensity IO)) a)

runClientM :: NFData a => ClientM a -> ClientEnv -> IO (Either ClientError a) Source #

A `runClientM` variant for streaming client.

It allows using this module's `ClientM` in a direct style. The `NFData` constraint however prevents using this function with genuine streaming response types (`SourceT`, `Conduit`, pipes `Proxy` or `Machine`). For those you have to use `withClientM`.

Note: we `force` the result, so the likelihood of accidentally leaking a connection is smaller. Use with care.

hoistClient :: HasClient ClientM api => Proxy api -> (forall a. m a -> n a) -> Client m api -> Client n api Source #

Change the monad the client functions live in, by supplying a conversion function (a natural transformation to be precise).

For example, assuming you have some `manager :: `Manager`` and `baseurl :: `BaseUrl`` around:

    type API = Get '[JSON] Int :<|> Capture "n" Int :> Post '[JSON] Int
    api :: Proxy API
    api = Proxy
    getInt :: IO Int
    postInt :: Int -> IO Int
    getInt :<|> postInt = hoistClient api (flip runClientM cenv) (client api)
      where cenv = mkClientEnv manager baseurl

withClientM :: ClientM a -> ClientEnv -> (Either ClientError a -> IO b) -> IO b Source #

performRequest :: Maybe [Status] -> Request -> ClientM Response Source #

performWithStreamingRequest :: Request -> (StreamingResponse -> IO a) -> ClientM a Source #

TODO: support UVerb (`acceptStatus` argument, like in `performRequest` above).

data ClientEnv Source #

The environment in which a request is run. The `baseUrl` and `makeClientRequest` function are used to create a `http-client` request. Cookies are then added to that request if a `CookieJar` is set on the environment. Finally the request is executed with the `manager`. The `makeClientRequest` function can be used to modify the request to execute and set values which are not specified on a `servant` `Request` like `responseTimeout` or `redirectCount`

Constructors

ClientEnv |

Fields

- manager :: Manager
- baseUrl :: BaseUrl
- cookieJar :: Maybe (TVar CookieJar)
- makeClientRequest :: BaseUrl -> Request -> IO Request

  this function can be used to customize the creation of `http-client` requests from `servant` requests. Default value: `defaultMakeClientRequest` Note that: 1. `makeClientRequest` exists to allow overriding operational semantics e.g. `responseTimeout` per request, If you need global modifications, you should use `managerModifyRequest` 2. the `cookieJar`, if defined, is being applied after `makeClientRequest` is called.

- middleware :: ClientMiddleware

#### Instances

Instances details

MonadReader ClientEnv ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient Methods ask :: ClientM ClientEnv Source # local :: (ClientEnv -> ClientEnv) -> ClientM a -> ClientM a Source # reader :: (ClientEnv -> a) -> ClientM a Source #
MonadReader ClientEnv ClientM Source # |
Instance details Defined in Servant.Client.Internal.HttpClient.Streaming Methods ask :: ClientM ClientEnv Source # local :: (ClientEnv -> ClientEnv) -> ClientM a -> ClientM a Source # reader :: (ClientEnv -> a) -> ClientM a Source #

mkClientEnv :: Manager -> BaseUrl -> ClientEnv Source #

`ClientEnv` smart constructor.

clientResponseToResponse :: (a -> b) -> Response a -> ResponseF b Source #

defaultMakeClientRequest :: Applicative f => BaseUrl -> Request -> f Request Source #

Create a `http-client` `Request` from a `servant` `Request` The `host`, `path` and `port` fields are extracted from the `BaseUrl` otherwise the body, headers and query string are derived from the `servant` `Request`

Note that `Applicative` dependency is not really needed for this function implementation. But in the past the return type was wrapped into `IO` without a necessity breaking the API backward-compatibility. In order to not break the API again it was changed to `Applicative` so that you can just use something like `Data.Functor.Identity` without a need to involve `IO` but still keeping it compatible with the code written when it was typed as `IO`.

catchConnectionError :: IO a -> IO (Either ClientError a) Source #