From ab6aecbbe767d193b05ca04c083d2a3b3b268cf0 Mon Sep 17 00:00:00 2001 From: Randy Lauen Date: Sat, 17 Aug 2019 18:50:53 -0500 Subject: solution for task 3 --- challenge-021/randy-lauen/perl6/ch-3.p6 | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 challenge-021/randy-lauen/perl6/ch-3.p6 diff --git a/challenge-021/randy-lauen/perl6/ch-3.p6 b/challenge-021/randy-lauen/perl6/ch-3.p6 new file mode 100644 index 0000000000..ca51f8a140 --- /dev/null +++ b/challenge-021/randy-lauen/perl6/ch-3.p6 @@ -0,0 +1,61 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: Write a script to use BC Gov News API. + +This script prints out the top and feature post headlines. + +Example output: + + $ perl6 ch-3.p6 + topPostKey + * News Release: Third urgent and primary care centre opens in Greater Vancouver + * Backgrounder: North Vancouver UPCC + + featurePostKey + * News Release: Province seeks feedback to ban, reduce, recycle more plastics + +=end SYNOPSIS + +use Cro::HTTP::Client; +use URI::Encode; + +class ApiClient { + has %.params; + has $.base-uri; + has $!client = Cro::HTTP::Client.new( + :http('1.1'), + :base-uri( $!base-uri ) + :headers( [ Accept => 'application/json' ] ), + ); + + method get( $path ) { + return self!get-json( "$path?" ~ %!params.pairs.map( { .key ~ '=' ~ .value } ).join('&') ); + } + + method !get-json( $uri ) { + my $encoded = uri_encode( $uri ); + my $response = await $!client.get( $encoded ); + return await $response.body; + } +} + + +sub MAIN() { + my $client = ApiClient.new( + base-uri => 'https://news.api.gov.bc.ca', + params => %( :api-version('1.0') ), + ); + my %homeInfo = $client.get( '/api/Home' ); + + for -> $key { + my %postInfo = $client.get( "/api/Posts/%homeInfo{$key}" ); + say $key; + + for %postInfo.flat -> $doc { + say "* $doc: $doc"; + } + say ''; + } +} -- cgit