http-dispatch (empty) → 0.1.0.0
raw patch · 7 files changed
+184/−0 lines, 7 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, case-insensitive, hspec, http-client, http-client-tls, http-dispatch, http-types
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- http-dispatch.cabal +41/−0
- src/Network/HTTP/Dispatch/Core.hs +51/−0
- src/Network/HTTP/Dispatch/Headers.hs +7/−0
- src/Network/HTTP/Dispatch/Types.hs +52/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Owain Lewis (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Owain Lewis nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ http-dispatch.cabal view
@@ -0,0 +1,41 @@+name: http-dispatch+version: 0.1.0.0+synopsis: High level HTTP client for Haskell+description: Please see README.md+homepage: http://github.com/owainlewis/http-dispatch#readme+license: BSD3+license-file: LICENSE+author: Owain Lewis+maintainer: owain@owainlewis.com+copyright: 2016 Owain Lewis+category: Web+build-type: Simple+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Network.HTTP.Dispatch.Core+ , Network.HTTP.Dispatch.Headers+ , Network.HTTP.Dispatch.Types+ build-depends: base >= 4.7 && < 5+ , http-client+ , http-client-tls+ , http-types+ , bytestring+ , aeson+ , case-insensitive+ default-language: Haskell2010++test-suite http-dispatch-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , http-dispatch+ , hspec+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/owainlewis/http-dispatch
+ src/Network/HTTP/Dispatch/Core.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE OverloadedStrings #-}+module Network.HTTP.Dispatch.Core+ ( HTTPRequest(..)+ , HTTPResponse(..)+ , runRequest+ , get+ , post+ , patch+ , delete+ , put+ , simpleGet+ , postString+ , postAeson+ ) 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++type Url = String++-- Request API++get :: Url -> [Header] -> HTTPRequest+get url headers = HTTPRequest GET url headers Nothing++simpleGet :: Url -> HTTPRequest+simpleGet url = HTTPRequest GET url [] Nothing++-- Post request with a lazy bytestring payload+post :: Url -> [Header] -> LBS.ByteString -> HTTPRequest+post url headers body = HTTPRequest POST url headers (pure body)++-- Post request with a string payload+postString :: String -> [Header] -> String -> HTTPRequest+postString url headers body = HTTPRequest POST url headers (pure . LBSC.pack $ 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)++put :: String -> [Header] -> LBS.ByteString -> HTTPRequest+put url headers body = HTTPRequest PUT url headers (pure body)++delete :: String -> [Header] -> Maybe LBS.ByteString -> HTTPRequest+delete url headers = HTTPRequest DELETE url headers++patch :: String -> [Header] -> LBS.ByteString -> HTTPRequest+patch url headers body = HTTPRequest PATCH url headers (pure body)
+ src/Network/HTTP/Dispatch/Headers.hs view
@@ -0,0 +1,7 @@+{-# LANGUAGE OverloadedStrings #-}+module Network.HTTP.Dispatch.Headers where++import Network.HTTP.Dispatch.Types (Header (..))++contentJSON :: Header+contentJSON = ("Content-Type", "application/json")
+ src/Network/HTTP/Dispatch/Types.hs view
@@ -0,0 +1,52 @@+module Network.HTTP.Dispatch.Types where++import qualified Data.ByteString.Lazy as LBS++data HTTPRequestMethod =+ GET+ | PUT+ | POST+ | PATCH+ | DELETE deriving ( Eq, Show )++type Header = (String, String)++data HTTPRequest = HTTPRequest {+ -- A HTTP request method e.g GET POST etc+ reqMethod :: HTTPRequestMethod+ -- A HTTP request URL+ , reqUrl :: String+ -- Optional HTTP headers+ , reqHeaders :: [Header]+ -- An optional request body+ , reqBody :: Maybe LBS.ByteString+} deriving ( Eq, Show )++data HTTPResponse = HTTPResponse {+ -- The response code+ respStatus :: Int+ -- The response headers+ , respHeaders :: [Header]+ -- The response body+ , respBody :: LBS.ByteString+} deriving ( Eq, Show )++-- Helper methods+---------------------------------------------------------------------------------------------++withHeader :: HTTPRequest -> Header -> HTTPRequest+withHeader req header = req { reqHeaders = header : (reqHeaders req) }++withHeaders :: HTTPRequest -> [Header] -> HTTPRequest+withHeaders req headers = req { reqHeaders = headers }++dropHeaderWithKey :: HTTPRequest -> String -> HTTPRequest+dropHeaderWithKey req@(HTTPRequest _ _ hdrs _) headerKey =+ let filteredHeaders = filter (\(k,v) -> k /= headerKey) hdrs in+ withHeaders req filteredHeaders++withBody :: HTTPRequest -> LBS.ByteString -> HTTPRequest+withBody req body = req { reqBody = pure body }++withMethod :: HTTPRequest -> HTTPRequestMethod -> HTTPRequest+withMethod req method = req { reqMethod = method }
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}