http-kinder-0.2.0.0: Generic kinds and types for working with HTTP

Safe HaskellNone
LanguageHaskell2010

Network.HTTP.Kinder.MediaType

Contents

Description

Unlike most other HTTP kinds descrbed in http-kinder, MediaTypes are special in that they're expected to be very open to extension and therefore are constrained only by kind *.

Synopsis

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.

Methods

mediaType :: sing t -> MediaType Source

class HasMediaType t => MimeEncode t a where Source

MediaTypes represent ways of encoding data as a bytestream---this class encodes that representation.

Methods

mimeEncode :: sing t -> a -> ByteString Source

class HasMediaType t => MimeDecode t a where Source

MediaTypes represent ways of encoding data as a bytestream---this class provides parsers for this representation.

Methods

mimeDecode :: sing t -> ByteString -> Either String a Source

Instances

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.

Equations

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.

Equations

AllMimeDecode a `[]` = () 
AllMimeDecode a (t : ts) = (MimeDecode t a, AllMimeDecode a ts) 

Common content types

data TextPlain Source

The TextPlain media type ("text/plain") is unformatted, raw text.

data JSON Source

The JSON media type ("application/json") is JSON formatted text. Any Haskell type with ToJSON or FromJSON values can participate.

Content type modifiers

newtype Ver n a Source

Versions a media type using mime type parameterization. Ver 1 JSON has a media type like "application/json; version=1". To use Ver create instances, e.g., for MimeEncode (Ver n t) a which specialize encoders for type t

Constructors

Ver 

Fields

getVer :: a
 

Instances

Monad (Ver n) Source 
Functor (Ver n) Source 
Applicative (Ver n) Source 
Eq a => Eq (Ver n a) Source 
Ord a => Ord (Ver n a) Source 
Show a => Show (Ver n a) Source 
(HasMediaType t, KnownNat n) => HasMediaType (Ver n t) Source 

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 Acceptable 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).

encodersOf :: AllMimeEncode a ts => Sing ts -> Map MediaType (a -> ByteString) Source

Provided a Sing representing a type-level list of mediatypes, produce a concrete mapping from MediaTypes to encoding functions.

decodersOf :: AllMimeDecode a ts => Sing ts -> Map MediaType (ByteString -> Either String a) Source

Provided a Sing representing a type-level list of mediatypes, produce a concrete mapping from MediaTypes to decoding functions.

Re-exports from Network.HTTP.Media

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.