| 1 | # Copyright (C) 2021-2022 Horimoto Yasuhiro <horimoto@clear-code.com> | ||||||||||
| 2 | # | ||||||||||
| 3 | # This program is free software: you can redistribute it and/or modify | ||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||||||||||
| 6 | # (at your option) any later version. | ||||||||||
| 7 | # | ||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||
| 12 | # | ||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||
| 15 | |||||||||||
| 16 | package Groonga::HTTP::Client; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use Groonga::ResultSet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use URI (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | my $prefix = ""; | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 0 | my ($class, %args) = @_; | |||||||||
| 31 | 0 | my $self = {%args}; | |||||||||
| 32 | |||||||||||
| 33 | 0 | my $host = $self->{host}; | |||||||||
| 34 | 0 | my $port = $self->{port}; | |||||||||
| 35 | |||||||||||
| 36 | 0 | $prefix = "http://${host}:${port}/d/"; | |||||||||
| 37 | 0 | return bless $self, $class; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub _uri_encode { | ||||||||
|
Subroutine
_uri_encode: not called CC=2 SCAR=17.9
| |||||||||||
| 41 | 0 | my $command = $_[1]; | |||||||||
| 42 | 0 | my $uri_encoded_query = URI->new($prefix . $command); | |||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | if (defined $_[2]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | my $command_args = $_[2]; | |||||||||
| 46 | 0 | $uri_encoded_query->query_form($command_args); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | return $uri_encoded_query; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub send { | ||||||||
|
Subroutine
send: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 53 | 0 | my $uri_encoded_query = _uri_encode(@_); | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $user_agent = LWP::UserAgent->new; | |||||||||
| 56 | 0 | my $http_response = $user_agent->get($uri_encoded_query); | |||||||||
| 57 | 0 | ▶ | if ($http_response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my $groonga_response = | |||||||||
| 59 | Groonga::ResultSet->new( | ||||||||||
| 60 | decoded_content => $http_response->decoded_content | ||||||||||
| 61 | ); | ||||||||||
| 62 | 0 | ▶ | if ($groonga_response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | return $groonga_response->content; | |||||||||
| 64 | } else { | ||||||||||
| 65 | 0 | croak $groonga_response->content; | |||||||||
| 66 | } | ||||||||||
| 67 | } else { | ||||||||||
| 68 | 0 | croak $http_response->status_line; | |||||||||
| 69 | } | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub send_post { | ||||||||
|
Subroutine
send_post: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 73 | 0 | my $command = $_[1]; | |||||||||
| 74 | 0 | my $uri = $prefix . $command; | |||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | if (defined $_[2]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | my $include_uri_parameter = $_[2]; | |||||||||
| 78 | 0 | $uri .= $include_uri_parameter; | |||||||||
| 79 | } | ||||||||||
| 80 | 0 | ▶ | unless (defined $_[3]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | croak "Missing POST data"; | |||||||||
| 82 | } | ||||||||||
| 83 | 0 | my $post_values = $_[3]; | |||||||||
| 84 | |||||||||||
| 85 | 0 | my $user_agent = LWP::UserAgent->new; | |||||||||
| 86 | 0 | my $post_request = HTTP::Request->new(POST => $uri); | |||||||||
| 87 | 0 | $post_request->content_type('application/json'); | |||||||||
| 88 | 0 | $post_request->content($post_values); | |||||||||
| 89 | |||||||||||
| 90 | 0 | my $http_response = $user_agent->request($post_request); | |||||||||
| 91 | 0 | ▶ | if ($http_response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | my $groonga_response = | |||||||||
| 93 | Groonga::ResultSet->new( | ||||||||||
| 94 | decoded_content => $http_response->decoded_content | ||||||||||
| 95 | ); | ||||||||||
| 96 | 0 | ▶ | if ($groonga_response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | return $groonga_response->content; | |||||||||
| 98 | } else { | ||||||||||
| 99 | 0 | croak $groonga_response->content; | |||||||||
| 100 | } | ||||||||||
| 101 | } else { | ||||||||||
| 102 | 0 | croak $http_response->status_line; | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 1 | 1; | |||||||||