Skip to content

Commit

Permalink
Merge pull request #13 from OpenCageData/set-lwp-user-agent
Browse files Browse the repository at this point in the history
Support LWP user agents again
  • Loading branch information
freyfogle committed Jun 21, 2021
2 parents 06afa1a + 38c5158 commit 899219a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Pod::Perldoc = >= 3.21 ;
URI = 0

[Prereqs / TestRequires]
LWP::UserAgent = 6.55
Net::Ping = 2.73
Test::Exception = 0.32
Test::More = 0.92
Expand Down
18 changes: 15 additions & 3 deletions lib/Geo/Coder/OpenCage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,25 @@ sub geocode {
my $response = $self->{ua}->get($URL);

if (!$response) {
warn "failed to fetch '$URL': ", $response->{reason};
my $reason = (ref($response) eq 'HTTP::Response')
? $response->status_line() # <code> <message>
: $response->{reason};
warn "failed to fetch '$URL': ", $reason;
return undef;
}

my $rh_content = $self->{json}->decode($response->{content});
# Support HTTP::Tiny and LWP:: CPAN packages
my $content = (ref($response) eq 'HTTP::Response')
? $response->decoded_content()
: $response->{content};
my $is_success = (ref($response) eq 'HTTP::Response')
? $response->is_success()
: $response->{success};

if (!$response->{success}) {
my $rh_content = $self->{json}->decode($content);


if (!$is_success) {
warn "response when requesting '$URL': " . $rh_content->{status}{code} . ', ' . $rh_content->{status}{message};
return undef;
}
Expand Down
40 changes: 40 additions & 0 deletions t/10-user-agent.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use strict;
use warnings;
use utf8;
use Net::Ping;
use Test::More;
use Test::Warn;
use LWP::UserAgent;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

binmode Test::More->builder->output, ":encoding(utf8)";
binmode Test::More->builder->failure_output, ":encoding(utf8)";
binmode Test::More->builder->todo_output, ":encoding(utf8)";

use lib './lib'; # actually use the module, not other versions installed
use Geo::Coder::OpenCage;

# TODO should move this into module to share with other tests
my $api_ip_num = '95.216.176.62';
my $p = Net::Ping->new;
my $have_connection = 0;
if ($p->ping($api_ip_num, 1)) {
$have_connection = 1;
}

SKIP: {
skip 'skipping test that requires connectivity', 2 unless ($have_connection);

my $user_agent = LWP::UserAgent->new();

# use special key OpenCage makes available for testing
# https://opencagedata.com/api#testingkeys
my $api_key = '6d0e711d72d74daeb2b0bfd2a5cdfdba';

my $geocoder = Geo::Coder::OpenCage->new(api_key => $api_key, ua => $user_agent);
my $result = $geocoder->reverse_geocode('lat' => 1, 'lng' => 2);
is($result->{status}->{code}, 200, 'got http 200 status');
}

done_testing();

0 comments on commit 899219a

Please sign in to comment.