File Coverage

blib/lib/Groonga/HTTP/Client.pm
Criterion Covered Total %
statement 18 58 31.0
branch 0 14 0.0
condition n/a
subroutine 6 10 60.0
pod 0 3 0.0
total 24 85 28.2


line stmt bran cond sub pod time code
1             # Copyright (C) 2021-2022 Horimoto Yasuhiro
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 .
15              
16             package Groonga::HTTP::Client;
17              
18 1     1   6 use LWP::UserAgent;
  1         1  
  1         23  
19 1     1   4 use Carp 'croak';
  1         2  
  1         37  
20              
21 1     1   5 use Groonga::ResultSet;
  1         1  
  1         13  
22 1     1   5 use URI ();
  1         1  
  1         11  
23              
24 1     1   3 use strict;
  1         27  
  1         23  
25 1     1   5 use warnings;
  1         1  
  1         413  
26              
27             my $prefix = "";
28              
29             sub new {
30 0     0 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             sub _uri_encode {
41 0     0     my $command = $_[1];
42 0           my $uri_encoded_query = URI->new($prefix . $command);
43              
44 0 0         if (defined $_[2]) {
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             sub send {
53 0     0 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 0         if ($http_response->is_success) {
58 0           my $groonga_response =
59             Groonga::ResultSet->new(
60             decoded_content => $http_response->decoded_content
61             );
62 0 0         if ($groonga_response->is_success) {
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             sub send_post {
73 0     0 0   my $command = $_[1];
74 0           my $uri = $prefix . $command;
75              
76 0 0         if (defined $_[2]) {
77 0           my $include_uri_parameter = $_[2];
78 0           $uri .= $include_uri_parameter;
79             }
80 0 0         unless (defined $_[3]) {
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 0         if ($http_response->is_success) {
92 0           my $groonga_response =
93             Groonga::ResultSet->new(
94             decoded_content => $http_response->decoded_content
95             );
96 0 0         if ($groonga_response->is_success) {
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;