http-api-data-0.3.6: Converting to/from HTTP API data like URL pieces, headers and query parameters.

Safe Haskell | None
Language | Haskell2010

Web.HttpApiData

Contents

- Examples
- Classes
- ``Maybe`` parsers
- Prefix parsers
- Multiple URL pieces
- Multiple query params
- Parsers for ``Bounded`` ``Enum``s
- Lenient data
- Other helpers

Description

Convert Haskell values to and from HTTP API data such as URL pieces, headers and query parameters.

Synopsis

- class ToHttpApiData a where
- class FromHttpApiData a where
- parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a
- parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a
- parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a
- parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a
- parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text
- parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
- toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text
- parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
- parseBoundedUrlPiece :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a
- parseBoundedQueryParam :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a
- parseBoundedHeader :: (ToHttpApiData a, Bounded a, Enum a) => ByteString -> Either Text a
- parseBoundedEnumOf :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a
- parseBoundedEnumOfI :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a
- parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a
- newtype LenientData a = LenientData {
  - getLenientData :: Either Text a
  }
- showTextData :: Show a => a -> Text
- readTextData :: Read a => Text -> Either Text a

# Examples

Booleans:

    >>> toUrlPiece True
    "true"
    >>> parseUrlPiece "false" :: Either Text Bool
    Right False
    >>> parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]
    Left "could not parse: `undefined'"

Numbers:

    >>> toQueryParam 45.2
    "45.2"
    >>> parseQueryParam "452" :: Either Text Int
    Right 452
    >>> toQueryParams [1..5] :: [Text]
    ["1","2","3","4","5"]
    >>> parseQueryParams ["127", "255"] :: Either Text [Int8]
    Left "out of bounds: `255' (should be between -128 and 127)"

Strings:

    >>> toHeader "hello"
    "hello"
    >>> parseHeader "world" :: Either Text String
    Right "world"

Calendar day:

    >>> toQueryParam (fromGregorian 2015 10 03)
    "2015-10-03"
    >>> toGregorian <$> parseQueryParam "2016-12-01"
    Right (2016,12,1)

# Classes

class ToHttpApiData a where #

Convert value to HTTP API data.

WARNING: Do not derive this using `DeriveAnyClass` as the generated instance will loop indefinitely.

Minimal complete definition

toUrlPiece | toQueryParam

Methods

toUrlPiece :: a -> Text #

Convert to URL path piece.

toEncodedUrlPiece :: a -> Builder #

Convert to a URL path piece, making sure to encode any special chars. The default definition uses `encodePathSegmentsRelative`, but this may be overriden with a more efficient version.

toHeader :: a -> ByteString #

Convert to HTTP header value.

toQueryParam :: a -> Text #

Convert to query param value.

Instances

