ensembl-hive  2.8.1
RESTclient.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  # Example without an instance:
10  my $task_id_mapping = Bio::EnsEMBL::Hive::Utils::RESTclient->POST(
11  'http://server.address:1234/create_tasks?with=parameters',
12  $extra_data_hash,
13  'intermediate_dump.json'
14  );
15 
16  # Example with an instance:
17  my $head_node = Bio::EnsEMBL::Hive::Utils::RESTclient->new( 'http://server.address:1234' );
18 
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" );
22 
23 =head1 DESCRIPTION
24 
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.
27 
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.
30 
31 =head1 LICENSE
32 
33 See the NOTICE file distributed with this work for additional information
34 regarding copyright ownership.
35 
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
39 
40  http://www.apache.org/licenses/LICENSE-2.0
41 
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.
47 
48 =cut
49 
50 
51 package Bio::EnsEMBL::Hive::Utils::RESTclient;
52 
53 use strict;
54 use warnings;
55 use JSON;
56 
57 
58 sub new {
59  my $class = shift @_;
60 
61  my $self = bless {}, $class;
62 
63  $self->base_url( shift @_ // '' );
64 
65  return $self;
66 }
67 
68 
69 sub base_url {
70  my $self = shift @_;
71 
72  $self->{'_base_url'} = shift if(@_);
73  return $self->{'_base_url'};
74 }
75 
76 
77 sub run_curl_capture_and_parse_result {
78  my ($self, $curl_cmd, $raw_json_output_filename) = @_;
79 
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;
83 
84  if($raw_json_output_filename) {
85  open(my $fh, '>', $raw_json_output_filename);
86  print $fh $json_output_string;
87  close $fh;
88  }
89 
90  my $perl_struct = $json_output_string && JSON->new->decode( $json_output_string );
91 
92  return $perl_struct;
93 }
94 
95 
96 sub GET {
97  my ($self, $request_url, $raw_json_output_filename) = @_;
98 
99  my $base_url = ref($self) ? $self->base_url : '';
100  my $curl_cmd = ['curl', '-g', '-s', $base_url.$request_url];
101 
102  return $self->run_curl_capture_and_parse_result( $curl_cmd, $raw_json_output_filename );
103 }
104 
105 
106 sub POST {
107  my ($self, $request_url, $request_data_struct, $raw_json_output_filename) = @_;
108 
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];
112 
113  return $self->run_curl_capture_and_parse_result( $curl_cmd, $raw_json_output_filename );
114 }
115 
116 1;
Bio::EnsEMBL::Hive::Utils::RESTclient
Definition: RESTclient.pm:33
Bio::EnsEMBL::Hive::Utils::RESTclient::POST
public POST()
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Utils::RESTclient::new
public new()