3 ms·
Very late to the thread, but I was wondering if you knew of a good example of Raku calling an API over https, polling the API until it returns a specific value?
by bloopernova 2y ago
Very late to the thread, but I was wondering if you knew of a good example of Raku calling an API over https, polling the API until it returns a specific value?
- antononcube 2y agoHere is one way: use HTTP::UserAgent; use JSON::Fast; my $url = 'https://api.coindesk.com/v1/bpi/currentprice.json'; my $ua = HTTP::UserAgent.new; my $total-time = 0; loop { my $response = $ua.get($url); if $response.is-success { my $data = from-json $response.content; my $rate = $data<chartName>; say "Current chart name: $rate"; #last if $rate eq 'Bitcoin'; last if $total-time ≥ 16; } else { say "Failed to fetch data: {$response.status-line}"; } sleep 3; # Poll every 3 seconds $total-time += 3; } (Tweak / uncomment / rename the $rate variable assignments and checks.)
- bloopernova 2y agoWow, huge thanks, that's super helpful :)
- antononcube 2y agoSure, good luck!