Created
April 30, 2015 13:08
-
-
Save CurtHagenlocher/68ac18caa0a17667c805 to your computer and use it in GitHub Desktop.
Demonstrates the use of a custom retry duration with Web.Contents.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
Value.WaitFor = (producer as function, interval as function, optional count as number) as any => | |
let | |
list = List.Generate( | |
() => {0, null}, | |
(state) => state{0} <> null and (count = null or state{0} < count), | |
(state) => if state{1} <> null | |
then {null, state{1}} | |
else {1 + state{0}, Function.InvokeAfter(() => producer(state{0}), interval(state{0}))}, | |
(state) => state{1}) | |
in | |
List.Last(list), | |
Web.ContentsCustomRetry = (url as text, optional options as record) => Value.WaitFor( | |
(i) => | |
let | |
options2 = if options = null then [] else options, | |
options3 = if i=0 then options2 else options2 & [IsRetry=true], | |
result = Web.Contents(url, options3 & [ManualStatusHandling={429}]), | |
buffered = Binary.Buffer(result), /* avoid risk of double request */ | |
status = if buffered = null then 0 else Value.Metadata(result)[Response.Status], | |
actualResult = if status = 429 then null else buffered | |
in | |
actualResult, | |
(i) => #duration(0, 0, 0, i*0.1)) | |
in | |
Web.ContentsCustomRetry("http://www.bing.com") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @CurtHagenlocher
I'm facing the same problem, I use your function to refresh manually my data and adapt to y script but now I want to refresh my Power BI in the Power BI service online but I didn't succeed, the service says that I'm using dynamic web sources so that it can't be refresh by PowerBI service. I tried to us the RelativePath function but it didn't succeed:
`let
Source = (string1 as text) =>
let
//Function to wait
Value.WaitFor = (producer as function, interval as function, optional count as number) as any =>
let
list = List.Generate(
() => {0, null},
(state) => state{0} <> null and (count = null or state{0} < count),
(state) => if state{1} <> null
then {null, state{1}}
else {1 + state{0}, Function.InvokeAfter(() => producer(state{0}), interval(state{0}))},
(state) => state{1})
in
List.Last(list),
//Function to Call API
Web.ContentsCustomRetry = (url as text, optional options as record) => Value.WaitFor(
(i) =>
let
options2 = if options = null then [] else options,
options3 = if i=0 then options2 else options2 & [IsRetry=true],
result = Web.Contents(url, options3 & [ManualStatusHandling={429}]),
buffered = Binary.Buffer(result), /* avoid risk of double request /
status = if buffered = null then 0 else Value.Metadata(result)[Response.Status],
actualResult = if status = 429 then null else buffered
in
actualResult,
(i) => #duration(0, 0, 0, i0.1)),
//Actual Execution Starts Here
url = "https://sandbox-oauth.piste.gouv.fr",
url2="api/oauth/token",
body = [
client_id = "xxxxxxxx",
client_secret = "xxxxxxxx",
audience = "openid",
grant_type = "client_credentials"
],
GetJson = Json.Document(
Web.Contents(
url,
[RelativePath= url2, Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(Uri.BuildQueryString(body))]
)
),
AccessTokenHeader = GetJson[token_type] & " " & GetJson[access_token],
corps = Json.FromValue([cid= string1]),
myRaw = Web.ContentsCustomRetry(
"https://sandbox-api.piste.gouv.fr", [RelativePath = "dila/legifrance-beta/lf-engine-app/consult/getSectionByCid", Headers=[Authorization=AccessTokenHeader,#"Content-Type"="application/json"], Content=corps]),
//actualMeta = Text.From(GetMetadata[Response.Status]),
//Final function to create response string
GetConsloidatedTargets = () =>
let
FinalContent = Json.Document(myRaw)
in
FinalContent,
finaldata = GetConsloidatedTargets()
in
finaldata
in
Source`
If you can maybe find a solution to my problem it will be very helpful.
Thank you per advance