Commit 49f2b062 authored by Simon Welsh's avatar Simon Welsh

Add a method to fetch all droplets (from the first page at least)

parent 0e5f7bae
...@@ -163,4 +163,35 @@ class Droplet extends Object { ...@@ -163,4 +163,35 @@ class Droplet extends Object {
$this->kernel = static::_toKernel($data->at('kernel'), 'kernel'); $this->kernel = static::_toKernel($data->at('kernel'), 'kernel');
} }
} }
public static function get_all_droplets(
string $api_token
): Map<string, Droplet> {
$ch = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $api_token,
'Content-Type: application/json',
),
));
$ret = curl_exec($ch);
$json = json_decode($ret, true);
$drops = Map {};
if (array_key_exists('droplets', $json)) {
$droplets = $json['droplets'];
foreach ($droplets as $drop) {
$data = new Map($drop);
$drop = new Droplet($data);
$drops[$drop->name] = $drop;
}
}
return $drops;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment