servant 0.2.1 → 0.2.2
raw patch · 12 files changed
+141/−70 lines, 12 filesnew-uploader
Files
- servant.cabal +2/−1
- src/Servant/API.hs +16/−13
- src/Servant/API/Delete.hs +1/−1
- src/Servant/API/Get.hs +1/−1
- src/Servant/API/Header.hs +0/−2
- src/Servant/API/MatrixParam.hs +35/−0
- src/Servant/API/Post.hs +2/−2
- src/Servant/API/Put.hs +1/−1
- src/Servant/API/Sub.hs +1/−2
- src/Servant/Common/Text.hs +5/−5
- src/Servant/QQ.hs +60/−30
- src/Servant/Utils/Links.hs +17/−12
servant.cabal view
@@ -1,5 +1,5 @@ name: servant-version: 0.2.1+version: 0.2.2 synopsis: A family of combinators for defining webservices APIs description: A family of combinators for defining webservices APIs and serving them@@ -34,6 +34,7 @@ Servant.API.Post Servant.API.Put Servant.API.QueryParam+ Servant.API.MatrixParam Servant.API.Raw Servant.API.ReqBody Servant.API.Sub
src/Servant/API.hs view
@@ -15,6 +15,8 @@ module Servant.API.QueryParam, -- | Accessing the request body as a JSON-encoded type: @'ReqBody'@ module Servant.API.ReqBody,+ -- | Retrieving matrix parameters from the 'URI' segment: @'MatrixParam'@+ module Servant.API.MatrixParam, -- * Actual endpoints, distinguished by HTTP method -- | GET requests@@ -37,16 +39,17 @@ module Servant.Utils.Links, ) where -import Servant.API.Alternative-import Servant.API.Capture-import Servant.API.Delete-import Servant.API.Get-import Servant.API.Header-import Servant.API.Post-import Servant.API.Put-import Servant.API.QueryParam-import Servant.API.Raw-import Servant.API.ReqBody-import Servant.API.Sub-import Servant.QQ (sitemap)-import Servant.Utils.Links (mkLink)+import Servant.API.Alternative ( (:<|>)(..) )+import Servant.API.Capture ( Capture )+import Servant.API.Delete ( Delete )+import Servant.API.Get ( Get )+import Servant.API.Header ( Header )+import Servant.API.Post ( Post )+import Servant.API.Put ( Put )+import Servant.API.QueryParam ( QueryFlag, QueryParams, QueryParam )+import Servant.API.MatrixParam ( MatrixFlag, MatrixParams, MatrixParam )+import Servant.API.Raw ( Raw )+import Servant.API.ReqBody ( ReqBody )+import Servant.API.Sub ( (:>)(..) )+import Servant.QQ ( sitemap )+import Servant.Utils.Links ( mkLink )
src/Servant/API/Delete.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE DeriveDataTypeable #-} module Servant.API.Delete where -import Data.Typeable+import Data.Typeable ( Typeable ) -- | Combinator for DELETE requests. --
src/Servant/API/Get.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE DeriveDataTypeable #-} module Servant.API.Get where -import Data.Typeable+import Data.Typeable ( Typeable ) -- | Endpoint for simple GET requests. Serves the result as JSON. --
src/Servant/API/Header.hs view
@@ -1,8 +1,6 @@ {-# LANGUAGE PolyKinds #-} module Servant.API.Header where -import GHC.TypeLits- -- | Extract the given header's value as a value of type @a@. -- -- Example:
+ src/Servant/API/MatrixParam.hs view
@@ -0,0 +1,35 @@+{-# LANGUAGE PolyKinds #-}+module Servant.API.MatrixParam where++-- | Lookup the value associated to the @sym@ matrix string parameter+-- and try to extract it as a value of type @a@.+--+-- Example:+--+-- > -- /books;author=<author name>+-- > type MyApi = "books" :> MatrixParam "author" Text :> Get [Book]+data MatrixParam sym a++-- | Lookup the values associated to the @sym@ matrix string parameter+-- and try to extract it as a value of type @[a]@. This is typically+-- meant to support matrix string parameters of the form+-- @param[]=val1;param[]=val2@ and so on. Note that servant doesn't actually+-- require the @[]@s and will fetch the values just fine with+-- @param=val1;param=val2@, too.+--+-- Example:+--+-- > -- /books;authors[]=<author1>;authors[]=<author2>;...+-- > type MyApi = "books" :> MatrixParams "authors" Text :> Get [Book]+data MatrixParams sym a++-- | Lookup a potentially value-less matrix string parameter+-- with boolean semantics. If the param @sym@ is there without any value,+-- or if it's there with value "true" or "1", it's interpreted as 'True'.+-- Otherwise, it's interpreted as 'False'.+--+-- Example:+--+-- > -- /books;published+-- > type MyApi = "books" :> MatrixFlag "published" :> Get [Book]+data MatrixFlag sym
src/Servant/API/Post.hs view
@@ -1,10 +1,10 @@ {-# LANGUAGE DeriveDataTypeable #-} module Servant.API.Post where -import Data.Typeable+import Data.Typeable ( Typeable ) -- | Endpoint for POST requests. The type variable represents the type of the--- response body (not the request body, use 'Servant.API.RQBody.RQBody' for+-- response body (not the request body, use 'Servant.API.ReqBody.ReqBody' for -- that). -- -- Example:
src/Servant/API/Put.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE DeriveDataTypeable #-} module Servant.API.Put where -import Data.Typeable+import Data.Typeable ( Typeable ) -- | Endpoint for PUT requests, usually used to update a ressource. -- The type @a@ is the type of the response body that's returned.
src/Servant/API/Sub.hs view
@@ -2,8 +2,7 @@ {-# LANGUAGE TypeOperators #-} module Servant.API.Sub where -import Data.Proxy-import GHC.TypeLits+import Data.Proxy ( Proxy ) -- | The contained API (second argument) can be found under @("/" ++ path)@ -- (path being the first argument).
src/Servant/Common/Text.hs view
@@ -6,11 +6,11 @@ , ToText(..) ) where -import Data.String.Conversions-import Data.Int-import Data.Text-import Data.Text.Read-import Data.Word+import Data.String.Conversions ( cs )+import Data.Int ( Int8, Int16, Int32, Int64 )+import Data.Text ( Text )+import Data.Text.Read ( rational, signed, decimal, Reader )+import Data.Word ( Word, Word8, Word16, Word32, Word64 ) -- | For getting values from url captures and query string parameters class FromText a where
src/Servant/QQ.hs view
@@ -28,21 +28,39 @@ -- Note the @/@ before a @QueryParam@! module Servant.QQ (sitemap) where -import Control.Monad (void)-import Control.Applicative hiding (many, (<|>), optional)-import Language.Haskell.TH.Quote+import Control.Monad ( void )+import Language.Haskell.TH.Quote ( QuasiQuoter(..) ) import Language.Haskell.TH+ ( mkName, Type(AppT, ConT, LitT), TyLit(StrTyLit) ) import Text.ParserCombinators.Parsec--import Servant.API.Capture-import Servant.API.Get-import Servant.API.Post-import Servant.API.Put-import Servant.API.Delete-import Servant.API.QueryParam-import Servant.API.ReqBody-import Servant.API.Sub-import Servant.API.Alternative+ ( try,+ Parser,+ manyTill,+ endBy,+ sepBy1,+ optional,+ optionMaybe,+ string,+ anyChar,+ char,+ spaces,+ noneOf,+ parse,+ skipMany,+ many,+ lookAhead,+ (<|>),+ (<?>) )+import Servant.API.Capture ( Capture )+import Servant.API.Get ( Get )+import Servant.API.Post ( Post )+import Servant.API.Put ( Put )+import Servant.API.Delete ( Delete )+import Servant.API.QueryParam ( QueryParam )+import Servant.API.MatrixParam ( MatrixParam )+import Servant.API.ReqBody ( ReqBody )+import Servant.API.Sub ( (:>) )+import Servant.API.Alternative ( (:<|>) ) -- | Finally-tagless encoding for our DSL. -- Keeping 'repr'' and 'repr' distinct when writing functions with an@@ -50,15 +68,16 @@ -- only one of 'get', 'post', 'put', and 'delete' in a value), but -- sometimes requires a little more work. class ExpSYM repr' repr | repr -> repr', repr' -> repr where- lit :: String -> repr' -> repr- capture :: String -> String -> repr -> repr- reqBody :: String -> repr -> repr- queryParam :: String -> String -> repr -> repr- conj :: repr' -> repr -> repr- get :: String -> repr- post :: String -> repr- put :: String -> repr- delete :: String -> repr+ lit :: String -> repr' -> repr+ capture :: String -> String -> repr -> repr+ reqBody :: String -> repr -> repr+ queryParam :: String -> String -> repr -> repr+ matrixParam :: String -> String -> repr -> repr+ conj :: repr' -> repr -> repr+ get :: String -> repr+ post :: String -> repr+ put :: String -> repr+ delete :: String -> repr infixr 6 >:@@ -72,8 +91,10 @@ capture name typ r = AppT (AppT (ConT ''Capture) (LitT (StrTyLit name))) (ConT $ mkName typ) >: r reqBody typ r = AppT (ConT ''ReqBody) (ConT $ mkName typ) >: r- queryParam name typ r = AppT (AppT (ConT ''QueryParam) (LitT (StrTyLit name)))+ queryParam name typ r = AppT (AppT (ConT ''QueryParam) (LitT (StrTyLit name))) (ConT $ mkName typ) >: r+ matrixParam name typ r = AppT (AppT (ConT ''MatrixParam) (LitT (StrTyLit name)))+ (ConT $ mkName typ) >: r conj x = AppT (AppT (ConT ''(:>)) x) get typ = AppT (ConT ''Get) (ConT $ mkName typ) post typ = AppT (ConT ''Post) (ConT $ mkName typ)@@ -93,17 +114,27 @@ <|> try parseLit where parseCapture = do- cname <- many (noneOf " ?/:")+ cname <- many (noneOf " ?/:;") char ':'- ctyp <- many (noneOf " ?/:")- return $ capture cname ctyp+ ctyp <- many (noneOf " ?/:;")+ mx <- many parseMatrixParam+ return $ capture cname ctyp . foldr (.) id mx parseQueryParam = do char '?'- cname <- many (noneOf " ?/:")+ cname <- many (noneOf " ?/:;") char ':'- ctyp <- many (noneOf " ?/:")+ ctyp <- many (noneOf " ?/:;") return $ queryParam cname ctyp- parseLit = lit <$> many (noneOf " ?/:")+ parseLit = do+ lt <- many (noneOf " ?/:;")+ mx <- many parseMatrixParam+ return $ lit lt . foldr (.) id mx+ parseMatrixParam = do+ char ';'+ cname <- many (noneOf " ?/:;")+ char ':'+ ctyp <- many (noneOf " ?/:;")+ return $ matrixParam cname ctyp parseUrl :: ExpSYM repr repr => Parser (repr -> repr) parseUrl = do@@ -195,4 +226,3 @@ Right st -> return st , quoteDec = undefined }-
src/Servant/Utils/Links.hs view
@@ -48,18 +48,19 @@ , IsLink )where -import Data.Proxy-import GHC.TypeLits+import Data.Proxy ( Proxy(..) )+import GHC.TypeLits ( KnownSymbol, Symbol, symbolVal ) -import Servant.API.Capture-import Servant.API.ReqBody-import Servant.API.QueryParam-import Servant.API.Get-import Servant.API.Post-import Servant.API.Put-import Servant.API.Delete-import Servant.API.Sub-import Servant.API.Alternative+import Servant.API.Capture ( Capture )+import Servant.API.ReqBody ( ReqBody )+import Servant.API.QueryParam ( QueryParam, QueryParams, QueryFlag )+import Servant.API.MatrixParam ( MatrixParam, MatrixParams, MatrixFlag )+import Servant.API.Get ( Get )+import Servant.API.Post ( Post )+import Servant.API.Put ( Put )+import Servant.API.Delete ( Delete )+import Servant.API.Sub ( type (:>) )+import Servant.API.Alternative ( type (:<|>) ) type family Or a b where@@ -78,6 +79,11 @@ IsElem (e :> sa) (Capture x y :> sb) = IsElem sa sb IsElem sa (ReqBody x :> sb) = IsElem sa sb IsElem sa (QueryParam x y :> sb) = IsElem sa sb+ IsElem sa (QueryParams x y :> sb) = IsElem sa sb+ IsElem sa (QueryFlag x :> sb) = IsElem sa sb+ IsElem sa (MatrixParam x y :> sb) = IsElem sa sb+ IsElem sa (MatrixParams x y :> sb) = IsElem sa sb+ IsElem sa (MatrixFlag x :> sb) = IsElem sa sb IsElem e e = 'True IsElem e a = 'False @@ -123,4 +129,3 @@ instance VLinkHelper (Post x) where vlh _ = ""-