| 1 | =encoding utf8 | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | WWW::Splunk::API - Splunk REST client | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | L<WWW::Splunk::API> is a low-level interface to Splunk | ||||||||||||||||||||||||||||
| 10 | log search engine. It deals with HTTP communication as well as | ||||||||||||||||||||||||||||
| 11 | working around certain interface glitches. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | See L<http://www.splunk.com/base/Documentation/latest/Developer/RESTSearch> | ||||||||||||||||||||||||||||
| 14 | for API definition. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This module is designed to be Splunk API version agnostic. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package WWW::Splunk::API; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use WWW::Splunk::XMLParser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | our $VERSION = '2.08'; | |||||||||||||||||||||||||||
| 31 | 4 | our $prefix = '/services'; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 B<new> (F<params>) | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | A constructor. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $splunk = WWW::Splunk::API->new({ | ||||||||||||||||||||||||||||
| 38 | host => $host, | ||||||||||||||||||||||||||||
| 39 | port => $port, | ||||||||||||||||||||||||||||
| 40 | login => $login, | ||||||||||||||||||||||||||||
| 41 | password => $password, | ||||||||||||||||||||||||||||
| 42 | unsafe_ssl => 0, | ||||||||||||||||||||||||||||
| 43 | verbose => 0, | ||||||||||||||||||||||||||||
| 44 | }); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub new { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my ($class, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $self->{port} ||= 8089; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"port"} ||= 8089
MC/DC0%0/1
$self->{"port"} ||= 8089
$self->{"port"}
Truth table0%0/2
A
A= $self->{"port"}
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $self->{host} ||= 'localhost'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"host"} ||= "localhost"
MC/DC0%0/1
$self->{"host"} ||= "localhost"
$self->{"host"}
Truth table0%0/2
A
A= $self->{"host"}
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $self->{url} ||= 'https://'.$self->{host}.':'.$self->{port}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"url"} ||= 'https://' . $self->{"host"} . ':' . $self->{"port"}
MC/DC0%0/1
$self->{"url"} ||= 'https://' . $self->{"host"} . ':' . $self->{"port"}
$self->{"url"}
Truth table0%0/2
A
A= $self->{"url"}
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $self->{verbose} ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"verbose"} ||= 0
MC/DC0%0/1
$self->{"verbose"} ||= 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Set up user agent unless an existing one was passed | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | unless ($self->{agent}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | $self->{agent} = new LWP::UserAgent | ||||||||||||||||||||||||||||
| 59 | 0 | (ssl_opts => {verify_hostname => (not $self->{unsafe_ssl})}); | |||||||||||||||||||||||||||
| 60 | 0 | $self->{agent}->cookie_jar ({}); | |||||||||||||||||||||||||||
| 61 | $self->{agent}->credentials ( | ||||||||||||||||||||||||||||
| 62 | delete ($self->{host}).':'.(delete $self->{port}), | ||||||||||||||||||||||||||||
| 63 | '/splunk', | ||||||||||||||||||||||||||||
| 64 | delete $self->{login}, | ||||||||||||||||||||||||||||
| 65 | delete $self->{password}, | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | ) if exists $self->{login}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $self->{agent}->agent ("$class/$VERSION "); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 B<delete> (F<parameters>) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Wrapper around HTTP::Request::Common::DELETE(). | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub delete { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | print "DELETE" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $self->request (\&DELETE, @args); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 B<post> (F<parameters>) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Wrapper around HTTP::Request::Common::POST(). | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub post { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | print "POST" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->request (\&POST, @args); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 B<get> (F<parameters>) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Wrapper around HTTP::Request::Common::GET(). | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub get { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | print "GET" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $self->request (\&GET, @args); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 B<head> (F<parameters>) | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Wrapper around HTTP::Request::Common::HEAD(). | ||||||||||||||||||||||||||||
| 115 | Not used anywhere in splunk API | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub head { | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
head: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | print "HEAD" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->request (\&HEAD, @args); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 B<put> (F<parameters>) | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Wrapper around HTTP::Request::Common::PUT(). | ||||||||||||||||||||||||||||
| 129 | Not used anywhere in splunk API | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub put { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
put: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | print "PUT" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $self->request (\&PUT, @args); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 B<request> (F<method>, F<location>, [F<data>], [F<callback>]) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Request a Splunk api and deal with the results. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Method can be either a L<HTTP::Request> instance (see L<HTTP::Request::Common> | ||||||||||||||||||||||||||||
| 145 | for useful ones), or a plain string, such as "GET" or "DELETE." | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Optional F<data> is has reference gets serialized into a request body for POST | ||||||||||||||||||||||||||||
| 148 | request. Use I<undef> in case you don't have any data to send, but need to | ||||||||||||||||||||||||||||
| 149 | specify a callback function in subsequent argument. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Call-back function can be specified for a single special case, where a XML stream | ||||||||||||||||||||||||||||
| 152 | of <results> elements is expected. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub request { | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my ($self, $method, $location, $data, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $url = $self->{url}.$prefix.$location; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($self->{verbose}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | print " $url\n"; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | foreach my $key (sort keys %$data) { | |||||||||||||||||||||||||||
| 164 | 0 | my $value = $data->{$key}; | |||||||||||||||||||||||||||
| 165 | 0 | $value =~ s/\n/ /msg; | |||||||||||||||||||||||||||
| 166 | 0 | print "- $key => $value\n"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Construct the request | ||||||||||||||||||||||||||||
| 172 | 0 | my $request; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (ref $method and ref $method eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $method and ref $method eq "CODE"
MC/DC0%0/2
ref $method and ref $method eq "CODE"
ref $method
ref $method eq "CODE"
Truth table0%0/3
A and B
A= ref $methodB= ref $method eq "CODE"
| |||||||||||||||||||||||||||||
| 174 | # Most likely a HTTP::Request::Common | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if (! defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | $request = $method->($url); | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 0 | $request = $method->($url, $data); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||
| 181 | # A method string | ||||||||||||||||||||||||||||
| 182 | 0 | $request = new HTTP::Request ($method, $url); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my $content_type = ''; | |||||||||||||||||||||||||||
| 186 | 0 | my $buffer; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | $self->{agent}->remove_handler ('response_header'); | |||||||||||||||||||||||||||
| 189 | $self->{agent}->add_handler (response_header => sub { | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | my($response, $ua, $h) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Do not think of async processing of error responses | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | return 0 unless $response->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $content_type_header = $response->header('Content-Type') // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$response->header("Content-Type") // ""
MC/DC0%0/1
$response->header("Content-Type") // ""
$response->header("Content-Type")
Truth table0%0/2
A
A= $response->header("Content-Type")
| |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ($content_type_header =~ /^([^\s;]+)/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 197 | 0 | $content_type = $1; | |||||||||||||||||||||||||||
| 198 | } elsif ($response->code ne 204) { | ||||||||||||||||||||||||||||
| 199 | # Sometimes splunk return HTTP 204 NO CONTENT during poll_search() call, | ||||||||||||||||||||||||||||
| 200 | # Content-Type header is empty in this case. We must not croak in this case. | ||||||||||||||||||||||||||||
| 201 | 0 | croak "Missing or invalid Content-Type: $content_type_header"; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if ($callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | $response->{default_add_content} = 0; | |||||||||||||||||||||||||||
| 206 | 0 | $buffer = ""; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 0 | }); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | $self->{agent}->remove_handler ('response_data'); | |||||||||||||||||||||||||||
| 211 | $self->{agent}->add_handler (response_data => sub { | ||||||||||||||||||||||||||||
| 212 | 0 | ▶ | my ($response, $ua, $h, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | return 1 unless defined $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | $buffer .= $data; | |||||||||||||||||||||||||||
| 216 | 0 | foreach (split /<\/results>\K/, $buffer) { | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | unless (/<\/results>$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | $buffer = $_; | |||||||||||||||||||||||||||
| 219 | 0 | last; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | my $xml = XML::LibXML->load_xml (string => $_); | |||||||||||||||||||||||||||
| 223 | 0 | $callback->(WWW::Splunk::XMLParser::parse ($xml)); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | return 1; | |||||||||||||||||||||||||||
| 227 | 0 | ▶ | }) if $callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Run it | ||||||||||||||||||||||||||||
| 230 | 0 | my $response = $self->{agent}->request ($request); | |||||||||||||||||||||||||||
| 231 | 0 | ▶ | croak $response->header ('X-Died') if $response->header ('X-Died'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # Deal with HTTP errors | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | my $content = WWW::Splunk::XMLParser::parse ($response->content) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | if $response->header ('Content-Type') =~ /xml/; | ||||||||||||||||||||||||||||
| 237 | 0 | my $error = "HTTP Error: ".$response->status_line; | |||||||||||||||||||||||||||
| 238 | $error .= sprintf "\n%s: %s", | ||||||||||||||||||||||||||||
| 239 | $content->findvalue ('/response/messages/msg/@type'), | ||||||||||||||||||||||||||||
| 240 | $content->findvalue ('/response/messages/msg') | ||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if eval { $content->isa ('XML::LibXML::Document') } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
$content->isa("XML::LibXML::Document")
}
} and $content->documentElement->nodeName eq "response"
MC/DC0%0/2
eval {
do {
$content->isa("XML::LibXML::Document")
}
} and $content->documentElement->nodeName eq "response"
eval {
do {
$content->isa("XML::LibXML::Document")
}
}
$content->documentElement->nodeName eq "response"
Truth table0%0/3
A and B
A= eval {
do {
$content->isa("XML::LibXML::Document")
}
}B= $content->documentElement->nodeName eq "response"
| |||||||||||||||||||||||||||||
| 242 | and $content->documentElement->nodeName eq 'response'; | ||||||||||||||||||||||||||||
| 243 | 0 | croak $error; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # We've gotten the response already | ||||||||||||||||||||||||||||
| 247 | 0 | ▶ | return if $callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Parse content from synchronous responses | ||||||||||||||||||||||||||||
| 250 | # TODO: use callback and m_media_type matchspecs | ||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ($content_type eq 'text/xml') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 252 | 0 | my $xml = XML::LibXML->load_xml (string => $response->content); | |||||||||||||||||||||||||||
| 253 | 0 | my @ret = WWW::Splunk::XMLParser::parse ($xml); | |||||||||||||||||||||||||||
| 254 | 0 | ▶ | return $#ret ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | } elsif ($response->code eq 204) { | ||||||||||||||||||||||||||||
| 256 | # "No content" | ||||||||||||||||||||||||||||
| 257 | # Happens when events are requested immediately | ||||||||||||||||||||||||||||
| 258 | # after the job is enqueued. With a text/plain content type | ||||||||||||||||||||||||||||
| 259 | # Empty array is the least disturbing thing to return here | ||||||||||||||||||||||||||||
| 260 | 0 | return (); | |||||||||||||||||||||||||||
| 261 | } elsif ($content_type eq 'text/plain') { | ||||||||||||||||||||||||||||
| 262 | # Sometimes an empty text/plain body is sent | ||||||||||||||||||||||||||||
| 263 | # even without 204 return code. | ||||||||||||||||||||||||||||
| 264 | 0 | return (); | |||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||
| 266 | # TODO: We probably can't do much about RAW | ||||||||||||||||||||||||||||
| 267 | # format, yet we could parse at least JSON | ||||||||||||||||||||||||||||
| 268 | 0 | croak "Unknown content type: $content_type"; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | L<WWW::Splunk>, L<sc> | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Lubomir Rintel, L<< <lkundrak@v3.sk> >>, | ||||||||||||||||||||||||||||
| 279 | Michal Josef Špaček L<< <skim@cpan.org> >> | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | The code is hosted on GitHub L<http://github.com/tupinek/perl-WWW-Splunk>. | ||||||||||||||||||||||||||||
| 282 | Bug fixes and feature enhancements are always welcome. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||