ToHttpApiData Bool # |
Methods toUrlPiece :: Bool -> Text # toEncodedUrlPiece :: Bool -> Builder # toHeader :: Bool -> ByteString # toQueryParam :: Bool -> Text #
ToHttpApiData Char # |
Methods toUrlPiece :: Char -> Text # toEncodedUrlPiece :: Char -> Builder # toHeader :: Char -> ByteString # toQueryParam :: Char -> Text #
ToHttpApiData Double # |
Methods toUrlPiece :: Double -> Text # toEncodedUrlPiece :: Double -> Builder # toHeader :: Double -> ByteString # toQueryParam :: Double -> Text #
ToHttpApiData Float # |
Methods toUrlPiece :: Float -> Text # toEncodedUrlPiece :: Float -> Builder # toHeader :: Float -> ByteString # toQueryParam :: Float -> Text #
ToHttpApiData Int # |
Methods toUrlPiece :: Int -> Text # toEncodedUrlPiece :: Int -> Builder # toHeader :: Int -> ByteString # toQueryParam :: Int -> Text #
ToHttpApiData Int8 # |
Methods toUrlPiece :: Int8 -> Text # toEncodedUrlPiece :: Int8 -> Builder # toHeader :: Int8 -> ByteString # toQueryParam :: Int8 -> Text #
ToHttpApiData Int16 # |
Methods toUrlPiece :: Int16 -> Text # toEncodedUrlPiece :: Int16 -> Builder # toHeader :: Int16 -> ByteString # toQueryParam :: Int16 -> Text #
ToHttpApiData Int32 # |
Methods toUrlPiece :: Int32 -> Text # toEncodedUrlPiece :: Int32 -> Builder # toHeader :: Int32 -> ByteString # toQueryParam :: Int32 -> Text #
ToHttpApiData Int64 # |
Methods toUrlPiece :: Int64 -> Text # toEncodedUrlPiece :: Int64 -> Builder # toHeader :: Int64 -> ByteString # toQueryParam :: Int64 -> Text #
ToHttpApiData Integer # |
Methods toUrlPiece :: Integer -> Text # toEncodedUrlPiece :: Integer -> Builder # toHeader :: Integer -> ByteString # toQueryParam :: Integer -> Text #
ToHttpApiData Ordering # |
Methods toUrlPiece :: Ordering -> Text # toEncodedUrlPiece :: Ordering -> Builder # toHeader :: Ordering -> ByteString # toQueryParam :: Ordering -> Text #
ToHttpApiData Word # |
Methods toUrlPiece :: Word -> Text # toEncodedUrlPiece :: Word -> Builder # toHeader :: Word -> ByteString # toQueryParam :: Word -> Text #
ToHttpApiData Word8 # |
Methods toUrlPiece :: Word8 -> Text # toEncodedUrlPiece :: Word8 -> Builder # toHeader :: Word8 -> ByteString # toQueryParam :: Word8 -> Text #
ToHttpApiData Word16 # |
Methods toUrlPiece :: Word16 -> Text # toEncodedUrlPiece :: Word16 -> Builder # toHeader :: Word16 -> ByteString # toQueryParam :: Word16 -> Text #
ToHttpApiData Word32 # |
Methods toUrlPiece :: Word32 -> Text # toEncodedUrlPiece :: Word32 -> Builder # toHeader :: Word32 -> ByteString # toQueryParam :: Word32 -> Text #
ToHttpApiData Word64 # |
Methods toUrlPiece :: Word64 -> Text # toEncodedUrlPiece :: Word64 -> Builder # toHeader :: Word64 -> ByteString # toQueryParam :: Word64 -> Text #
ToHttpApiData () # |
    >>> toUrlPiece ()
    "_"
Methods toUrlPiece :: () -> Text # toEncodedUrlPiece :: () -> Builder # toHeader :: () -> ByteString # toQueryParam :: () -> Text #
ToHttpApiData Text # |
Methods toUrlPiece :: Text -> Text # toEncodedUrlPiece :: Text -> Builder # toHeader :: Text -> ByteString # toQueryParam :: Text -> Text #
ToHttpApiData Text # |
Methods toUrlPiece :: Text -> Text # toEncodedUrlPiece :: Text -> Builder # toHeader :: Text -> ByteString # toQueryParam :: Text -> Text #
ToHttpApiData String # |
Methods toUrlPiece :: String -> Text # toEncodedUrlPiece :: String -> Builder # toHeader :: String -> ByteString # toQueryParam :: String -> Text #
ToHttpApiData Natural # |
Methods toUrlPiece :: Natural -> Text # toEncodedUrlPiece :: Natural -> Builder # toHeader :: Natural -> ByteString # toQueryParam :: Natural -> Text #
ToHttpApiData Void # |
Methods toUrlPiece :: Void -> Text # toEncodedUrlPiece :: Void -> Builder # toHeader :: Void -> ByteString # toQueryParam :: Void -> Text #
ToHttpApiData Version # |
    >>> toUrlPiece (Version [1, 2, 3] [])
    "1.2.3"
