Skip to content

Commit

Permalink
documentation fixes, switch to LWP
Browse files Browse the repository at this point in the history
  • Loading branch information
willamowius committed Feb 14, 2024
1 parent e216f70 commit 5aa8167
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Revision history for Perl extension Net::EANSearch.

1.02 Wed Feb 14 14:06:00 2024
- switch internally to LWP for broader plattform support
- documentation fixes

1.01 Wed Feb 14 00:15:58 2024
- declare dependecies

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use Net::EANSearch;

my $API_TOKEN = $ENV{EAN_SEARCH_API_TOKEN};

my $eansearch = EANSearch->new($API_TOKEN);
my $eansearch = Net::EANSearch->new($API_TOKEN);

my $ean = '5099750442227';
my $isbn = '1119578884';
Expand All @@ -66,15 +66,15 @@ if (!defined($book)) {
}

my @product_list;
@product_list = $eansearch->barcodePrefixSearch('885909', $EANSearch::ENGLISH);
@product_list = $eansearch->barcodePrefixSearch('885909', $Net::EANSearch::ENGLISH);
foreach my $p (@product_list) {
print "EAN $p->{ean} is $p->{name}\n";
}

my $page = 0;
do {
$page++;
@product_list = $eansearch->productSearch('Bananaboat', $EANSearch::ALL_LANGUAGES, $page);
@product_list = $eansearch->productSearch('Bananaboat', $Net::EANSearch::ALL_LANGUAGES, $page);

foreach my $p (@product_list) {
print "EAN $p->{ean} is $p->{name}\n";
Expand Down
26 changes: 10 additions & 16 deletions lib/Net/EANSearch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use 5.030000;
use strict;
use warnings;

use WWW::Curl::Easy;
use LWP;
use JSON;
use URL::Encode;
use MIME::Base64 qw(decode_base64);
Expand All @@ -30,7 +30,7 @@ our @EXPORT = qw(
);

our $VERSION = '1.01';
our $VERSION = '1.02';

our $ALL_LANGUAGES = 99;
our $ENGLISH = 1;
Expand All @@ -53,19 +53,16 @@ our $ROMAINAN = 19;
our $BULGARIAN = 20;
our $GREEK = 21;

my $BASE_URI = 'https://api.ean-search.org/api?token=';
my $BASE_URI = 'https://api.ean-search.org/api?format=json&token=';

sub new {
my $class = shift;
my $token = shift;

my $curl = WWW::Curl::Easy->new;
my @myheaders = ('Accept: application/json');
$curl->setopt(CURLOPT_HTTPHEADER, \@myheaders);
$curl->setopt(CURLOPT_ACCEPT_ENCODING, 'gzip, deflate');
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); # TODO set better cert store instead ?
my $ua = LWP::UserAgent->new();
$ua->timeout(10);

my $self = bless { base_uri => $BASE_URI . $token, curl => $curl }, $class;
my $self = bless { base_uri => $BASE_URI . $token, ua => $ua }, $class;

return $self;
}
Expand Down Expand Up @@ -159,15 +156,12 @@ sub _apiCall {
my $self = shift;
my $url = shift;

$self->{curl}->setopt(CURLOPT_URL, $url);
my $result;
$self->{curl}->setopt(CURLOPT_WRITEDATA, \$result);
my $retcode = $self->{curl}->perform;
if (!defined($result) || $retcode != 0) {
print STDERR 'Network error: ' . $self->{curl}->strerror($retcode) . "\n";
my $doc = $self->{ua}->request(HTTP::Request->new(GET => $url));
if (!defined($doc) || $doc->is_error()) {
print STDERR 'Network error: ' . (defined($doc) ? $doc->code : 'unknown') . "\n";
return undef;
} else {
return $result;
return $doc->content;
}
}

Expand Down

0 comments on commit 5aa8167

Please sign in to comment.