API-Octopart
view release on metacpan or search on metacpan
lib/API/Octopart.pm view on Meta::CPAN
=cut
sub get_part_stock_detail
{
my ($self, $part, %opts) = @_;
my $p = $self->query_part_detail($part);
return $self->_parse_part_stock($p, %opts);
}
=item * $o->octo_query($q) - Queries the Octopart API
Return the JSON response structure as a perl ARRAY/HASH given a query meeting Octopart's
API specification.
=cut
sub octo_query
{
my ($self, $q) = @_;
my $part = shift;
my ($content, $hashfile);
if ($self->{cache})
{
system('mkdir', '-p', $self->{cache}) if (! -d $self->{cache});
my $h = md5_hex($q);
$hashfile = "$self->{cache}/$h.query";
# Load the cached version if older than cache_age days.
my $age_days = (-M $hashfile);
if (-e $hashfile && $age_days < $self->{cache_age})
{
if ($self->{ua_debug})
{
print STDERR "Reading from cache file (age=$age_days days): $hashfile\n";
}
if (open(my $in, $hashfile))
{
local $/;
$content = <$in>;
close($in);
}
else
{
die "$hashfile: $!";
}
}
}
if (!$content)
{
my $ua = LWP::UserAgent->new( agent => 'mdf-perl/1.0', keep_alive => 3);
$self->{api_queries} //= 0;
if ($self->{query_limit} && $self->{api_queries} >= $self->{query_limit})
{
die "query limit exceeded: $self->{api_queries} >= $self->{query_limit}";
}
$self->{api_queries}++;
if ($self->{ua_debug})
{
$ua->add_handler(
"request_send",
sub {
my $msg = shift; # HTTP::Request
print STDERR "SEND >> \n"
. $msg->headers->as_string . "\n"
. "\n";
return;
}
);
$ua->add_handler(
"response_done",
sub {
my $msg = shift; # HTTP::Response
print STDERR "RECV << \n"
. $msg->headers->as_string . "\n"
. $msg->status_line . "\n"
. "\n";
return;
}
);
}
my $req;
my $response;
my $tries = 0;
while ($tries < 3)
{
$req = HTTP::Request->new('POST' => 'https://octopart.com/api/v4/endpoint',
HTTP::Headers->new(
'Host' => 'octopart.com',
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip, deflate',
'token' => $self->{token},
'DNT' => 1,
'Origin' => 'https://octopart.com',
),
encode_json( { query => $q }));
$response = $ua->request($req);
if (!$response->is_success)
{
$tries++;
print STDERR "query error, retry $tries. "
( run in 1.093 second using v1.01-cache-2.11-cpan-39bf76dae61 )