1 | module Stellar.HTTP.Types
5 | import public Data.Buffer
8 | import public TyTTP.URL
10 | import public System.Path
14 | import public JSON.Simple.Derive
22 | WFURL = URL String System.Path.Path (List (String, String))
25 | allowsBody : Method -> Bool
26 | allowsBody GET = False
27 | allowsBody HEAD = False
28 | allowsBody DELETE = False
29 | allowsBody CONNECT = False
35 | DepBody : Method -> Type -> Type
36 | DepBody m = if allowsBody m then id else const Unit
41 | record RichRequest (bodyType : Type) where
42 | constructor MkRichReq
46 | headers : List (String, String)
47 | body : DepBody method bodyType
50 | (.getBody') : RichRequest b -> Maybe b
51 | (.getBody') (MkRichReq method url version headers body) with (allowsBody method )
52 | (.getBody') (MkRichReq method url version headers body) | True = Just body
53 | (.getBody') (MkRichReq method url version headers body) | False = Nothing
57 | MkRichRequest : {0 body : Type} -> (m : Method) -> WFURL -> Version -> List (String, String) -> DepBody m body -> RichRequest body
58 | MkRichRequest = MkRichReq
66 | interface HasHeaders (0 t : Type) where
67 | (.headers) : t -> List (String, String)
71 | interface HasVersion (0 t : Type) where
72 | (.version) : t -> Version
76 | interface HasMethod (0 t : Type) where
77 | (.method) : t -> Method
81 | interface HasBody (0 r : Type) (0 t : Type) | r where
86 | interface HasURL (0 t : Type) where
95 | HasVersion (Request m u Version h b) where
96 | (.version) = Request.version
100 | HasMethod (Request Method u v h b) where
101 | (.method) = Request.method
105 | HasHeaders (Request m u v (List (String, String)) b) where
106 | (.headers) = Request.headers
110 | HasHeaders (Response v (List (String, String)) b) where
111 | (.headers) = Response.headers
115 | HasBody (Response v h b) b where
116 | (.body) = Response.body
120 | HasBody (Request m u v h b) b where
121 | (.body) = Request.body
124 | HasURL (Request m WFURL v h b) where
125 | (.url) = Request.url
129 | HasHeaders (RichRequest t) where
130 | (.headers) = RichRequest.headers
134 | HasVersion (RichRequest t) where
135 | (.version) = RichRequest.version
139 | HasMethod (RichRequest t) where
140 | (.method) = RichRequest.method
143 | HasURL (RichRequest t) where
144 | (.url) = RichRequest.url
146 | %hide RichRequest.headers
147 | %hide RichRequest.(.headers)
148 | %hide RichRequest.method
149 | %hide RichRequest.(.method)
150 | %hide RichRequest.version
151 | %hide RichRequest.(.version)
152 | %hide RichRequest.url
153 | %hide RichRequest.(.url)
154 | %hide Request.(.headers)
155 | %hide Request.headers
156 | %hide Request.(.version)
157 | %hide Request.version
158 | %hide Request.(.method)
159 | %hide Request.method
161 | %hide Request.(.url)
169 | anyGet : (r : RichRequest t) -> (noBody : allowsBody r.method === False) => RichRequest s
170 | anyGet (MkRichReq m url version headers body) = MkRichReq m url version headers (rewrite noBody in ())
174 | (.getBody) : (r : RichRequest t) -> (hasBody : allowsBody r.method === True) => t
175 | (.getBody) (MkRichReq method url version headers body) with (allowsBody method ) proof p
176 | (.getBody) (MkRichReq method url version headers body) | True = body
177 | (.getBody) (MkRichReq method url version headers body) | False = absurd hasBody
185 | GenericHttpResponse : Type -> Type
186 | GenericHttpResponse = Response Status (List (String, String))
189 | PlainRequest : Type
190 | PlainRequest = HttpRequest String (List (String, String)) Buffer
193 | PlainResponse : Type
194 | PlainResponse = GenericHttpResponse Buffer
198 | JSONRequest = RichRequest JSON
203 | JSONResponse : Type
204 | JSONResponse = GenericHttpResponse (Maybe JSON)
209 | Ok = MkResponse OK [] emptyBuffer
212 | Bad : PlainResponse
213 | Bad = MkResponse BAD_REQUEST [] emptyBuffer
216 | BadJSON : JSONResponse
217 | BadJSON = MkResponse BAD_REQUEST [] Nothing
222 | PlainHTTP = PlainRequest :- PlainResponse
227 | RichHTTP = RichRequest Buffer :- PlainResponse
232 | JSONHTTP = JSONRequest :- JSONResponse
235 | URLHttpRequest : Type -> Type
236 | URLHttpRequest = HttpRequest WFURL (List (String, String))
244 | interface HTTPDebug (0 t : Type) where
245 | renderDebug : t -> String
250 | HTTPDebug Buffer where
251 | renderDebug = bufferToStringUnsafe
255 | HTTPDebug WFURL where
256 | renderDebug (MkURL host auth path params) = "\{show host}://\{show auth}\{show path}?\{show params}"
258 | printHeaders : List (String, String) -> String
259 | printHeaders headers = unlines (map (\(x, y) => "\{x}: \{y}") headers)
263 | HTTPDebug (List (String, String)) where
264 | renderDebug = printHeaders
267 | HTTPDebug JSON where
272 | HTTPDebug Status where
273 | renderDebug = show . (.code)
277 | HTTPDebug String where
282 | HTTPDebug a => HTTPDebug b => HTTPDebug h => Show (HttpRequest a h b) where
283 | show (MkRequest method url version headers body) =
285 | \{show method} \{renderDebug url} \{show version}
287 | \{renderDebug headers}
289 | \{renderDebug body}
294 | HTTPDebug a => Show (GenericHttpResponse a) where
295 | show (MkResponse status headers body) =
297 | \{show status.code}
299 | \{printHeaders headers}
301 | \{renderDebug body}