Methods toUrlPiece :: Version -> Text # toEncodedUrlPiece :: Version -> Builder # toHeader :: Version -> ByteString # toQueryParam :: Version -> Text #
ToHttpApiData All # |
Methods toUrlPiece :: All -> Text # toEncodedUrlPiece :: All -> Builder # toHeader :: All -> ByteString # toQueryParam :: All -> Text #
ToHttpApiData Any # |
Methods toUrlPiece :: Any -> Text # toEncodedUrlPiece :: Any -> Builder # toHeader :: Any -> ByteString # toQueryParam :: Any -> Text #
ToHttpApiData LocalTime # |
    >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)
    "2015-10-03T14:55:01"
Methods toUrlPiece :: LocalTime -> Text # toEncodedUrlPiece :: LocalTime -> Builder # toHeader :: LocalTime -> ByteString # toQueryParam :: LocalTime -> Text #
ToHttpApiData ZonedTime # |
    >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 01)) utc
    "2015-10-03T14:55:01+0000"
Methods toUrlPiece :: ZonedTime -> Text # toEncodedUrlPiece :: ZonedTime -> Builder # toHeader :: ZonedTime -> ByteString # toQueryParam :: ZonedTime -> Text #
ToHttpApiData UTCTime # |
    >>> toUrlPiece $ UTCTime (fromGregorian 2015 10 03) 864
    "2015-10-03T00:14:24Z"
Methods toUrlPiece :: UTCTime -> Text # toEncodedUrlPiece :: UTCTime -> Builder # toHeader :: UTCTime -> ByteString # toQueryParam :: UTCTime -> Text #
ToHttpApiData NominalDiffTime # |
Methods toUrlPiece :: NominalDiffTime -> Text # toEncodedUrlPiece :: NominalDiffTime -> Builder # toHeader :: NominalDiffTime -> ByteString # toQueryParam :: NominalDiffTime -> Text #
ToHttpApiData Day # |
    >>> toUrlPiece (fromGregorian 2015 10 03)
    "2015-10-03"
Methods toUrlPiece :: Day -> Text # toEncodedUrlPiece :: Day -> Builder # toHeader :: Day -> ByteString # toQueryParam :: Day -> Text #
ToHttpApiData UUID # |
Methods toUrlPiece :: UUID -> Text # toEncodedUrlPiece :: UUID -> Builder # toHeader :: UUID -> ByteString # toQueryParam :: UUID -> Text #
ToHttpApiData a => ToHttpApiData (Maybe a) # |
    >>> toUrlPiece (Just "Hello")
    "just Hello"
Methods toUrlPiece :: Maybe a -> Text # toEncodedUrlPiece :: Maybe a -> Builder # toHeader :: Maybe a -> ByteString # toQueryParam :: Maybe a -> Text #
ToHttpApiData a => ToHttpApiData (Dual a) # |
Methods toUrlPiece :: Dual a -> Text # toEncodedUrlPiece :: Dual a -> Builder # toHeader :: Dual a -> ByteString # toQueryParam :: Dual a -> Text #
ToHttpApiData a => ToHttpApiData (Sum a) # |
Methods toUrlPiece :: Sum a -> Text # toEncodedUrlPiece :: Sum a -> Builder # toHeader :: Sum a -> ByteString # toQueryParam :: Sum a -> Text #
ToHttpApiData a => ToHttpApiData (Product a) # |
Methods toUrlPiece :: Product a -> Text # toEncodedUrlPiece :: Product a -> Builder # toHeader :: Product a -> ByteString # toQueryParam :: Product a -> Text #
ToHttpApiData a => ToHttpApiData (First a) # |
Methods toUrlPiece :: First a -> Text # toEncodedUrlPiece :: First a -> Builder # toHeader :: First a -> ByteString # toQueryParam :: First a -> Text #
ToHttpApiData a => ToHttpApiData (Last a) # |
Methods toUrlPiece :: Last a -> Text # toEncodedUrlPiece :: Last a -> Builder # toHeader :: Last a -> ByteString # toQueryParam :: Last a -> Text #
(ToHttpApiData a, ToHttpApiData b) => ToHttpApiData (Either a b) # |
    >>> toUrlPiece (Left "err" :: Either String Int)
    "left err"
    >>> toUrlPiece (Right 3 :: Either String Int)
    "right 3"
