]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
1 package PVE
::APIClient
::Helpers
;
8 use PVE
::APIClient
::Exception
qw(raise);
11 use HTTP
::Status
qw(:constants);
13 my $pve_api_definition;
14 my $pve_api_path_hash;
16 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.js";
18 my $build_pve_api_path_hash;
19 $build_pve_api_path_hash = sub {
22 my $class = ref($tree);
23 return $tree if !$class;
25 if ($class eq 'ARRAY') {
26 foreach my $el (@$tree) {
27 $build_pve_api_path_hash->($el);
29 } elsif ($class eq 'HASH') {
30 if (defined($tree->{leaf
}) && defined(my $path = $tree->{path
})) {
31 $pve_api_path_hash->{$path} = $tree;
33 foreach my $k (keys %$tree) {
34 $build_pve_api_path_hash->($tree->{$k});
39 sub get_api_definition
{
41 if (!defined($pve_api_definition)) {
43 open(my $fh, '<', $pve_api_definition_fn) ||
44 die "unable to open '$pve_api_definition_fn' - $!\n";
45 my $json_text = <$fh>;
46 $pve_api_definition = decode_json
($json_text);
48 $build_pve_api_path_hash->($pve_api_definition);
52 return $pve_api_definition;
55 sub lookup_api_method
{
56 my ($path, $method) = @_;
58 get_api_definition
(); # make sure API data is loaded
60 my $info = $pve_api_path_hash->{$path} ||
61 die "unable to find API info for path '$path'\n";
63 my $data = $info->{info
}->{$method} ||
64 die "unable to find API method '$method' for path '$path'\n";