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