| 1 | package Net::Async::WebSearch::Provider::Brave; | ||||||||||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Brave Search API provider | ||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'Net::Async::WebSearch::Provider'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use HTTP::Request::Common qw( GET ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Net::Async::WebSearch::Result; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | sub _init { | ||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | croak "Brave provider requires 'api_key'" unless $self->{api_key}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $self->{endpoint} ||= 'https://api.search.brave.com/res/v1/web/search'; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $self->{name} ||= 'brave'; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"name"} ||= "brave"
MC/DC0%0/1
$self->{"name"} ||= "brave"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
| |||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub endpoint { $_[0]->{endpoint} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub api_key { $_[0]->{api_key} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
api_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | sub search { | ||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ( $self, $http, $query, $opts ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $limit = $opts->{limit} || 10; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'limit'} || 10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table0%0/2
A
A= $opts->{'limit'}
| |||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $uri = URI->new( $self->endpoint ); | |||||||||||||||||||||||||||||||||||
| 31 | 0 | my %q = ( q => $query, count => $limit ); | |||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $q{country} = $opts->{region} if defined $opts->{region}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $q{search_lang}= $opts->{language} if defined $opts->{language}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $q{safesearch} = $opts->{safesearch} if defined $opts->{safesearch}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 35 | 0 | $uri->query_form(%q); | |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $req = GET( $uri->as_string ); | |||||||||||||||||||||||||||||||||||
| 38 | 0 | $req->header( 'User-Agent' => $self->user_agent_string ); | |||||||||||||||||||||||||||||||||||
| 39 | 0 | $req->header( 'Accept' => 'application/json' ); | |||||||||||||||||||||||||||||||||||
| 40 | 0 | $req->header( 'X-Subscription-Token' => $self->api_key ); | |||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | return $http->do_request( request => $req )->then(sub { | ||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ( $resp ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless ( $resp->is_success ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 45 | 0 | return Future->fail( | |||||||||||||||||||||||||||||||||||
| 46 | $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name, | ||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $data = eval { decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | 0 | return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name ); | |||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||
| 53 | 0 | my @out; | |||||||||||||||||||||||||||||||||||
| 54 | 0 | my $rank = 0; | |||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | for my $r ( @{ ($data->{web} || {})->{results} || [] } ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$data->{'web'} || {}
MC/DC0%0/1
$data->{'web'} || {}
$data->{'web'}
Truth table0%0/2
A
A= $data->{'web'}
| |||||||||||||||||||||||||||||||||||||
| 56 | 0 | $rank++; | |||||||||||||||||||||||||||||||||||
| 57 | push @out, Net::Async::WebSearch::Result->new( | ||||||||||||||||||||||||||||||||||||
| 58 | url => $r->{url}, | ||||||||||||||||||||||||||||||||||||
| 59 | title => $r->{title}, | ||||||||||||||||||||||||||||||||||||
| 60 | snippet => $r->{description}, | ||||||||||||||||||||||||||||||||||||
| 61 | provider => $self->name, | ||||||||||||||||||||||||||||||||||||
| 62 | rank => $rank, | ||||||||||||||||||||||||||||||||||||
| 63 | # Brave gives both age (human-readable like "3 days ago") and | ||||||||||||||||||||||||||||||||||||
| 64 | # page_age (ISO 8601). Prefer the ISO one. | ||||||||||||||||||||||||||||||||||||
| 65 | published_at => $r->{page_age} // $r->{age}, | ||||||||||||||||||||||||||||||||||||
| 66 | language => $r->{language}, | ||||||||||||||||||||||||||||||||||||
| 67 | raw => $r, | ||||||||||||||||||||||||||||||||||||
| 68 | extra => { | ||||||||||||||||||||||||||||||||||||
| 69 | ( defined $r->{age} ? ( age => $r->{age} ) : () ), | ||||||||||||||||||||||||||||||||||||
| 70 | ( defined $r->{profile} ? ( profile => $r->{profile} ) : () ), | ||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | ( defined $r->{subtype} ? ( subtype => $r->{subtype} ) : () ), | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$r->{"page_age"} // $r->{"age"}
MC/DC0%0/2
$r->{"page_age"} // $r->{"age"}
$r->{"page_age"}
$r->{"age"}
Truth table0%0/3
A // B
A= $r->{"page_age"}B= $r->{"age"}
| |||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | last if $rank >= $limit; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | 0 | return Future->done(\@out); | |||||||||||||||||||||||||||||||||||
| 77 | 0 | }); | |||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||||||||