Methods toUrlPiece :: Either a b -> Text # toEncodedUrlPiece :: Either a b -> Builder # toHeader :: Either a b -> ByteString # toQueryParam :: Either a b -> Text #

class FromHttpApiData a where #

Parse value from HTTP API data.

WARNING: Do not derive this using `DeriveAnyClass` as the generated instance will loop indefinitely.

Minimal complete definition

parseUrlPiece | parseQueryParam

Methods

parseUrlPiece :: Text -> Either Text a #

Parse URL path piece.

parseHeader :: ByteString -> Either Text a #

Parse HTTP header value.

parseQueryParam :: Text -> Either Text a #

Parse query param value.

Instances

FromHttpApiData Bool # |
Methods parseUrlPiece :: Text -> Either Text Bool # parseHeader :: ByteString -> Either Text Bool # parseQueryParam :: Text -> Either Text Bool #
FromHttpApiData Char # |
Methods parseUrlPiece :: Text -> Either Text Char # parseHeader :: ByteString -> Either Text Char # parseQueryParam :: Text -> Either Text Char #
FromHttpApiData Double # |
Methods parseUrlPiece :: Text -> Either Text Double # parseHeader :: ByteString -> Either Text Double # parseQueryParam :: Text -> Either Text Double #
FromHttpApiData Float # |
Methods parseUrlPiece :: Text -> Either Text Float # parseHeader :: ByteString -> Either Text Float # parseQueryParam :: Text -> Either Text Float #
FromHttpApiData Int # |
Methods parseUrlPiece :: Text -> Either Text Int # parseHeader :: ByteString -> Either Text Int # parseQueryParam :: Text -> Either Text Int #
FromHttpApiData Int8 # |
Methods parseUrlPiece :: Text -> Either Text Int8 # parseHeader :: ByteString -> Either Text Int8 # parseQueryParam :: Text -> Either Text Int8 #
FromHttpApiData Int16 # |
Methods parseUrlPiece :: Text -> Either Text Int16 # parseHeader :: ByteString -> Either Text Int16 # parseQueryParam :: Text -> Either Text Int16 #
FromHttpApiData Int32 # |
Methods parseUrlPiece :: Text -> Either Text Int32 # parseHeader :: ByteString -> Either Text Int32 # parseQueryParam :: Text -> Either Text Int32 #
FromHttpApiData Int64 # |
Methods parseUrlPiece :: Text -> Either Text Int64 # parseHeader :: ByteString -> Either Text Int64 # parseQueryParam :: Text -> Either Text Int64 #
FromHttpApiData Integer # |
Methods parseUrlPiece :: Text -> Either Text Integer # parseHeader :: ByteString -> Either Text Integer # parseQueryParam :: Text -> Either Text Integer #
FromHttpApiData Ordering # |
Methods parseUrlPiece :: Text -> Either Text Ordering # parseHeader :: ByteString -> Either Text Ordering # parseQueryParam :: Text -> Either Text Ordering #
FromHttpApiData Word # |
Methods parseUrlPiece :: Text -> Either Text Word # parseHeader :: ByteString -> Either Text Word # parseQueryParam :: Text -> Either Text Word #
FromHttpApiData Word8 # |
Methods parseUrlPiece :: Text -> Either Text Word8 # parseHeader :: ByteString -> Either Text Word8 # parseQueryParam :: Text -> Either Text Word8 #
FromHttpApiData Word16 # |
Methods parseUrlPiece :: Text -> Either Text Word16 # parseHeader :: ByteString -> Either Text Word16 # parseQueryParam :: Text -> Either Text Word16 #
FromHttpApiData Word32 # |
Methods parseUrlPiece :: Text -> Either Text Word32 # parseHeader :: ByteString -> Either Text Word32 # parseQueryParam :: Text -> Either Text Word32 #
FromHttpApiData Word64 # |
Methods parseUrlPiece :: Text -> Either Text Word64 # parseHeader :: ByteString -> Either Text Word64 # parseQueryParam :: Text -> Either Text Word64 #
FromHttpApiData () # |
    >>> parseUrlPiece "_" :: Either Text ()
    Right ()
