@@ -46,6 +46,7 @@ module GitHub.Request (
4646 -- * Request execution in IO
4747 executeRequest ,
4848 executeRequestWithMgr ,
49+ executeRequestWithMgrAndRes ,
4950 executeRequest' ,
5051 executeRequestWithMgr' ,
5152 executeRequestMaybe ,
@@ -66,7 +67,7 @@ module GitHub.Request (
6667 -- | This always exist, independently of @openssl@ configuration flag.
6768 -- They change accordingly, to make use of the library simpler.
6869 withOpenSSL ,
69- tlsManagerSettings ,
70+ tlsManagerSettings ,
7071 ) where
7172
7273import GitHub.Internal.Prelude
@@ -112,7 +113,7 @@ import qualified OpenSSL.Session as SSL
112113import qualified OpenSSL.X509.SystemStore as SSL
113114#endif
114115
115- import GitHub.Auth (Auth , AuthMethod , endpoint , setAuthRequest )
116+ import GitHub.Auth (AuthMethod , endpoint , setAuthRequest )
116117import GitHub.Data (Error (.. ))
117118import GitHub.Data.PullRequests (MergeResult (.. ))
118119import GitHub.Data.Request
@@ -206,33 +207,46 @@ lessFetchCount :: Int -> FetchCount -> Bool
206207lessFetchCount _ FetchAll = True
207208lessFetchCount i (FetchAtLeast j) = i < fromIntegral j
208209
210+
209211-- | Like 'executeRequest' but with provided 'Manager'.
210212executeRequestWithMgr
211213 :: (AuthMethod am , ParseResponse mt a )
212214 => Manager
213215 -> am
214216 -> GenRequest mt rw a
215217 -> IO (Either Error a )
216- executeRequestWithMgr mgr auth req = runExceptT $ do
218+ executeRequestWithMgr mgr auth req =
219+ fmap (fmap responseBody) (executeRequestWithMgrAndRes mgr auth req)
220+
221+ -- | Execute request and return the last received 'HTTP.Response'.
222+ --
223+ -- @since 0.24
224+ executeRequestWithMgrAndRes
225+ :: (AuthMethod am , ParseResponse mt a )
226+ => Manager
227+ -> am
228+ -> GenRequest mt rw a
229+ -> IO (Either Error (HTTP. Response a ))
230+ executeRequestWithMgrAndRes mgr auth req = runExceptT $ do
217231 httpReq <- makeHttpRequest (Just auth) req
218232 performHttpReq httpReq req
219233 where
220- httpLbs' :: HTTP. Request -> ExceptT Error IO (Response LBS. ByteString )
234+ httpLbs' :: HTTP. Request -> ExceptT Error IO (HTTP. Response LBS. ByteString )
221235 httpLbs' req' = lift (httpLbs req' mgr) `catch` onHttpException
222236
223- performHttpReq :: forall rw mt b . ParseResponse mt b => HTTP. Request -> GenRequest mt rw b -> ExceptT Error IO b
237+ performHttpReq :: forall rw mt b . ParseResponse mt b => HTTP. Request -> GenRequest mt rw b -> ExceptT Error IO ( HTTP. Response b )
224238 performHttpReq httpReq Query {} = do
225239 res <- httpLbs' httpReq
226- unTagged (parseResponse httpReq res :: Tagged mt (ExceptT Error IO b ))
240+ ( <$ res) <$> unTagged (parseResponse httpReq res :: Tagged mt (ExceptT Error IO b ))
227241
228242 performHttpReq httpReq (PagedQuery _ _ l) =
229- unTagged (performPagedRequest httpLbs' predicate httpReq :: Tagged mt (ExceptT Error IO b ))
243+ unTagged (performPagedRequest httpLbs' predicate httpReq :: Tagged mt (ExceptT Error IO ( HTTP. Response b ) ))
230244 where
231245 predicate v = lessFetchCount (V. length v) l
232246
233247 performHttpReq httpReq (Command _ _ _) = do
234248 res <- httpLbs' httpReq
235- unTagged (parseResponse httpReq res :: Tagged mt (ExceptT Error IO b ))
249+ ( <$ res) <$> unTagged (parseResponse httpReq res :: Tagged mt (ExceptT Error IO b ))
236250
237251-- | Like 'executeRequest' but without authentication.
238252executeRequest' :: ParseResponse mt a => GenRequest mt 'RO a -> IO (Either Error a )
@@ -246,21 +260,7 @@ executeRequestWithMgr'
246260 => Manager
247261 -> GenRequest mt 'RO a
248262 -> IO (Either Error a )
249- executeRequestWithMgr' mgr req = runExceptT $ do
250- httpReq <- makeHttpRequest (Nothing :: Maybe Auth ) req
251- performHttpReq httpReq req
252- where
253- httpLbs' :: HTTP. Request -> ExceptT Error IO (Response LBS. ByteString )
254- httpLbs' req' = lift (httpLbs req' mgr) `catch` onHttpException
255-
256- performHttpReq :: forall mt b . ParseResponse mt b => HTTP. Request -> GenRequest mt 'RO b -> ExceptT Error IO b
257- performHttpReq httpReq Query {} = do
258- res <- httpLbs' httpReq
259- unTagged (parseResponse httpReq res :: Tagged mt (ExceptT Error IO b ))
260- performHttpReq httpReq (PagedQuery _ _ l) =
261- unTagged (performPagedRequest httpLbs' predicate httpReq :: Tagged mt (ExceptT Error IO b ))
262- where
263- predicate v = lessFetchCount (V. length v) l
263+ executeRequestWithMgr' mgr = executeRequestWithMgr mgr ()
264264
265265-- | Helper for picking between 'executeRequest' and 'executeRequest''.
266266--
@@ -302,9 +302,9 @@ class Accept mt => ParseResponse (mt :: MediaType *) a where
302302-- | Parse API response.
303303--
304304-- @
305- -- parseResponse :: 'FromJSON' a => 'Response' 'LBS.ByteString' -> 'Either' 'Error' a
305+ -- parseResponse :: 'FromJSON' a => 'HTTP. Response' 'LBS.ByteString' -> 'Either' 'Error' a
306306-- @
307- parseResponseJSON :: (FromJSON a , MonadError Error m ) => Response LBS. ByteString -> m a
307+ parseResponseJSON :: (FromJSON a , MonadError Error m ) => HTTP. Response LBS. ByteString -> m a
308308parseResponseJSON res = case eitherDecode (responseBody res) of
309309 Right x -> return x
310310 Left err -> throwError . ParseError . T. pack $ err
@@ -349,9 +349,9 @@ instance b ~ URI => ParseResponse 'MtRedirect b where
349349-- | Helper for handling of 'RequestRedirect'.
350350--
351351-- @
352- -- parseRedirect :: 'Response' 'LBS.ByteString' -> 'Either' 'Error' a
352+ -- parseRedirect :: 'HTTP. Response' 'LBS.ByteString' -> 'Either' 'Error' a
353353-- @
354- parseRedirect :: MonadError Error m => URI -> Response LBS. ByteString -> m URI
354+ parseRedirect :: MonadError Error m => URI -> HTTP. Response LBS. ByteString -> m URI
355355parseRedirect originalUri rsp = do
356356 let status = responseStatus rsp
357357 when (statusCode status /= 302 ) $
@@ -501,7 +501,7 @@ makeHttpRequest auth r = case r of
501501 setBody body req = req { requestBody = RequestBodyLBS body }
502502
503503-- | Query @Link@ header with @rel=next@ from the request headers.
504- getNextUrl :: Response a -> Maybe URI
504+ getNextUrl :: HTTP. Response a -> Maybe URI
505505getNextUrl req = do
506506 linkHeader <- lookup " Link" (responseHeaders req)
507507 links <- parseLinkHeaderBS linkHeader
@@ -516,33 +516,35 @@ getNextUrl req = do
516516
517517-- | Helper for making paginated requests. Responses, @a@ are combined monoidally.
518518--
519+ -- The result is wrapped in the last received 'HTTP.Response'.
520+ --
519521-- @
520522-- performPagedRequest :: ('FromJSON' a, 'Semigroup' a)
521- -- => ('HTTP.Request' -> 'ExceptT' 'Error' 'IO' ('Response' 'LBS.ByteString'))
523+ -- => ('HTTP.Request' -> 'ExceptT' 'Error' 'IO' ('HTTP. Response' 'LBS.ByteString'))
522524-- -> (a -> 'Bool')
523525-- -> 'HTTP.Request'
524- -- -> 'ExceptT' 'Error' 'IO' a
526+ -- -> 'ExceptT' 'Error' 'IO' ('HTTP.Response' a)
525527-- @
526528performPagedRequest
527529 :: forall a m mt . (ParseResponse mt a , Semigroup a , MonadCatch m , MonadError Error m )
528- => (HTTP. Request -> m (Response LBS. ByteString )) -- ^ `httpLbs` analogue
529- -> (a -> Bool ) -- ^ predicate to continue iteration
530- -> HTTP. Request -- ^ initial request
531- -> Tagged mt (m a )
530+ => (HTTP. Request -> m (HTTP. Response LBS. ByteString )) -- ^ `httpLbs` analogue
531+ -> (a -> Bool ) -- ^ predicate to continue iteration
532+ -> HTTP. Request -- ^ initial request
533+ -> Tagged mt (m ( HTTP. Response a ) )
532534performPagedRequest httpLbs' predicate initReq = Tagged $ do
533535 res <- httpLbs' initReq
534536 m <- unTagged (parseResponse initReq res :: Tagged mt (m a ))
535537 go m res initReq
536538 where
537- go :: a -> Response LBS. ByteString -> HTTP. Request -> m a
539+ go :: a -> HTTP. Response LBS. ByteString -> HTTP. Request -> m ( HTTP. Response a )
538540 go acc res req =
539541 case (predicate acc, getNextUrl res) of
540542 (True , Just uri) -> do
541543 req' <- HTTP. setUri req uri
542544 res' <- httpLbs' req'
543545 m <- unTagged (parseResponse req' res' :: Tagged mt (m a ))
544546 go (acc <> m) res' req'
545- (_, _) -> return acc
547+ (_, _) -> return ( acc <$ res)
546548
547549-------------------------------------------------------------------------------
548550-- Internal
0 commit comments