diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-08-11 12:43:04 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-08-11 12:43:04 -0600 |
| commit | 3a7aa55b8ea967c93c04c562227be9bf17afdd10 (patch) | |
| tree | 0e3c45e4fb13ecb2b7f6ff67af78843cb5122d56 | |
| parent | 4457dc04004a77929653f257800f8350f8bc3948 (diff) | |
| download | perlweeklychallenge-club-3a7aa55b8ea967c93c04c562227be9bf17afdd10.tar.gz perlweeklychallenge-club-3a7aa55b8ea967c93c04c562227be9bf17afdd10.tar.bz2 perlweeklychallenge-club-3a7aa55b8ea967c93c04c562227be9bf17afdd10.zip | |
Joelle's solutions to 20.3 in P6 & P5
| -rwxr-xr-x | challenge-020/joelle-maslak/perl5/ch-3.pl | 79 | ||||
| -rwxr-xr-x | challenge-020/joelle-maslak/perl6/ch-3.p6 | 88 |
2 files changed, 167 insertions, 0 deletions
diff --git a/challenge-020/joelle-maslak/perl5/ch-3.pl b/challenge-020/joelle-maslak/perl5/ch-3.pl new file mode 100755 index 0000000000..f22c234b26 --- /dev/null +++ b/challenge-020/joelle-maslak/perl5/ch-3.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use v5.26; +use strict; +use warnings; + +# Gets the image for an MB car based on VIN +# +# This uses the sandbox URL, pass in different base-url to use the +# production service. +# +# Download the front view of MB based on VIN (roof closed, daytime). + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; +use Getopt::Long; +use Mojo::UserAgent; +use Mojo::Util qw(url_escape); +use Path::Tiny; +use Perl6::Slurp; + +MAIN: { + my $configfile = path( $ENV{HOME} // "", ".mb" )->stringify(); + my $baseurl = 'https://api.mercedes-benz.com/tryout/vehicle_images/v1/'; + my $outfile = 'image.png'; + GetOptions( + "base-url=s" => \$baseurl, + "config-file=s" => \$configfile, + "output-file=s" => \$outfile, + ); + if ( @ARGV == 0 ) { die("Must provide VIN") } + my $vin = shift(@ARGV); + if ( @ARGV > 0 ) { die("Invalid parameter") } + + my $secret = get_client_secret($configfile); + my $imageids = get_image_ids( $secret, $vin, $baseurl ); + download_image($secret, $imageids->{EXT000}, $outfile, $baseurl); +} + +sub get_client_secret($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of one line" if @lines != 1; + chomp $lines[0]; + return $lines[0]; +} + +sub get_image_ids( $secret, $vin, $baseurl ) { + my $ua = Mojo::UserAgent->new(); + + my $tx = $ua->get( + "${baseurl}vehicles/" . url_escape($vin) . "?apikey=" . url_escape($secret), + ); + + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + return $tx->result->json; + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{errorMessage}; +} + + +sub download_image( $secret, $imageid, $outfile,$baseurl ) { + my $ua = Mojo::UserAgent->new(); + + my $tx = $ua->get( + "${baseurl}images/" . url_escape($imageid) . "?apikey=" . url_escape($secret), + ); + + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + return $tx->result->save_to($outfile); + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{errorMessage}; +} + diff --git a/challenge-020/joelle-maslak/perl6/ch-3.p6 b/challenge-020/joelle-maslak/perl6/ch-3.p6 new file mode 100755 index 0000000000..994afd3d63 --- /dev/null +++ b/challenge-020/joelle-maslak/perl6/ch-3.p6 @@ -0,0 +1,88 @@ +#!/usr/bin/env perl6 +use v6; + +# Gets the image for an MB car based on VIN +# +# This uses the sandbox URL, pass in a different base-url to use the +# production service. +# +# Download the front view of a MB based on VIN (roof closed, daytime). + +use Cro::HTTP::Client; +use URI::Encode; + +sub MAIN( + Str:D $vin, + Str:D :$config-file? = $*HOME.add(".mb").Str, + Str:D :$base-url = 'https://api.mercedes-benz.com/tryout/vehicle_images/v1/', + Str:D :$output-file = 'image.png', +) { + my $image-ids = get-image-ids(:$config-file, :$base-url, :$vin); + my $png = get-image(:$config-file, :$base-url, :image-id($image-ids<EXT000>), :$output-file); + + my $fh = open $output-file, :w, :bin; + $fh.write($png); + $fh.close; +} + +sub get-client-secret(Str:D $config-file -->Str:D) { + my @lines = $config-file.IO.lines().grep( *.chars > 0 ); + die "Config-file ($config-file) must consist of one line" if @lines.elems ≠ 1; + + return @lines[0]; +} + +sub get-image-ids( + Str:D :$vin, + Str:D :$base-url, + Str:D :$config-file, +) { + my $secret = get-client-secret($config-file); + + my $client = Cro::HTTP::Client.new( + base-uri => $base-url, + ); + + my $resp = await $client.get( + "vehicles/{ uri_encode_component($vin) }" ~ + "?apikey={ uri_encode_component($secret) }", + ); + my $json = await $resp.body; + return $json; + + CATCH { + when X::Cro::HTTP::Error { + my $body = await .response.body; + die "Error from API endpoint: $body<errorMessage>"; + } + } +} + +sub get-image( + Str:D :$image-id, + Str:D :$output-file, + Str:D :$base-url, + Str:D :$config-file, +) { + my $secret = get-client-secret($config-file); + + my $client = Cro::HTTP::Client.new( + base-uri => $base-url, + ); + + my $resp = await $client.get( + "images/{ uri_encode_component($image-id) }" ~ + "?apikey={ uri_encode_component($secret) }", + ); + my $img = await $resp.body; + return $img; + + CATCH { + when X::Cro::HTTP::Error { + my $body = await .response.body; + die "Error from API endpoint: $body<errorMessage>"; + } + } +} + + |
