1 {-# LANGUAGE OverloadedStrings #-}
2 module Language.Symantic.HTTP.Media where
4 import Data.Function (($), (.), id)
5 import Data.Proxy (Proxy(..))
6 import Data.String (String)
7 import qualified Data.ByteString as BS
8 import qualified Data.ByteString.Lazy as BSL
9 import qualified Data.ByteString.Lazy.Char8 as BSLC
10 import qualified Data.Text as T
11 import qualified Data.Text.Encoding as T
12 import qualified Data.Text.Lazy as TL
13 import qualified Data.Text.Lazy.Encoding as TL
14 import qualified Network.HTTP.Media as Media
16 -- * Class 'MediaTypeable'
17 class MediaTypeable mt where
18 mediaType :: Proxy mt -> MediaType
19 mediaTypes :: Proxy mt -> [MediaType]
20 mediaTypes mt = [mediaType mt]
21 type MediaType = Media.MediaType
23 charsetUTF8 :: MediaType -> MediaType
24 charsetUTF8 = (Media./: ("charset", "utf-8"))
26 anyMediaType :: MediaType
33 instance MediaTypeable JSON where
34 mediaType _mt = charsetUTF8 $ "application"Media.//"json"
35 mediaTypes mt = [mediaType mt, "application"Media.//"json"]
41 instance MediaTypeable HTML where
42 mediaType _mt = charsetUTF8 $ "text"Media.//"html"
43 mediaTypes mt = [mediaType mt, "text"Media.//"html"]
45 -- ** Type 'FormUrlEncoded'
47 formUrlEncoded :: Proxy FormUrlEncoded
48 formUrlEncoded = Proxy
49 instance MediaTypeable FormUrlEncoded where
50 mediaType _mt = "application"Media.//"x-www-form-urlencoded"
52 -- ** Type 'OctetStream'
54 octetStream :: Proxy OctetStream
56 instance MediaTypeable OctetStream where
57 mediaType _mt = "application"Media.//"octet-stream"
59 -- ** Type 'PlainText'
61 plainText :: Proxy PlainText
63 instance MediaTypeable PlainText where
64 mediaType _mt = charsetUTF8 $ "text"Media.//"plain"
65 instance MediaTypeable () where
66 mediaType _mt = mediaType (Proxy::Proxy PlainText)
68 -- * Class 'ToMediaType'
69 class MediaTypeable mt => ToMediaType mt a where
70 toMediaType :: Proxy mt -> a -> BSL.ByteString
71 instance ToMediaType PlainText () where
72 toMediaType _mt () = BSL.empty
73 instance ToMediaType PlainText T.Text where
74 toMediaType _mt = BSL.fromStrict . T.encodeUtf8
75 instance ToMediaType PlainText TL.Text where
76 toMediaType _mt = TL.encodeUtf8
77 instance ToMediaType PlainText String where
78 toMediaType _mt = BSLC.pack
79 instance ToMediaType OctetStream BS.ByteString where
80 toMediaType _mt = BSL.fromStrict
81 instance ToMediaType OctetStream BSL.ByteString where
83 {- TODO: uncomment when Web is imported
84 instance ToMediaType FormUrlEncoded a where
85 toMediaType _mt = Web.urlEncodeAsForm