]>
git.proxmox.com Git - pve-manager.git/blob - lib/PVE/API2Client.pm
cf998f75bd537caf87c18741428c276e4f283727
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, $method, $path, $param) = @_;
40 #print "wrapper called\n";
44 my $ua = $self->{useragent
};
45 my $cj = $self->{cookie_jar
};
48 my ($version, $key, $val) = @_;
49 $ticket = $val if $key eq 'PVEAuthCookie';
52 if (!$ticket && $self->{username
} && $self->{password
}) {
54 $uri->scheme($self->{protocol
});
55 $uri->host($self->{host
});
56 $uri->port($self->{port
});
57 $uri->path('/api2/json/ticket');
59 my $response = $ua->post($uri, {
60 username
=> $self->{username
},
61 password
=> $self->{password
}});
63 if (!$response->is_success) {
64 die $response->status_line . "\n";
66 # the auth cookie should be set now
70 $uri->scheme($self->{protocol
});
71 $uri->host($self->{host
});
72 $uri->port($self->{port
});
75 # print $ua->{cookie_jar}->as_string;
77 #print "CALL $method : " . $uri->as_string() . "\n";
80 if ($method eq 'GET') {
81 $uri->query_form($param);
82 $response = $ua->request(HTTP
::Request
::Common
::GET
($uri));
83 } elsif ($method eq 'POST') {
84 $response = $ua->request(HTTP
::Request
::Common
::POST
($uri, Content
=> $param));
85 } elsif ($method eq 'PUT') {
86 $response = $ua->request(HTTP
::Request
::Common
::PUT
($uri, Content
=> $param));
87 } elsif ($method eq 'DELETE') {
88 $response = $ua->request(HTTP
::Request
::Common
::DELETE
($uri));
90 die "method $method not implemented\n";
93 #print "RESP: " . Dumper($response) . "\n";
95 if ($response->is_success) {
96 my $ct = $response->header('Content-Type');
98 die "got unexpected content type" if $ct ne 'application/json';
100 return from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
104 die $response->status_line . "\n";
110 my ($class, %param) = @_;
113 ticket
=> $param{ticket
},
114 username
=> $param{username
},
115 password
=> $param{password
},
116 host
=> $param{host
} || 'localhost',
117 port
=> $param{port
},
118 timeout
=> $param{timeout
} || 60,
122 if (!$self->{port
}) {
123 $self->{port
} = $self->{host
} eq 'localhost' ?
85 : 8006;
125 if (!$self->{protocol
}) {
126 $self->{protocol
} = $self->{host
} eq 'localhost' ?
'http' : 'https';
129 $self->{cookie_jar
} = HTTP
::Cookies-
>new (ignore_discard
=> 1);
131 if ($self->{ticket
}) {
132 my $domain = "$self->{host}.local" unless $self->{host
} =~ /\./;
133 $self->{cookie_jar
}->set_cookie(0, 'PVEAuthCookie', $self->{ticket
},
137 $self->{useragent
} = LWP
::UserAgent-
>new(
138 cookie_jar
=> $self->{cookie_jar
},
139 protocols_allowed
=> [ 'http', 'https'],
140 timeout
=> $self->{timeout
},
143 $self->{useragent
}->default_header('Accept-Encoding' => 'gzip'); # allow gzip