]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Helpers.pm
use get_options from PVE::JSONSchema
[pve-client.git] / PVE / APIClient / Helpers.pm
1 package PVE::APIClient::Helpers;
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7 use JSON;
8 use PVE::APIClient::Exception qw(raise);
9 use Encode::Locale;
10 use Encode;
11 use HTTP::Status qw(:constants);
12
13 my $pve_api_definition;
14 my $pve_api_path_hash;
15
16 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.js";
17
18 my $build_pve_api_path_hash;
19 $build_pve_api_path_hash = sub {
20 my ($tree) = @_;
21
22 my $class = ref($tree);
23 return $tree if !$class;
24
25 if ($class eq 'ARRAY') {
26 foreach my $el (@$tree) {
27 $build_pve_api_path_hash->($el);
28 }
29 } elsif ($class eq 'HASH') {
30 if (defined($tree->{leaf}) && defined(my $path = $tree->{path})) {
31 $pve_api_path_hash->{$path} = $tree;
32 }
33 foreach my $k (keys %$tree) {
34 $build_pve_api_path_hash->($tree->{$k});
35 }
36 }
37 };
38
39 sub get_api_definition {
40
41 if (!defined($pve_api_definition)) {
42 local $/;
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);
47
48 $build_pve_api_path_hash->($pve_api_definition);
49 }
50
51
52 return $pve_api_definition;
53 }
54
55 sub lookup_api_method {
56 my ($path, $method) = @_;
57
58 get_api_definition(); # make sure API data is loaded
59
60 my $info = $pve_api_path_hash->{$path} ||
61 die "unable to find API info for path '$path'\n";
62
63 my $data = $info->{info}->{$method} ||
64 die "unable to find API method '$method' for path '$path'\n";
65
66 return $data;
67 }
68
69 1;