Hacker News new | ask | show | jobs
by bloopernova 588 days 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?
1 comments

Here 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.)
Wow, huge thanks, that's super helpful :)
Sure, good luck!