apiary-http-client 0.1.0.0 → 0.1.1.0
raw patch · 3 files changed
+47/−22 lines, 3 filesdep ~apiaryPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: apiary
API changes (from Hackage documentation)
Files
- README.md +11/−5
- Web/Apiary/HTTP/Client.hs +25/−16
- apiary-http-client.cabal +11/−1
README.md view
@@ -1,11 +1,20 @@ Apiary HTTP Client ================== +[](http://hackage.haskell.org/package/apiary-http-client)+ A HTTP Client for [Apiary](http://hackage.haskell.org/package/apiary), using `Apiary`'s extension api, suitable for proxying HTTP request to backend API, with flexible APIs and streamming proxying abilities. -This module also reexport `Network.HTTP.Client`. Example:+This module also reexport `Network.HTTP.Client`. +Example+-------+ ```haskell+import Web.Apiary+import Network.Wai.Handler.Warp+import Web.Apiary.HTTP.Client as HTTP+ main :: IO () main = runApiaryWith (run serverPort) (HTTP.initHTTPClient HTTP.defaultManagerSettings) def $ do @@ -18,11 +27,8 @@ -- set proxying host and port -- use function from Network.HTTP.Client to modify more- let req' = HTTP.setHost influxDB_API_HOST . HTTP.setPort 80 $ req+ let req' = HTTP.setHost "api.backend.com" . HTTP.setPort 80 $ req -- send request and proxy respond in streamming fashion. HTTP.proxyTo req' ```---More document are W.I.P.
Web/Apiary/HTTP/Client.hs view
@@ -1,4 +1,3 @@-{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeOperators #-}@@ -7,25 +6,28 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE DataKinds #-} +{-|+ A http client for Apiary's 'ActionT' monad stack.+-} module Web.Apiary.HTTP.Client ( HTTPClient , initHTTPClient , getManager , withHTTPClient- -- ** helpers to make new Request+ -- ** Helpers to make new Request , fromWaiRequest , fromRequest , resetHeaders , setPort- , setHost , setHostName , setHostHeader- -- ** send request and get respond+ , setHost+ -- ** Send request and get respond , sendRequset , openRequset- -- ** send request for side effect+ -- ** Send request for side effect , sendRequsetNoBody- -- ** send request and proxy respond+ -- ** Send request and proxy respond , proxyTo , proxyWith , module Network.HTTP.Client@@ -50,12 +52,12 @@ instance Extension HTTPClient --- |Initialize a @MonadExts@ with @Network.HTTP.Client.ManagerSettings@.+-- |Initialize a 'MonadExts' with 'ManagerSettings'. initHTTPClient :: MonadIO m => ManagerSettings -> Initializer m exts (HTTPClient ': exts) initHTTPClient ms = initializer' . liftIO $ newManager ms >>= return . HTTPClient --- |Get @Network.HTTP.Client.Manage@ from Apiary's @MonadExts@ context.+-- |Get 'Manager' from Apiary's 'MonadExts' context. getManager :: (Has HTTPClient es, MonadExts es m, MonadIO m) => m Manager getManager = do (HTTPClient manager) <- getExt (P.Proxy :: P.Proxy HTTPClient)@@ -68,11 +70,11 @@ manager <- getManager liftIO $ f r manager --- |Copy path, headers, body and queryString from @Network.Wai.Request@+-- |Copy path, headers, method, body and queryString from @"Network.Wai.Request"@ fromWaiRequest :: ([T.Text] -> [T.Text]) -- ^ Function to modify request path -> ([Header] -> [Header]) -- ^ Function to modify request headers- -> W.Request -> Request -- ^ From @Network.Wai.Request@ To @Network.HTTP.Client.Request@+ -> W.Request -> Request -- ^ From @"Network.Wai.Request"@ To @"Network.HTTP.Client.Request"@ fromWaiRequest pm hm req = let needsPopper = \ r -> r (W.requestBody req)@@ -85,6 +87,7 @@ def { queryString = W.rawQueryString req , path = T.encodeUtf8 (T.intercalate "/" path)+ , method = W.requestMethod req , requestHeaders = headers , requestBody = requestBody' }@@ -95,7 +98,7 @@ resetHeaders :: [Header] -> [Header] resetHeaders = filter (\ (name, _) -> name `notElem` [hTransferEncoding, hContentLength, hContentEncoding, hAcceptEncoding]) --- |Copy path, headers, body and queryString from current @ActionT@'s context.+-- |Copy path, headers, method, body and queryString from current 'ActionT's context. fromRequest :: (Has HTTPClient exts, MonadIO m) => ([T.Text] -> [T.Text]) -- ^ Function to modify request path@@ -103,38 +106,43 @@ -> A.ActionT exts prms m Request fromRequest pm hm= A.getRequest >>= return . fromWaiRequest pm hm +-- | > setPort port req = req{port = port} setPort :: Int -> Request -> Request setPort port req = req{ port = port } +-- | > setHostName host req = req{host = host} setHostName :: B.ByteString -> Request -> Request setHostName host req = req{ host = host } +-- |Set HOST header. setHostHeader :: B.ByteString -> Request -> Request setHostHeader host req = req{ requestHeaders = headers } where oHeaders = requestHeaders req headers = (hHost, host) : filter (\ (name, _) -> name /= hHost ) oHeaders +-- |Set both HOST header and request's host. setHost :: B.ByteString -> Request -> Request setHost host = setHostHeader host . setHostName host --- |send requset and get @Response@ @ByteString@--- For large response consider using @openRequset@ and @responseClose@ instead.+-- |Send requset and get 'Response' 'BL.ByteString'+-- For large response consider using 'openRequset' and 'responseClose' instead. sendRequset :: (Has HTTPClient exts, MonadIO m) => Request -> A.ActionT exts prms m (Response LB.ByteString) sendRequset req = withHTTPClient httpLbs req --- |send request without receive any body.+-- |Send request without receive any body. sendRequsetNoBody :: (Has HTTPClient exts, MonadIO m) => Request -> A.ActionT exts prms m (Response ()) sendRequsetNoBody req = withHTTPClient httpNoBody req --- |send request and get @Response@ @BodyReader@+-- |Send request and get 'Response' 'BodyReader' openRequset :: (Has HTTPClient exts, MonadIO m) => Request -> A.ActionT exts prms m (Response BodyReader) openRequset req = withHTTPClient responseOpen req --- |streamming response directly from proxy target.+-- |Streamming response directly from proxy target.+-- Reset headers automatically using 'resetHeaders'. proxyTo :: (Has HTTPClient exts, MonadIO m) => Request -> A.ActionT exts prms m () proxyTo req = do@@ -153,6 +161,7 @@ in loop -- |Modify response from proxy target then send.+-- This method caches response body in memory so be careful. -- You should consider remove following headers: -- Transfer-Encoding, Content-Length, Content-Encoding and Accept-Encoding. proxyWith
apiary-http-client.cabal view
@@ -2,8 +2,13 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: apiary-http-client-version: 0.1.0.0+version: 0.1.1.0 synopsis: A http client for Apiary.+description: + A HTTP Client for Apiary, using Apiary's extension api, suitable for proxying HTTP request to backend API, with flexible APIs and streamming proxying abilities.+ .+ This module also reexport Network.HTTP.Client.+ -- synopsis: -- description: homepage: https://github.com/winterland1989/apiary-http-client@@ -32,3 +37,8 @@ , text -- hs-source-dirs: default-language: Haskell2010+++source-repository head+ type: git+ location: https://github.com/winterland1989/apiary-http-client.git