-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebRequest.pq
39 lines (34 loc) · 1.42 KB
/
WebRequest.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let
/*
Yet another web request implementation
*/
WebRequest = (
urlBase, // text or uri
config as record,
optional options as nullable record
) as record =>
let return = [
defaults = [ Buffer = false ],
options = Record.Combine({ defaults, options ?? [] }),
bytes =
// this could be simplifeid with a shadowed ref
if options[Buffer]? ?? false
then Binary.Buffer( Web.Contents( urlBase, config ) )
else Web.Contents( urlBase, config ),
Data = if not IsJson then null else Json{0}?[Data]?,
AsText = Text.FromBinary( bytes, TextEncoding.Utf8 ),
Json = Json.Document( bytes, TextEncoding.Utf8 ),
IsJson = not (try Json)[HasError]?,
Meta = Value.Metadata( bytes ),
Response.Status = Meta[Response.Status],
Infer.ContentType = Binary.InferContentType( bytes ),
TotalBytes = Binary.ApproximateLength( bytes ),
Url = Meta[Content.Uri]()
], returnFields = {
"Response.Status",
"Url", "Infer.ContentType", "TotalBytes",
"Meta", "Data", "AsText", "Json", "IsJson",
"bytes" }
in
Record.SelectFields(return, returnFields, MissingField.Error),
in WebRequest