Safe Haskell | None |
---|---|
Language | Haskell2010 |
Unlike most other HTTP kinds descrbed in http-kinder
, MediaType
s
are special in that they're expected to be very open to extension and
therefore are constrained only by kind *
.
- class HasMediaType t where
- class HasMediaType t => MimeEncode t a where
- mimeEncode :: sing t -> a -> ByteString
- class HasMediaType t => MimeDecode t a where
- mimeDecode :: sing t -> ByteString -> Either String a
- type family AllMimeEncode a ts :: Constraint
- type family AllMimeDecode a ts :: Constraint
- data TextPlain
- data JSON
- newtype Ver n a = Ver {
- getVer :: a
- negotiatedMimeEncode :: AllMimeEncode a ts => Sing ts -> Maybe ([Quality MediaType] -> a -> (MediaType, ByteString))
- negotiatedMimeDecode :: AllMimeDecode a ts => Sing ts -> Maybe (Maybe MediaType -> ByteString -> NegotiatedDecodeResult a)
- data NegotiatedDecodeResult a
- encodersOf :: AllMimeEncode a ts => Sing ts -> Map MediaType (a -> ByteString)
- decodersOf :: AllMimeDecode a ts => Sing ts -> Map MediaType (ByteString -> Either String a)
- data MediaType :: *
- mainType :: MediaType -> CI ByteString
- subType :: MediaType -> CI ByteString
- (/?) :: MediaType -> ByteString -> Bool
- (/.) :: MediaType -> ByteString -> Maybe (CI ByteString)
- data Quality a :: * -> *
Classes for encoding and decoding
class HasMediaType t where Source
Any Haskell type which instantiates HasMediaType
can be thought of
as a representative of that MediaType
. Users can construct their own
types and then instantiate HasMediaType
to extend the media type system.
HasMediaType JSON Source | |
HasMediaType TextPlain Source | |
(HasMediaType t, KnownNat n) => HasMediaType (Ver n t) Source |
class HasMediaType t => MimeEncode t a where Source
MediaType
s represent ways of encoding data as a bytestream---this
class encodes that representation.
mimeEncode :: sing t -> a -> ByteString Source
ToJSON a => MimeEncode JSON a Source | |
MimeEncode TextPlain Text Source |
class HasMediaType t => MimeDecode t a where Source
MediaType
s represent ways of encoding data as a bytestream---this
class provides parsers for this representation.
mimeDecode :: sing t -> ByteString -> Either String a Source
FromJSON a => MimeDecode JSON a Source |
Listing constraints to type-level lists
type family AllMimeEncode a ts :: Constraint Source
For a given concrete type a
, a list of types ts
satisfies
AllMimeEncode a ts
if each t
in ts
has
.MimeEncode
t a
AllMimeEncode a `[]` = () | |
AllMimeEncode a (t : ts) = (MimeEncode t a, AllMimeEncode a ts) |
type family AllMimeDecode a ts :: Constraint Source
For a given concrete type a
, a list of types ts
satisfies
MAllMimeDecode a ts
if each t
in ts
has
.MimeDecode
t a
AllMimeDecode a `[]` = () | |
AllMimeDecode a (t : ts) = (MimeDecode t a, AllMimeDecode a ts) |
Common content types
The TextPlain
media type ("text/plain") is unformatted, raw text.
The JSON
media type ("application/json") is JSON formatted text. Any
Haskell type with ToJSON
or FromJSON
values can
participate.
HasMediaType JSON Source | |
FromJSON a => MimeDecode JSON a Source | |
ToJSON a => MimeEncode JSON a Source |
Content type modifiers
Versions a media type using mime type parameterization.
has a media type like Ver
1 JSON"application/json; version=1"
. To use Ver
create instances, e.g., for
which
specialize encoders for type MimeEncode
(Ver
n t) at
Content negotiation
negotiatedMimeEncode :: AllMimeEncode a ts => Sing ts -> Maybe ([Quality MediaType] -> a -> (MediaType, ByteString)) Source
Encode a value using a list of valid media types and a list of
Accept
able media types. If no provided media type is acceptable then
the first of the provided is chosen by default. If the valid media type
listing is empty then no encoder can be negotiated ever---we fail early.
negotiatedMimeDecode :: AllMimeDecode a ts => Sing ts -> Maybe (Maybe MediaType -> ByteString -> NegotiatedDecodeResult a) Source
Decode a value using a list of valid media types and (maybe)
a provided Content-Type
MediaType
. If the Content-Type
is not
provided then the decoder for the first valid content type is attempted.
If the valid media type listing is empty then no decoder could ever be
negotiated---we fail early.
Utilities for negotiation
data NegotiatedDecodeResult a Source
Negoatiated decodes can fail for two reasons: it could be that the decoder failed (malformed input) or that the negotiation failed (a content type was provided which isn't accepted by the server).
Eq a => Eq (NegotiatedDecodeResult a) Source | |
Ord a => Ord (NegotiatedDecodeResult a) Source | |
Show a => Show (NegotiatedDecodeResult a) Source |
encodersOf :: AllMimeEncode a ts => Sing ts -> Map MediaType (a -> ByteString) Source
decodersOf :: AllMimeDecode a ts => Sing ts -> Map MediaType (ByteString -> Either String a) Source
Re-exports from Network.HTTP.Media
data MediaType :: *
An HTTP media type, consisting of the type, subtype, and parameters.
mainType :: MediaType -> CI ByteString
Retrieves the main type of a MediaType
.
subType :: MediaType -> CI ByteString
Retrieves the sub type of a MediaType
.
(/?) :: MediaType -> ByteString -> Bool
Evaluates if a MediaType
has a parameter of the given name.
(/.) :: MediaType -> ByteString -> Maybe (CI ByteString)
Retrieves a parameter from a MediaType
.
data Quality a :: * -> *
Attaches a quality value to data.
HeaderDecode Accept [Quality MediaType] Source | |
Eq a => Eq (Quality a) | |
Ord a => Ord (Quality a) | |
RenderHeader a => Show (Quality a) | |
RenderHeader h => RenderHeader (Quality h) |