dormouse: Simple, Type-Safe, Testable HTTP Client

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

An HTTP client designed to be productive, easy to use, easy to test, flexible and safe!

It was designed with the following objectives in mind:

[Skip to Readme]


Change log
Dependencies aeson (>=1.4.2 && <2.0.0), attoparsec (>= && <0.14), base (>=4.7 && <5), bytestring (>=0.10.8 && <0.11.0), case-insensitive (>= && <2.0.0), containers (>= && <0.7), http-api-data (>= && <0.5), http-client (>= && <0.7.0), http-client-tls (>= && <0.4), http-types (>=0.12.3 && <0.13), mtl (>=2.2.2 && <3), safe-exceptions (>=0.1.7 && <0.2.0), streamly (>=0.7.2 && <0.8), streamly-bytestring (>=0.1.2 && <0.2), template-haskell (>=2.15.0 && <3.0.0), text (>=1.2.3 && <2.0.0) [details]
License BSD-3-Clause
Copyright 2020 Phil Curzon
Author Phil Curzon
Category Web
Home page
Bug tracker
Source repo head: git clone
Uploaded by philcurzon at 2020-09-24T23:29:50Z



Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Readme for dormouse-

[back to package description]


Dormouse is an HTTP client that will help you REST.

It was designed with the following objectives in mind:

Example use:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}

import Control.Monad.IO.Class
import Dormouse
import Data.Aeson.TH 
import Dormouse.Url.QQ

data UserDetails = UserDetails 
  { name :: String
  , nickname :: String
  , email :: String
  } deriving (Eq, Show)

deriveJSON defaultOptions ''UserDetails

data EchoedJson a = EchoedJson 
  { echoedjson :: a
  } deriving (Eq, Show)

deriveJSON defaultOptions {fieldLabelModifier = drop 6} ''EchoedJson

main :: IO ()
main = do
  manager <- newManager tlsManagerSettings
  runDormouse (DormouseConfig { clientManager = manager }) $ do
    let userDetails = UserDetails { name = "James T. Kirk", nickname = "Jim", email = ""}
    let req = accept json $ supplyBody json userDetails $ post [https||]
    response :: HttpResponse (EchoedJson UserDetails) <- expect req
    liftIO $ print response
    return ()

Constructing Urls

You can construct Urls using the helper QuasiQuoters:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}

import Dormouse
import Dormouse.Url.QQ

githubHttpsUrl :: Url "https"
githubHttpsUrl = [https||]

githubHttpUrl :: Url "http"
githubHttpUrl = [http||]

githubAnyUrl :: AnyUrl
githubAnyUrl = [url||]

You can use the Url Builder syntax to modify an existing Url safely to include paths, adding the import:

import Dormouse.Url.Builder

To allow:

dormouseHttpsUrl :: Url "https"
dormouseHttpsUrl = githubHttpsUrl </> "TheInnerLight" </> "dormouse"

The Url will be constructed safely so that any characters that wouldn't normally be allowed in a Url path are percent-encoded before the url is resolved by Dormouse.

You can also handle query parameters using similar syntax:

searchUrl :: Url "https"
searchUrl = [https||] </> "search" ? "q" =: ("haskell" :: String)

Building requests

GET requests

Building a GET request is simple using a Url

postmanEchoGetUrl :: Url "http"
postmanEchoGetUrl = [http||]

postmanEchoGetReq :: HttpRequest (Url "http") "GET" Empty EmptyPayload acceptTag
postmanEchoGetReq = get postmanEchoGetUrl

It is often useful to tell Dormouse about the expected Content-Type of the response in advance so that the correct Accept headers can be sent:

postmanEchoGetReq' :: HttpRequest (Url "http") "GET" Empty EmptyPayload acceptTag
postmanEchoGetReq' = accept json $ get postmanEchoGetUrl

POST requests

You can build POST requests in the same way

postmanEchoPostUrl :: Url "https"
postmanEchoPostUrl = [https||]

postmanEchoPostReq :: HttpRequest (Url "https") "POST" Empty EmptyPayload JsonPayload
postmanEchoPostReq = accept json $ post postmanEchoPostUrl

Expecting a response

Since we're expecting json, we also need data types and FromJSON instances to interpret the response with. Let's start with an example to handle the GET request.

{-# LANGUAGE DeriveGeneric #-}
data Args = Args 
  { foo1 :: String
  , foo2 :: String
  } deriving (Eq, Show, Generic)

data PostmanEchoResponse = PostmanEchoResponse
  { args :: Args
  } deriving (Eq, Show, Generic)

Once the request has been built, you can send it and expect a response of a particular type in any MonadDormouse m.

sendPostmanEchoGetReq :: MonadDormouse m => m PostmanEchoResponse
sendPostmanEchoGetReq = do
  (resp :: HttpResponse PostmanEchoResponse) <- expect postmanEchoGetReq'
  return $ responseBody resp

Running Dormouse

Dormouse is not opinionated about how you run it.

You can use a concrete type.

main :: IO ()
main = do
  manager <- newManager tlsManagerSettings
  postmanResponse <- runDormouse (DormouseConfig { clientManager = manager }) sendPostmanEchoGetReq
  print postmanResponse

You can integrate the DormouseT Monad Transformer into your transformer stack.

main :: IO ()
main = do
  manager <- newManager tlsManagerSettings
  postmanResponse <- runDormouseT (DormouseConfig { clientManager = manager }) sendPostmanEchoGetReq
  print postmanResponse

You can also integrate into your own Application monad using the sendHttp function from Dormouse.MonadIOImpl and by providing an instance of HasDormouseConfig for your application environment.

data MyEnv = MyEnv 
  { dormouseEnv :: DormouseConfig

instance HasDormouseConfig MyEnv where
  getDormouseConfig = dormouseEnv

newtype AppM a = AppM
  { unAppM :: ReaderT Env IO a 
  } deriving (Functor, Applicative, Monad, MonadReader Env, MonadIO, MonadThrow)

instance MonadDormouse (AppM) where
  send = IOImpl.sendHttp

runAppM :: Env -> AppM a -> IO a
runAppM deps app = flip runReaderT deps $ unAppM app