http-dispatch 0.3.0.0 → 0.4.0.0
raw patch · 5 files changed
+68/−31 lines, 5 files
Files
- http-dispatch.cabal +3/−2
- src/Network/HTTP/Dispatch/Core.hs +33/−26
- src/Network/HTTP/Dispatch/Extra.hs +18/−0
- src/Network/HTTP/Dispatch/Request.hs +4/−2
- src/Network/HTTP/Dispatch/Types.hs +10/−1
http-dispatch.cabal view
@@ -1,5 +1,5 @@ name: http-dispatch-version: 0.3.0.0+version: 0.4.0.0 synopsis: High level HTTP client for Haskell description: Please see README.md homepage: http://github.com/owainlewis/http-dispatch#readme@@ -17,12 +17,12 @@ exposed-modules: Network.HTTP.Dispatch.Core , Network.HTTP.Dispatch.Types , Network.HTTP.Dispatch.Request+ , Network.HTTP.Dispatch.Extra build-depends: base >= 4.7 && < 5 , http-client , http-client-tls , http-types , bytestring- , aeson , case-insensitive default-language: Haskell2010 @@ -33,6 +33,7 @@ build-depends: base , http-dispatch , hspec+ , aeson ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010
src/Network/HTTP/Dispatch/Core.hs view
@@ -5,48 +5,55 @@ , HTTPRequestMethod(..) , runRequest , get+ , getWithHeaders , post+ , postWithHeaders+ , put+ , putWithHeaders , patch+ , patchWithHeaders , delete- , put- , simpleGet- , postString- , postAeson+ , deleteWithHeaders ) where -import qualified Data.Aeson as Aeson (ToJSON, encode) import qualified Data.ByteString.Lazy as LBS-import qualified Data.ByteString.Lazy.Char8 as LBSC import Network.HTTP.Dispatch.Request import Network.HTTP.Dispatch.Types +-----------------------------------------------------------------------------------+-- Request API+-----------------------------------------------------------------------------------+ type Url = String+type Headers = [Header]+type Body = LBS.ByteString --- Request API+get :: Url -> HTTPRequest+get url = HTTPRequest GET url [] Nothing -get :: Url -> [Header] -> HTTPRequest-get url headers = HTTPRequest GET url headers Nothing+getWithHeaders :: String -> [Header] -> HTTPRequest+getWithHeaders url headers = HTTPRequest GET url headers Nothing -simpleGet :: Url -> HTTPRequest-simpleGet url = HTTPRequest GET url [] Nothing+post :: Url -> Body -> HTTPRequest+post url body = postWithHeaders url [] body --- Post request with a lazy bytestring payload-post :: Url -> [Header] -> LBS.ByteString -> HTTPRequest-post url headers body = HTTPRequest POST url headers (pure body)+postWithHeaders :: Url -> Headers -> Body -> HTTPRequest+postWithHeaders url headers body = HTTPRequest POST url headers (Just body) --- Post request with a string payload-postString :: String -> [Header] -> String -> HTTPRequest-postString url headers body = HTTPRequest POST url headers (pure . LBSC.pack $ body)+put :: Url -> Body -> HTTPRequest+put url body = putWithHeaders url [] body --- Post request where the payload is some type that has a ToJSON instance defined-postAeson :: Aeson.ToJSON a => Url -> [Header] -> a -> HTTPRequest-postAeson url headers body = HTTPRequest POST url headers (pure $ Aeson.encode body)+putWithHeaders :: Url -> Headers -> Body -> HTTPRequest+putWithHeaders url headers body = HTTPRequest PUT url headers (Just body) -put :: String -> [Header] -> LBS.ByteString -> HTTPRequest-put url headers body = HTTPRequest PUT url headers (pure body)+patch :: Url -> Body -> HTTPRequest+patch url body = patchWithHeaders url [] body -delete :: String -> [Header] -> Maybe LBS.ByteString -> HTTPRequest-delete url headers = HTTPRequest DELETE url headers+patchWithHeaders :: Url -> Headers -> Body -> HTTPRequest+patchWithHeaders url headers body = HTTPRequest PATCH url headers (Just body) -patch :: String -> [Header] -> LBS.ByteString -> HTTPRequest-patch url headers body = HTTPRequest PATCH url headers (pure body)+delete :: Url -> HTTPRequest+delete url = deleteWithHeaders url []++deleteWithHeaders :: Url -> Headers -> HTTPRequest+deleteWithHeaders url headers = HTTPRequest DELETE url headers Nothing
+ src/Network/HTTP/Dispatch/Extra.hs view
@@ -0,0 +1,18 @@+{-# LANGUAGE FlexibleInstances #-}+module Network.HTTP.Dispatch.Extra+ ( fromString+ ) where++import qualified Data.ByteString.Lazy as LBS+import qualified Data.ByteString.Lazy.Char8 as LBSC++-----------------------------------------------------------------------------------+-- Extra methods for friendly API (experimental)+-----------------------------------------------------------------------------------++-- Can be used to generate a HTTP request without needing to prepare Lazy ByteStrings.+--+-- Example:+-- HTTPRequest POST "http://api.mysite.com" [("Content-Type", "application/json")] (fromString "HELLO WORLD")+fromString :: String -> LBS.ByteString+fromString = LBSC.pack
src/Network/HTTP/Dispatch/Request.hs view
@@ -32,8 +32,10 @@ , checkStatus = \_ _ _ -> Nothing } case body of- Just lbs -> return $ req { requestBody = RequestBodyLBS lbs }- Nothing -> return $ req+ Just lbs -> + return $ req { requestBody = RequestBodyLBS lbs }+ Nothing -> + return req getManagerForUrl :: String -> IO Manager getManagerForUrl url =
src/Network/HTTP/Dispatch/Types.hs view
@@ -1,6 +1,9 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeSynonymInstances #-} module Network.HTTP.Dispatch.Types where -import qualified Data.ByteString.Lazy as LBS+import qualified Data.ByteString.Lazy as LBS+import qualified Data.ByteString.Lazy.Char8 as LBSC data HTTPRequestMethod = GET@@ -10,6 +13,12 @@ | DELETE deriving ( Eq, Show ) type Header = (String, String)++class Packable a where+ pack :: a -> LBS.ByteString++instance Packable String where+ pack = LBSC.pack data HTTPRequest = HTTPRequest { -- A HTTP request method e.g GET POST etc