API-Octopart
view release on metacpan or search on metacpan
Revision history for API-Octopart
1.003 2023-01-14T12:14:57-08:00
- Set minimum perl version to 5.10
1.002 2023-01-11T16:44:52-08:00
- Added new options: query_limit, json_debug
- Added 3x auto-retry if the request fails
- Added constructor option validation
- Fixed Changes file formatting
1.001 2022-08-05T17:36:28-07:00
- First version, released on an unsuspecting world.
# NAME
API::Octopart - Simple inteface for querying part status across vendors at octopart.com.
# SYNOPSIS
my $o = API::Octopart->new(
token => 'abcdefg-your-octopart-token-here',
cache => "$ENV{HOME}/.octopart/cache",
include_specs => 1,
ua_debug => 1,
query_limit => 10
);
# Query part stock:
my %opts = (
currency => 'USD',
max_moq => 100,
min_qty => 10,
max_price => 4,
#mfg => 'Murata',
- cache\_age => 3
The cache age (in days) before re-querying octopart. Defaults to 30 days.
- query\_limit: die if too many API requests are made.
Defaults to no limit. I exhasted 20,000 queries very quickly due to a bug!
This might help with that, set to a reasonable limit while testing.
- ua\_debug => 1
User Agent debugging. This is very verbose and provides API communication details.
- json\_debug => 1
JSON response debugging. This is very verbose and dumps the Octopart response
in JSON.
- $o->has\_stock($part, %opts) - Returns the number of items in stock
$part: The model number of the part
%opts: Optional filters. No defaults are specified, it will return all unless limited.
- min\_qty => <n> - Minimum stock quantity, per seller.
lib/API/Octopart.pm view on Meta::CPAN
=head1 NAME
API::Octopart - Simple inteface for querying part status across vendors at octopart.com.
=head1 SYNOPSIS
my $o = API::Octopart->new(
token => 'abcdefg-your-octopart-token-here',
cache => "$ENV{HOME}/.octopart/cache",
include_specs => 1,
ua_debug => 1,
query_limit => 10
);
# Query part stock:
my %opts = (
currency => 'USD',
max_moq => 100,
min_qty => 10,
max_price => 4,
#mfg => 'Murata',
lib/API/Octopart.pm view on Meta::CPAN
=item * cache_age => 3
The cache age (in days) before re-querying octopart. Defaults to 30 days.
=item * query_limit: die if too many API requests are made.
Defaults to no limit. I exhasted 20,000 queries very quickly due to a bug!
This might help with that, set to a reasonable limit while testing.
=item * ua_debug => 1
User Agent debugging. This is very verbose and provides API communication details.
=item * json_debug => 1
JSON response debugging. This is very verbose and dumps the Octopart response
in JSON.
=back
=cut
our %valid_opts = map { $_ => 1 } qw/token include_specs cache cache_age ua_debug query_limit json_debug/;
sub new
{
my ($class, %args) = @_;
foreach my $arg (keys %args)
{
die "invalid option: $arg => $args{$arg}" if !$valid_opts{$arg};
}
$args{api_queries} = 0;
lib/API/Octopart.pm view on Meta::CPAN
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);
}
lib/API/Octopart.pm view on Meta::CPAN
$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;
}
lib/API/Octopart.pm view on Meta::CPAN
else
{
my %errors;
foreach my $e (@{ $j->{errors} })
{
$errors{$e->{message}}++;
}
die "Octopart: " . join("\n", keys(%errors)) . "\n";
}
if ($self->{json_debug})
{
if ($hashfile)
{
my $age_days = (-M $hashfile);
print STDERR "======= cache: $hashfile (age=$age_days days) =====\n"
}
print STDERR Dumper $j;
}
return $j;
( run in 1.419 second using v1.01-cache-2.11-cpan-49f99fa48dc )