-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A Type dependent Highlevel HTTP client library. -- -- A Type dependent highlevel HTTP client library inspired by -- servant-client. @package hreq-client @version 0.1.0.0 -- | HttpConfig is used in the Hreq Monad for HTTP client -- configuration module Hreq.Client.Internal.Config data HttpConfig HttpConfig :: BaseUrl -> StatusRange -> Maybe (TVar CookieJar) -> RetryPolicy -> Manager -> HttpConfig [httpBaseUrl] :: HttpConfig -> BaseUrl [httpStatuses] :: HttpConfig -> StatusRange [httpCookieJar] :: HttpConfig -> Maybe (TVar CookieJar) [httpRetryPolicy] :: HttpConfig -> RetryPolicy [httpManager] :: HttpConfig -> Manager -- | Valid Response status code range data StatusRange StatusRange :: StatusCode -> StatusCode -> StatusRange [statusUpper] :: StatusRange -> StatusCode [statusLower] :: StatusRange -> StatusCode -- | Function for creating a default HttpConfig createDefConfig :: BaseUrl -> IO HttpConfig -- | This module provides functionality for running the Hreq Monad -- as an HTTP client and the necessary required class instances, such as -- RunClient instance. module Hreq.Client.Internal.HTTP -- | Monad for running Http client requests newtype Hreq m a Hreq :: ReaderT HttpConfig m a -> Hreq m a [runHreq'] :: Hreq m a -> ReaderT HttpConfig m a class Monad m => RunClient (m :: Type -> Type) runClient :: RunClient m => Request -> m Response throwHttpError :: RunClient m => ClientError -> m a checkResponse :: RunClient m => Request -> Response -> m (Maybe ClientError) runHreq :: MonadIO m => BaseUrl -> Hreq m a -> m a runHreqWithConfig :: HttpConfig -> Hreq m a -> m a runHttpClient :: (MonadReader HttpConfig m, MonadIO m, RunClient m) => Request -> m Response checkHttpResponse :: MonadReader HttpConfig m => Request -> Response -> m (Maybe ClientError) requestToHTTPRequest :: BaseUrl -> Request -> Request httpResponsetoResponse :: (a -> b) -> Response a -> ResponseF b catchConnectionError :: IO a -> IO (Either ClientError a) instance Control.Monad.IO.Class.MonadIO m => Control.Monad.IO.Class.MonadIO (Hreq.Client.Internal.HTTP.Hreq m) instance Control.Monad.Trans.Class.MonadTrans Hreq.Client.Internal.HTTP.Hreq instance GHC.Base.Monad m => Control.Monad.Reader.Class.MonadReader Hreq.Client.Internal.Config.HttpConfig (Hreq.Client.Internal.HTTP.Hreq m) instance GHC.Base.Monad m => GHC.Base.Monad (Hreq.Client.Internal.HTTP.Hreq m) instance GHC.Base.Applicative m => GHC.Base.Applicative (Hreq.Client.Internal.HTTP.Hreq m) instance GHC.Base.Functor m => GHC.Base.Functor (Hreq.Client.Internal.HTTP.Hreq m) instance Control.Monad.IO.Unlift.MonadUnliftIO m => Control.Monad.IO.Unlift.MonadUnliftIO (Hreq.Client.Internal.HTTP.Hreq m) instance Hreq.Core.Client.RunClient.RunClient (Hreq.Client.Internal.HTTP.Hreq GHC.Types.IO) -- | Hreq is a high-level easy to use type-driven HTTP client library -- inspired by Servant-Client. Hreq provides an alternative approach to -- type-safe construction and interpretation of API endpoints for Http -- client requests. Hreq is greatly inspired by Servant Client. -- --
-- {-# LANGUAGE DataKinds #-}
-- {-# LANGUAGE DeriveAnyClass #-}
-- {-# LANGUAGE DeriveGeneric #-}
-- {-# LANGUAGE OverloadedStrings #-}
-- {-# LANGUAGE TypeApplications #-}
-- {-# LANGUAGE TypeOperators #-}
--
-- import Control.Monad.IO.Class (liftIO)
-- import Data.Aeson (FromJSON, ToJSON)
-- import Data.Text (Text)
-- import GHC.Generics (Generic)
-- import Hreq.Client
--
-- data User = User
-- { name :: Text
-- , age :: Int
-- } deriving (Show, Generic, FromJSON, ToJSON)
--
--
-- User service API URL
--
-- -- baseUrl :: BaseUrl -- baseUrl = HttpUrl "example.com" "user" ---- --
-- getUserByName :: RunClient m => Text -> m User -- getUserByName userName = hreq @(Capture Text :> GetJson User) (userName :. Empty) ---- -- The Capture Text :> GetJson User type with in -- getUserByName is an API endpoint type definition. -- -- The API type definition in this instance demands that a heterogeneous -- list containing a Text value is supplied to the hreq -- function. -- -- userName :. Empty forms the required -- heterogeneous list value for the hreq function. Finally, the -- API type states that we will obtain a JSON User -- response output. -- --
-- createUser :: RunClient m => User -> m () -- createUser user = hreq @(JsonBody User :> EmptyResponse POST) (user :. Empty) ---- --
-- getAllUsers :: RunClient m => m [User]
-- getAllUsers = hreq @("all" :> QueryFlag "old" :> GetJson [User]) Empty
--
--
-- -- main :: IO () -- main = runHreq baseUrl $ do -- reqUser <- getUserByName "allan" -- createdUser <- createUser newUser -- allUsers <- getAllUsers -- --Delete users with age equal to 20 -- hreq @(Capture Int :> EmptyResponse DELETE) (20 :. Empty) -- -- do something with API data -- liftIO $ print (reqUser, createdUser, allUsers) -- where -- newUser :: User -- newUser = User "allan" 12 ---- --
-- >>> type PathsQuery = "user" :> "allan" :> GetJson User ---- --
-- pathsExample :: RunClient m => m User ---- --
-- >>> pathsExample = hreq @PathsQuery Empty ---- --
-- >>> type SingleParam = Param "name" String :> GetJson User ---- --
-- singleParamExample :: RunClient m => m Response ---- --
-- >>> singleParamsExample = hreq @SingleParam ("allan" :. Empty)
--
--
-- -- >>> type MultiParams = Param "name" String :> Param "age" Int :> GetJson User ---- --
-- >>> type MultiParamsList = Params '["name" := String, "age" := Int] :> GetJson User ---- --
-- -- Note MultiParams and MultiParamsList are the same. -- -- Resulting URL is of the form http://example.com/api?name="allan"&age=20 ---- --
-- multiParamsExample :: RunClient m => m User ---- --
-- >>> multiParamsExample = hreq @MultiParams ("allan" :. 20 :. Empty)
--
--
-- -- >>> type SingleQueryFlag = "user" :> QueryFlag "male" :> GetJson User ---- --
-- singleQueryFlag :: RunClient m => m User ---- --
-- >>> singleQueryFlag = hreq @SingleQueryFlag Empty ---- --
-- >>> type MultiQueryFlags = "user" :> QueryFlag "male" :> QueryFlag "old" :> GetJson User ---- --
-- >>> type MultiQueryFlagList = "user" :> QueryFlags '["male", "old"] :> GetJson User ---- --
-- -- Note MultiQueryFlags and MultiQueryFlagsList are the same -- -- The query flag values are inferred from provided type level strings (symbols) -- -- Resulting URL is of the form http://example.com/api?male&old ---- --
-- multiFlagsExample :: RunClient m => m User ---- --
-- >>> multiFlagsExample = hreq @MultiQueryFlagList Empty ---- --
-- >>> type SingleCapture = Capture UserName :> GetJson User ---- --
-- >>> type MultiCapturesList = "users" :> Captures '[UserName, UserAge] :> GetJson User -- -- >>> type MultiCaptures = "users" :> Capture UserName :> Capture UserAge :> GetJson User ---- --
-- -- Resulting URL is of the form http://example.com/users/allan/12 -- -- Note that MultiCapturesList is equal to MultiCaptures. ---- --
-- captureExample :: RunClient m => m User ---- --
-- >>> captureExample = hreq @MultiCaptures $ UserName "allan" :. UserAge 12 :. Empty ---- --
-- >>> type CaptureAllExample = "users" :> CaptureAll String :> GetJson User ---- --
-- captureAllExample :: RunClient m => m User ---- --
-- >>> captureAllExample = hreq @CaptureAllExample $ ["allan", "alex", "brian"] :. Empty ---- --
-- >>> type ReqBodyQuery = "users" :> ReqBody User JSON :> GetJson User ---- -- The above query can be written as below: -- --
-- >>> type JsonBodyQuery = "users" :> JsonBody User :> GetJson User ---- --
-- >>> type GetPlainText a = Get '[ResBody PlainText a] ---- --
-- plainTextResponse :: RunClient m => m String ---- --
-- >>> plainTextResponse = hreq @("user" :> GetPlainText String) Empty
--
--
-- -- >>> type MultiResultsQuery = Get '[ ResBody JSON User, ResHeaders '[ "key-header" := String ] ] ---- --
-- multiResults :: RunClient m => m (Hlist '[ User, [Header] ]) ---- --
-- >>> multiResults = hreq @MultiResultsQuery Empty --module Hreq.Client class Monad m => RunClient (m :: Type -> Type) runClient :: RunClient m => Request -> m Response throwHttpError :: RunClient m => ClientError -> m a checkResponse :: RunClient m => Request -> Response -> m (Maybe ClientError) -- | Monad for running Http client requests newtype Hreq m a Hreq :: ReaderT HttpConfig m a -> Hreq m a [runHreq'] :: Hreq m a -> ReaderT HttpConfig m a runHreq :: MonadIO m => BaseUrl -> Hreq m a -> m a runHreqWithConfig :: HttpConfig -> Hreq m a -> m a data HttpConfig HttpConfig :: BaseUrl -> StatusRange -> Maybe (TVar CookieJar) -> RetryPolicy -> Manager -> HttpConfig [httpBaseUrl] :: HttpConfig -> BaseUrl [httpStatuses] :: HttpConfig -> StatusRange [httpCookieJar] :: HttpConfig -> Maybe (TVar CookieJar) [httpRetryPolicy] :: HttpConfig -> RetryPolicy [httpManager] :: HttpConfig -> Manager -- | Valid Response status code range data StatusRange StatusRange :: StatusCode -> StatusCode -> StatusRange [statusUpper] :: StatusRange -> StatusCode [statusLower] :: StatusRange -> StatusCode -- | Function for creating a default HttpConfig createDefConfig :: BaseUrl -> IO HttpConfig