| 1 | package WebService::Azure::Search; | ||||||||||
| 2 | 3 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use HTTP::Headers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use Encode 'encode'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 3 | our $VERSION = "0.04"; | |||||||||
| 17 | |||||||||||
| 18 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | 2 | my ($class, %opts) = @_; | |||||||||
| 20 | 2 | my $self = bless {%opts}, $class; | |||||||||
| 21 | 2 | $self->_init($self); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 6 | ▶ | sub _init { | ||||||||
|
Subroutine
_init: called CC=5 SCAR=16.6
| |||||||||||
| 25 | 6 | my ($self) = @_; | |||||||||
| 26 | $self->{setting} = +{ | ||||||||||
| 27 | 6 | base => undef, | |||||||||
| 28 | index => undef, | ||||||||||
| 29 | api => undef, | ||||||||||
| 30 | admin => undef, | ||||||||||
| 31 | }; | ||||||||||
| 32 | |||||||||||
| 33 | 6 | ▶ | if ($self->{service}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 6 | $self->{setting}{base} = sprintf("https://%s.search.windows.net", $self->{service}); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 6 | ▶ | if ($self->{index}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 6 | $self->{setting}{index} = $self->{index}; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 6 | ▶ | if ($self->{api}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 6 | $self->{setting}{api} = $self->{api}; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 6 | ▶ | if ($self->{admin}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 6 | $self->{setting}{admin} = $self->{admin}; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 6 | $self->{params}{accept} = "application/json"; | |||||||||
| 50 | |||||||||||
| 51 | 6 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | $self->{params}{url} = sprintf( | ||||||||||
| 53 | "%s/indexes/%s/docs/index?api-version=%s", | ||||||||||
| 54 | $self->{setting}{base}, | ||||||||||
| 55 | $self->{setting}{index}, | ||||||||||
| 56 | $self->{setting}{api} | ||||||||||
| 57 | 6 | ); | |||||||||
| 58 | 6 | return $self; | |||||||||
| 59 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 60 | 0 | carp "can't create request url.detail : $_"; | |||||||||
| 61 | 6 | }; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # select, insert, update, delete Method only make parameters. | ||||||||||
| 65 | 1 | ▶ | sub select { | ||||||||
|
Subroutine
select: called CC=8 SCAR=21.3
Pod
undocumented
| |||||||||||
| 66 | 1 | my ($self, %params) = @_; | |||||||||
| 67 | 1 | $self->{params} = {}; | |||||||||
| 68 | 1 | $self = $self->_init($self->{setting}); | |||||||||
| 69 | 1 | my $params = +{%params}; | |||||||||
| 70 | |||||||||||
| 71 | # Set value | ||||||||||
| 72 | 1 | $self->{params}{query}{search} = undef; | |||||||||
| 73 | 1 | ▶ | if ($params->{search}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 1 | $self->{params}{query}{search} = $params->{search}; | |||||||||
| 75 | } | ||||||||||
| 76 | 1 | $self->{params}{query}{searchMode} = "any"; # default is 'any' | |||||||||
| 77 | 1 | ▶ | if ($params->{searchMode}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 1 | $self->{params}{query}{searchMode} = $params->{searchMode}; | |||||||||
| 79 | } | ||||||||||
| 80 | 1 | $self->{params}{query}{searchFields} = undef; | |||||||||
| 81 | 1 | ▶ | if ($params->{searchFields}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 1 | $self->{params}{query}{searchFields} = $params->{searchFields}; | |||||||||
| 83 | } | ||||||||||
| 84 | 1 | $self->{params}{query}{count} = "false"; | |||||||||
| 85 | 1 | ▶ | if ($params->{count}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 1 | $self->{params}{query}{count} = $params->{count}; | |||||||||
| 87 | } | ||||||||||
| 88 | 1 | $self->{params}{query}{skip} = 0; # default is 0 | |||||||||
| 89 | 1 | ▶ | if ($params->{skip}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | $self->{params}{query}{skip} = $params->{skip}; | |||||||||
| 91 | } | ||||||||||
| 92 | 1 | $self->{params}{query}{top} = 50; # default is 50 | |||||||||
| 93 | 1 | ▶ | if ($params->{top}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 1 | $self->{params}{query}{top} = $params->{top}; | |||||||||
| 95 | } | ||||||||||
| 96 | 1 | ▶ | if ($params->{filter}) { # filter is optional | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 1 | $self->{params}{query}{filter} = $params->{filter}; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1 | $self->{params}{url} = undef; | |||||||||
| 101 | 1 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | # Create URL for SELECT | ||||||||||
| 103 | $self->{params}{url} = sprintf( | ||||||||||
| 104 | "%s/indexes/%s/docs/search?api-version=%s", | ||||||||||
| 105 | $self->{setting}{base}, | ||||||||||
| 106 | $self->{setting}{index}, | ||||||||||
| 107 | $self->{setting}{api}, | ||||||||||
| 108 | 1 | ); | |||||||||
| 109 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 110 | 0 | carp "cant't create request url for SELECT. detail : $_"; | |||||||||
| 111 | 1 | }; | |||||||||
| 112 | 1 | return $self; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 1 | ▶ | sub insert { | ||||||||
|
Subroutine
insert: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 116 | 1 | my ($self, $params) = @_; | |||||||||
| 117 | 1 | $self->{params} = {}; | |||||||||
| 118 | 1 | $self = $self->_init($self->{setting}); | |||||||||
| 119 | 1 | ▶ | for(my $count=0;$count<@$params;$count++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 1 | $params->[$count]->{'@search.action'} = 'upload'; | |||||||||
| 121 | } | ||||||||||
| 122 | 1 | $self->{params}{query}{value} = $params; | |||||||||
| 123 | 1 | return $self; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 1 | ▶ | sub update { | ||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 127 | 1 | my ($self, $params) = @_; | |||||||||
| 128 | 1 | $self->{params} = {}; | |||||||||
| 129 | 1 | $self = $self->_init($self->{setting}); | |||||||||
| 130 | 1 | ▶ | for(my $count=0;$count<@$params;$count++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 1 | $params->[$count]->{'@search.action'} = 'merge'; | |||||||||
| 132 | } | ||||||||||
| 133 | 1 | $self->{params}{query}{value} = $params; | |||||||||
| 134 | 1 | return $self; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 1 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 138 | 1 | my ($self, $params) = @_; | |||||||||
| 139 | 1 | $self->{params} = {}; | |||||||||
| 140 | 1 | $self = $self->_init($self->{setting}); | |||||||||
| 141 | 1 | ▶ | for(my $count=0;$count<@$params;$count++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 1 | $params->[$count]->{'@search.action'} = 'delete'; | |||||||||
| 143 | } | ||||||||||
| 144 | 1 | $self->{params}{query}{value} = $params; | |||||||||
| 145 | 1 | return $self; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | # Only http request. | ||||||||||
| 149 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 150 | 0 | my ($self) = @_; | |||||||||
| 151 | 0 | my $bless_query = $self->{params}{query}; | |||||||||
| 152 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 153 | 0 | my $hashref = {%$bless_query}; | |||||||||
| 154 | 0 | my $json_query = JSON->new->encode($hashref); | |||||||||
| 155 | 0 | my $ua = LWP::UserAgent->new; | |||||||||
| 156 | 0 | my $req = HTTP::Request->new('POST' => $self->{params}{url}); | |||||||||
| 157 | 0 | $req->content_type('application/json'); | |||||||||
| 158 | 0 | $req->header('api-key' => $self->{setting}{admin}); | |||||||||
| 159 | 0 | $req->content($json_query); | |||||||||
| 160 | 0 | return JSON->new->utf8->decode($ua->request($req)->content); | |||||||||
| 161 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 162 | 0 | carp "can't access AzureSearch.detail: $_"; | |||||||||
| 163 | 0 | return undef; | |||||||||
| 164 | } | ||||||||||
| 165 | 0 | } | |||||||||
| 166 | |||||||||||
| 167 | 3 | 1; | |||||||||
| 168 | __END__ | ||||||||||