From 44866277df96a5f0ec320581a0cef3d21c3a39cb Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 21 Jul 2019 15:24:05 -0400 Subject: adding code for optional API challenge --- challenge-017/adam-russell/perl5/ch-3.pl | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-017/adam-russell/perl5/ch-3.pl diff --git a/challenge-017/adam-russell/perl5/ch-3.pl b/challenge-017/adam-russell/perl5/ch-3.pl new file mode 100644 index 0000000000..8cddfa5629 --- /dev/null +++ b/challenge-017/adam-russell/perl5/ch-3.pl @@ -0,0 +1,43 @@ +use strict; +use warnings; +## +# Write a script to use Bhagavad Gita API. +## +use JSON; +use REST::Client; +use constant ACCESS_TOKEN_URL => "https://bhagavadgita.io/auth/oauth/token"; +use constant API_URL => "https://bhagavadgita.io/api/v1/chapters/CHAPTER/verses/VERSE?access_token=ACCESS_TOKEN"; + +sub get_access_token{ + my($client) = @_; + my $body = "client_id=&client_secret=&grant_type=client_credentials&scope=verse%20chapter"; + $$client->addHeader("accept", "application/json"); + $$client->addHeader("content-type", "application/x-www-form-urlencoded"); + $$client->POST(ACCESS_TOKEN_URL, $body); + my $json = $$client->responseContent(); + $json = decode_json($json); + return $json->{access_token}; +} + +sub get_sloka{ + my($client, $chapter, $verse, $access_token) = @_; + my $url = API_URL; + $url =~ s/CHAPTER/$chapter/; + $url =~ s/VERSE/$verse/; + $url =~ s/ACCESS_TOKEN/$access_token/; + $$client->addHeader("accept", "application/json"); + $$client->addHeader("content-type", "application/x-www-form-urlencoded"); + $$client->GET($url); + my $json = $$client->responseContent(); + $json = decode_json($json); + return $json->{meaning}; +} + +MAIN:{ + my($chapter, $verse) = @ARGV[0 .. 1]; + my $client = new REST::Client; + my $access_token = get_access_token(\$client); + my $translation = get_sloka(\$client, $chapter, $verse, $access_token); + print "$translation\n"; +} + -- cgit