API-Octopart
view release on metacpan or search on metacpan
);
# Query part stock:
my %opts = (
currency => 'USD',
max_moq => 100,
min_qty => 10,
max_price => 4,
#mfg => 'Murata',
);
print Dumper $o->get_part_stock_detail('RC0805FR-0710KL', %opts);
print Dumper $o->get_part_stock_detail('GQM1555C2DR90BB01D', %opts);
# METHODS
- $o = API::Octopart->new(%opts) - Returns new Octopart object.
Object Options (%opt):
- token => 'abcdefg-your-octopart-token-here',
This is your Octopart API token. You could do something like this to read the token from a file:
lib/API/Octopart.pm view on Meta::CPAN
);
# Query part stock:
my %opts = (
currency => 'USD',
max_moq => 100,
min_qty => 10,
max_price => 4,
#mfg => 'Murata',
);
print Dumper $o->get_part_stock_detail('RC0805FR-0710KL', %opts);
print Dumper $o->get_part_stock_detail('GQM1555C2DR90BB01D', %opts);
=head1 METHODS
=over 4
=item * $o = API::Octopart->new(%opts) - Returns new Octopart object.
Object Options (%opt):
=over 4
lib/API/Octopart.pm view on Meta::CPAN
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
{
lib/API/Octopart.pm view on Meta::CPAN
$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;
lib/API/Octopart.pm view on Meta::CPAN
'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. "
. $response->code . ": "
. $response->message . "\n";
sleep 2**$tries;
}
else
{
last;
}
}
lib/API/Octopart.pm view on Meta::CPAN
}
my $j = from_json($content);
if (!$j->{errors})
{
if ($hashfile)
{
open(my $out, ">", $hashfile) or die "$hashfile: $!";
print $out $content;
close($out);
}
}
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;
}
=item * $o->octo_query_count() - Return the number of API calls so far.
=cut
sub octo_query_count
perl -MJSON -le '
undef $/;
%d=eval(<STDIN>);
%i=do("./ignoredeps");
$j=JSON::from_json(`cat MYMETA.json`);
foreach (sort keys(%d)) {
$missing .= "'\''$_'\'' => '\''0'\'',\t# $d{$_}\n" if !defined($j->{prereqs}{runtime}{requires}{$_}) && !defined($i{$_})
}
if ($missing) {
print "\nMissing deps:\n(\n$missing)\n" ;
exit 1
}
'
make && make test && make distcheck && make dist
t/00-load.t view on Meta::CPAN
#!perl
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'API::Octopart' ) || print "Bail out!\n";
}
diag( "Testing API::Octopart $API::Octopart::VERSION, Perl $], $^X" );
( run in 0.598 second using v1.01-cache-2.11-cpan-de7293f3b23 )