| 1 | package Net::Async::WebSearch::Provider::Exa; | ||||||||||||||||||
| 2 | 2 | our $VERSION = '0.003'; | |||||||||||||||||
| 3 | # ABSTRACT: Exa neural 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 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 "Exa provider requires 'api_key'" unless $self->{api_key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 17 | 1 | ▶ | $self->{endpoint} ||= 'https://api.exa.ai/search'; | ||||||||||||||||
|
Condition50%1/2
$self->{"endpoint"} ||= "https://api.exa.ai/search"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.exa.ai/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||
| 18 | 1 | ▶ | $self->{name} ||= 'exa'; | ||||||||||||||||
|
Condition50%1/2
$self->{"name"} ||= "exa"
MC/DC0%0/1
$self->{"name"} ||= "exa"
$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=3 SCAR=11.5
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 | |||||||||||||||||||
| 29 | # Exa has no native language/region/safesearch parameters, so those opts are | ||||||||||||||||||
| 30 | # silently ignored. Snippet text only comes back when `contents.text` is | ||||||||||||||||||
| 31 | # requested — without it every hit has an empty snippet. | ||||||||||||||||||
| 32 | 1 | my %body = ( | |||||||||||||||||
| 33 | query => $query, | ||||||||||||||||||
| 34 | numResults => $limit, | ||||||||||||||||||
| 35 | contents => { text => JSON->true }, | ||||||||||||||||||
| 36 | ); | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 1 | my $req = HTTP::Request->new( | |||||||||||||||||
| 39 | POST => $self->endpoint, | ||||||||||||||||||
| 40 | [ | ||||||||||||||||||
| 41 | 'x-api-key' => $self->api_key, | ||||||||||||||||||
| 42 | 'Content-Type' => 'application/json', | ||||||||||||||||||
| 43 | 'Accept' => 'application/json', | ||||||||||||||||||
| 44 | 'User-Agent' => $self->user_agent_string, | ||||||||||||||||||
| 45 | ], | ||||||||||||||||||
| 46 | encode_json(\%body), | ||||||||||||||||||
| 47 | ); | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 1 | ▶ | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.7
| |||||||||||||||||||
| 50 | 1 | my ( $resp ) = @_; | |||||||||||||||||
| 51 | 1 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 0 | return Future->fail( | |||||||||||||||||
| 53 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||
| 54 | ); | ||||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | 1 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||
| 57 | 1 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 58 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||
| 59 | } | ||||||||||||||||||
| 60 | 1 | my @out; | |||||||||||||||||
| 61 | 1 | my $rank = 0; | |||||||||||||||||
| 62 | 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'}
| |||||||||||||||||||
| 63 | 2 | $rank++; | |||||||||||||||||
| 64 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||
| 65 | url => $r->{url}, | ||||||||||||||||||
| 66 | title => $r->{title}, | ||||||||||||||||||
| 67 | snippet => $r->{text}, | ||||||||||||||||||
| 68 | provider => $self->name, | ||||||||||||||||||
| 69 | rank => $rank, | ||||||||||||||||||
| 70 | published_at => $r->{publishedDate}, | ||||||||||||||||||
| 71 | raw => $r, | ||||||||||||||||||
| 72 | extra => { | ||||||||||||||||||
| 73 | ( defined $r->{author} ? ( author => $r->{author} ) : () ), | ||||||||||||||||||
| 74 | ( defined $r->{id} ? ( id => $r->{id} ) : () ), | ||||||||||||||||||
| 75 | 2 | ▶ | ( defined $r->{score} ? ( score => $r->{score} ) : () ), | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 76 | }, | ||||||||||||||||||
| 77 | ); | ||||||||||||||||||
| 78 | 2 | ▶ | last if $rank >= $limit; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | 1 | return Future->done(\@out); | |||||||||||||||||
| 81 | 1 | }); | |||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 2 | 1; | |||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||