http-dispatch 0.5.0.0 → 0.5.0.1
raw patch · 5 files changed
+68/−10 lines, 5 files
Files
- http-dispatch.cabal +2/−1
- src/Network/HTTP/Dispatch/Core.hs +35/−7
- src/Network/HTTP/Dispatch/Headers.hs +30/−0
- src/Network/HTTP/Dispatch/Internal/Request.hs +1/−1
- src/Network/HTTP/Dispatch/Types.hs +0/−1
http-dispatch.cabal view
@@ -1,5 +1,5 @@ name: http-dispatch-version: 0.5.0.0+version: 0.5.0.1 synopsis: High level HTTP client for Haskell description: Please see README.md homepage: http://github.com/owainlewis/http-dispatch#readme@@ -16,6 +16,7 @@ hs-source-dirs: src exposed-modules: Network.HTTP.Dispatch.Core , Network.HTTP.Dispatch.Types+ , Network.HTTP.Dispatch.Headers , Network.HTTP.Dispatch.Internal.Request build-depends: base >= 4.5 && < 5 , http-client
src/Network/HTTP/Dispatch/Core.hs view
@@ -29,8 +29,13 @@ -- | Make a simple HTTP GET request -- -- @--- get "http://google.com" =>--- HTTPRequest {reqMethod = GET, reqUrl = "http://google.com", reqHeaders = [], reqBody = Nothing}+-- get "http://google.com"+--+-- HTTPRequest { reqMethod = GET+-- , reqUrl = "http://google.com"+-- , reqHeaders = []+-- , reqBody = Nothing+-- } -- @ get :: Url -> HTTPRequest get url = HTTPRequest GET url [] Nothing@@ -39,13 +44,19 @@ -- -- @ -- getWithHeaders "http://google.com" [header "Content-Type" "application/json"]--- HTTPRequest {reqMethod = GET, reqUrl = "http://google.com", reqHeaders = [("Content-Type","application/json")], reqBody = Nothing}+--+-- HTTPRequest { reqMethod = GET+-- , reqUrl = "http://google.com"+-- , reqHeaders = [("Content-Type","application/json")]+-- , reqBody = Nothing+-- } -- @ getWithHeaders :: String -> [Header] -> HTTPRequest getWithHeaders url headers = HTTPRequest GET url headers Nothing -- | Make a simple HTTP POST request --+-- post :: Url -> Body -> HTTPRequest post url body = postWithHeaders url [] body @@ -54,34 +65,51 @@ postWithHeaders :: Url -> Headers -> Body -> HTTPRequest postWithHeaders url headers body = HTTPRequest POST url headers (Just body) +-- | Make a HTTP PUT request+-- put :: Url -> Body -> HTTPRequest put url body = putWithHeaders url [] body +-- | Make a HTTP PUT request with headers+-- putWithHeaders :: Url -> Headers -> Body -> HTTPRequest putWithHeaders url headers body = HTTPRequest PUT url headers (Just body) +-- | Make a HTTP PATCH request+-- patch :: Url -> Body -> HTTPRequest patch url body = patchWithHeaders url [] body +-- | Make a HTTP PATCH request with headers+-- patchWithHeaders :: Url -> Headers -> Body -> HTTPRequest patchWithHeaders url headers body = HTTPRequest PATCH url headers (Just body) +-- | Make a HTTP DELETE request+-- delete :: Url -> HTTPRequest delete url = deleteWithHeaders url [] +-- | Make a HTTP DELETE request with headers+-- deleteWithHeaders :: Url -> Headers -> HTTPRequest deleteWithHeaders url headers = HTTPRequest DELETE url headers Nothing -- | Add query params to a request URL -- -- @--- withQueryParams (get "http://google.com") [("foo", "bar")] =>--- HTTPRequest {reqMethod = GET, reqUrl = "http://google.com?foo=bar", reqHeaders = [], reqBody = Nothing}+-- withQueryParams (get "http://google.com") [("foo", "bar")]+--+-- HTTPRequest { reqMethod = GET+-- , reqUrl = "http://google.com?foo=bar"+-- , reqHeaders = []+-- , reqBody = Nothing+-- } -- @ withQueryParams :: HTTPRequest -> [(String, String)] -> HTTPRequest withQueryParams req params = req { reqUrl = u ++ p } where u = reqUrl req p = compileParams params- compileParams params = "?" ++ kweryParams :: String+ compileParams params = "?" ++ qParams :: String where parts = map (\(k,v) -> mconcat [k, "=", v]) params- kweryParams = mconcat (intersperse "&" parts)+ qParams = mconcat (intersperse "&" parts)
+ src/Network/HTTP/Dispatch/Headers.hs view
@@ -0,0 +1,30 @@+{-# LANGUAGE OverloadedStrings #-}+module Network.HTTP.Dispatch.Headers+ ( contentType+ , contentJSON+ , contentXML+ , basicAuth+ ) where++import qualified Data.ByteString as S+import qualified Data.ByteString.Base64 as B64+import Data.Monoid ((<>))+import Network.HTTP.Dispatch.Types++-- | Create a new content type header+contentType :: String -> Header+contentType = header "Content-Type"++-- | Return a JSON content type header+contentJSON :: Header+contentJSON = contentType "application/json"++-- | Return an XML content type header+contentXML :: Header+contentXML = contentType "application/xml"++-- | Helper to generate Basic authentication+basicAuth :: S.ByteString -> S.ByteString -> Header+basicAuth user pass = ("Authorization", auth)+ where auth = "Basic: " <> userPassEncoded+ userPassEncoded = B64.encode $ user <> ":" <> pass
src/Network/HTTP/Dispatch/Internal/Request.hs view
@@ -69,4 +69,4 @@ runRequestWithSettings httpRequest settings = do manager <- newManager settings request <- toRequest httpRequest- httpLbs request manager >>= return . toResponse+ toResponse <$> httpLbs request manager
src/Network/HTTP/Dispatch/Types.hs view
@@ -42,7 +42,6 @@ , respBody :: LBS.ByteString } deriving ( Eq, Show ) --- | Helper function that contstructs HTTP headers from string values header :: String -> String -> Header header k v = (SC.pack k , SC.pack v)