0 | module HTTP.API.Encode
2 | import Data.List.Quantifiers as L
4 | import public Data.ByteString
5 | import public HTTP.Header.Types
16 | interface Encode (0 a : Type) where
17 | encode : a -> ByteString
21 | interface EncodeMany (0 a : Type) where
22 | encodeMany : a -> List ByteString
25 | Encode a => EncodeMany a where
26 | encodeMany v = [encode v]
29 | {auto all : L.All.All (EncodeMany . f) ts}
33 | encodeHL @{_ :: _} (x::xs) = encodeMany x ++ encodeHL xs
36 | L.All.All (EncodeMany . f) ts => EncodeMany (L.All.All f ts) where
37 | encodeMany = encodeHL
39 | encodeList : EncodeMany t => List t -> List ByteString
41 | encodeList (x::xs) = encodeMany x ++ encodeList xs
44 | EncodeMany a => EncodeMany (Vect n a) where
45 | encodeMany = encodeList . toList
48 | EncodeMany a => EncodeMany (List a) where
49 | encodeMany = encodeList
52 | EncodeMany a => EncodeMany (SnocList a) where
53 | encodeMany = encodeList . (<>> [])
60 | applicationOctettStream : MediaType
61 | applicationOctettStream = MT "application" "octett-stream"
64 | applicationJSON : MediaType
65 | applicationJSON = MT "application" "json"
68 | textPlain : MediaType
69 | textPlain = MT "text" "plain"
72 | multipartFormData : MediaType
73 | multipartFormData = MT "multipart" "form-data"
85 | OctettList = List ByteString
88 | interface EncodeVia (0 from, to : Type) where
89 | encodeAs : from -> to
90 | toBytes : to -> List ByteString
91 | mediaType : MediaType
94 | encodeVia : (v : f) -> EncodeVia f t -> List ByteString
95 | encodeVia v c = toBytes @{c} $
encodeAs @{c} v
98 | Interpolation a => EncodeVia a String where
99 | encodeAs = interpolate
100 | toBytes = pure . fromString
101 | mediaType = textPlain
104 | Cast a ByteString => EncodeVia a ByteString where
107 | mediaType = applicationOctettStream
110 | Cast a (List ByteString) => EncodeVia a (List ByteString) where
113 | mediaType = applicationOctettStream
116 | ToJSON a => EncodeVia a JSON where
118 | toBytes = pure . fromString . show
119 | mediaType = applicationJSON
126 | Encode ByteString where encode = id
129 | Encode String where encode = fromString
133 | encode = encode . show
136 | Encode Integer where
137 | encode = encode. show
141 | encode = encode. show
144 | Encode Bits16 where
145 | encode = encode. show
148 | Encode Bits32 where
149 | encode = encode. show
152 | Encode Bits64 where
153 | encode = encode. show
157 | encode = encode. show
161 | encode = encode. show
165 | encode = encode. show
169 | encode = encode. show
172 | Encode Double where
173 | encode = encode. show