Methods parseUrlPiece :: Text -> Either Text () # parseHeader :: ByteString -> Either Text () # parseQueryParam :: Text -> Either Text () #
FromHttpApiData Text # |
Methods parseUrlPiece :: Text -> Either Text Text # parseHeader :: ByteString -> Either Text Text # parseQueryParam :: Text -> Either Text Text #
FromHttpApiData Text # |
Methods parseUrlPiece :: Text -> Either Text Text # parseHeader :: ByteString -> Either Text Text # parseQueryParam :: Text -> Either Text Text #
FromHttpApiData String # |
Methods parseUrlPiece :: Text -> Either Text String # parseHeader :: ByteString -> Either Text String # parseQueryParam :: Text -> Either Text String #
FromHttpApiData Natural # |
Methods parseUrlPiece :: Text -> Either Text Natural # parseHeader :: ByteString -> Either Text Natural # parseQueryParam :: Text -> Either Text Natural #
FromHttpApiData Void # | Parsing a ``Void`` value is always an error, considering ``Void`` as a data type with no constructors.
Methods parseUrlPiece :: Text -> Either Text Void # parseHeader :: ByteString -> Either Text Void # parseQueryParam :: Text -> Either Text Void #
FromHttpApiData Version # |
    >>> showVersion <$> parseUrlPiece "1.2.3"
    Right "1.2.3"
Methods parseUrlPiece :: Text -> Either Text Version # parseHeader :: ByteString -> Either Text Version # parseQueryParam :: Text -> Either Text Version #
FromHttpApiData All # |
Methods parseUrlPiece :: Text -> Either Text All # parseHeader :: ByteString -> Either Text All # parseQueryParam :: Text -> Either Text All #
FromHttpApiData Any # |
Methods parseUrlPiece :: Text -> Either Text Any # parseHeader :: ByteString -> Either Text Any # parseQueryParam :: Text -> Either Text Any #
FromHttpApiData LocalTime # |
    >>> parseUrlPiece "2015-10-03T14:55:01" :: Either Text LocalTime
    Right 2015-10-03 14:55:01
Methods parseUrlPiece :: Text -> Either Text LocalTime # parseHeader :: ByteString -> Either Text LocalTime # parseQueryParam :: Text -> Either Text LocalTime #
FromHttpApiData ZonedTime # |
    >>> parseUrlPiece "2015-10-03T14:55:01+0000" :: Either Text ZonedTime
    Right 2015-10-03 14:55:01 +0000
Methods parseUrlPiece :: Text -> Either Text ZonedTime # parseHeader :: ByteString -> Either Text ZonedTime # parseQueryParam :: Text -> Either Text ZonedTime #
FromHttpApiData UTCTime # |
    >>> parseUrlPiece "2015-10-03T00:14:24Z" :: Either Text UTCTime
    Right 2015-10-03 00:14:24 UTC
Methods parseUrlPiece :: Text -> Either Text UTCTime # parseHeader :: ByteString -> Either Text UTCTime # parseQueryParam :: Text -> Either Text UTCTime #
FromHttpApiData NominalDiffTime # |
Methods parseUrlPiece :: Text -> Either Text NominalDiffTime # parseHeader :: ByteString -> Either Text NominalDiffTime # parseQueryParam :: Text -> Either Text NominalDiffTime #
FromHttpApiData Day # |
    >>> toGregorian <$> parseUrlPiece "2016-12-01"
    Right (2016,12,1)
