| 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
| |||||||||||||||
| 17 | 1 | ▶ | $self->{endpoint} ||= 'https://api.tavily.com/search'; | ||||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://api.tavily.com/search"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.tavily.com/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||||
| 18 | 1 | ▶ | $self->{name} ||= 'tavily'; | ||||||||||||
|
Condition50%1/2
$self->{"name"} ||= "tavily"
MC/DC0%0/1
$self->{"name"} ||= "tavily"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
| |||||||||||||||
| 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 ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||
| 27 | 1 | ▶ | my $limit = $opts->{limit} || 10; | ||||||||||||
|
Condition50%1/2
$opts->{'limit'} || 10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table50%1/2
A
A= $opts->{'limit'}
| |||||||||||||||
| 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
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | my %body = ( | ||||||||||||||
| 33 | query => $query, | ||||||||||||||
| 34 | max_results => $limit, | ||||||||||||||
| 35 | 1 | ▶ | topic => $opts->{topic} // 'general', | ||||||||||||
|
Condition50%1/2
$opts->{"topic"} // "general"
MC/DC0%0/1
$opts->{"topic"} // "general"
$opts->{"topic"}
Truth table50%1/2
A
A= $opts->{"topic"}
| |||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | 1 | ▶ | $body{language} = $opts->{language} if defined $opts->{language}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 1 | ▶ | $body{country} = $opts->{region} if defined $opts->{region}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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'} || []
MC/DC0%0/1
$data->{'results'} || []
$data->{'results'}
Truth table50%1/2
A
A= $data->{'results'}
| |||||||||||||||
| 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
| |||||||||||||||
| 77 | }, | ||||||||||||||
| 78 | ); | ||||||||||||||
| 79 | 3 | ▶ | last if $rank >= $limit; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | } | ||||||||||||||
| 81 | 1 | return Future->done(\@out); | |||||||||||||
| 82 | 1 | }); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 2 | 1; | |||||||||||||
| 86 | |||||||||||||||
| 87 | __END__ | ||||||||||||||