]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2Client.pm
1 package PVE
::API2Client
;
9 use Data
::Dumper
; # fixme: remove
10 use HTTP
::Request
::Common
;
13 my ($self, $path, $param) = @_;
15 return $self->call('GET', $path, $param);
19 my ($self, $path, $param) = @_;
21 return $self->call('POST', $path, $param);
25 my ($self, $path, $param) = @_;
27 return $self->call('PUT', $path, $param);
31 my ($self, $path, $param) = @_;
33 return $self->call('DELETE', $path, $param);
37 my ($self, $ticket) = @_;
39 my $domain = "$self->{host}.local" unless $self->{host
} =~ /\./;
40 $self->{cookie_jar
}->set_cookie(0, 'PVEAuthCookie', $ticket, '/', $domain);
47 $uri->scheme($self->{protocol
});
48 $uri->host($self->{host
});
49 $uri->port($self->{port
});
50 $uri->path('/api2/json/access/ticket');
52 my $ua = $self->{useragent
};
54 my $response = $ua->post($uri, {
55 username
=> $self->{username
} || 'unknown',
56 password
=> $self->{password
} || ''});
58 if (!$response->is_success) {
59 die $response->status_line . "\n";
62 my $data = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
64 $self->update_ticket($data->{data
}->{ticket
});
65 $self->{csrftoken
} = $data->{data
}->{CSRFPreventionToken
};
71 my ($self, $method, $path, $param) = @_;
73 #print "wrapper called\n";
77 my $ua = $self->{useragent
};
78 my $cj = $self->{cookie_jar
};
81 my ($version, $key, $val) = @_;
82 $ticket = $val if $key eq 'PVEAuthCookie';
85 if (!$ticket && $self->{username
} && $self->{password
}) {
90 $uri->scheme($self->{protocol
});
91 $uri->host($self->{host
});
92 $uri->port($self->{port
});
95 # print $ua->{cookie_jar}->as_string;
97 #print "CALL $method : " . $uri->as_string() . "\n";
99 if ($self->{csrftoken
}) {
100 $self->{useragent
}->default_header('CSRFPreventionToken' => $self->{csrftoken
});
104 if ($method eq 'GET') {
105 $uri->query_form($param);
106 $response = $ua->request(HTTP
::Request
::Common
::GET
($uri));
107 } elsif ($method eq 'POST') {
108 $response = $ua->request(HTTP
::Request
::Common
::POST
($uri, Content
=> $param));
109 } elsif ($method eq 'PUT') {
110 # We use another temporary URI object to format
111 # the application/x-www-form-urlencoded content.
113 my $tmpurl = URI-
>new('http:');
114 $tmpurl->query_form(%$param);
115 my $content = $tmpurl->query;
117 $response = $ua->request(HTTP
::Request
::Common
::PUT
($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content
=> $content));
119 } elsif ($method eq 'DELETE') {
120 $response = $ua->request(HTTP
::Request
::Common
::DELETE
($uri));
122 die "method $method not implemented\n";
125 #print "RESP: " . Dumper($response) . "\n";
127 my $ct = $response->header('Content-Type') || '';
129 if ($response->is_success) {
131 die "got unexpected content type" if $ct !~ m
|application
/json
|;
133 return from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
137 my $msg = $response->status_line . "\n";
139 return if $ct !~ m
|application
/json
|;
140 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
141 if (my $errors = $res->{errors
}) {
142 foreach my $key (keys $errors) {
143 my $m = $errors->{$key};
146 $msg .= " $key: $m\n";
156 my ($class, %param) = @_;
159 ticket
=> $param{ticket
},
160 csrftoken
=> $param{csrftoken
},
161 username
=> $param{username
},
162 password
=> $param{password
},
163 host
=> $param{host
} || 'localhost',
164 port
=> $param{port
},
165 protocol
=> $param{protocol
},
166 timeout
=> $param{timeout
} || 60,
170 if (!$self->{port
}) {
171 $self->{port
} = $self->{host
} eq 'localhost' ?
85 : 8006;
173 if (!$self->{protocol
}) {
174 $self->{protocol
} = $self->{host
} eq 'localhost' ?
'http' : 'https';
177 $self->{cookie_jar
} = HTTP
::Cookies-
>new (ignore_discard
=> 1);
179 $self->update_ticket($self->{ticket
}) if $self->{ticket
};
181 $self->{useragent
} = LWP
::UserAgent-
>new(
182 cookie_jar
=> $self->{cookie_jar
},
183 protocols_allowed
=> [ 'http', 'https'],
184 ssl_opts
=> { verify_hostname
=> 0 },
185 timeout
=> $self->{timeout
},
188 $self->{useragent
}->default_header('Accept-Encoding' => 'gzip'); # allow gzip