|
| 1 | +let |
| 2 | + /* to make refreshing easier: |
| 3 | + - the first argument to Web.Contents should end at the domain. Nothing more. |
| 4 | + - making it be a "report parameter" instead of a basic variable allows variables but still be a refreshable query |
| 5 | + - Everything else goes into relativePath, |
| 6 | + - You can use query to build a query string from a record |
| 7 | + */ |
| 8 | + |
| 9 | + WebRequest = ( |
| 10 | + baseUrl as nullable text, // optional is implicitly 'nullable type' |
| 11 | + optional relativePath as text, |
| 12 | + optional query as record |
| 13 | + ) => let |
| 14 | + baseUrl = baseUrl ?? "https://httpbin.org", |
| 15 | + options = [ |
| 16 | + RelativePath = relativePath ?? null, |
| 17 | + Query = query ?? [], |
| 18 | + |
| 19 | + // this allows you to catch HTTP Status errors. ( except for 401 and 403 which are Auth ) |
| 20 | + ManualStatusHandling = { 400, 404, 427, 500 } |
| 21 | + ], |
| 22 | + bytes = Web.Contents( baseUrl, options ), |
| 23 | + Summary = [ |
| 24 | + bytes = bytes, |
| 25 | + Response.Status = Meta[Response.Status], |
| 26 | + FullRequestUrl = Meta[Content.Uri](), |
| 27 | + Json = Json.Document( bytes, TextEncoding.Utf8 ), |
| 28 | + IsJson = not ( try Json )[HasError], |
| 29 | + AsText = Text.FromBinary( bytes, TextEncoding.Utf8 ), |
| 30 | + Meta = Value.Metadata( bytes ) |
| 31 | + ] |
| 32 | + in |
| 33 | + Summary, |
| 34 | + |
| 35 | + tests = [ |
| 36 | + |
| 37 | + expectJson = WebRequest( "https://httpbin.org", "json" ), |
| 38 | + expectJson2 = WebRequest( "https://httpbin.org", "anything", [ |
| 39 | + Text = "Hi world", |
| 40 | + language = "en-US", |
| 41 | + pageNumber = Number.ToText( 9 ) |
| 42 | + ] ), |
| 43 | + expectErrorCodes = WebRequest( "https://httpbin.org", "/status/500" ) |
| 44 | + ], |
| 45 | + TestSummary = Table.FromRecords( Record.FieldValues( tests ) ), |
| 46 | + SummaryWithTypes = Table.TransformColumnTypes( TestSummary, |
| 47 | + { |
| 48 | + {"Response.Status", Int64.Type}, |
| 49 | + {"FullRequestUrl", type text}, |
| 50 | + {"IsJson", type logical}, |
| 51 | + {"AsText", type text} |
| 52 | + }, |
| 53 | + "en-us" ) |
| 54 | + |
| 55 | + |
| 56 | +in |
| 57 | + SummaryWithTypes |
0 commit comments