blib/lib/WWW/Foreman/API.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 18 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 115 CC 10 SCAR 19.9
CC
10
Cov
0%
get
12.0
post
12.0
put
12.0
CRAP 7.3
1 package WWW::Foreman::API;
2 $WWW::Foreman::API::VERSION = '0.001';
3 use strict;
4 use warnings;
5 use Carp 'croak';
6 use Encode;
7 use Exporter 'import';
8 my @EXPORT = ();
9 my @EXPORT_OK = qw(get post put delete);
10 use JSON;
11 use MIME::Base64;
12 use REST::Client;
13  
14 # ABSTRACT: Perl client to the Foreman API
15  
16 sub new {
17 my ($class, %params) = @_;
18 my $self = {
19 user => $params{user},
20 password => $params{password},
21 url => $params{url},
22 verify_ssl => $params{verify_ssl}
23 };
24 bless($self, $class);
25  
26 return $self;
27 }
28  
29 sub get {
30 my ($self, $path) = @_;
31 croak 'get() must be called with 1 argument' if @_ != 2;
32 croak 'The #1 argument to get() must be a scalar' if ref($_[1]);
33 my $client = $self->create_client();
34 my $headers = $self->set_headers();
35 $client->GET($path, $headers);
36 my $response = decode_json(decode_utf8($client->responseContent()));
37  
38 return $response;
39 }
40  
41 sub post {
42 my ($self, $path, $params) = @_;
43 croak 'post() must be called with 2 argument' if @_ != 3;
44 croak 'The #2 argument to post() must be a hash' if ref($_[2]) ne 'HASH';
45 $params = JSON::encode_json($params);
46 my $client = $self->create_client();
47 my $headers = $self->set_headers();
48 $client->POST($path, $params, $headers);
49 my $response = decode_utf8($client->responseContent());
50  
51 return $response;
52 }
53  
54 sub put {
55 my ($self, $path, $params) = @_;
56 croak 'put() must be called with 2 argument' if @_ != 3;
57 croak 'The #2 argument to put() must be a hash' if ref($_[2]) ne 'HASH';
58 $params = JSON::encode_json($params);
59 my $client = $self->create_client();
60 my $headers = $self->set_headers();
61 $client->PUT($path, $params, $headers);
62 my $response = decode_utf8($client->responseContent());
63  
64 return $response;
65 }
66  
67 sub delete {
68 my ($self, $path) = @_;
69 croak 'delete() must be called with 1 argument' if @_ != 2;
70 croak 'The #1 argument to delete() must be a scalar' if ref($_[1]);
71 my $client = $self->create_client();
72 my $headers = $self->set_headers();
73 $client->DELETE($path, $headers);
74 my $response = decode_json(decode_utf8($client->responseContent()));
75  
76 return $response;
77 }
78  
79 sub create_client {
80 my $self = shift;
81 my $client = REST::Client->new();
82 if($self->{verify_ssl} == 0) {
83 $client->getUseragent()->ssl_opts(verify_hostname => 0);
84 $client->getUseragent()->ssl_opts(SSL_verify_mode => 0);
85 }
86 $client->setHost($self->{url});
87  
88 return $client;
89 }
90  
91 sub set_headers {
92 my $self = shift;
93 my $headers = {
94 Content_Type => 'application/json;charset=utf8',
95 Accept => 'application/json',
96 Authorization => 'Basic ' . encode_base64($self->{user} . ':' . $self->{password})
97 };
98  
99 return $headers;
100 }
101  
102 =head1 NAME
103  
104 WWW::Foreman::API - Perl client to the Foreman API
105  
106 =head1 SYNOPSIS
107  
108 use WWW::Foreman::API;
109 use Data::Dumper;
110  
111 my $api = WWW::Foreman::API->new(
112 user => $user,
113 password => $password,
114 url => $foreman_api_url,
115 verify_ssl => 1
116 );
117  
118 print Dumper $api->get('hosts');
119  
120  
121  
122 =head1 DESCRIPTION
123  
124 This module is a generic client to the Foreman API. To use this module, you should use the C<post()>, C<get()>, C<put()> and C<delete()> methods.
125  
126 =head3 Methods:
127  
128 =head4 C<new()>
129  
130 Create a Foreman API object.
131  
132  
133 my $api = WWW::Foreman::API->new(
134 user => $user,
135 password => $password,
136 url => $foreman_api_url,
137 verify_ssl => 1
138 );
139  
140 =head4 Parameters:
141  
142 =over
143  
144 =item user
145  
146 The user who will be used for the API requests.
147  
148 =item password
149  
150 The password of the user.
151  
152 =item url
153  
154 The url of the Foreman API. For example, L<https://foreman/api>.
155  
156 =item verify_ssl
157  
158 If this parameter is set to 0, this disables certificate chain checking, as well as host name checking.
159  
160 =back
161  
162 =head4 C<get()>:
163  
164 The C<get()> method sends a GET request to the Foreman API, using the API end point supplied as an argument.
165 For example, this code:
166  
167 $api->get('hosts/2');
168  
169 will send a GET request to L<https://foreman_url/api/hosts/2>
170  
171 =head4 C<post()>:
172  
173 The C<post()> method sends a POST request to the Foreman API, using the API end point and the parameters supplied as arguments.
174 For example, this code:
175  
176 $api->post('architectures', \%params);
177  
178 will send a POST request to L<https://foreman_url/api/architecures>. C<\%params> is a hash ref which contains the parameters being send within the request.
179  
180 =head4 C<put()>:
181  
182 The C<put()> method sends a PUT request to the Foreman API, using the API end point and the parameters supplied as arguments.
183 For example, this code:
184  
185 $api->put('architectures/2', \%params);
186  
187 will send a PUT request to L<https://foreman_url/api/architecures/2>. C<\%params> is a hash ref which contains the parameters being send within the request.
188  
189 =head4 C<delete()>:
190  
191 The C<delete()> method sends a DELETE request to the Foreman API, using the API end point supplied as argument.
192 For example, this code:
193  
194 $api->delete('hosts/2');
195  
196 will send a DELETE request to L<https://<url_foreman/api/hosts/2>
197  
198 =head4 Getting more help
199  
200 For more information about the api endpoints and the parameters for each request, please refer to the official documentation: L<https://theforeman.org/api/1.12/index.html>.
201  
202 =head3 Return values
203  
204 The return value is an hash reference which is the deserialised json returned by the API. For example:
205  
206  
207 $VAR1 = {
208 'page' => 1,
209 'per_page' => 20,
210 'results' => [
211 {
212 'created_at' => '2015-04-03T13:59:04.398Z',
213 'id' => 1,
214 'updated_at' => '2015-05-14T13:59:04.398Z',
215 'name' => 'x86_64'
216 }
217 ],
218 'total' => 1,
219 'subtotal' => 1,
220 'search' => undef,
221 'sort' => {
222 'by' => undef,
223 'order' => undef
224 }
225 };
226  
227 =head1 AUTHOR
228  
229 Vincent Lequertier <vi.le@autistici.org>
230  
231 =head1 LICENSE
232  
233 This library is free software; you can redistribute it and/or modify
234 it under the same terms as Perl itself.
235  
236 =cut
237  
238 1;