diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-21 23:07:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-21 23:07:11 +0100 |
| commit | 79f684845db0e84f0876a147d2590e2bcfcdd89a (patch) | |
| tree | e160623856263bd1054f54e5962d93e1b2c45464 | |
| parent | d62b85003dc0bf26d579a93fd0230b74d18f35cf (diff) | |
| parent | 85e978869cd283a0e177baabe5305affbc6c99dd (diff) | |
| download | perlweeklychallenge-club-79f684845db0e84f0876a147d2590e2bcfcdd89a.tar.gz perlweeklychallenge-club-79f684845db0e84f0876a147d2590e2bcfcdd89a.tar.bz2 perlweeklychallenge-club-79f684845db0e84f0876a147d2590e2bcfcdd89a.zip | |
Merge pull request #409 from jmaslak/joelle-17-3-2
Perl 5 solution to 17.3
| -rwxr-xr-x | challenge-017/joelle-maslak/perl5/ch-3.pl | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/challenge-017/joelle-maslak/perl5/ch-3.pl b/challenge-017/joelle-maslak/perl5/ch-3.pl new file mode 100755 index 0000000000..3b2f6d643c --- /dev/null +++ b/challenge-017/joelle-maslak/perl5/ch-3.pl @@ -0,0 +1,95 @@ +#!/usr/bin/env perl +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; +use Getopt::Long; +use Mojo::UserAgent; +use Path::Tiny; +use Perl6::Slurp; + +MAIN: { + binmode STDOUT, ':utf8'; + + my $configfile = path( $ENV{HOME} // "", ".bhagavad-gita" )->stringify(); + my $baseurl = 'https://bhagavadgita.io/'; + GetOptions( + "base-url=s" => \$baseurl, + "config-file=s" => \$configfile, + ); + die "Must provide chapter and verse" if @ARGV < 2; + my $chapter = 0+shift(@ARGV); + my $verse = 0+shift(@ARGV); + + if ( @ARGV != 0 ) { die("Unknown parameter"); } + + my $id = get_client_id($configfile); + my $secret = get_client_secret($configfile); + my $token = get_token($id, $secret, $baseurl); + say get_verse($token, $chapter, $verse, $baseurl); +} + +sub get_client_id($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of two lines" if @lines != 2; + chomp $lines[0]; + return $lines[0]; +} + +sub get_client_secret($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of two lines" if @lines != 2; + chomp $lines[1]; + return $lines[1]; +} + +sub get_token($id, $secret, $baseurl) { + my $ua = Mojo::UserAgent->new(); + $ua->max_redirects(16); + + my $tx = $ua->post( + "${baseurl}auth/oauth/token", + form => { + client_id => $id, + client_secret => $secret, + grant_type => 'client_credentials', + scope => 'verse chapter', + }, + ); + + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + my $json = $tx->result->json; + return $json->{access_token}; + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{error}; +} + +sub get_verse($token, $chapter, $verse, $baseurl) { + my $ua = Mojo::UserAgent->new(); + $ua->once( + start => sub ( $ua, $tx ) { + $tx->req->headers->header( 'Authorization' => "Bearer " . $token); + } + ); + $ua->max_redirects(16); + + my $tx = $ua->get( + "${baseurl}api/v1/chapters/$chapter/verses/$verse", + ); + + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + my $json = $tx->result->json; + return $json->{text}; + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{message}; +} + |
