@@ -1,254 +1,282 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE LambdaCase #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE QuasiQuotes #-}
5+{-# LANGUAGE RecordWildCards #-}
6+{-# LANGUAGE RoleAnnotations #-}
7+{-# LANGUAGE TemplateHaskellQuotes #-}
8+{-# LANGUAGE TypeFamilies #-}
9+{-# LANGUAGE UndecidableInstances #-}
10+
111-- |
212-- Module : Network.HTTP.Req
3−-- Copyright : © 2016 Mark Karpov
13+-- Copyright : © 2016–present Mark Karpov
414-- License : BSD 3 clause
515--
6−-- Maintainer : Mark Karpov <markkarpov@openmailbox.org>
16+-- Maintainer : Mark Karpov <markkarpov92@gmail.com>
717-- Stability : experimental
818-- Portability : portable
919--
10−-- The documentation below is structured in such a way that most important
11−-- information goes first: you learn how to do HTTP requests, then how to
12−-- embed them in any monad you have, then it goes on giving you details
13−-- about less-common things you may want to know about. The documentation is
14−-- written with sufficient coverage of details and examples, it's designed
15−-- to be a complete tutorial on its own.
16−--
17−-- /(A modest intro goes here, click on 'req' to start making requests.)/
20+-- The documentation below is structured in such a way that the most
21+-- important information is presented first: you learn how to do HTTP
22+-- requests, how to embed them in the monad you have, and then it gives you
23+-- details about less-common things you may want to know about. The
24+-- documentation is written with sufficient coverage of details and
25+-- examples, and it's designed to be a complete tutorial on its own.
1826--
1927-- === About the library
2028--
21−-- This is an easy-to-use, type-safe, expandable, high-level HTTP library
22−-- that just works without any fooling around.
23−--
24−-- What does the “easy-to-use” phrase mean? It means that the library is
25−-- designed to be beginner-friendly, so it's simple to add it to your monad
26−-- stack, intuitive to work with, well-documented, and does not get in your
27−-- way. Doing HTTP requests is a common task and Haskell library for this
28−-- should be very approachable and clear to beginners, thus certain
29−-- compromises were made. For example, one cannot currently modify
30−-- 'L.ManagerSettings' of default manager because the library always uses
31−-- the same implicit global manager for simplicity and maximal connection
32−-- sharing. There is a way to use your own manager with different settings,
33−-- but it requires a bit more typing.
34−--
35−-- “Type-safe” means that the library is protective and eliminates certain
36−-- class of errors. For example, we have correct-by-construction 'Url's,
37−-- it's guaranteed that user does not send request body when using methods
38−-- like 'GET' or 'OPTIONS', amount of implicit assumptions is minimized by
39−-- making user specify his\/her intentions in explicit form (for example,
40−-- it's not possible to avoid specifying body or method of a request).
41−-- Authentication methods that assume TLS force user to use TLS on type
42−-- level. The library carefully hides underlying types from lower-level
43−-- @http-client@ package because it's not safe enough (for example
44−-- 'L.Request' is an instance of 'Data.String.IsString' and if it's
45−-- malformed, it will blow up at run-time).
46−--
47−-- “Expandable” refers to the ability of the library to be expanded without
48−-- ugly hacking. For example, it's possible to define your own HTTP methods,
49−-- new ways to construct body of request, new authorization options, new
50−-- ways to actually perform request and how to represent\/parse response. As
51−-- user extends the library to satisfy his\/her special needs, the new
52−-- solutions work just like built-ins. That said, all common cases are
53−-- covered by the library out-of-the-box.
54−--
55−-- “High-level” means that there are less details to worry about. The
56−-- library is a result of my experiences as a Haskell consultant, working
57−-- for several clients who have very different projects and so the library
58−-- adapts easily to any particular style of writing Haskell applications.
59−-- For example, some people prefer throwing exceptions, while others are
60−-- concerned with purity: just define 'handleHttpException' accordingly when
61−-- making your monad instance of 'MonadHttp' and it will play seamlessly.
62−-- Finally, the library cuts boilerplate considerably and helps write
63−-- concise, easy to read and maintain code.
29+-- Req is an HTTP client library that attempts to be easy-to-use, type-safe,
30+-- and expandable.
31+--
32+-- “Easy-to-use” means that the library is designed to be beginner-friendly
33+-- so it's simple to add to your monad stack, intuitive to work with,
34+-- well-documented, and does not get in your way. Doing HTTP requests is a
35+-- common task and a Haskell library for this should be approachable and
36+-- clear to beginners, thus certain compromises were made. For example, one
37+-- cannot currently modify 'L.ManagerSettings' of the default manager
38+-- because the library always uses the same implicit global manager for
39+-- simplicity and maximal connection sharing. There is a way to use your own
40+-- manager with different settings, but it requires more typing.
41+--
42+-- “Type-safe” means that the library tries to eliminate certain classes of
43+-- errors. For example, we have correct-by-construction URLs; it is
44+-- guaranteed that the user does not send the request body when using
45+-- methods like GET or OPTIONS, and the amount of implicit assumptions is
46+-- minimized by making the user specify their intentions in an explicit
47+-- form. For example, it's not possible to avoid specifying the body or the
48+-- method of a request. Authentication methods that assume HTTPS force the
49+-- user to use HTTPS at the type level.
50+--
51+-- “Expandable” refers to the ability to create new components without
52+-- having to resort to hacking. For example, it's possible to define your
53+-- own HTTP methods, create new ways to construct the body of a request,
54+-- create new authorization options, perform a request in a different way,
55+-- and create your own methods to parse a response.
6456--
6557-- === Using with other libraries
6658--
67−-- * You won't need low-level interface of @http-client@ most of the
68−-- time, but when you do, it's better import it qualified because it
69−-- has naming conflicts with @req@.
70−-- * For streaming of large request bodies see companion package
59+-- * You won't need the low-level interface of @http-client@ most of the
60+-- time, but when you do, it's better to do a qualified import,
61+-- because @http-client@ has naming conflicts with @req@.
62+-- * For streaming of large request bodies see the companion package
7163-- @req-conduit@: <https://hackage.haskell.org/package/req-conduit>.
7264--
7365-- === Lightweight, no risk solution
7466--
7567-- The library uses the following mature packages under the hood to
76−-- guarantee you best experience without bugs or other funny business:
68+-- guarantee you the best experience:
7769--
78−-- * <https://hackage.haskell.org/package/http-client> — low level HTTP
70+-- * <https://hackage.haskell.org/package/http-client>—low level HTTP
7971-- client used everywhere in Haskell.
80−-- * <https://hackage.haskell.org/package/http-client-tls> — TLS (HTTPS)
72+-- * <https://hackage.haskell.org/package/http-client-tls>—TLS (HTTPS)
8173-- support for @http-client@.
8274--
8375-- It's important to note that since we leverage well-known libraries that
84−-- the whole Haskell ecosystem uses, there is no risk in using @req@, as the
76+-- the whole Haskell ecosystem uses, there is no risk in using @req@. The
8577-- machinery for performing requests is the same as with @http-conduit@ and
86−-- @wreq@, it's just the API is different.
87−
88−{-# LANGUAGE CPP #-}
89−{-# LANGUAGE DataKinds #-}
90−{-# LANGUAGE DeriveDataTypeable #-}
91−{-# LANGUAGE DeriveGeneric #-}
92−{-# LANGUAGE FlexibleInstances #-}
93−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
94−{-# LANGUAGE KindSignatures #-}
95−{-# LANGUAGE OverloadedStrings #-}
96−{-# LANGUAGE RecordWildCards #-}
97−{-# LANGUAGE ScopedTypeVariables #-}
98−{-# LANGUAGE TypeFamilies #-}
99−
100−#if MIN_VERSION_base(4,9,0)
101−{-# LANGUAGE UndecidableInstances #-}
102−#endif
103−
104−#if __GLASGOW_HASKELL__ < 710
105−{-# LANGUAGE ConstraintKinds #-}
106−#endif
107−
108−#if __GLASGOW_HASKELL__ >= 800
109−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
110−#endif
111−
78+-- @wreq@. The only difference is the API.
11279module Network.HTTP.Req
11380 ( -- * Making a request
11481 -- $making-a-request
115− req
116− , withReqManager
117− -- * Embedding requests into your monad
82+ req,
83+ reqBr,
84+ reqCb,
85+ req',
86+ withReqManager,
87+
88+ -- * Embedding requests in your monad
11889 -- $embedding-requests
119− , MonadHttp (..)
120− , HttpConfig (..)
90+ MonadHttp (..),
91+ HttpConfig (..),
92+ defaultHttpConfig,
93+ Req,
94+ runReq,
95+
12196 -- * Request
97+
12298 -- ** Method
12399 -- $method
124− , GET (..)
125− , POST (..)
126− , HEAD (..)
127− , PUT (..)
128− , DELETE (..)
129− , TRACE (..)
130− , CONNECT (..)
131− , OPTIONS (..)
132− , PATCH (..)
133− , HttpMethod (..)
100+ GET (..),
101+ POST (..),
102+ HEAD (..),
103+ PUT (..),
104+ DELETE (..),
105+ TRACE (..),
106+ CONNECT (..),
107+ OPTIONS (..),
108+ PATCH (..),
109+ HttpMethod (..),
110+
134111 -- ** URL
135112 -- $url
136− , Url
137− , http
138− , https
139− , (/~)
140− , (/:)
141− , parseUrlHttp
142− , parseUrlHttps
113+ Url,
114+ http,
115+ https,
116+ (/~),
117+ (/:),
118+ useHttpURI,
119+ useHttpsURI,
120+ useURI,
121+ urlQ,
122+ renderUrl,
123+
143124 -- ** Body
144125 -- $body
145− , NoReqBody (..)
146− , ReqBodyJson (..)
147− , ReqBodyFile (..)
148− , ReqBodyBs (..)
149− , ReqBodyLbs (..)
150− , ReqBodyUrlEnc (..)
151− , FormUrlEncodedParam
152− , HttpBody (..)
153− , ProvidesBody
154− , HttpBodyAllowed
126+ NoReqBody (..),
127+ ReqBodyJson (..),
128+ ReqBodyFile (..),
129+ ReqBodyBs (..),
130+ ReqBodyLbs (..),
131+ ReqBodyUrlEnc (..),
132+ FormUrlEncodedParam,
133+ ReqBodyMultipart,
134+ reqBodyMultipart,
135+ HttpBody (..),
136+ ProvidesBody,
137+ HttpBodyAllowed,
138+
155139 -- ** Optional parameters
156140 -- $optional-parameters
157− , Option
141+ Option,
142+
158143 -- *** Query parameters
159144 -- $query-parameters
160− , (=:)
161− , queryFlag
162− , QueryParam (..)
145+ (=:),
146+ queryFlag,
147+ formToQuery,
148+ QueryParam (..),
149+
163150 -- *** Headers
164− , header
151+ header,
152+ attachHeader,
153+ headerRedacted,
154+
165155 -- *** Cookies
166156 -- $cookies
167− , cookieJar
157+ cookieJar,
158+
168159 -- *** Authentication
169160 -- $authentication
170− , basicAuth
171− , oAuth2Bearer
172− , oAuth2Token
161+ basicAuth,
162+ basicAuthUnsafe,
163+ basicProxyAuth,
164+ oAuth1,
165+ oAuth2Bearer,
166+ oAuth2Token,
167+ customAuth,
168+
173169 -- *** Other
174− , port
175− , decompress
176− , responseTimeout
177− , httpVersion
170+ port,
171+ decompress,
172+ responseTimeout,
173+ httpVersion,
174+
178175 -- * Response
176+
179177 -- ** Response interpretations
180− , IgnoreResponse
181− , ignoreResponse
182− , JsonResponse
183− , jsonResponse
184− , BsResponse
185− , bsResponse
186− , LbsResponse
187− , lbsResponse
188− , ReturnRequest
189− , returnRequest
178+ IgnoreResponse,
179+ ignoreResponse,
180+ JsonResponse,
181+ jsonResponse,
182+ BsResponse,
183+ bsResponse,
184+ LbsResponse,
185+ lbsResponse,
186+
190187 -- ** Inspecting a response
191− , responseBody
192− , responseStatusCode
193− , responseStatusMessage
194− , responseHeader
195− , responseCookieJar
196− , responseRequest
188+ responseBody,
189+ responseStatusCode,
190+ responseStatusMessage,
191+ responseHeader,
192+ responseCookieJar,
193+
197194 -- ** Defining your own interpretation
198195 -- $new-response-interpretation
199− , HttpResponse (..)
196+ HttpResponse (..),
197+
200198 -- * Other
201− , HttpException (..)
202− , CanHaveBody (..)
203− , Scheme (..) )
199+ HttpException (..),
200+ isStatusCodeException,
201+ CanHaveBody (..),
202+ Scheme (..),
203+ )
204204where
205205
206+import Blaze.ByteString.Builder qualified as BB
206207import Control.Applicative
207208import Control.Arrow (first, second)
208−import Control.Exception (Exception, try, catch, throwIO)
209−import Control.Monad
209+import Control.Exception hiding (Handler (..), TypeError)
210+import Control.Monad (guard, void, (>=>))
211+import Control.Monad.Base
212+import Control.Monad.Catch (Handler (..), MonadCatch, MonadMask, MonadThrow)
210213import Control.Monad.IO.Class
211−import Data.Aeson (ToJSON (..), FromJSON (..))
214+import Control.Monad.IO.Unlift
215+import Control.Monad.Reader (ReaderT (ReaderT), ask, lift, runReaderT)
216+import Control.Monad.Trans.Accum (AccumT)
217+import Control.Monad.Trans.Cont (ContT)
218+import Control.Monad.Trans.Control
219+import Control.Monad.Trans.Except (ExceptT)
220+import Control.Monad.Trans.Identity (IdentityT)
221+import Control.Monad.Trans.Maybe (MaybeT)
222+import Control.Monad.Trans.RWS.CPS qualified as RWS.CPS
223+import Control.Monad.Trans.RWS.Lazy qualified as RWS.Lazy
224+import Control.Monad.Trans.RWS.Strict qualified as RWS.Strict
225+import Control.Monad.Trans.Select (SelectT)
226+import Control.Monad.Trans.State.Lazy qualified as State.Lazy
227+import Control.Monad.Trans.State.Strict qualified as State.Strict
228+import Control.Monad.Trans.Writer.CPS qualified as Writer.CPS
229+import Control.Monad.Trans.Writer.Lazy qualified as Writer.Lazy
230+import Control.Monad.Trans.Writer.Strict qualified as Writer.Strict
231+import Control.Retry
232+import Data.Aeson (FromJSON (..), ToJSON (..))
233+import Data.Aeson qualified as A
212234import Data.ByteString (ByteString)
235+import Data.ByteString qualified as B
236+import Data.ByteString.Lazy qualified as BL
237+import Data.CaseInsensitive qualified as CI
213238import Data.Data (Data)
214−import Data.Default.Class
239+import Data.Default.Class (def)
215240import Data.Function (on)
216241import Data.IORef
217−import Data.List (nubBy)
242+import Data.Kind (Constraint, Type)
243+import Data.List (foldl', nubBy)
218244import Data.List.NonEmpty (NonEmpty (..))
245+import Data.List.NonEmpty qualified as NE
246+import Data.Maybe (fromMaybe)
219247import Data.Proxy
220−import Data.Semigroup hiding (Option, option)
221−import Data.Typeable (Typeable)
248+import Data.Semigroup (Endo (..))
249+import Data.Set qualified as S
250+import Data.Text (Text)
251+import Data.Text qualified as T
252+import Data.Text.Encoding qualified as T
253+import Data.Typeable (Typeable, cast)
222254import GHC.Generics
223255import GHC.TypeLits
256+import Language.Haskell.TH qualified as TH
257+import Language.Haskell.TH.Quote qualified as TH
258+import Language.Haskell.TH.Syntax qualified as TH
259+import Network.Connection qualified as NC
260+import Network.HTTP.Client qualified as L
261+import Network.HTTP.Client.Internal qualified as LI
262+import Network.HTTP.Client.MultipartFormData qualified as LM
263+import Network.HTTP.Client.TLS qualified as L
264+import Network.HTTP.Types qualified as Y
224265import System.IO.Unsafe (unsafePerformIO)
266+import Text.URI (URI)
267+import Text.URI qualified as URI
268+import Text.URI.QQ qualified as QQ
269+import Web.Authenticate.OAuth qualified as OAuth
270+import Web.FormUrlEncoded (FromForm (..), ToForm (..))
271+import Web.FormUrlEncoded qualified as Form
225272import Web.HttpApiData (ToHttpApiData (..))
226−import qualified Blaze.ByteString.Builder as BB
227−import qualified Data.Aeson as A
228−import qualified Data.ByteString as B
229−import qualified Data.ByteString.Lazy as BL
230−import qualified Data.CaseInsensitive as CI
231−import qualified Data.List.NonEmpty as NE
232−import qualified Data.Text as T
233−import qualified Data.Text.Encoding as T
234−import qualified Network.Connection as NC
235−import qualified Network.HTTP.Client as L
236−import qualified Network.HTTP.Client.Internal as LI
237−import qualified Network.HTTP.Client.TLS as L
238−import qualified Network.HTTP.Types as Y
239−
240−#if MIN_VERSION_base(4,9,0)
241−import Data.Kind (Constraint)
242−#else
243−import GHC.Exts (Constraint)
244−#endif
245273
246274----------------------------------------------------------------------------
247275-- Making a request
248276
249277-- $making-a-request
250278--
251−-- To make an HTTP request you need only one function: 'req'.
279+-- To make an HTTP request you normally need only one function: 'req'.
252280
253281-- | Make an HTTP request. The function takes 5 arguments, 4 of which
254282-- specify required parameters and the final 'Option' argument is a
@@ -260,42 +288,44 @@ import GHC.Exts (Constraint)
260288-- @method@ is an HTTP method such as 'GET' or 'POST'. The documentation has
261289-- a dedicated section about HTTP methods below.
262290--
263−-- @url@ is a URL that describes location of resource you want to interact
264−-- with. It's of type 'Url' (if you click the link it will tell everything
265−-- about construction of 'Url' things).
291+-- @url@ is a 'Url' that describes location of resource you want to interact
292+-- with.
266293--
267294-- @body@ is a body option such as 'NoReqBody' or 'ReqBodyJson'. The
268−-- tutorial has a section about HTTP bodies, but usage in very
269−-- straightforward and should be clear from the examples below.
270−--
271−-- @response@ is a type hint how to make and interpret response of HTTP
272−-- request, out-of-the-box it can be the following: 'ignoreResponse',
273−-- 'jsonResponse', 'bsResponse' (to get strict 'ByteString'), 'lbsResponse'
274−-- (to get lazy 'BL.ByteString'), and 'returnRequest' (makes no request,
275−-- just returns response, used for testing).
276−--
277−-- Finally @options@ is a 'Monoid' that holds a composite 'Option' for all
278−-- other optional things like query parameters, headers, non-standard port
279−-- number, etc. There are quite a few things you can put there, see
295+-- tutorial has a section about HTTP bodies, but usage is very
296+-- straightforward and should be clear from the examples.
297+--
298+-- @response@ is a type hint how to make and interpret response of an HTTP
299+-- request. Out-of-the-box it can be the following:
300+--
301+-- * 'ignoreResponse'
302+-- * 'jsonResponse'
303+-- * 'bsResponse' (to get a strict 'ByteString')
304+-- * 'lbsResponse' (to get a lazy 'BL.ByteString')
305+--
306+-- Finally, @options@ is a 'Monoid' that holds a composite 'Option' for all
307+-- other optional settings like query parameters, headers, non-standard port
308+-- number, etc. There are quite a few things you can put there, see the
280309-- corresponding section in the documentation. If you don't need anything at
281310-- all, pass 'mempty'.
282311--
283312-- __Note__ that if you use 'req' to do all your requests, connection
284313-- sharing and reuse is done for you automatically.
285314--
286−-- See the examples below to get on the speed very quickly.
315+-- See the examples below to get on the speed quickly.
287316--
288317-- ==== __Examples__
289318--
290319-- First, this is a piece of boilerplate that should be in place before you
291320-- try the examples:
292321--
322+-- > {-# LANGUAGE DeriveGeneric #-}
293323-- > {-# LANGUAGE OverloadedStrings #-}
294324-- >
295325-- > module Main (main) where
296326-- >
297−-- > import Control.Exception (throwIO)
298327-- > import Control.Monad
328+-- > import Control.Monad.IO.Class
299329-- > import Data.Aeson
300330-- > import Data.Maybe (fromJust)
301331-- > import Data.Monoid ((<>))
@@ -303,32 +333,30 @@ import GHC.Exts (Constraint)
303333-- > import GHC.Generics
304334-- > import Network.HTTP.Req
305335-- > import qualified Data.ByteString.Char8 as B
306−-- >
307−-- > instance MonadHttp IO where
308−-- > handleHttpException = throwIO
336+-- > import qualified Text.URI as URI
309337--
310338-- We will be making requests against the <https://httpbin.org> service.
311339--
312340-- Make a GET request, grab 5 random bytes:
313341--
314342-- > main :: IO ()
315−-- > main = do
343+-- > main = runReq defaultHttpConfig $ do
316344-- > let n :: Int
317345-- > n = 5
318346-- > bs <- req GET (https "httpbin.org" /: "bytes" /~ n) NoReqBody bsResponse mempty
319−-- > B.putStrLn (responseBody bs)
347+-- > liftIO $ B.putStrLn (responseBody bs)
320348--
321349-- The same, but now we use a query parameter named @\"seed\"@ to control
322350-- seed of the generator:
323351--
324352-- > main :: IO ()
325−-- > main = do
353+-- > main = runReq defaultHttpConfig $ do
326354-- > let n, seed :: Int
327355-- > n = 5
328356-- > seed = 100
329357-- > bs <- req GET (https "httpbin.org" /: "bytes" /~ n) NoReqBody bsResponse $
330358-- > "seed" =: seed
331−-- > B.putStrLn (responseBody bs)
359+-- > liftIO $ B.putStrLn (responseBody bs)
332360--
333361-- POST JSON data and get some info about the POST request:
334362--
@@ -341,103 +369,252 @@ import GHC.Exts (Constraint)
341369-- > instance FromJSON MyData
342370-- >
343371-- > main :: IO ()
344−-- > main = do
372+-- > main = runReq defaultHttpConfig $ do
345373-- > let myData = MyData
346374-- > { size = 6
347375-- > , color = "Green" }
348376-- > v <- req POST (https "httpbin.org" /: "post") (ReqBodyJson myData) jsonResponse mempty
349−-- > print (responseBody v :: Value)
377+-- > liftIO $ print (responseBody v :: Value)
350378--
351379-- Sending URL-encoded body:
352380--
353381-- > main :: IO ()
354−-- > main = do
382+-- > main = runReq defaultHttpConfig $ do
355383-- > let params =
356384-- > "foo" =: ("bar" :: Text) <>
357385-- > queryFlag "baz"
358386-- > response <- req POST (https "httpbin.org" /: "post") (ReqBodyUrlEnc params) jsonResponse mempty
359−-- > print (responseBody response :: Value)
387+-- > liftIO $ print (responseBody response :: Value)
360388--
361389-- Using various optional parameters and URL that is not known in advance:
362390--
363391-- > main :: IO ()
364−-- > main = do
392+-- > main = runReq defaultHttpConfig $ do
365393-- > -- This is an example of what to do when URL is given dynamically. Of
366394-- > -- course in a real application you may not want to use 'fromJust'.
367−-- > let (url, options) = fromJust (parseUrlHttps "https://httpbin.org/get?foo=bar")
395+-- > uri <- URI.mkURI "https://httpbin.org/get?foo=bar"
396+-- > let (url, options) = fromJust (useHttpsURI uri)
368397-- > response <- req GET url NoReqBody jsonResponse $
369398-- > "from" =: (15 :: Int) <>
370399-- > "to" =: (67 :: Int) <>
371400-- > basicAuth "username" "password" <>
372401-- > options <> -- contains the ?foo=bar part
373402-- > port 443 -- here you can put any port of course
374−-- > print (responseBody response :: Value)
375−
376−req
377− :: forall m method body response scheme.
378− ( MonadHttp m
379− , HttpMethod method
380− , HttpBody body
381− , HttpResponse response
382− , HttpBodyAllowed (AllowsBody method) (ProvidesBody body) )
383− => method -- ^ HTTP method
384− -> Url scheme -- ^ 'Url' — location of resource
385− -> body -- ^ Body of the request
386− -> Proxy response -- ^ A hint how to interpret response
387− -> Option scheme -- ^ Collection of optional parameters
388− -> m response -- ^ Response
389−req method url body Proxy options = do
390− config <- getHttpConfig
391− manager <- liftIO (readIORef globalManager)
403+-- > liftIO $ print (responseBody response :: Value)
404+req ::
405+ ( MonadHttp m,
406+ HttpMethod method,
407+ HttpBody body,
408+ HttpResponse response,
409+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
410+ ) =>
411+ -- | HTTP method
412+ method ->
413+ -- | 'Url'—location of resource
414+ Url scheme ->
415+ -- | Body of the request
416+ body ->
417+ -- | A hint how to interpret response
418+ Proxy response ->
419+ -- | Collection of optional parameters
420+ Option scheme ->
421+ -- | Response
422+ m response
423+req method url body responseProxy options =
424+ reqCb method url body responseProxy options pure
425+
426+-- | A version of 'req' that does not use one of the predefined instances of
427+-- 'HttpResponse' but instead allows the user to consume @'L.Response'
428+-- 'L.BodyReader'@ manually, in a custom way.
429+--
430+-- @since 1.0.0
431+reqBr ::
432+ ( MonadHttp m,
433+ HttpMethod method,
434+ HttpBody body,
435+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
436+ ) =>
437+ -- | HTTP method
438+ method ->
439+ -- | 'Url'—location of resource
440+ Url scheme ->
441+ -- | Body of the request
442+ body ->
443+ -- | Collection of optional parameters
444+ Option scheme ->
445+ -- | How to consume response
446+ (L.Response L.BodyReader -> IO a) ->
447+ -- | Result
448+ m a
449+reqBr method url body options consume =
450+ req' method url body options (reqHandler consume)
451+
452+-- | A version of 'req' that takes a callback to modify the 'L.Request', but
453+-- otherwise performs the request identically.
454+--
455+-- @since 3.7.0
456+reqCb ::
457+ ( MonadHttp m,
458+ HttpMethod method,
459+ HttpBody body,
460+ HttpResponse response,
461+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
462+ ) =>
463+ -- | HTTP method
464+ method ->
465+ -- | 'Url'—location of resource
466+ Url scheme ->
467+ -- | Body of the request
468+ body ->
469+ -- | A hint how to interpret response
470+ Proxy response ->
471+ -- | Collection of optional parameters
472+ Option scheme ->
473+ -- | Callback to modify the request
474+ (L.Request -> m L.Request) ->
475+ -- | Response
476+ m response
477+reqCb method url body responseProxy options adjustRequest =
478+ req' method url body (options <> extraOptions) $ \request manager -> do
479+ request' <- adjustRequest request
480+ reqHandler getHttpResponse request' manager
481+ where
482+ extraOptions =
483+ case acceptHeader responseProxy of
484+ Nothing -> mempty
485+ Just accept -> header "Accept" accept
486+
487+-- | The default handler function that the higher-level request functions
488+-- pass to 'req''. Internal function.
489+--
490+-- @since 3.7.0
491+reqHandler ::
492+ (MonadHttp m) =>
493+ -- | How to get final result from a 'L.Response'
494+ (L.Response L.BodyReader -> IO b) ->
495+ -- | 'L.Request' to perform
496+ L.Request ->
497+ -- | 'L.Manager' to use
498+ L.Manager ->
499+ m b
500+reqHandler consume request manager = do
501+ HttpConfig {..} <- getHttpConfig
502+ let wrapVanilla = handle (throwIO . VanillaHttpException)
503+ wrapExc = handle (throwIO . LI.toHttpException request)
504+ withRRef =
505+ bracket
506+ (newIORef Nothing)
507+ (readIORef >=> mapM_ L.responseClose)
508+ (liftIO . try . wrapVanilla . wrapExc)
509+ ( withRRef $ \rref -> do
510+ let openResponse = mask_ $ do
511+ r <- readIORef rref
512+ mapM_ L.responseClose r
513+ r' <- L.responseOpen request manager
514+ writeIORef rref (Just r')
515+ return r'
516+ exceptionRetryPolicies =
517+ skipAsyncExceptions
518+ ++ [ \retryStatus -> Handler $ \e ->
519+ return $ httpConfigRetryJudgeException retryStatus e
520+ ]
521+ r <-
522+ retrying
523+ httpConfigRetryPolicy
524+ (\retryStatus r -> return $ httpConfigRetryJudge retryStatus r)
525+ ( const
526+ ( recovering
527+ httpConfigRetryPolicy
528+ exceptionRetryPolicies
529+ (const openResponse)
530+ )
531+ )
532+ (preview, r') <- grabPreview httpConfigBodyPreviewLength r
533+ mapM_ LI.throwHttp (httpConfigCheckResponse request r' preview)
534+ consume r'
535+ )
536+ >>= either handleHttpException return
537+
538+-- | Mostly like 'req' with respect to its arguments, but accepts a callback
539+-- that allows to perform a request in arbitrary fashion.
540+--
541+-- This function /does not/ perform handling\/wrapping exceptions, checking
542+-- response (with 'httpConfigCheckResponse'), and retrying. It only prepares
543+-- 'L.Request' and allows you to use it.
544+--
545+-- @since 0.3.0
546+req' ::
547+ forall m method body scheme a.
548+ ( MonadHttp m,
549+ HttpMethod method,
550+ HttpBody body,
551+ HttpBodyAllowed (AllowsBody method) (ProvidesBody body)
552+ ) =>
553+ -- | HTTP method
554+ method ->
555+ -- | 'Url'—location of resource
556+ Url scheme ->
557+ -- | Body of the request
558+ body ->
559+ -- | Collection of optional parameters
560+ Option scheme ->
561+ -- | How to perform request
562+ (L.Request -> L.Manager -> m a) ->
563+ -- | Result
564+ m a
565+req' method url body options m = do
566+ config <- getHttpConfig
392567 let -- NOTE First appearance of any given header wins. This allows to
393568 -- “overwrite” headers when we construct a request by cons-ing.
394569 nubHeaders = Endo $ \x ->
395− x { L.requestHeaders = nubBy ((==) `on` fst) (L.requestHeaders x) }
396− request = flip appEndo L.defaultRequest $
397− -- NOTE Order of 'mappend's matters, here method is overwritten first
398− -- and 'options' take effect last. In particular, this means that
399− -- 'options' can overwrite things set by other request components,
400− -- which is useful for setting port number, "Content-Type" header,
401− -- etc.
402− nubHeaders <>
403− getRequestMod options <>
404− getRequestMod config <>
405− getRequestMod (Womb body :: Womb "body" body) <>
406− getRequestMod url <>
407− getRequestMod (Womb method :: Womb "method" method)
408− wrappingVanilla m = catch m (throwIO . VanillaHttpException)
409− (liftIO . try . wrappingVanilla) (getHttpResponse request manager)
410− >>= either handleHttpException return
411−
412−-- | Global 'L.Manager' that 'req' uses. Here we just go with the default
413−-- settings, so users don't need to deal with this manager stuff at all, but
414−-- when we create a request, instance 'HttpConfig' can affect the default
415−-- settings via 'getHttpConfig'.
570+ x {L.requestHeaders = nubBy ((==) `on` fst) (L.requestHeaders x)}
571+ request' =
572+ flip appEndo L.defaultRequest $
573+ -- NOTE The order of 'mappend's matters, here method is overwritten
574+ -- first and 'options' take effect last. In particular, this means
575+ -- that 'options' can overwrite things set by other request
576+ -- components, which is useful for setting port number,
577+ -- "Content-Type" header, etc.
578+ nubHeaders
579+ <> getRequestMod options
580+ <> getRequestMod config
581+ <> getRequestMod (Tagged body :: Tagged "body" body)
582+ <> getRequestMod url
583+ <> getRequestMod (Tagged method :: Tagged "method" method)
584+ request <- finalizeRequest options request'
585+ withReqManager (m request)
586+
587+-- | Perform an action using the global implicit 'L.Manager' that the rest
588+-- of the library uses. This allows us to reuse connections that the
589+-- 'L.Manager' controls.
590+withReqManager :: (MonadIO m) => (L.Manager -> m a) -> m a
591+withReqManager m = m globalManager
592+
593+-- | The global 'L.Manager' that 'req' uses. Here we just go with the
594+-- default settings, so users don't need to deal with this manager stuff at
595+-- all, but when we create a request, instance 'HttpConfig' can affect the
596+-- default settings via 'getHttpConfig'.
416597--
417598-- A note about safety, in case 'unsafePerformIO' looks suspicious to you.
418599-- The value of 'globalManager' is named and lives on top level. This means
419−-- it will be shared, i.e. computed only once on first use of manager. From
420−-- that moment on the 'IORef' will be just reused — exactly the behaviour we
421−-- want here in order to maximize connection sharing. GHC could spoil the
422−-- plan by inlining the definition, hence the @NOINLINE@ pragma.
423−
424−globalManager :: IORef L.Manager
600+-- it will be shared, i.e. computed only once on the first use of the
601+-- manager. From that moment on the 'IORef' will be just reused—exactly the
602+-- behavior we want here in order to maximize connection sharing. GHC could
603+-- spoil the plan by inlining the definition, hence the @NOINLINE@ pragma.
604+globalManager :: L.Manager
425605globalManager = unsafePerformIO $ do
426606 context <- NC.initConnectionContext
427− let settings = L.mkManagerSettingsContext (Just context) def Nothing
607+ let settings =
608+ L.mkManagerSettingsContext
609+ (Just context)
610+ def
611+ Nothing
428612 manager <- L.newManager settings
429− newIORef manager
613+ return manager
430614{-# NOINLINE globalManager #-}
431615
432−-- | Perform an action using global implicit 'L.Manager' that the rest of
433−-- the library uses. This allows to reuse connections that the 'L.Manager'
434−-- controls.
435−
436−withReqManager :: MonadIO m => (L.Manager -> m a) -> m a
437−withReqManager m = liftIO (readIORef globalManager) >>= m
438−
439616----------------------------------------------------------------------------
440−-- Embedding requests into your monad
617+-- Embedding requests in your monad
441618
442619-- $embedding-requests
443620--
@@ -446,91 +623,281 @@ withReqManager m = liftIO (readIORef globalManager) >>= m
446623--
447624-- When writing a library, keep your API polymorphic in terms of
448625-- 'MonadHttp', only define instance of 'MonadHttp' in final application.
626+-- Another option is to use a @newtype@-wrapped monad stack and define
627+-- 'MonadHttp' for it. As of the version /0.4.0/, the 'Req' monad that
628+-- follows this strategy is provided out-of-the-box (see below).
449629
450630-- | A type class for monads that support performing HTTP requests.
451631-- Typically, you only need to define the 'handleHttpException' method
452632-- unless you want to tweak 'HttpConfig'.
453−
454−class MonadIO m => MonadHttp m where
455−
456− {-# MINIMAL handleHttpException #-}
457−
633+class (MonadIO m) => MonadHttp m where
458634 -- | This method describes how to deal with 'HttpException' that was
459635 -- caught by the library. One option is to re-throw it if you are OK with
460636 -- exceptions, but if you prefer working with something like
461− -- 'Control.Monad.Error.MonadError', this is the right place to pass it to
462− -- 'Control.Monad.Error.throwError'.
463−
637+ -- 'Control.Monad.Except.MonadError', this is the right place to pass it to
638+ -- 'Control.Monad.Except.throwError'.
464639 handleHttpException :: HttpException -> m a
465640
466− -- | Return 'HttpConfig' to be used when performing HTTP requests. Default
467− -- implementation returns its 'def' value, which is described in the
468− -- documentation for the type. Common usage pattern with manually defined
469− -- 'getHttpConfig' is to return some hard-coded value, or value extracted
470− -- from 'Control.Monad.Reader.MonadReader' if a more flexible approach to
471− -- configuration is desirable.
472−
641+ -- | Return the 'HttpConfig' to be used when performing HTTP requests.
642+ -- Default implementation returns its 'def' value, which is described in
643+ -- the documentation for the type. Common usage pattern with manually
644+ -- defined 'getHttpConfig' is to return some hard-coded value, or a value
645+ -- extracted from 'Control.Monad.Reader.MonadReader' if a more flexible
646+ -- approach to configuration is desirable.
473647 getHttpConfig :: m HttpConfig
474− getHttpConfig = return def
475−
476−-- | 'HttpConfig' contains general and default settings to be used when
477−-- making HTTP requests.
648+ getHttpConfig = return defaultHttpConfig
478649
650+-- | 'HttpConfig' contains settings to be used when making HTTP requests.
479651data HttpConfig = HttpConfig
480− { httpConfigProxy :: Maybe L.Proxy
481− -- ^ Proxy to use. By default values of @HTTP_PROXY@ and @HTTPS_PROXY@
652+ { -- | Proxy to use. By default values of @HTTP_PROXY@ and @HTTPS_PROXY@
482653 -- environment variables are respected, this setting overwrites them.
483654 -- Default value: 'Nothing'.
484− , httpConfigRedirectCount :: Int
485− -- ^ How many redirects to follow when getting a resource. Default
655+ httpConfigProxy :: Maybe L.Proxy,
656+ -- | How many redirects to follow when getting a resource. Default
486657 -- value: 10.
487− , httpConfigAltManager :: Maybe L.Manager
488− -- ^ Alternative 'L.Manager' to use. 'Nothing' (default value) means
489− -- that default implicit manager will be used (that's what you want in
490− -- 99% of cases).
491− , httpConfigCheckResponse :: L.Request -> L.Response L.BodyReader -> IO ()
492− -- ^ Function to check the response immediately after receiving the
493− -- status and headers. This is used for throwing exceptions on
494− -- non-success status codes by default (set to @\\_ _ -> return ()@ if
495− -- this behavior is not desirable). Throwing is better then just
496− -- returning a request with non-2xx status code because in that case
497− -- something is wrong and we need a way to short-cut execution. The
498− -- thrown exception is caught by the library though and is available in
499− -- 'handleHttpException'.
500− } deriving Typeable
501−
502−instance Default HttpConfig where
503− def = HttpConfig
504− { httpConfigProxy = Nothing
505− , httpConfigRedirectCount = 10
506− , httpConfigAltManager = Nothing
507− , httpConfigCheckResponse = \_ response ->
508− let Y.Status statusCode _ = L.responseStatus response in
509− unless (200 <= statusCode && statusCode < 300) $ do
510− chunk <- BL.toStrict <$> L.brReadSome (L.responseBody response) 1024
511− LI.throwHttp (L.StatusCodeException (void response) chunk) }
658+ httpConfigRedirectCount :: Int,
659+ -- | Alternative 'L.Manager' to use. 'Nothing' (default value) means
660+ -- that the default implicit manager will be used (that's what you want
661+ -- in 99% of cases).
662+ httpConfigAltManager :: Maybe L.Manager,
663+ -- | Function to check the response immediately after receiving the
664+ -- status and headers, before streaming of response body. The third
665+ -- argument is the beginning of response body (typically first 1024
666+ -- bytes). This is used for throwing exceptions on non-success status
667+ -- codes by default (set to @\\_ _ _ -> Nothing@ if this behavior is not
668+ -- desirable).
669+ --
670+ -- When the value this function returns is 'Nothing', nothing will
671+ -- happen. When it there is 'L.HttpExceptionContent' inside 'Just', it
672+ -- will be thrown.
673+ --
674+ -- Throwing is better then just returning a request with non-2xx status
675+ -- code because in that case something is wrong and we need a way to
676+ -- short-cut execution (also remember that Req retries automatically on
677+ -- request timeouts and such, so when your request fails, it's certainly
678+ -- something exceptional). The thrown exception is caught by the library
679+ -- though and is available in 'handleHttpException'.
680+ --
681+ -- __Note__: signature of this function was changed in the version
682+ -- /1.0.0/.
683+ --
684+ -- @since 0.3.0
685+ httpConfigCheckResponse ::
686+ forall b.
687+ L.Request ->
688+ L.Response b ->
689+ ByteString ->
690+ Maybe L.HttpExceptionContent,
691+ -- | The retry policy to use for request retrying. By default 'def' is
692+ -- used (see 'RetryPolicyM').
693+ --
694+ -- __Note__: signature of this function was changed to disallow 'IO' in
695+ -- version /1.0.0/ and then changed back to its current form in /3.1.0/.
696+ --
697+ -- @since 0.3.0
698+ httpConfigRetryPolicy :: RetryPolicyM IO,
699+ -- | The function is used to decide whether to retry a request. 'True'
700+ -- means that the request should be retried.
701+ --
702+ -- __Note__: signature of this function was changed in the version
703+ -- /1.0.0/.
704+ --
705+ -- @since 0.3.0
706+ httpConfigRetryJudge :: forall b. RetryStatus -> L.Response b -> Bool,
707+ -- | Similar to 'httpConfigRetryJudge', but is used to decide when to
708+ -- retry requests that resulted in an exception. By default it retries
709+ -- on response timeout and connection timeout (changed in version
710+ -- /3.8.0/).
711+ --
712+ -- @since 3.4.0
713+ httpConfigRetryJudgeException :: RetryStatus -> SomeException -> Bool,
714+ -- | Max length of preview fragment of response body.
715+ --
716+ -- @since 3.6.0
717+ httpConfigBodyPreviewLength :: forall a. (Num a) => a
718+ }
719+
720+-- | The default value of 'HttpConfig'.
721+--
722+-- @since 2.0.0
723+defaultHttpConfig :: HttpConfig
724+defaultHttpConfig =
725+ HttpConfig
726+ { httpConfigProxy = Nothing,
727+ httpConfigRedirectCount = 10,
728+ httpConfigAltManager = Nothing,
729+ httpConfigCheckResponse = \_ response preview ->
730+ let scode = statusCode response
731+ in if 200 <= scode && scode < 300
732+ then Nothing
733+ else Just (L.StatusCodeException (void response) preview),
734+ httpConfigRetryPolicy = retryPolicyDefault,
735+ httpConfigRetryJudge = \_ response ->
736+ statusCode response
737+ `elem` [ 408, -- Request timeout
738+ 504, -- Gateway timeout
739+ 524, -- A timeout occurred
740+ 598, -- (Informal convention) Network read timeout error
741+ 599 -- (Informal convention) Network connect timeout error
742+ ],
743+ httpConfigRetryJudgeException = \_ e ->
744+ case fromException e of
745+ Just (L.HttpExceptionRequest _ c) ->
746+ case c of
747+ L.ResponseTimeout -> True
748+ L.ConnectionTimeout -> True
749+ _ -> False
750+ _ -> False,
751+ httpConfigBodyPreviewLength = 1024
752+ }
753+ where
754+ statusCode = Y.statusCode . L.responseStatus
512755
513756instance RequestComponent HttpConfig where
514757 getRequestMod HttpConfig {..} = Endo $ \x ->
515− x { L.proxy = httpConfigProxy
516− , L.redirectCount = httpConfigRedirectCount
517− , LI.requestManagerOverride = httpConfigAltManager
518− , LI.checkResponse = httpConfigCheckResponse }
758+ x
759+ { L.proxy = httpConfigProxy,
760+ L.redirectCount = httpConfigRedirectCount,
761+ LI.requestManagerOverride = httpConfigAltManager
762+ }
763+
764+-- | A monad that allows us to run 'req' in any 'IO'-enabled monad without
765+-- having to define new instances.
766+--
767+-- @since 0.4.0
768+newtype Req a = Req (ReaderT HttpConfig IO a)
769+ deriving
770+ ( Functor,
771+ Applicative,
772+ Monad,
773+ MonadIO,
774+ MonadUnliftIO
775+ )
776+
777+-- | @since 3.7.0
778+deriving instance MonadThrow Req
779+
780+-- | @since 3.7.0
781+deriving instance MonadCatch Req
782+
783+-- | @since 3.7.0
784+deriving instance MonadMask Req
785+
786+instance MonadBase IO Req where
787+ liftBase = liftIO
788+
789+instance MonadBaseControl IO Req where
790+ type StM Req a = a
791+ liftBaseWith f = Req . ReaderT $ \r -> f (runReq r)
792+ {-# INLINEABLE liftBaseWith #-}
793+ restoreM = Req . ReaderT . const . return
794+ {-# INLINEABLE restoreM #-}
795+
796+instance MonadHttp Req where
797+ handleHttpException = Req . lift . throwIO
798+ getHttpConfig = Req ask
799+
800+-- | @since 3.10.0
801+instance (MonadHttp m, Monoid w) => MonadHttp (AccumT w m) where
802+ handleHttpException = lift . handleHttpException
803+ getHttpConfig = lift getHttpConfig
804+
805+-- | @since 3.10.0
806+instance (MonadHttp m) => MonadHttp (ContT r m) where
807+ handleHttpException = lift . handleHttpException
808+ getHttpConfig = lift getHttpConfig
809+
810+-- | @since 3.10.0
811+instance (MonadHttp m) => MonadHttp (ExceptT e m) where
812+ handleHttpException = lift . handleHttpException
813+ getHttpConfig = lift getHttpConfig
814+
815+-- | @since 3.10.0
816+instance (MonadHttp m) => MonadHttp (IdentityT m) where
817+ handleHttpException = lift . handleHttpException
818+ getHttpConfig = lift getHttpConfig
819+
820+-- | @since 3.10.0
821+instance (MonadHttp m) => MonadHttp (MaybeT m) where
822+ handleHttpException = lift . handleHttpException
823+ getHttpConfig = lift getHttpConfig
824+
825+-- | @since 3.10.0
826+instance (MonadHttp m) => MonadHttp (ReaderT r m) where
827+ handleHttpException = lift . handleHttpException
828+ getHttpConfig = lift getHttpConfig
829+
830+-- | @since 3.10.0
831+instance (MonadHttp m, Monoid w) => MonadHttp (RWS.CPS.RWST r w s m) where
832+ handleHttpException = lift . handleHttpException
833+ getHttpConfig = lift getHttpConfig
834+
835+-- | @since 3.10.0
836+instance (MonadHttp m, Monoid w) => MonadHttp (RWS.Lazy.RWST r w s m) where
837+ handleHttpException = lift . handleHttpException
838+ getHttpConfig = lift getHttpConfig
839+
840+-- | @since 3.10.0
841+instance (MonadHttp m, Monoid w) => MonadHttp (RWS.Strict.RWST r w s m) where
842+ handleHttpException = lift . handleHttpException
843+ getHttpConfig = lift getHttpConfig
844+
845+-- | @since 3.10.0
846+instance (MonadHttp m) => MonadHttp (SelectT r m) where
847+ handleHttpException = lift . handleHttpException
848+ getHttpConfig = lift getHttpConfig
849+
850+-- | @since 3.10.0
851+instance (MonadHttp m) => MonadHttp (State.Lazy.StateT s m) where
852+ handleHttpException = lift . handleHttpException
853+ getHttpConfig = lift getHttpConfig
854+
855+-- | @since 3.10.0
856+instance (MonadHttp m) => MonadHttp (State.Strict.StateT s m) where
857+ handleHttpException = lift . handleHttpException
858+ getHttpConfig = lift getHttpConfig
859+
860+-- | @since 3.10.0
861+instance (MonadHttp m, Monoid w) => MonadHttp (Writer.CPS.WriterT w m) where
862+ handleHttpException = lift . handleHttpException
863+ getHttpConfig = lift getHttpConfig
864+
865+-- | @since 3.10.0
866+instance (MonadHttp m, Monoid w) => MonadHttp (Writer.Lazy.WriterT w m) where
867+ handleHttpException = lift . handleHttpException
868+ getHttpConfig = lift getHttpConfig
869+
870+-- | @since 3.10.0
871+instance (MonadHttp m, Monoid w) => MonadHttp (Writer.Strict.WriterT w m) where
872+ handleHttpException = lift . handleHttpException
873+ getHttpConfig = lift getHttpConfig
874+
875+-- | Run a computation in the 'Req' monad with the given 'HttpConfig'. In
876+-- the case of an exceptional situation an 'HttpException' will be thrown.
877+--
878+-- @since 0.4.0
879+runReq ::
880+ (MonadIO m) =>
881+ -- | 'HttpConfig' to use
882+ HttpConfig ->
883+ -- | Computation to run
884+ Req a ->
885+ m a
886+runReq config (Req m) = liftIO (runReaderT m config)
519887
520888----------------------------------------------------------------------------
521−-- Request — Method
889+-- Request—Method
522890
523891-- $method
524892--
525893-- The package supports all methods as defined by RFC 2616, and 'PATCH'
526−-- which is defined by RFC 5789 — that should be enough to talk to RESTful
894+-- which is defined by RFC 5789—that should be enough to talk to RESTful
527895-- APIs. In some cases, however, you may want to add more methods (e.g. you
528896-- work with WebDAV <https://en.wikipedia.org/wiki/WebDAV>); no need to
529897-- compromise on type safety and hack, it only takes a couple of seconds to
530898-- define a new method that will works seamlessly, see 'HttpMethod'.
531899
532900-- | 'GET' method.
533−
534901data GET = GET
535902
536903instance HttpMethod GET where
@@ -538,7 +905,6 @@ instance HttpMethod GET where
538905 httpMethodName Proxy = Y.methodGet
539906
540907-- | 'POST' method.
541−
542908data POST = POST
543909
544910instance HttpMethod POST where
@@ -546,7 +912,6 @@ instance HttpMethod POST where
546912 httpMethodName Proxy = Y.methodPost
547913
548914-- | 'HEAD' method.
549−
550915data HEAD = HEAD
551916
552917instance HttpMethod HEAD where
@@ -554,26 +919,24 @@ instance HttpMethod HEAD where
554919 httpMethodName Proxy = Y.methodHead
555920
556921-- | 'PUT' method.
557−
558922data PUT = PUT
559923
560924instance HttpMethod PUT where
561925 type AllowsBody PUT = 'CanHaveBody
562926 httpMethodName Proxy = Y.methodPut
563927
564−-- | 'DELETE' method. This data type does not allow having request body with
565−-- 'DELETE' requests, as it should be, however some APIs may expect 'DELETE'
566−-- requests to have bodies, in that case define your own variation of
567−-- 'DELETE' method and allow it to have a body.
568−
928+-- | 'DELETE' method. RFC 7231 allows a payload in DELETE but without
929+-- semantics.
930+--
931+-- __Note__: before version /3.4.0/ this method did not allow request
932+-- bodies.
569933data DELETE = DELETE
570934
571935instance HttpMethod DELETE where
572− type AllowsBody DELETE = 'NoBody
936+ type AllowsBody DELETE = 'CanHaveBody
573937 httpMethodName Proxy = Y.methodDelete
574938
575939-- | 'TRACE' method.
576−
577940data TRACE = TRACE
578941
579942instance HttpMethod TRACE where
@@ -581,7 +944,6 @@ instance HttpMethod TRACE where
581944 httpMethodName Proxy = Y.methodTrace
582945
583946-- | 'CONNECT' method.
584−
585947data CONNECT = CONNECT
586948
587949instance HttpMethod CONNECT where
@@ -589,7 +951,6 @@ instance HttpMethod CONNECT where
589951 httpMethodName Proxy = Y.methodConnect
590952
591953-- | 'OPTIONS' method.
592−
593954data OPTIONS = OPTIONS
594955
595956instance HttpMethod OPTIONS where
@@ -597,7 +958,6 @@ instance HttpMethod OPTIONS where
597958 httpMethodName Proxy = Y.methodOptions
598959
599960-- | 'PATCH' method.
600−
601961data PATCH = PATCH
602962
603963instance HttpMethod PATCH where
@@ -605,42 +965,38 @@ instance HttpMethod PATCH where
605965 httpMethodName Proxy = Y.methodPatch
606966
607967-- | A type class for types that can be used as an HTTP method. To define a
608−-- non-standard method, follow this example that defines COPY:
968+-- non-standard method, follow this example that defines @COPY@:
609969--
610970-- > data COPY = COPY
611971-- >
612972-- > instance HttpMethod COPY where
613973-- > type AllowsBody COPY = 'CanHaveBody
614974-- > httpMethodName Proxy = "COPY"
615−
616975class HttpMethod a where
617−
618− -- | Type function 'AllowsBody' returns type of kind 'CanHaveBody' which
619− -- tells the rest of the library whether the method can have a body or
620− -- not. We use the special type 'CanHaveBody' “lifted” into kind instead
976+ -- | Type function 'AllowsBody' returns a type of kind 'CanHaveBody' which
977+ -- tells the rest of the library whether the method can have body or not.
978+ -- We use the special type 'CanHaveBody' lifted to the kind level instead
621979 -- of 'Bool' to get more user-friendly compiler messages.
622−
623980 type AllowsBody a :: CanHaveBody
624981
625982 -- | Return name of the method as a 'ByteString'.
626−
627983 httpMethodName :: Proxy a -> ByteString
628984
629−instance HttpMethod method => RequestComponent (Womb "method" method) where
985+instance (HttpMethod method) => RequestComponent (Tagged "method" method) where
630986 getRequestMod _ = Endo $ \x ->
631− x { L.method = httpMethodName (Proxy :: Proxy method) }
987+ x {L.method = httpMethodName (Proxy :: Proxy method)}
632988
633989----------------------------------------------------------------------------
634−-- Request — URL
990+-- Request—URL
635991
636992-- $url
637993--
638994-- We use 'Url's which are correct by construction, see 'Url'. To build a
639−-- 'Url' from a 'ByteString', use 'parseUrlHttp' or 'parseUrlHttps'.
995+-- 'Url' from a 'URI', use 'useHttpURI', 'useHttpsURI', or generic 'useURI'.
640996
641997-- | Request's 'Url'. Start constructing your 'Url' with 'http' or 'https'
642998-- specifying the scheme and host at the same time. Then use the @('/~')@
643−-- and @('/:')@ operators to grow path one piece at a time. Every single
999+-- and @('/:')@ operators to grow the path one piece at a time. Every single
6441000-- piece of path will be url(percent)-encoded, so using @('/~')@ and
6451001-- @('/:')@ is the only way to have forward slashes between path segments.
6461002-- This approach makes working with dynamic path segments easy and safe. See
@@ -666,125 +1022,233 @@ instance HttpMethod method => RequestComponent (Womb "method" method) where
6661022--
6671023-- > https "юникод.рф"
6681024-- > -- https://%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.%D1%80%D1%84
669−
670−data Url (scheme :: Scheme) = Url Scheme (NonEmpty T.Text)
671− -- NOTE The second value is path segments in reversed order.
672− deriving (Eq, Ord, Show, Data, Typeable, Generic)
673−
674−-- | Given host name, produce a 'Url' which have “http” as its scheme and
1025+data Url (scheme :: Scheme) = Url Scheme (NonEmpty Text)
1026+ -- NOTE The second value is the path segments in reversed order.
1027+ deriving (Eq, Ord, Show, Data, Generic)
1028+
1029+type role Url nominal
1030+
1031+-- With template-haskell >=2.15 and text >=1.2.4 Lift can be derived, however
1032+-- the derived lift forgets the type of the scheme.
1033+instance (Typeable scheme) => TH.Lift (Url scheme) where
1034+ lift url =
1035+ TH.dataToExpQ (fmap liftText . cast) url `TH.sigE` case url of
1036+ Url Http _ -> [t|Url 'Http|]
1037+ Url Https _ -> [t|Url 'Https|]
1038+ where
1039+ liftText t = TH.AppE (TH.VarE 'T.pack) <$> TH.lift (T.unpack t)
1040+ liftTyped = TH.Code . TH.unsafeTExpCoerce . TH.lift
1041+
1042+-- | Given host name, produce a 'Url' which has “http” as its scheme and
6751043-- empty path. This also sets port to @80@.
676−
677−http :: T.Text -> Url 'Http
1044+http :: Text -> Url 'Http
6781045http = Url Http . pure
6791046
680−-- | Given host name, produce a 'Url' which have “https” as its scheme and
1047+-- | Given host name, produce a 'Url' which has “https” as its scheme and
6811048-- empty path. This also sets port to @443@.
682−
683−https :: T.Text -> Url 'Https
1049+https :: Text -> Url 'Https
6841050https = Url Https . pure
6851051
686−-- | Grow given 'Url' appending a single path segment to it. Note that the
1052+-- | Grow a given 'Url' appending a single path segment to it. Note that the
6871053-- path segment can be of any type that is an instance of 'ToHttpApiData'.
688−
6891054infixl 5 /~
690−(/~) :: ToHttpApiData a => Url scheme -> a -> Url scheme
691−Url secure path /~ segment = Url secure (NE.cons (toUrlPiece segment) path)
6921055
693−-- | Type-constrained version of @('/~')@ to remove ambiguity in cases when
694−-- next URL piece is a 'Text' literal.
1056+(/~) :: (ToHttpApiData a) => Url scheme -> a -> Url scheme
1057+Url secure path /~ segment = Url secure (NE.cons (toUrlPiece segment) path)
6951058
1059+-- | A type-constrained version of @('/~')@ to remove ambiguity in the cases
1060+-- when next URL piece is a 'Data.Text.Text' literal.
6961061infixl 5 /:
697−(/:) :: Url scheme -> T.Text -> Url scheme
1062+
1063+(/:) :: Url scheme -> Text -> Url scheme
6981064(/:) = (/~)
6991065
700−-- | The 'parseUrlHttp' function provides an alternative method to get 'Url'
701−-- (possibly with some 'Option's) from a 'ByteString'. This is useful when
702−-- you are given a URL to query dynamically and don't know it beforehand.
703−-- The function parses 'ByteString' because it's the correct type to
704−-- represent a URL, as 'Url' cannot contain characters outside of ASCII
705−-- range, thus we can consider every character a 'Data.Word.Word8' value.
706−--
707−-- This function only parses 'Url' (scheme, host, path) and optional query
708−-- parameters that are returned as 'Option'. It does not parse method name
709−-- or authentication info from given 'ByteString'.
710−
711−parseUrlHttp :: ByteString -> Maybe (Url 'Http, Option scheme)
712−parseUrlHttp url' = do
713− url <- B.stripPrefix "http://" url'
714− (host :| path, option) <- parseUrlHelper url
715− return (foldl (/:) (http host) path, option)
716−
717−-- | Just like 'parseUrlHttp', but expects “https” scheme.
718−
719−parseUrlHttps :: ByteString -> Maybe (Url 'Https, Option scheme)
720−parseUrlHttps url' = do
721− url <- B.stripPrefix "https://" url'
722− (host :| path, option) <- parseUrlHelper url
723− return (foldl (/:) (https host) path, option)
724−
725−-- | Get host\/collection of path pieces and possibly query parameters
726−-- already converted to 'Option'. This function is not public.
727−
728−parseUrlHelper :: ByteString -> Maybe (NonEmpty T.Text, Option scheme)
729−parseUrlHelper url = do
730− let (path', query') = B.break (== 0x3f) url
731− query = mconcat (uncurry queryParam <$> Y.parseQueryText query')
732− path <- NE.nonEmpty (Y.decodePathSegments path')
733− return (path, query)
1066+-- | Render a 'Url' as 'Text'.
1067+--
1068+-- @since 3.4.0
1069+renderUrl :: Url scheme -> Text
1070+renderUrl = \case
1071+ Url Https parts ->
1072+ "https://" <> renderParts parts
1073+ Url Http parts ->
1074+ "http://" <> renderParts parts
1075+ where
1076+ renderParts parts =
1077+ T.intercalate "/" (reverse $ NE.toList parts)
1078+
1079+-- | The 'useHttpURI' function provides an alternative method to get 'Url'
1080+-- (possibly with some 'Option's) from a 'URI'. This is useful when you are
1081+-- given a URL to query dynamically and don't know it beforehand.
1082+--
1083+-- This function expects the scheme to be “http” and host to be present.
1084+--
1085+-- @since 3.0.0
1086+useHttpURI :: URI -> Maybe (Url 'Http, Option scheme)
1087+useHttpURI uri = do
1088+ guard (URI.uriScheme uri == Just [QQ.scheme|http|])
1089+ urlHead <- http <$> uriHost uri
1090+ let url = case URI.uriPath uri of
1091+ Nothing -> urlHead
1092+ Just uriPath -> uriPathToUrl uriPath urlHead
1093+ return (url, uriOptions uri)
1094+
1095+-- | Just like 'useHttpURI', but expects the “https” scheme.
1096+--
1097+-- @since 3.0.0
1098+useHttpsURI :: URI -> Maybe (Url 'Https, Option scheme)
1099+useHttpsURI uri = do
1100+ guard (URI.uriScheme uri == Just [QQ.scheme|https|])
1101+ urlHead <- https <$> uriHost uri
1102+ let url = case URI.uriPath uri of
1103+ Nothing -> urlHead
1104+ Just uriPath -> uriPathToUrl uriPath urlHead
1105+ return (url, uriOptions uri)
1106+
1107+-- | Convert URI path to a 'Url'. Internal.
1108+--
1109+-- @since 3.9.0
1110+uriPathToUrl ::
1111+ (Bool, NonEmpty (URI.RText 'URI.PathPiece)) ->
1112+ Url scheme ->
1113+ Url scheme
1114+uriPathToUrl (trailingSlash, xs) urlHead =
1115+ if trailingSlash then path /: T.empty else path
1116+ where
1117+ path = foldl' (/:) urlHead (URI.unRText <$> NE.toList xs)
1118+
1119+-- | A combination of 'useHttpURI' and 'useHttpsURI' for cases when scheme
1120+-- is not known in advance.
1121+--
1122+-- @since 3.0.0
1123+useURI ::
1124+ URI ->
1125+ Maybe
1126+ ( Either
1127+ (Url 'Http, Option scheme0)
1128+ (Url 'Https, Option scheme1)
1129+ )
1130+useURI uri =
1131+ (Left <$> useHttpURI uri) <|> (Right <$> useHttpsURI uri)
1132+
1133+-- | An internal helper function to extract host from a 'URI'.
1134+uriHost :: URI -> Maybe Text
1135+uriHost uri = case URI.uriAuthority uri of
1136+ Left _ -> Nothing
1137+ Right URI.Authority {..} ->
1138+ Just (URI.unRText authHost)
1139+
1140+-- | A quasiquoter to build an 'Url' and 'Option' tuple. The type of the
1141+-- generated expression is @('Url' scheme0, 'Option' scheme1)@ with
1142+-- @scheme0@ being either 'Http' or 'Https' depending on the input.
1143+--
1144+-- @since 3.2.0
1145+urlQ :: TH.QuasiQuoter
1146+urlQ =
1147+ TH.QuasiQuoter
1148+ { quoteExp = \str ->
1149+ case URI.mkURI (T.pack str) of
1150+ Left err -> fail (displayException err)
1151+ Right uri -> case useURI uri of
1152+ Nothing -> fail "Not a HTTP or HTTPS URL"
1153+ Just eurl ->
1154+ TH.tupE
1155+ [ either (TH.lift . fst) (TH.lift . fst) eurl,
1156+ [|uriOptions uri|]
1157+ ],
1158+ quotePat = error "This usage is not supported",
1159+ quoteType = error "This usage is not supported",
1160+ quoteDec = error "This usage is not supported"
1161+ }
1162+
1163+-- | An internal helper function to extract 'Option's from a 'URI'.
1164+uriOptions :: forall scheme. URI -> Option scheme
1165+uriOptions uri =
1166+ mconcat
1167+ [ auth,
1168+ query,
1169+ port'
1170+ -- , fragment'
1171+ ]
1172+ where
1173+ (auth, port') =
1174+ case URI.uriAuthority uri of
1175+ Left _ -> (mempty, mempty)
1176+ Right URI.Authority {..} ->
1177+ let auth0 = case authUserInfo of
1178+ Nothing -> mempty
1179+ Just URI.UserInfo {..} ->
1180+ let username = T.encodeUtf8 (URI.unRText uiUsername)
1181+ password = maybe "" (T.encodeUtf8 . URI.unRText) uiPassword
1182+ in basicAuthUnsafe username password
1183+ port0 = case authPort of
1184+ Nothing -> mempty
1185+ Just port'' -> port (fromIntegral port'')
1186+ in (auth0, port0)
1187+ query =
1188+ let liftQueryParam = \case
1189+ URI.QueryFlag t -> queryFlag (URI.unRText t)
1190+ URI.QueryParam k v -> URI.unRText k =: URI.unRText v
1191+ in mconcat (liftQueryParam <$> URI.uriQuery uri)
1192+
1193+-- TODO Blocked on upstream: https://github.com/snoyberg/http-client/issues/424
1194+-- fragment' =
1195+-- case URI.uriFragment uri of
1196+-- Nothing -> mempty
1197+-- Just fragment'' -> fragment (URI.unRText fragment'')
7341198
7351199instance RequestComponent (Url scheme) where
7361200 getRequestMod (Url scheme segments) = Endo $ \x ->
737− let (host :| path) = NE.reverse segments in
738− x { L.secure = case scheme of
739− Http -> False
740− Https -> True
741− , L.port = case scheme of
742− Http -> 80
743− Https -> 443
744− , L.host = Y.urlEncode False (T.encodeUtf8 host)
745− , L.path =
746− (BL.toStrict . BB.toLazyByteString . Y.encodePathSegments) path }
1201+ let (host :| path) = NE.reverse segments
1202+ in x
1203+ { L.secure = case scheme of
1204+ Http -> False
1205+ Https -> True,
1206+ L.port = case scheme of
1207+ Http -> 80
1208+ Https -> 443,
1209+ L.host = Y.urlEncode False (T.encodeUtf8 host),
1210+ L.path =
1211+ (BL.toStrict . BB.toLazyByteString . Y.encodePathSegments) path
1212+ }
7471213
7481214----------------------------------------------------------------------------
749−-- Request — Body
1215+-- Request—Body
7501216
7511217-- $body
7521218--
7531219-- A number of options for request bodies are available. The @Content-Type@
754−-- header is set for you automatically according to body option you use
755−-- (it's always specified in documentation for given body option). To add
756−-- your own way to represent request body, see 'HttpBody'.
1220+-- header is set for you automatically according to the body option you use
1221+-- (it's always specified in the documentation for a given body option). To
1222+-- add your own way to represent request body, define an instance of
1223+-- 'HttpBody'.
7571224
7581225-- | This data type represents empty body of an HTTP request. This is the
7591226-- data type to use with 'HttpMethod's that cannot have a body, as it's the
7601227-- only type for which 'ProvidesBody' returns 'NoBody'.
7611228--
7621229-- Using of this body option does not set the @Content-Type@ header.
763−
7641230data NoReqBody = NoReqBody
7651231
7661232instance HttpBody NoReqBody where
7671233 getRequestBody NoReqBody = L.RequestBodyBS B.empty
7681234
769−-- | This body option allows to use a JSON object as request body — probably
770−-- the most popular format right now. Just wrap a data type that is an
771−-- instance of 'ToJSON' type class and you are done: it will be converted to
772−-- JSON and inserted as request body.
1235+-- | This body option allows us to use a JSON object as the request
1236+-- body—probably the most popular format right now. Just wrap a data type
1237+-- that is an instance of 'ToJSON' type class and you are done: it will be
1238+-- converted to JSON and inserted as request body.
7731239--
7741240-- This body option sets the @Content-Type@ header to @\"application/json;
7751241-- charset=utf-8\"@ value.
776−
7771242newtype ReqBodyJson a = ReqBodyJson a
7781243
779−instance ToJSON a => HttpBody (ReqBodyJson a) where
1244+instance (ToJSON a) => HttpBody (ReqBodyJson a) where
7801245 getRequestBody (ReqBodyJson a) = L.RequestBodyLBS (A.encode a)
781− getRequestContentType Proxy = pure "application/json; charset=utf-8"
1246+ getRequestContentType _ = pure "application/json; charset=utf-8"
7821247
7831248-- | This body option streams request body from a file. It is expected that
784−-- the file size does not change during the streaming.
1249+-- the file size does not change during streaming.
7851250--
7861251-- Using of this body option does not set the @Content-Type@ header.
787−
7881252newtype ReqBodyFile = ReqBodyFile FilePath
7891253
7901254instance HttpBody ReqBodyFile where
@@ -794,7 +1258,6 @@ instance HttpBody ReqBodyFile where
7941258-- | HTTP request body represented by a strict 'ByteString'.
7951259--
7961260-- Using of this body option does not set the @Content-Type@ header.
797−
7981261newtype ReqBodyBs = ReqBodyBs ByteString
7991262
8001263instance HttpBody ReqBodyBs where
@@ -803,257 +1266,387 @@ instance HttpBody ReqBodyBs where
8031266-- | HTTP request body represented by a lazy 'BL.ByteString'.
8041267--
8051268-- Using of this body option does not set the @Content-Type@ header.
806−
8071269newtype ReqBodyLbs = ReqBodyLbs BL.ByteString
8081270
8091271instance HttpBody ReqBodyLbs where
8101272 getRequestBody (ReqBodyLbs bs) = L.RequestBodyLBS bs
8111273
812−-- | Form URL-encoded body. This can hold a collection of parameters which
813−-- are encoded similarly to query parameters at the end of query string,
814−-- with the only difference that they are stored in request body. The
815−-- similarity is reflected in the API as well, as you can use the same
816−-- combinators you would use to add query parameters: @('=:')@ and
817−-- 'queryFlag'.
1274+-- | URL-encoded body. This can hold a collection of parameters which are
1275+-- encoded similarly to query parameters at the end of query string, with
1276+-- the only difference that they are stored in request body. The similarity
1277+-- is reflected in the API as well, as you can use the same combinators you
1278+-- would use to add query parameters: @('=:')@ and 'queryFlag'.
8181279--
8191280-- This body option sets the @Content-Type@ header to
820−-- @\"application/x-www-from-urlencoded\"@ value.
821−
1281+-- @\"application/x-www-form-urlencoded\"@ value.
8221282newtype ReqBodyUrlEnc = ReqBodyUrlEnc FormUrlEncodedParam
8231283
8241284instance HttpBody ReqBodyUrlEnc where
8251285 getRequestBody (ReqBodyUrlEnc (FormUrlEncodedParam params)) =
8261286 (L.RequestBodyLBS . BB.toLazyByteString) (Y.renderQueryText False params)
827− getRequestContentType Proxy = pure "application/x-www-form-urlencoded"
1287+ getRequestContentType _ = pure "application/x-www-form-urlencoded"
8281288
8291289-- | An opaque monoidal value that allows to collect URL-encoded parameters
8301290-- to be wrapped in 'ReqBodyUrlEnc'.
831−
832−newtype FormUrlEncodedParam = FormUrlEncodedParam [(T.Text, Maybe T.Text)]
1291+newtype FormUrlEncodedParam = FormUrlEncodedParam [(Text, Maybe Text)]
8331292 deriving (Semigroup, Monoid)
8341293
8351294instance QueryParam FormUrlEncodedParam where
8361295 queryParam name mvalue =
8371296 FormUrlEncodedParam [(name, toQueryParam <$> mvalue)]
1297+ queryParamToList (FormUrlEncodedParam p) = p
8381298
839−-- | A type class for things that can be interpreted as HTTP
840−-- 'L.RequestBody'.
1299+-- | Use 'formToQuery'.
1300+--
1301+-- @since 3.11.0
1302+instance FromForm FormUrlEncodedParam where
1303+ fromForm = Right . formToQuery
8411304
1305+-- | Multipart form data. Please consult the
1306+-- "Network.HTTP.Client.MultipartFormData" module for how to construct
1307+-- parts, then use 'reqBodyMultipart' to create actual request body from the
1308+-- parts. 'reqBodyMultipart' is the only way to get a value of the type
1309+-- 'ReqBodyMultipart', as its constructor is not exported on purpose.
1310+--
1311+-- @since 0.2.0
1312+--
1313+-- ==== __Examples__
1314+--
1315+-- > import Control.Monad.IO.Class
1316+-- > import Data.Default.Class
1317+-- > import Network.HTTP.Req
1318+-- > import qualified Network.HTTP.Client.MultipartFormData as LM
1319+-- >
1320+-- > main :: IO ()
1321+-- > main = runReq def $ do
1322+-- > body <-
1323+-- > reqBodyMultipart
1324+-- > [ LM.partBS "title" "My Image"
1325+-- > , LM.partFileSource "file1" "/tmp/image.jpg"
1326+-- > ]
1327+-- > response <-
1328+-- > req POST (http "example.com" /: "post")
1329+-- > body
1330+-- > bsResponse
1331+-- > mempty
1332+-- > liftIO $ print (responseBody response)
1333+data ReqBodyMultipart = ReqBodyMultipart ByteString LI.RequestBody
1334+
1335+instance HttpBody ReqBodyMultipart where
1336+ getRequestBody (ReqBodyMultipart _ body) = body
1337+ getRequestContentType (ReqBodyMultipart boundary _) =
1338+ pure ("multipart/form-data; boundary=" <> boundary)
1339+
1340+-- | Create 'ReqBodyMultipart' request body from a collection of 'LM.Part's.
1341+--
1342+-- @since 0.2.0
1343+reqBodyMultipart :: (MonadIO m) => [LM.Part] -> m ReqBodyMultipart
1344+reqBodyMultipart parts = liftIO $ do
1345+ boundary <- LM.webkitBoundary
1346+ body <- LM.renderParts boundary parts
1347+ return (ReqBodyMultipart boundary body)
1348+
1349+-- | A type class for things that can be interpreted as an HTTP
1350+-- 'L.RequestBody'.
8421351class HttpBody body where
843−
844− {-# MINIMAL getRequestBody #-}
845−
8461352 -- | How to get actual 'L.RequestBody'.
847−
8481353 getRequestBody :: body -> L.RequestBody
8491354
850− -- | This method allows to optionally specify value of @Content-Type@
851− -- header that should be used with particular body option. By default it
852− -- returns 'Nothing' and so @Content-Type@ is not set.
853−
854− getRequestContentType :: Proxy body -> Maybe ByteString
855− getRequestContentType Proxy = Nothing
1355+ -- | This method allows us to optionally specify the value of
1356+ -- @Content-Type@ header that should be used with particular body option.
1357+ -- By default it returns 'Nothing' and so @Content-Type@ is not set.
1358+ getRequestContentType :: body -> Maybe ByteString
1359+ getRequestContentType = const Nothing
8561360
8571361-- | The type function recognizes 'NoReqBody' as having 'NoBody', while any
858−-- other body option 'CanHaveBody'. This forces user to use 'NoReqBody' with
859−-- 'GET' method and other methods that should not send a body.
860−
1362+-- other body option 'CanHaveBody'. This forces the user to use 'NoReqBody'
1363+-- with 'GET' method and other methods that should not have body.
8611364type family ProvidesBody body :: CanHaveBody where
8621365 ProvidesBody NoReqBody = 'NoBody
863− ProvidesBody body = 'CanHaveBody
1366+ ProvidesBody body = 'CanHaveBody
8641367
8651368-- | This type function allows any HTTP body if method says it
866−-- 'CanHaveBody'. When method says it should have 'NoBody', the only body
867−-- option to use is 'NoReqBody'.
868−--
869−-- __Note__: users of GHC 8.0.1 will see slightly more friendly error
870−-- messages when method does not allow a body and body is provided.
871−
872−type family HttpBodyAllowed
873− (allowsBody :: CanHaveBody)
874− (providesBody :: CanHaveBody) :: Constraint where
875− HttpBodyAllowed 'NoBody 'NoBody = ()
876− HttpBodyAllowed 'CanHaveBody body = ()
877−#if MIN_VERSION_base(4,9,0)
878− HttpBodyAllowed 'NoBody 'CanHaveBody = TypeError
879− ('Text "This HTTP method does not allow attaching a request body.")
880−#endif
881−
882−instance HttpBody body => RequestComponent (Womb "body" body) where
883− getRequestMod (Womb body) = Endo $ \x ->
884− x { L.requestBody = getRequestBody body
885− , L.requestHeaders =
886− let old = L.requestHeaders x in
887− case getRequestContentType (Proxy :: Proxy body) of
888− Nothing -> old
889− Just contentType ->
890− (Y.hContentType, contentType) : old }
1369+-- 'CanHaveBody'. When the method says it should have 'NoBody', the only
1370+-- body option to use is 'NoReqBody'.
1371+type family
1372+ HttpBodyAllowed
1373+ (allowsBody :: CanHaveBody)
1374+ (providesBody :: CanHaveBody) ::
1375+ Constraint
1376+ where
1377+ HttpBodyAllowed 'NoBody 'NoBody = ()
1378+ HttpBodyAllowed 'CanHaveBody body = ()
1379+ HttpBodyAllowed 'NoBody 'CanHaveBody =
1380+ TypeError
1381+ ('Text "This HTTP method does not allow attaching a request body.")
1382+
1383+instance (HttpBody body) => RequestComponent (Tagged "body" body) where
1384+ getRequestMod (Tagged body) = Endo $ \x ->
1385+ x
1386+ { L.requestBody = getRequestBody body,
1387+ L.requestHeaders =
1388+ let old = L.requestHeaders x
1389+ in case getRequestContentType body of
1390+ Nothing -> old
1391+ Just contentType ->
1392+ (Y.hContentType, contentType) : old
1393+ }
8911394
8921395----------------------------------------------------------------------------
893−-- Request — Optional parameters
1396+-- Request—Optional parameters
8941397
8951398-- $optional-parameters
8961399--
897−-- Optional parameters to a request include things like query parameters,
1400+-- Optional parameters of request include things like query parameters,
8981401-- headers, port number, etc. All optional parameters have the type
8991402-- 'Option', which is a 'Monoid'. This means that you can use 'mempty' as
9001403-- the last argument of 'req' to specify no optional parameters, or combine
901−-- 'Option's using 'mappend' (or @('<>')@) to have several of them at once.
1404+-- 'Option's using 'mappend' or @('<>')@ to have several of them at once.
9021405
903−-- | Opaque 'Option' type is a 'Monoid' you can use to pack collection of
904−-- optional parameters like query parameters and headers. See sections below
905−-- to learn which 'Option' primitives are available.
1406+-- | The opaque 'Option' type is a 'Monoid' you can use to pack collection
1407+-- of optional parameters like query parameters and headers. See sections
1408+-- below to learn which 'Option' primitives are available.
1409+data Option (scheme :: Scheme)
1410+ = Option (Endo (Y.QueryText, L.Request)) (Maybe (L.Request -> IO L.Request))
9061411
907−data Option (scheme :: Scheme) =
908− Option (Endo (Y.QueryText, L.Request)) (Maybe (Endo L.Request))
909− -- NOTE 'QueryText' is just [(Text, Maybe Text)], we keep it along with
910− -- Request to avoid appending to existing query string in request every
911− -- time new parameter is added. Additional Maybe (Endo Request) is a
912− -- finalizer that will be applied after all over options. This is for
913− -- authentication methods that sign requests based on data in Request.
1412+-- NOTE 'QueryText' is just [(Text, Maybe Text)], we keep it along with
1413+-- Request to avoid appending to an existing query string in request every
1414+-- time new parameter is added. The additional Maybe (L.Request -> IO
1415+-- L.Request) is a finalizer that will be applied after all other
1416+-- transformations. This is for authentication methods that sign requests
1417+-- based on data in Request.
9141418
9151419instance Semigroup (Option scheme) where
916− Option er0 mr0 <> Option er1 mr1 = Option
917− (er0 <> er1) (mr0 <|> mr1)
1420+ Option er0 mr0 <> Option er1 mr1 =
1421+ Option
1422+ (er0 <> er1)
1423+ (mr0 <|> mr1)
9181424
9191425instance Monoid (Option scheme) where
920− mempty = Option mempty Nothing
1426+ mempty = Option mempty Nothing
9211427 mappend = (<>)
9221428
923−-- | A helper to create an 'Option' that modifies only collection of query
924−-- parameters. This helper is not a part of public API.
1429+-- | Use 'formToQuery'.
1430+--
1431+-- @since 3.11.0
1432+instance FromForm (Option scheme) where
1433+ fromForm = Right . formToQuery
9251434
1435+-- | A helper to create an 'Option' that modifies only collection of query
1436+-- parameters. This helper is not a part of the public API.
9261437withQueryParams :: (Y.QueryText -> Y.QueryText) -> Option scheme
9271438withQueryParams f = Option (Endo (first f)) Nothing
9281439
9291440-- | A helper to create an 'Option' that modifies only 'L.Request'. This
9301441-- helper is not a part of public API.
931−
9321442withRequest :: (L.Request -> L.Request) -> Option scheme
9331443withRequest f = Option (Endo (second f)) Nothing
9341444
935−-- | A helper to create an 'Option' that adds a finalizer (request
936−-- endomorphism that is run after all other modifications).
937−
938−asFinalizer :: (L.Request -> L.Request) -> Option scheme
939−asFinalizer f = Option mempty (Just (Endo f))
940−
9411445instance RequestComponent (Option scheme) where
942− getRequestMod (Option f finalizer) = Endo $ \x ->
1446+ getRequestMod (Option f _) = Endo $ \x ->
9431447 let (qparams, x') = appEndo f ([], x)
944− query = Y.renderQuery True (Y.queryTextToQuery qparams)
945− in maybe id appEndo finalizer x' { L.queryString = query }
1448+ query = Y.renderQuery True (Y.queryTextToQuery qparams)
1449+ in x' {L.queryString = query}
1450+
1451+-- | Finalize given 'L.Request' by applying a finalizer from the given
1452+-- 'Option' (if it has any).
1453+finalizeRequest :: (MonadIO m) => Option scheme -> L.Request -> m L.Request
1454+finalizeRequest (Option _ mfinalizer) = liftIO . fromMaybe pure mfinalizer
9461455
9471456----------------------------------------------------------------------------
948−-- Request — Optional parameters — Query Parameters
1457+-- Request—Optional parameters—Query Parameters
9491458
9501459-- $query-parameters
9511460--
9521461-- This section describes a polymorphic interface that can be used to
953−-- construct query parameters (of type 'Option') and form URL-encoded bodies
954−-- (of type 'FormUrlEncodedParam').
1462+-- construct query parameters (of the type 'Option') and form URL-encoded
1463+-- bodies (of the type 'FormUrlEncodedParam').
9551464
9561465-- | This operator builds a query parameter that will be included in URL of
957−-- your request after question sign @?@. This is the same syntax you use
1466+-- your request after the question sign @?@. This is the same syntax you use
9581467-- with form URL encoded request bodies.
9591468--
9601469-- This operator is defined in terms of 'queryParam':
9611470--
9621471-- > name =: value = queryParam name (pure value)
963−
9641472infix 7 =:
965−(=:) :: (QueryParam param, ToHttpApiData a) => T.Text -> a -> param
1473+
1474+(=:) :: (QueryParam param, ToHttpApiData a) => Text -> a -> param
9661475name =: value = queryParam name (pure value)
9671476
968−-- | Construct a flag, that is, valueless query parameter. For example, in
969−-- the following URL @a@ is a flag, @b@ is a query parameter with a value:
1477+-- | Construct a flag, that is, a valueless query parameter. For example, in
1478+-- the following URL @\"a\"@ is a flag, while @\"b\"@ is a query parameter
1479+-- with a value:
9701480--
9711481-- > https://httpbin.org/foo/bar?a&b=10
9721482--
9731483-- This operator is defined in terms of 'queryParam':
9741484--
975−-- > queryFlag name = queryParam name Nothing
976−
977−queryFlag :: QueryParam param => T.Text -> param
1485+-- > queryFlag name = queryParam name (Nothing :: Maybe ())
1486+queryFlag :: (QueryParam param) => Text -> param
9781487queryFlag name = queryParam name (Nothing :: Maybe ())
9791488
980−-- | A type class for query-parameter-like things. The reason to have
1489+-- | Construct query parameters from a 'ToForm' instance. This function
1490+-- produces the same query params as 'Form.urlEncodeAsFormStable'.
1491+--
1492+-- Note that 'Form.Form' doesn't have the concept of parameters with the
1493+-- empty value (i.e. what you can get by @key =: ""@). If the value is
1494+-- empty, it will be encoded as a valueless parameter (i.e. what you can get
1495+-- by @queryFlag key@).
1496+--
1497+-- @since 3.11.0
1498+formToQuery :: (QueryParam param, Monoid param, ToForm f) => f -> param
1499+formToQuery f = mconcat . fmap toParam . Form.toListStable $ toForm f
1500+ where
1501+ toParam (key, val) =
1502+ queryParam key $
1503+ if val == ""
1504+ then Nothing
1505+ else Just val
1506+
1507+-- | A type class for query-parameter-like things. The reason to have an
9811508-- overloaded 'queryParam' is to be able to use it as an 'Option' and as a
9821509-- 'FormUrlEncodedParam' when constructing form URL encoded request bodies.
9831510-- Having the same syntax for these cases seems natural and user-friendly.
984−
9851511class QueryParam param where
986−
9871512 -- | Create a query parameter with given name and value. If value is
9881513 -- 'Nothing', it won't be included at all (i.e. you create a flag this
9891514 -- way). It's recommended to use @('=:')@ and 'queryFlag' instead of this
9901515 -- method, because they are easier to read.
1516+ queryParam :: (ToHttpApiData a) => Text -> Maybe a -> param
9911517
992− queryParam :: ToHttpApiData a => T.Text -> Maybe a -> param
1518+ -- | Get the query parameter names and values set by 'queryParam'.
1519+ --
1520+ -- @since 3.11.0
1521+ queryParamToList :: param -> [(Text, Maybe Text)]
9931522
9941523instance QueryParam (Option scheme) where
9951524 queryParam name mvalue =
9961525 withQueryParams ((:) (name, toQueryParam <$> mvalue))
1526+ queryParamToList (Option f _) = fst $ appEndo f ([], L.defaultRequest)
9971527
9981528----------------------------------------------------------------------------
999−-- Request — Optional parameters — Headers
1529+-- Request—Optional parameters—Headers
10001530
10011531-- | Create an 'Option' that adds a header. Note that if you 'mappend' two
10021532-- headers with the same names the leftmost header will win. This means, in
10031533-- particular, that you cannot create a request with several headers of the
10041534-- same name.
1005−
1006−header
1007− :: ByteString -- ^ Header name
1008− -> ByteString -- ^ Header value
1009− -> Option scheme
1535+header ::
1536+ -- | Header name
1537+ ByteString ->
1538+ -- | Header value
1539+ ByteString ->
1540+ Option scheme
10101541header name value = withRequest (attachHeader name value)
10111542
1012−-- | A non-public helper that attaches a header with given name and content
1013−-- to a 'L.Request'.
1014−
1543+-- | Attach a header with given name and content to a 'L.Request'.
1544+--
1545+-- @since 1.1.0
10151546attachHeader :: ByteString -> ByteString -> L.Request -> L.Request
10161547attachHeader name value x =
1017− x { L.requestHeaders = (CI.mk name, value) : L.requestHeaders x }
1548+ x {L.requestHeaders = (CI.mk name, value) : L.requestHeaders x}
1549+
1550+-- | Same as 'header', but with redacted values on print.
1551+--
1552+-- @since 3.13.0
1553+headerRedacted :: ByteString -> ByteString -> Option scheme
1554+headerRedacted name value = withRequest $ \x ->
1555+ let y = attachHeader name value x
1556+ in y {L.redactHeaders = CI.mk name `S.insert` L.redactHeaders y}
10181557
10191558----------------------------------------------------------------------------
1020−-- Request — Optional parameters — Cookies
1559+-- Request—Optional parameters—Cookies
10211560
10221561-- $cookies
10231562--
1024−-- Support for cookies is quite minimalistic at the moment, its' possible to
1563+-- Support for cookies is quite minimalistic at the moment. It's possible to
10251564-- specify which cookies to send using 'cookieJar' and inspect 'L.Response'
10261565-- to extract 'L.CookieJar' from it (see 'responseCookieJar').
10271566
10281567-- | Use the given 'L.CookieJar'. A 'L.CookieJar' can be obtained from a
10291568-- 'L.Response' record.
1030−
10311569cookieJar :: L.CookieJar -> Option scheme
10321570cookieJar jar = withRequest $ \x ->
1033− x { L.cookieJar = Just jar }
1571+ x {L.cookieJar = Just jar}
10341572
10351573----------------------------------------------------------------------------
1036−-- Request — Optional parameters — Authentication
1574+-- Request—Optional parameters—Authentication
10371575
10381576-- $authentication
10391577--
1040−-- This section provides common authentication helpers in form of 'Option's.
1041−-- You should always prefer the provided authentication 'Option's to manual
1042−-- construction of headers because it ensures that you only use one
1043−-- authentication method at a time (they overwrite each other) and provides
1044−-- additional type safety that prevents leaking of credentials in cases when
1045−-- authentication relies on TLS for encrypting sensitive data.
1578+-- This section provides the common authentication helpers in the form of
1579+-- 'Option's. You should always prefer the provided authentication 'Option's
1580+-- to manual construction of headers because it ensures that you only use
1581+-- one authentication method at a time (they overwrite each other) and
1582+-- provides additional type safety that prevents leaking of credentials in
1583+-- the cases when authentication relies on HTTPS for encrypting sensitive
1584+-- data.
10461585
10471586-- | The 'Option' adds basic authentication.
10481587--
10491588-- See also: <https://en.wikipedia.org/wiki/Basic_access_authentication>.
1050−
1051−basicAuth
1052− :: ByteString -- ^ Username
1053− -> ByteString -- ^ Password
1054− -> Option 'Https -- ^ Auth 'Option'
1055−basicAuth username password = asFinalizer
1056− (L.applyBasicAuth username password)
1589+basicAuth ::
1590+ -- | Username
1591+ ByteString ->
1592+ -- | Password
1593+ ByteString ->
1594+ -- | Auth 'Option'
1595+ Option 'Https
1596+basicAuth = basicAuthUnsafe
1597+
1598+-- | An alternative to 'basicAuth' which works for any scheme. Note that
1599+-- using basic access authentication without SSL\/TLS is vulnerable to
1600+-- attacks. Use 'basicAuth' instead unless you know what you are doing.
1601+--
1602+-- @since 0.3.1
1603+basicAuthUnsafe ::
1604+ -- | Username
1605+ ByteString ->
1606+ -- | Password
1607+ ByteString ->
1608+ -- | Auth 'Option'
1609+ Option scheme
1610+basicAuthUnsafe username password =
1611+ customAuth
1612+ (pure . L.applyBasicAuth username password)
1613+
1614+-- | The 'Option' set basic proxy authentication header.
1615+--
1616+-- @since 1.1.0
1617+basicProxyAuth ::
1618+ -- | Username
1619+ ByteString ->
1620+ -- | Password
1621+ ByteString ->
1622+ -- | Auth 'Option'
1623+ Option scheme
1624+basicProxyAuth username password =
1625+ withRequest (L.applyBasicProxyAuth username password)
1626+
1627+-- | The 'Option' adds OAuth1 authentication.
1628+--
1629+-- @since 0.2.0
1630+oAuth1 ::
1631+ -- | Consumer token
1632+ ByteString ->
1633+ -- | Consumer secret
1634+ ByteString ->
1635+ -- | OAuth token
1636+ ByteString ->
1637+ -- | OAuth token secret
1638+ ByteString ->
1639+ -- | Auth 'Option'
1640+ Option scheme
1641+oAuth1 consumerToken consumerSecret token tokenSecret =
1642+ customAuth (OAuth.signOAuth app creds)
1643+ where
1644+ app =
1645+ OAuth.newOAuth
1646+ { OAuth.oauthConsumerKey = consumerToken,
1647+ OAuth.oauthConsumerSecret = consumerSecret
1648+ }
1649+ creds = OAuth.newCredential token tokenSecret
10571650
10581651-- | The 'Option' adds an OAuth2 bearer token. This is treated by many
10591652-- services as the equivalent of a username and password.
@@ -1063,12 +1656,14 @@ basicAuth username password = asFinalizer
10631656-- > oAuth2Bearer token = header "Authorization" ("Bearer " <> token)
10641657--
10651658-- See also: <https://en.wikipedia.org/wiki/OAuth>.
1066−
1067−oAuth2Bearer
1068− :: ByteString -- ^ Token
1069− -> Option 'Https -- ^ Auth 'Option'
1070−oAuth2Bearer token = asFinalizer
1071− (attachHeader "Authorization" ("Bearer " <> token))
1659+oAuth2Bearer ::
1660+ -- | Token
1661+ ByteString ->
1662+ -- | Auth 'Option'
1663+ Option 'Https
1664+oAuth2Bearer token =
1665+ customAuth
1666+ (pure . attachHeader "Authorization" ("Bearer " <> token))
10721667
10731668-- | The 'Option' adds a not-quite-standard OAuth2 bearer token (that seems
10741669-- to be used only by GitHub). This will be treated by whatever services
@@ -1079,26 +1674,35 @@ oAuth2Bearer token = asFinalizer
10791674-- > oAuth2Token token = header "Authorization" ("token" <> token)
10801675--
10811676-- See also: <https://developer.github.com/v3/oauth#3-use-the-access-token-to-access-the-api>.
1082−
1083−oAuth2Token
1084− :: ByteString -- ^ Token
1085− -> Option 'Https -- ^ Auth 'Option'
1086−oAuth2Token token = asFinalizer
1087− (attachHeader "Authorization" ("token " <> token))
1677+oAuth2Token ::
1678+ -- | Token
1679+ ByteString ->
1680+ -- | Auth 'Option'
1681+ Option 'Https
1682+oAuth2Token token =
1683+ customAuth
1684+ (pure . attachHeader "Authorization" ("token " <> token))
1685+
1686+-- | A helper to create custom authentication 'Option's. The given
1687+-- 'IO'-enabled request transformation is applied after all other
1688+-- modifications when constructing a request. Use wisely.
1689+--
1690+-- @since 1.1.0
1691+customAuth :: (L.Request -> IO L.Request) -> Option scheme
1692+customAuth = Option mempty . pure
10881693
10891694----------------------------------------------------------------------------
1090−-- Request — Optional parameters — Other
1695+-- Request—Optional parameters—Other
10911696
10921697-- | Specify the port to connect to explicitly. Normally, 'Url' you use
1093−-- determines default port, @80@ for HTTP and @443@ for HTTPS, this 'Option'
1094−-- allows to choose arbitrary port overwriting the defaults.
1095−
1698+-- determines the default port: @80@ for HTTP and @443@ for HTTPS. This
1699+-- 'Option' allows us to choose an arbitrary port overwriting the defaults.
10961700port :: Int -> Option scheme
10971701port n = withRequest $ \x ->
1098− x { L.port = n }
1702+ x {L.port = n}
10991703
11001704-- | This 'Option' controls whether gzipped data should be decompressed on
1101−-- the fly. By default everything except for @application\/x-tar@ is
1705+-- the fly. By default everything except for @\"application\/x-tar\"@ is
11021706-- decompressed, i.e. we have:
11031707--
11041708-- > decompress (/= "application/x-tar")
@@ -1106,262 +1710,342 @@ port n = withRequest $ \x ->
11061710-- You can also choose to decompress everything like this:
11071711--
11081712-- > decompress (const True)
1109−
1110−decompress
1111− :: (ByteString -> Bool) -- ^ Predicate that is given MIME type, it
1112− -- returns 'True' when content should be decompressed on the fly.
1113− -> Option scheme
1713+decompress ::
1714+ -- | Predicate that is given MIME type, it returns 'True' when content
1715+ -- should be decompressed on the fly.
1716+ (ByteString -> Bool) ->
1717+ Option scheme
11141718decompress f = withRequest $ \x ->
1115− x { L.decompress = f }
1116−
1117−-- | Specify number of microseconds to wait for response. Default is 30
1118−-- seconds.
1119−
1120−responseTimeout
1121− :: Int -- ^ Number of microseconds to wait
1122− -> Option scheme
1719+ x {L.decompress = f}
1720+
1721+-- | Specify the number of microseconds to wait for response. The default
1722+-- value is 30 seconds (defined in 'L.ManagerSettings' of connection
1723+-- 'L.Manager').
1724+responseTimeout ::
1725+ -- | Number of microseconds to wait
1726+ Int ->
1727+ Option scheme
11231728responseTimeout n = withRequest $ \x ->
1124− x { L.responseTimeout = LI.ResponseTimeoutMicro n }
1125−
1126−-- | HTTP version to send to server, default is HTTP 1.1.
1127−
1128−httpVersion
1129− :: Int -- ^ Major version number
1130− -> Int -- ^ Minor version number
1131− -> Option scheme
1729+ x {L.responseTimeout = LI.ResponseTimeoutMicro n}
1730+
1731+-- | HTTP version to send to the server, the default is HTTP 1.1.
1732+httpVersion ::
1733+ -- | Major version number
1734+ Int ->
1735+ -- | Minor version number
1736+ Int ->
1737+ Option scheme
11321738httpVersion major minor = withRequest $ \x ->
1133− x { L.requestVersion = Y.HttpVersion major minor }
1739+ x {L.requestVersion = Y.HttpVersion major minor}
11341740
11351741----------------------------------------------------------------------------
11361742-- Response interpretations
11371743
1138−-- | Make a request and ignore body of response.
1139−
1140−data IgnoreResponse = IgnoreResponse (L.Response ())
1744+-- | Make a request and ignore the body of the response.
1745+newtype IgnoreResponse = IgnoreResponse (L.Response ())
1746+ deriving (Show)
11411747
11421748instance HttpResponse IgnoreResponse where
11431749 type HttpResponseBody IgnoreResponse = ()
1144− toVanillaResponse (IgnoreResponse response) = response
1145− getHttpResponse request manager =
1146− IgnoreResponse <$> liftIO (L.httpNoBody request manager)
1750+ toVanillaResponse (IgnoreResponse r) = r
1751+ getHttpResponse r = return $ IgnoreResponse (void r)
11471752
11481753-- | Use this as the fourth argument of 'req' to specify that you want it to
1149−-- return the 'IgnoreResponse' interpretation.
1150−
1754+-- ignore the response body.
11511755ignoreResponse :: Proxy IgnoreResponse
11521756ignoreResponse = Proxy
11531757
1154−-- | Make a request and interpret body of response as JSON. The
1758+-- | Make a request and interpret the body of the response as JSON. The
11551759-- 'handleHttpException' method of 'MonadHttp' instance corresponding to
11561760-- monad in which you use 'req' will determine what to do in the case when
1157−-- parsing fails ('JsonHttpException' constructor will be used).
1158−
1761+-- parsing fails (the 'JsonHttpException' constructor will be used).
11591762newtype JsonResponse a = JsonResponse (L.Response a)
1763+ deriving (Show)
11601764
1161−instance FromJSON a => HttpResponse (JsonResponse a) where
1765+instance (FromJSON a) => HttpResponse (JsonResponse a) where
11621766 type HttpResponseBody (JsonResponse a) = a
1163− toVanillaResponse (JsonResponse response) = response
1164− getHttpResponse request manager = do
1165− response <- L.httpLbs request manager
1166− case A.eitherDecode (L.responseBody response) of
1767+ toVanillaResponse (JsonResponse r) = r
1768+ getHttpResponse r = do
1769+ chunks <- L.brConsume (L.responseBody r)
1770+ case A.eitherDecode (BL.fromChunks chunks) of
11671771 Left e -> throwIO (JsonHttpException e)
1168− Right x -> return $ JsonResponse response { L.responseBody = x }
1772+ Right x -> return $ JsonResponse (x <$ r)
1773+ acceptHeader Proxy = Just "application/json"
11691774
1170−-- | Use this as the forth argument of 'req' to specify that you want it to
1775+-- | Use this as the fourth argument of 'req' to specify that you want it to
11711776-- return the 'JsonResponse' interpretation.
1172−
11731777jsonResponse :: Proxy (JsonResponse a)
11741778jsonResponse = Proxy
11751779
1176−-- | Make a request and interpret body of response as a strict 'ByteString'.
1177−
1780+-- | Make a request and interpret the body of the response as a strict
1781+-- 'ByteString'.
11781782newtype BsResponse = BsResponse (L.Response ByteString)
1783+ deriving (Show)
11791784
11801785instance HttpResponse BsResponse where
11811786 type HttpResponseBody BsResponse = ByteString
1182− toVanillaResponse (BsResponse response) = response
1183− getHttpResponse request manager =
1184− L.withResponse request manager $ \response -> do
1185− chunks <- L.brConsume (L.responseBody response)
1186− return $ BsResponse response { L.responseBody = B.concat chunks }
1187−
1188−-- | Use this as the forth argument of 'req' to specify that you want to
1189−-- interpret response body as a strict 'ByteString'.
1787+ toVanillaResponse (BsResponse r) = r
1788+ getHttpResponse r = do
1789+ chunks <- L.brConsume (L.responseBody r)
1790+ return $ BsResponse (B.concat chunks <$ r)
11901791
1792+-- | Use this as the fourth argument of 'req' to specify that you want to
1793+-- interpret the response body as a strict 'ByteString'.
11911794bsResponse :: Proxy BsResponse
11921795bsResponse = Proxy
11931796
1194−-- | Make a request and interpret body of response as a lazy
1797+-- | Make a request and interpret the body of the response as a lazy
11951798-- 'BL.ByteString'.
1196−
11971799newtype LbsResponse = LbsResponse (L.Response BL.ByteString)
1800+ deriving (Show)
11981801
11991802instance HttpResponse LbsResponse where
12001803 type HttpResponseBody LbsResponse = BL.ByteString
1201− toVanillaResponse (LbsResponse response) = response
1202− getHttpResponse request manager =
1203− LbsResponse <$> L.httpLbs request manager
1204−
1205−-- | Use this as the forth argument of 'req' to specify that you want to
1206−-- interpret response body as a lazy 'BL.ByteString'.
1804+ toVanillaResponse (LbsResponse r) = r
1805+ getHttpResponse r = do
1806+ chunks <- L.brConsume (L.responseBody r)
1807+ return $ LbsResponse (BL.fromChunks chunks <$ r)
12071808
1809+-- | Use this as the fourth argument of 'req' to specify that you want to
1810+-- interpret the response body as a lazy 'BL.ByteString'.
12081811lbsResponse :: Proxy LbsResponse
12091812lbsResponse = Proxy
12101813
1211−-- | This interpretation does not result in any call at all, but you can use
1212−-- the 'responseRequest' function to extract 'L.Request' that 'req' has
1213−-- prepared. This is useful primarily for testing.
1214−--
1215−-- Note that when you use this interpretation inspecting response will
1216−-- diverge (i.e. it'll blow up with an error, don't do that).
1217−
1218−newtype ReturnRequest = ReturnRequest L.Request
1219−
1220−instance HttpResponse ReturnRequest where
1221− type HttpResponseBody ReturnRequest = ()
1222− toVanillaResponse (ReturnRequest _) = error
1223− "Network.HTTP.Req.ReturnRequest interpretation does not make requests"
1224− getHttpResponse request _ = return (ReturnRequest request)
1225−
1226−-- | Use this as the forth argument of 'req' to specify that you want it to
1227−-- just return the request it consturcted without making any requests.
1228−
1229−returnRequest :: Proxy ReturnRequest
1230−returnRequest = Proxy
1814+----------------------------------------------------------------------------
1815+-- Helpers for response interpretations
1816+
1817+-- | Fetch beginning of the response and return it together with a new
1818+-- @'L.Response' 'L.BodyReader'@ that can be passed to 'getHttpResponse' and
1819+-- such.
1820+grabPreview ::
1821+ -- | How many bytes to fetch
1822+ Int ->
1823+ -- | Response with body reader inside
1824+ L.Response L.BodyReader ->
1825+ -- | Preview 'ByteString' and new response with body reader inside
1826+ IO (ByteString, L.Response L.BodyReader)
1827+grabPreview nbytes r = do
1828+ let br = L.responseBody r
1829+ (target, leftover, done) <- brReadN br nbytes
1830+ nref <- newIORef (0 :: Int)
1831+ let br' = do
1832+ n <- readIORef nref
1833+ let incn = modifyIORef' nref (+ 1)
1834+ case n of
1835+ 0 -> do
1836+ incn
1837+ if B.null target
1838+ then br'
1839+ else return target
1840+ 1 -> do
1841+ incn
1842+ if B.null leftover
1843+ then br'
1844+ else return leftover
1845+ _ ->
1846+ if done
1847+ then return B.empty
1848+ else br
1849+ return (target, r {L.responseBody = br'})
1850+
1851+-- | Consume N bytes from 'L.BodyReader', return the target chunk, the
1852+-- leftover (may be empty), and whether we're done consuming the body.
1853+brReadN ::
1854+ -- | Body reader to stream from
1855+ L.BodyReader ->
1856+ -- | How many bytes to consume
1857+ Int ->
1858+ -- | Target chunk, the leftover, whether we're done
1859+ IO (ByteString, ByteString, Bool)
1860+brReadN br n = go 0 id id
1861+ where
1862+ go !tlen t l = do
1863+ chunk <- br
1864+ if B.null chunk
1865+ then return (r t, r l, True)
1866+ else do
1867+ let (target, leftover) = B.splitAt (n - tlen) chunk
1868+ tlen' = B.length target
1869+ t' = t . (target :)
1870+ l' = l . (leftover :)
1871+ if tlen + tlen' < n
1872+ then go (tlen + tlen') t' l'
1873+ else return (r t', r l', False)
1874+ r f = B.concat (f [])
12311875
12321876----------------------------------------------------------------------------
12331877-- Inspecting a response
12341878
1235−-- | Get response body.
1236−
1237−responseBody
1238− :: HttpResponse response
1239− => response
1240− -> HttpResponseBody response
1879+-- | Get the response body.
1880+responseBody ::
1881+ (HttpResponse response) =>
1882+ response ->
1883+ HttpResponseBody response
12411884responseBody = L.responseBody . toVanillaResponse
12421885
1243−-- | Get response status code.
1244−
1245−responseStatusCode
1246− :: HttpResponse response
1247− => response
1248− -> Int
1886+-- | Get the response status code.
1887+responseStatusCode ::
1888+ (HttpResponse response) =>
1889+ response ->
1890+ Int
12491891responseStatusCode =
12501892 Y.statusCode . L.responseStatus . toVanillaResponse
12511893
1252−-- | Get response status message.
1253−
1254−responseStatusMessage
1255− :: HttpResponse response
1256− => response
1257− -> ByteString
1894+-- | Get the response status message.
1895+responseStatusMessage ::
1896+ (HttpResponse response) =>
1897+ response ->
1898+ ByteString
12581899responseStatusMessage =
12591900 Y.statusMessage . L.responseStatus . toVanillaResponse
12601901
1261−-- | Look a particular header from a response.
1262−
1263−responseHeader
1264− :: HttpResponse response
1265− => response -- ^ Response interpretation
1266− -> ByteString -- ^ Header to lookup
1267− -> Maybe ByteString -- ^ Header value if found
1902+-- | Lookup a particular header from a response.
1903+responseHeader ::
1904+ (HttpResponse response) =>
1905+ -- | Response interpretation
1906+ response ->
1907+ -- | Header to lookup
1908+ ByteString ->
1909+ -- | Header value if found
1910+ Maybe ByteString
12681911responseHeader r h =
12691912 (lookup (CI.mk h) . L.responseHeaders . toVanillaResponse) r
12701913
1271−-- | Get response 'L.CookieJar'.
1272−
1273−responseCookieJar
1274− :: HttpResponse response
1275− => response
1276− -> L.CookieJar
1914+-- | Get the response 'L.CookieJar'.
1915+responseCookieJar ::
1916+ (HttpResponse response) =>
1917+ response ->
1918+ L.CookieJar
12771919responseCookieJar = L.responseCookieJar . toVanillaResponse
12781920
1279−-- | Get the original request from 'ReturnRequest' response interpretation.
1280−
1281−responseRequest :: ReturnRequest -> L.Request
1282−responseRequest (ReturnRequest request) = request
1283−
12841921----------------------------------------------------------------------------
1285−-- Response — defining your own interpretation
1922+-- Response—Defining your own interpretation
12861923
12871924-- $new-response-interpretation
12881925--
12891926-- To create a new response interpretation you just need to make your data
1290−-- type an instance of 'HttpResponse' type class.
1291−
1292−-- | A type class for response interpretations. It allows to fully control
1293−-- how request is made and how its body is parsed.
1927+-- type an instance of the 'HttpResponse' type class.
12941928
1929+-- | A type class for response interpretations. It allows us to describe how
1930+-- to consume the response from a @'L.Response' 'L.BodyReader'@ and produce
1931+-- the final result that is to be returned to the user.
12951932class HttpResponse response where
1296−
1297− -- | The associated type is type of body that can be extracted from a
1933+ -- | The associated type is the type of body that can be extracted from an
12981934 -- instance of 'HttpResponse'.
1935+ type HttpResponseBody response :: Type
12991936
1300− type HttpResponseBody response :: *
1301−
1302− -- | The method describes how to get underlying 'L.Response' record.
1303−
1937+ -- | The method describes how to get the underlying 'L.Response' record.
13041938 toVanillaResponse :: response -> L.Response (HttpResponseBody response)
13051939
1306− -- | This method describes how to make an HTTP request given 'L.Request'
1307− -- (prepared by the rest of the library) and 'L.Manager'.
1308−
1309− getHttpResponse :: L.Request -> L.Manager -> IO response
1940+ -- | This method describes how to consume response body and, more
1941+ -- generally, obtain @response@ value from @'L.Response' 'L.BodyReader'@.
1942+ --
1943+ -- __Note__: 'L.BodyReader' is nothing but @'IO' 'ByteString'@. You should
1944+ -- call this action repeatedly until it yields the empty 'ByteString'. In
1945+ -- that case streaming of response is finished (which apparently leads to
1946+ -- closing of the connection, so don't call the reader after it has
1947+ -- returned the empty 'ByteString' once) and you can concatenate the
1948+ -- chunks to obtain the final result. (Of course you could as well stream
1949+ -- the contents to a file or do whatever you want.)
1950+ --
1951+ -- __Note__: signature of this function was changed in the version
1952+ -- /1.0.0/.
1953+ getHttpResponse ::
1954+ -- | Response with body reader inside
1955+ L.Response L.BodyReader ->
1956+ -- | The final result
1957+ IO response
1958+
1959+ -- | The value of @\"Accept\"@ header. This is useful, for example, if a
1960+ -- website supports both @XML@ and @JSON@ responses, and decides what to
1961+ -- reply with based on what @Accept@ headers you have sent.
1962+ --
1963+ -- __Note__: manually specified 'Options' that set the @\"Accept\"@ header
1964+ -- will take precedence.
1965+ --
1966+ -- @since 2.1.0
1967+ acceptHeader :: Proxy response -> Maybe ByteString
1968+ acceptHeader Proxy = Nothing
1969+
1970+-- | This instance has been added to make it easier to inspect 'L.Response'
1971+-- using Req's functions like 'responseStatusCode', 'responseStatusMessage',
1972+-- etc.
1973+--
1974+-- @since 3.12.0
1975+instance HttpResponse (L.Response ()) where
1976+ type HttpResponseBody (L.Response ()) = ()
1977+ toVanillaResponse = id
1978+ getHttpResponse = return . void
13101979
13111980----------------------------------------------------------------------------
13121981-- Other
13131982
13141983-- | The main class for things that are “parts” of 'L.Request' in the sense
13151984-- that if we have a 'L.Request', then we know how to apply an instance of
1316−-- 'RequestComponent' changing\/overwriting something in it. 'Endo' is
1317−-- endomorphism of functions under composition, it's used to chain different
1318−-- request component easier using @('<>')@.
1319−
1985+-- 'RequestComponent' changing\/overwriting something in it. 'Endo' is a
1986+-- monoid of endomorphisms under composition, it's used to chain different
1987+-- request components easier using @('<>')@.
1988+--
1989+-- __Note__: this type class is not a part of the public API.
13201990class RequestComponent a where
1321−
13221991 -- | Get a function that takes a 'L.Request' and changes it somehow
1323− -- returning another 'L.Request'. For example HTTP method instance of
1324− -- 'RequestComponent' just overwrites method. The function is wrapped in
1325− -- 'Endo' so it's easier to chain such “modifying applications” together
1326− -- building bigger and bigger 'RequestComponent's.
1327−
1992+ -- returning another 'L.Request'. For example, the 'HttpMethod' instance
1993+ -- of 'RequestComponent' just overwrites method. The function is wrapped
1994+ -- in 'Endo' so it's easier to chain such “modifying applications”
1995+ -- together building bigger and bigger 'RequestComponent's.
13281996 getRequestMod :: a -> Endo L.Request
13291997
1330−-- | This wrapper is only used to attach a type-level tag to given type.
1998+-- | This wrapper is only used to attach a type-level tag to a given type.
13311999-- This is necessary to define instances of 'RequestComponent' for any thing
1332−-- that implements 'HttpMethod' or 'HttpBody'. Without the tag, GHC is not
1333−-- able to see difference between @'HttpMethod' method => 'RequestComponent'
2000+-- that implements 'HttpMethod' or 'HttpBody'. Without the tag, GHC can't
2001+-- see the difference between @'HttpMethod' method => 'RequestComponent'
13342002-- method@ and @'HttpBody' body => 'RequestComponent' body@ when it decides
1335−-- which instance to use (i.e. constraints are taken into account later,
2003+-- which instance to use (i.e. the constraints are taken into account later,
13362004-- when instance is already chosen).
1337−
1338−newtype Womb (tag :: Symbol) a = Womb a
2005+newtype Tagged (tag :: Symbol) a = Tagged a
13392006
13402007-- | Exceptions that this library throws.
1341−
13422008data HttpException
1343− = VanillaHttpException L.HttpException
1344− -- ^ A wrapper with an 'L.HttpException' from "Network.HTTP.Client"
1345− | JsonHttpException String
1346− -- ^ A wrapper with Aeson-produced 'String' describing why decoding failed
1347− deriving (Show, Typeable, Generic)
2009+ = -- | A wrapper with an 'L.HttpException' from "Network.HTTP.Client"
2010+ VanillaHttpException L.HttpException
2011+ | -- | A wrapper with Aeson-produced 'String' describing why decoding
2012+ -- failed
2013+ JsonHttpException String
2014+ deriving (Show, Generic)
13482015
13492016instance Exception HttpException
13502017
1351−-- | A simple 'Bool'-like type we only have for better error messages. We
1352−-- use it as a kind and its data constructors as type-level tags.
2018+-- | Return 'Just' if the given 'HttpException' is wrapping a http-client's
2019+-- 'L.StatusCodeException'. Otherwise, return 'Nothing'.
13532020--
1354−-- See also: 'HttpMethod' and 'HttpBody'.
2021+-- @since 3.12.0
2022+isStatusCodeException :: HttpException -> Maybe (L.Response ())
2023+isStatusCodeException
2024+ ( VanillaHttpException
2025+ ( L.HttpExceptionRequest
2026+ _
2027+ (L.StatusCodeException r _)
2028+ )
2029+ ) = Just r
2030+isStatusCodeException _ = Nothing
13552031
2032+-- | A simple type isomorphic to 'Bool' that we only have for better error
2033+-- messages. We use it as a kind and its data constructors as type-level
2034+-- tags.
2035+--
2036+-- See also: 'HttpMethod' and 'HttpBody'.
13562037data CanHaveBody
1357− = CanHaveBody -- ^ Indeed can have a body
1358− | NoBody -- ^ Should not have a body
2038+ = -- | Indeed can have a body
2039+ CanHaveBody
2040+ | -- | Should not have a body
2041+ NoBody
13592042
1360−-- | A type-level tag that specifies URL scheme used (and thus if TLS is
2043+-- | A type-level tag that specifies URL scheme used (and thus if HTTPS is
13612044-- enabled). This is used to force TLS requirement for some authentication
13622045-- 'Option's.
1363−
13642046data Scheme
1365− = Http -- ^ HTTP, no TLS
1366− | Https -- ^ HTTPS
1367− deriving (Eq, Ord, Show, Data, Typeable, Generic)
2047+ = -- | HTTP
2048+ Http
2049+ | -- | HTTPS
2050+ Https
2051+ deriving (Eq, Ord, Show, Data, Generic, TH.Lift)