9 # Example without an instance:
11 'http://server.address:1234/create_tasks?with=parameters',
13 'intermediate_dump.json'
16 # Example with an instance:
19 my $swarm_id = $head_node->GET(
'/swarm' )->{
'ID'};
20 my $tasks_struct = $head_node->GET(
'/tasks',
"${output_prefix}/${container_prefix}.tasks.json" );
21 my $nodes_struct = $head_node->GET(
'/nodes',
"${output_prefix}/${container_prefix}.nodes.json" );
25 This module provides a
generic REST client
interface via GET and POST methods.
26 The current implementation is via calling
'curl' external command and capturing its output.
28 There is no requirement to instantiate an
object when
using this module (same methods will work as
class methods),
29 but
if you need to make multiple requests to the same server you may find it convenient to store its base_url in the
object.
33 See the NOTICE file distributed with
this work
for additional information
34 regarding copyright ownership.
36 Licensed under the Apache License,
Version 2.0 (the
"License");
37 you may not use
this file except in compliance with the License.
38 You may obtain a copy of the License at
42 Unless required by applicable law or agreed to in writing, software
43 distributed under the License is distributed on an
"AS IS" BASIS,
44 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 See the License
for the specific language governing permissions and
46 limitations under the License.
51 package Bio::EnsEMBL::Hive::Utils::RESTclient;
61 my $self = bless {}, $class;
63 $self->base_url( shift @_
72 $self->{
'_base_url'} = shift
if(@_);
73 return $self->{
'_base_url'};
77 sub run_curl_capture_and_parse_result {
78 my ($self, $curl_cmd, $raw_json_output_filename) = @_;
80 open(my $curl_output_fh,
"-|", @$curl_cmd) || die
"Could not run '".join(
' ',@$curl_cmd).
"' : $!, $?";
81 my $json_output_string = <$curl_output_fh>;
82 close $curl_output_fh;
84 if($raw_json_output_filename) {
85 open(my $fh,
'>', $raw_json_output_filename);
86 print $fh $json_output_string;
90 my $perl_struct = $json_output_string && JSON->new->decode( $json_output_string );
97 my ($self, $request_url, $raw_json_output_filename) = @_;
99 my $base_url = ref($self) ? $self->base_url :
'';
100 my $curl_cmd = [
'curl',
'-g',
'-s', $base_url.$request_url];
102 return $self->run_curl_capture_and_parse_result( $curl_cmd, $raw_json_output_filename );
107 my ($self, $request_url, $request_data_struct, $raw_json_output_filename) = @_;
109 my $base_url = ref($self) ? $self->base_url :
'';
110 my $request_data = JSON->new->encode( $request_data_struct );
111 my $curl_cmd = [
'curl',
'-g',
'-s',
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d', $request_data, $base_url.$request_url];
113 return $self->run_curl_capture_and_parse_result( $curl_cmd, $raw_json_output_filename );