0 | module HTTP.API.Client.Request
 1 |
 2 | import HTTP.API
 3 | import Web.Internal.Types
 4 |
 5 | %hide Types.Headers
 6 | %default total
 7 |
 8 | ||| Part of a `FormData` object
 9 | public export
10 | data FDPart : Type where
11 |   FDBlob   : Blob -> FDPart
12 |   FDBytes  : ByteString -> FDPart
13 |   FDFile   : File -> FDPart
14 |   FDString : String -> FDPart
15 |
16 | public export
17 | data RequestBody : Type where
18 |   None  : RequestBody
19 |   Bytes : MediaType -> ByteString -> RequestBody
20 |   Str   : MediaType -> String -> RequestBody
21 |   FD    : List (String,FDPart) -> RequestBody
22 |
23 | public export
24 | record HTTPRequest where
25 |   constructor R
26 |   method  : Method
27 |   uri     : URI
28 |   headers : Headers
29 |   body    : RequestBody
30 |
31 | export %inline
32 | adjURI : (URI -> URI) -> HTTPRequest -> HTTPRequest
33 | adjURI f = {uri $= f}
34 |
35 | export
36 | emptyRequest : HTTPRequest
37 | emptyRequest =
38 |   R GET (MkURI Nothing Nothing False [] [] Nothing) emptyHeaders None
39 |
40 | --------------------------------------------------------------------------------
41 | -- Interface
42 | --------------------------------------------------------------------------------
43 |
44 | public export
45 | interface RequestEncode (0 from,to : Type) where
46 |   reqEncodeAs  : from -> to
47 |   toBody       : to -> RequestBody
48 |
49 | export %inline
50 | (e : EncodeVia f t) => RequestEncode f t where
51 |   reqEncodeAs  = encodeAs
52 |   toBody       = Bytes (mediaType @{e}) . fastConcat . toBytes @{e}
53 |