on master help sign in
Implement first version of ‘req’
c6f91c2mrkkrp
Network/HTTP/Req.hs+44−28
@@ -32,22 +32,24 @@
32-- when you do, it's better import it qualified because it has naming
32-- when you do, it's better import it qualified because it has naming
33-- conflicts with @req@.
33-- conflicts with @req@.
34
34
35−{-# LANGUAGE DataKinds #-}
35+{-# LANGUAGE DataKinds #-}
36−{-# LANGUAGE DeriveDataTypeable #-}
36+{-# LANGUAGE DeriveDataTypeable #-}
37−{-# LANGUAGE DeriveGeneric #-}
37+{-# LANGUAGE DeriveGeneric #-}
38−{-# LANGUAGE FlexibleInstances #-}
38+{-# LANGUAGE FlexibleInstances #-}
39−{-# LANGUAGE GADTs #-}
39+{-# LANGUAGE GADTs #-}
40−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
40+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
41−{-# LANGUAGE KindSignatures #-}
41+{-# LANGUAGE KindSignatures #-}
42−{-# LANGUAGE MultiParamTypeClasses #-}
42+{-# LANGUAGE MultiParamTypeClasses #-}
43−{-# LANGUAGE OverloadedStrings #-}
43+{-# LANGUAGE OverloadedStrings #-}
44−{-# LANGUAGE RecordWildCards #-}
44+{-# LANGUAGE RecordWildCards #-}
45−{-# LANGUAGE ScopedTypeVariables #-}
45+{-# LANGUAGE ScopedTypeVariables #-}
46−{-# LANGUAGE TypeFamilies #-}
46+{-# LANGUAGE TypeFamilies #-}
47−{-# LANGUAGE UndecidableInstances #-}
47+{-# LANGUAGE UndecidableInstances #-}
48+{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
48
49
49module Network.HTTP.Req
50module Network.HTTP.Req
50 ( -- * Making a request
51 ( -- * Making a request
52+ -- $making-a-request
51 req
53 req
52 -- * Embedding requests into your monad
54 -- * Embedding requests into your monad
53 -- $embedding-requests
55 -- $embedding-requests
@@ -91,9 +93,8 @@ module Network.HTTP.Req
91 , CanHaveBody (..) )
93 , CanHaveBody (..) )
92where
94where
93
95
94−import Control.Monad.Error (MonadError (..))
96+import Control.Exception (try)
95import Control.Monad.IO.Class
97import Control.Monad.IO.Class
96−import Control.Monad.Reader
97import Data.Aeson
98import Data.Aeson
98import Data.ByteString
99import Data.ByteString
99import Data.Data (Data)
100import Data.Data (Data)
@@ -117,24 +118,37 @@ import qualified Network.HTTP.Types as Y
117----------------------------------------------------------------------------
118----------------------------------------------------------------------------
118-- Making a request
119-- Making a request
119
120
121+-- $making-a-request
122+--
123+-- To make an HTTP request you need only one function: 'req'.
124+
120−-- | Perform an HTTP request.
125+-- | Make an HTTP request.
126+--
127+-- TODO Finish docs of this function when the package is more developed.
121
128
122req
129req
130+ :: forall m method body response.
123− :: ( MonadHttp m
131+ ( MonadHttp m
124 , HttpMethod method
132 , HttpMethod method
125 , HttpBody body
133 , HttpBody body
126 , HttpResponse response
134 , HttpResponse response
127 , AllowsBody method ~ ProvidesBody body )
135 , AllowsBody method ~ ProvidesBody body )
128− => method
136+ => method -- ^ HTTP method
129− -> Endpoint
137+ -> Endpoint -- ^ Endpoint to make the request against
130− -> body
138+ -> body -- ^ Body of the request
131− -> Option
139+ -> Option -- ^ Collection of optional parameters
132− -> m response
140+ -> m response -- ^ Response
133−req = undefined
141+req method url body options = do
134−
142+ config <- getHttpConfig
135−-- TODO We need to catch exceptions here, only relevant ones and only
143+ manager <- liftIO (readIORef globalManager)
136−-- synchronous to give user a chance to work with pure, explicit exceptions
144+ let request = flip appEndo L.defaultRequest $
137−-- if he wants to do so.
145+ getRequestMod (Womb method :: Womb "method" method) <>
146+ getRequestMod url <>
147+ getRequestMod (Womb body :: Womb "body" body) <>
148+ getRequestMod options <>
149+ getRequestMod config
150+ liftIO (try $ getHttpResponse manager request)
151+ >>= either handleHttpException return
138
152
139----------------------------------------------------------------------------
153----------------------------------------------------------------------------
140-- Embedding requests into your monad
154-- Embedding requests into your monad
@@ -448,7 +462,9 @@ port = undefined -- TODO
448-- Here we need to provide various options how to consume responses.
462-- Here we need to provide various options how to consume responses.
449
463
450class HttpResponse response where
464class HttpResponse response where
451− interpretResponse :: ByteString -> response -- FIXME use conduit
465+ getHttpResponse :: L.Manager -> L.Request -> IO response
466+
467+-- helpers to
452
468
453----------------------------------------------------------------------------
469----------------------------------------------------------------------------
454-- Other
470-- Other