Methods parseUrlPiece :: Text -> Either Text Day # parseHeader :: ByteString -> Either Text Day # parseQueryParam :: Text -> Either Text Day #
FromHttpApiData UUID # |
Methods parseUrlPiece :: Text -> Either Text UUID # parseHeader :: ByteString -> Either Text UUID # parseQueryParam :: Text -> Either Text UUID #
FromHttpApiData a => FromHttpApiData (Maybe a) # |
    >>> parseUrlPiece "Just 123" :: Either Text (Maybe Int)
    Right (Just 123)
Methods parseUrlPiece :: Text -> Either Text (Maybe a) # parseHeader :: ByteString -> Either Text (Maybe a) # parseQueryParam :: Text -> Either Text (Maybe a) #
FromHttpApiData a => FromHttpApiData (Dual a) # |
Methods parseUrlPiece :: Text -> Either Text (Dual a) # parseHeader :: ByteString -> Either Text (Dual a) # parseQueryParam :: Text -> Either Text (Dual a) #
FromHttpApiData a => FromHttpApiData (Sum a) # |
Methods parseUrlPiece :: Text -> Either Text (Sum a) # parseHeader :: ByteString -> Either Text (Sum a) # parseQueryParam :: Text -> Either Text (Sum a) #
FromHttpApiData a => FromHttpApiData (Product a) # |
Methods parseUrlPiece :: Text -> Either Text (Product a) # parseHeader :: ByteString -> Either Text (Product a) # parseQueryParam :: Text -> Either Text (Product a) #
FromHttpApiData a => FromHttpApiData (First a) # |
Methods parseUrlPiece :: Text -> Either Text (First a) # parseHeader :: ByteString -> Either Text (First a) # parseQueryParam :: Text -> Either Text (First a) #
FromHttpApiData a => FromHttpApiData (Last a) # |
Methods parseUrlPiece :: Text -> Either Text (Last a) # parseHeader :: ByteString -> Either Text (Last a) # parseQueryParam :: Text -> Either Text (Last a) #
FromHttpApiData a => FromHttpApiData (LenientData a) # |
Methods parseUrlPiece :: Text -> Either Text (LenientData a) # parseHeader :: ByteString -> Either Text (LenientData a) # parseQueryParam :: Text -> Either Text (LenientData a) #
(FromHttpApiData a, FromHttpApiData b) => FromHttpApiData (Either a b) # |
    >>> parseUrlPiece "Right 123" :: Either Text (Either String Int)
    Right (Right 123)
Methods parseUrlPiece :: Text -> Either Text (Either a b) # parseHeader :: ByteString -> Either Text (Either a b) # parseQueryParam :: Text -> Either Text (Either a b) #

# ``Maybe`` parsers

parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a #

Parse URL path piece in a ``Maybe``.

    >>> parseUrlPieceMaybe "12" :: Maybe Int
    Just 12

parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a #

Parse HTTP header value in a ``Maybe``.

    >>> parseHeaderMaybe "hello" :: Maybe Text
    Just "hello"

parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a #

Parse query param value in a ``Maybe``.

    >>> parseQueryParamMaybe "true" :: Maybe Bool
    Just True

# Prefix parsers

parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a #

Case insensitive.

Parse given text case insensitive and then parse the rest of the input using ``parseUrlPiece``.

    >>> parseUrlPieceWithPrefix "Just " "just 10" :: Either Text Int
    Right 10
    >>> parseUrlPieceWithPrefix "Left " "left" :: Either Text Bool
    Left "could not parse: `left'"

This can be used to implement ``FromHttpApiData`` for single field constructors:

    >>> data Foo = Foo Int deriving (Show)
    >>> instance FromHttpApiData Foo where parseUrlPiece s = Foo <$> parseUrlPieceWithPrefix "Foo " s
    >>> parseUrlPiece "foo 1" :: Either Text Foo
    Right (Foo 1)

parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a #

