1e65058effa55784869c7e01a76774c82bcb1469
1 package PVE
::API2Client
;
10 use Data
::Dumper
; # fixme: remove
11 use HTTP
::Request
::Common
;
14 my ($self, $path, $param) = @_;
16 return $self->call('GET', $path, $param);
20 my ($self, $path, $param) = @_;
22 return $self->call('POST', $path, $param);
26 my ($self, $path, $param) = @_;
28 return $self->call('PUT', $path, $param);
32 my ($self, $path, $param) = @_;
34 return $self->call('DELETE', $path, $param);
38 my ($self, $ticket) = @_;
40 my $domain = "$self->{host}.local" unless $self->{host
} =~ /\./;
41 $self->{cookie_jar
}->set_cookie(0, 'PVEAuthCookie', $ticket, '/', $domain);
45 my ($self, $method, $path, $param) = @_;
47 #print "wrapper called\n";
51 my $ua = $self->{useragent
};
52 my $cj = $self->{cookie_jar
};
55 my ($version, $key, $val) = @_;
56 $ticket = $val if $key eq 'PVEAuthCookie';
59 if (!$ticket && $self->{username
} && $self->{password
}) {
61 $uri->scheme($self->{protocol
});
62 $uri->host($self->{host
});
63 $uri->port($self->{port
});
64 $uri->path('/api2/json/access/ticket');
66 my $response = $ua->post($uri, {
67 username
=> $self->{username
},
68 password
=> $self->{password
}});
70 if (!$response->is_success) {
71 die $response->status_line . "\n";
74 my $data = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
76 $self->update_ticket($data->{data
}->{ticket
});
77 $self->{csrftoken
} = $data->{data
}->{CSRFPreventionToken
};
81 $uri->scheme($self->{protocol
});
82 $uri->host($self->{host
});
83 $uri->port($self->{port
});
86 # print $ua->{cookie_jar}->as_string;
88 #print "CALL $method : " . $uri->as_string() . "\n";
90 if ($self->{csrftoken
}) {
91 $self->{useragent
}->default_header('CSRFPreventionToken' => $self->{csrftoken
});
95 if ($method eq 'GET') {
96 $uri->query_form($param);
97 $response = $ua->request(HTTP
::Request
::Common
::GET
($uri));
98 } elsif ($method eq 'POST') {
99 $response = $ua->request(HTTP
::Request
::Common
::POST
($uri, Content
=> $param));
100 } elsif ($method eq 'PUT') {
101 # We use another temporary URI object to format
102 # the application/x-www-form-urlencoded content.
104 my $tmpurl = URI-
>new('http:');
105 $tmpurl->query_form(%$param);
106 my $content = $tmpurl->query;
108 $response = $ua->request(HTTP
::Request
::Common
::PUT
($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content
=> $content));
110 } elsif ($method eq 'DELETE') {
111 $response = $ua->request(HTTP
::Request
::Common
::DELETE
($uri));
113 die "method $method not implemented\n";
116 #print "RESP: " . Dumper($response) . "\n";
118 my $ct = $response->header('Content-Type') || '';
120 if ($response->is_success) {
122 die "got unexpected content type" if $ct !~ m
|application
/json
|;
124 return from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
128 my $msg = $response->status_line . "\n";
130 return if $ct !~ m
|application
/json
|;
131 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
132 if (my $errors = $res->{errors
}) {
133 foreach my $key (keys $errors) {
134 my $m = $errors->{$key};
137 $msg .= " $key: $m\n";
147 my ($class, %param) = @_;
150 ticket
=> $param{ticket
},
151 csrftoken
=> $param{csrftoken
},
152 username
=> $param{username
},
153 password
=> $param{password
},
154 host
=> $param{host
} || 'localhost',
155 port
=> $param{port
},
156 protocol
=> $param{protocol
},
157 timeout
=> $param{timeout
} || 60,
161 if (!$self->{port
}) {
162 $self->{port
} = $self->{host
} eq 'localhost' ?
85 : 8006;
164 if (!$self->{protocol
}) {
165 $self->{protocol
} = $self->{host
} eq 'localhost' ?
'http' : 'https';
168 $self->{cookie_jar
} = HTTP
::Cookies-
>new (ignore_discard
=> 1);
170 $self->update_ticket($self->{ticket
}) if $self->{ticket
};
172 $self->{useragent
} = LWP
::UserAgent-
>new(
173 cookie_jar
=> $self->{cookie_jar
},
174 protocols_allowed
=> [ 'http', 'https'],
175 ssl_opts
=> { verify_hostname
=> 0 },
176 timeout
=> $self->{timeout
},
179 $self->{useragent
}->default_header('Accept-Encoding' => 'gzip'); # allow gzip