WebSearch/Provider/Tavily.pm

filter: Statementstmt 96.4% 54 / 56 Branchbran 55.5% 10 / 18 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 79.6% 86 / 108 CC 17 SCAR 16.9
CC
17
Cov
81%
__ANON__
9.1
search
8.0
_init
4.4
CRAP 5.4
« WebSearch/Provider/Serper.pm ↑ Summary WebSearch/Provider/Exa.pm »
1 package Net::Async::WebSearch::Provider::Tavily;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Tavily Search API provider
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON::MaybeXS qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.9
15 1 my ( $self ) = @_;
16 1 ▶ croak "Tavily provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 1 1
17 1 ▶ $self->{endpoint} ||= 'https://api.tavily.com/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://api.tavily.com/search"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.tavily.com/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
18 1 ▶ $self->{name} ||= 'tavily';
Condition50%1/2
$self->{"name"} ||= "tavily"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "tavily"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
19 }
20  
21 1 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
22 1 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
23  
24 1 ▶sub search {
Subroutine search: called CC=7 SCAR=20.8
Pod documented
25 1 my ( $self, $http, $query, $opts ) = @_;
26 1 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
10
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
27 1 ▶ my $limit = $opts->{limit} || 10;
Condition50%1/2
$opts->{'limit'} || 10
l!l
10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table50%1/2
A
A= $opts->{'limit'}
Aresult
11
00
28 # max_results is capped at 20 upstream; clamp so the body param stays valid
29 # and the trim below never overshoots.
30 1 ▶ $limit = 20 if $limit > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $limit > 20 0 1 1
31  
32 my %body = (
33 query => $query,
34 max_results => $limit,
35 1 ▶ topic => $opts->{topic} // 'general',
Condition50%1/2
$opts->{"topic"} // "general"
l!l
01
MC/DC0%0/1
$opts->{"topic"} // "general"
$opts->{"topic"}
Truth table50%1/2
A
A= $opts->{"topic"}
Aresult
11
00
36 );
37 1 ▶ $body{language} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 1 1
38 1 ▶ $body{country} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 1 1
39  
40 1 my $req = HTTP::Request->new(
41 POST => $self->endpoint,
42 [
43 'Authorization' => 'Bearer ' . $self->api_key,
44 'Content-Type' => 'application/json',
45 'Accept' => 'application/json',
46 'User-Agent' => $self->user_agent_string,
47 ],
48 encode_json(\%body),
49 );
50  
51 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
52 1 my ( $resp ) = @_;
53 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
54 0 return Future->fail(
55 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
56 );
57 }
58 1 my $data = eval { decode_json( $resp->decoded_content ) };
59 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
60 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
61 }
62 1 my @out;
63 1 my $rank = 0;
64 1 ▶ for my $r ( @{ $data->{results} || [] } ) {
Condition50%1/2
$data->{'results'} || []
l!l
10
MC/DC0%0/1
$data->{'results'} || []
$data->{'results'}
Truth table50%1/2
A
A= $data->{'results'}
Aresult
11
00
65 3 $rank++;
66 push @out, Net::Async::WebSearch::Result->new(
67 url => $r->{url},
68 title => $r->{title},
69 snippet => $r->{content},
70 provider => $self->name,
71 rank => $rank,
72 published_at => $r->{published_date},
73 raw => $r,
74 extra => {
75 ( defined $r->{score} ? ( score => $r->{score} ) : () ),
76 3 ▶ ( defined $r->{favicon} ? ( favicon => $r->{favicon} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $r->{'score'} ? : 3 0 3
defined $r->{'favicon'} ? : 1 2 3
77 },
78 );
79 3 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 3 3
80 }
81 1 return Future->done(\@out);
82 1 });
83 }
84  
85 2 1;
86  
87 __END__
« WebSearch/Provider/Serper.pm ↑ Summary WebSearch/Provider/Exa.pm »