Parse given bytestring then parse the rest of the input using ``parseHeader``.

    data BasicAuthToken = BasicAuthToken Text deriving (Show)

    instance FromHttpApiData BasicAuthToken where
      parseHeader h     = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h
      parseQueryParam p = BasicAuthToken <$> parseQueryParam p

    >>> parseHeader "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" :: Either Text BasicAuthToken
    Right (BasicAuthToken "QWxhZGRpbjpvcGVuIHNlc2FtZQ==")

parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a #

Case insensitive.

Parse given text case insensitive and then parse the rest of the input using ``parseQueryParam``.

    >>> parseQueryParamWithPrefix "z" "z10" :: Either Text Int
    Right 10

# Multiple URL pieces

toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text #

Convert multiple values to a list of URL pieces.

    >>> toUrlPieces [1, 2, 3] :: [Text]
    ["1","2","3"]

parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a) #

Parse multiple URL pieces.

    >>> parseUrlPieces ["true", "false"] :: Either Text [Bool]
    Right [True,False]
    >>> parseUrlPieces ["123", "hello", "world"] :: Either Text [Int]
    Left "could not parse: `hello' (input does not start with a digit)"

# Multiple query params

toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text #

Convert multiple values to a list of query parameter values.

    >>> toQueryParams [fromGregorian 2015 10 03, fromGregorian 2015 12 01] :: [Text]
    ["2015-10-03","2015-12-01"]

parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a) #

Parse multiple query parameters.

    >>> parseQueryParams ["1", "2", "3"] :: Either Text [Int]
    Right [1,2,3]
    >>> parseQueryParams ["64", "128", "256"] :: Either Text [Word8]
    Left "out of bounds: `256' (should be between 0 and 255)"

# Parsers for ``Bounded`` ``Enum``s

parseBoundedUrlPiece :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a #

Case insensitive.

Parse values case insensitively based on ``ToHttpApiData`` instance. Uses ``toUrlPiece`` to get possible values.

parseBoundedQueryParam :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a #

Case insensitive.

Parse values case insensitively based on ``ToHttpApiData`` instance. Uses ``toQueryParam`` to get possible values.

parseBoundedHeader :: (ToHttpApiData a, Bounded a, Enum a) => ByteString -> Either Text a #

Parse values based on ``ToHttpApiData`` instance. Uses ``toHeader`` to get possible values.

parseBoundedEnumOf :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a #

Parse values based on a precalculated mapping of their ``Text`` representation.

    >>> parseBoundedEnumOf toUrlPiece "true" :: Either Text Bool
    Right True

For case sensitive parser see `parseBoundedEnumOfI`.

parseBoundedEnumOfI :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a #

Case insensitive.

Parse values case insensitively based on a precalculated mapping of their ``Text`` representations.

    >>> parseBoundedEnumOfI toUrlPiece "FALSE" :: Either Text Bool
    Right False

For case sensitive parser see `parseBoundedEnumOf`.

parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a #

Case insensitive.

Parse values case insensitively based on ``Show`` instance.

    >>> parseBoundedTextData "true" :: Either Text Bool
    Right True
    >>> parseBoundedTextData "FALSE" :: Either Text Bool
    Right False

This can be used as a default implementation for enumeration types:

    >>> data MyData = Foo | Bar | Baz deriving (Show, Bounded, Enum)
    >>> instance FromHttpApiData MyData where parseUrlPiece = parseBoundedTextData
    >>> parseUrlPiece "foo" :: Either Text MyData
    Right Foo

# Lenient data

newtype LenientData a #

Lenient parameters. `FromHttpApiData` combinators always return `Right`.

Since: 0.3.5

Constructors

LenientData |
Fields getLenientData :: Either Text a

Instances

Functor LenientData # |
Methods fmap :: (a -> b) -> LenientData a -> LenientData b # (<$) :: a -> LenientData b -> LenientData a #
Foldable LenientData # |
Methods fold :: Monoid m => LenientData m -> m # foldMap :: Monoid m => (a -> m) -> LenientData a -> m # foldr :: (a -> b -> b) -> b -> LenientData a -> b # foldr' :: (a -> b -> b) -> b -> LenientData a -> b # foldl :: (b -> a -> b) -> b -> LenientData a -> b # foldl' :: (b -> a -> b) -> b -> LenientData a -> b # foldr1 :: (a -> a -> a) -> LenientData a -> a # foldl1 :: (a -> a -> a) -> LenientData a -> a # toList :: LenientData a -> [a] # null :: LenientData a -> Bool # length :: LenientData a -> Int # elem :: Eq a => a -> LenientData a -> Bool # maximum :: Ord a => LenientData a -> a # minimum :: Ord a => LenientData a -> a # sum :: Num a => LenientData a -> a # product :: Num a => LenientData a -> a #
Traversable LenientData # |
Methods traverse :: Applicative f => (a -> f b) -> LenientData a -> f (LenientData b) # sequenceA :: Applicative f => LenientData (f a) -> f (LenientData a) # mapM :: Monad m => (a -> m b) -> LenientData a -> m (LenientData b) # sequence :: Monad m => LenientData (m a) -> m (LenientData a) #
Eq a => Eq (LenientData a) # |
Methods (==) :: LenientData a -> LenientData a -> Bool # (/=) :: LenientData a -> LenientData a -> Bool #
Data a => Data (LenientData a) # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> LenientData a -> c (LenientData a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (LenientData a) # toConstr :: LenientData a -> Constr # dataTypeOf :: LenientData a -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (LenientData a)) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (LenientData a)) # gmapT :: (forall b. Data b => b -> b) -> LenientData a -> LenientData a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> LenientData a -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> LenientData a -> r # gmapQ :: (forall d. Data d => d -> u) -> LenientData a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> LenientData a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> LenientData a -> m (LenientData a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> LenientData a -> m (LenientData a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> LenientData a -> m (LenientData a) #
Ord a => Ord (LenientData a) # |
Methods compare :: LenientData a -> LenientData a -> Ordering # (<) :: LenientData a -> LenientData a -> Bool # (<=) :: LenientData a -> LenientData a -> Bool # (>) :: LenientData a -> LenientData a -> Bool # (>=) :: LenientData a -> LenientData a -> Bool # max :: LenientData a -> LenientData a -> LenientData a # min :: LenientData a -> LenientData a -> LenientData a #
Read a => Read (LenientData a) # |
Methods readsPrec :: Int -> ReadS (LenientData a) # readList :: ReadS [LenientData a] # readPrec :: ReadPrec (LenientData a) # readListPrec :: ReadPrec [LenientData a] #
Show a => Show (LenientData a) # |
Methods showsPrec :: Int -> LenientData a -> ShowS # show :: LenientData a -> String # showList :: [LenientData a] -> ShowS #
FromHttpApiData a => FromHttpApiData (LenientData a) # |
Methods parseUrlPiece :: Text -> Either Text (LenientData a) # parseHeader :: ByteString -> Either Text (LenientData a) # parseQueryParam :: Text -> Either Text (LenientData a) #

# Other helpers

showTextData :: Show a => a -> Text #

Lower case.

Convert to URL piece using ``Show`` instance. The result is always lower cased.

    >>> showTextData True
    "true"

This can be used as a default implementation for enumeration types:

    >>> data MyData = Foo | Bar | Baz deriving (Show)
    >>> instance ToHttpApiData MyData where toUrlPiece = showTextData
    >>> toUrlPiece Foo
    "foo"

readTextData :: Read a => Text -> Either Text a #

Parse URL piece using ``Read`` instance.

Use for types which do not involve letters:

    >>> readTextData "1991-06-02" :: Either Text Day
    Right 1991-06-02

This parser is case sensitive and will not match ``showTextData`` in presense of letters:

    >>> readTextData (showTextData True) :: Either Text Bool
    Left "could not parse: `true'"

See ``parseBoundedTextData``.