Skip to content

Commit 3e99b7b

Browse files
committed
Merge branch 'main' of github.com:Art-of-WiFi/UniFi-API-client into main
merging PRs
2 parents 23b931a + ee0bfe4 commit 3e99b7b

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

src/Client.php

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use UniFi_API\Exceptions\LoginFailedException;
1515
use UniFi_API\Exceptions\LoginRequiredException;
1616
use UniFi_API\Exceptions\MethodDeprecatedException;
17+
use UniFi_API\Exceptions\NotAnOsConsoleException;
1718

1819
/**
1920
* The UniFi API client class.
@@ -3257,6 +3258,40 @@ public function check_controller_update()
32573258
return $this->fetch_results('/api/s/' . $this->site . '/stat/fwupdate/latest-version');
32583259
}
32593260

3261+
/**
3262+
* Get the recent firmware update for an UniFi-OS console
3263+
*
3264+
* @return array|bool returns an array with a single object containing details of the current known latest
3265+
* UniFi OS version info on success, else returns false
3266+
* @throws Exception
3267+
*/
3268+
public function get_update_os_console()
3269+
{
3270+
if (!$this->is_unifi_os) {
3271+
throw new NotAnOsConsoleException();
3272+
}
3273+
3274+
return $this->fetch_results('/api/firmware/update', null, false, true, false);
3275+
}
3276+
3277+
/**
3278+
* Update the OS for an UniFi-OS console
3279+
*
3280+
* @note triggers an UniFi OS Update in Control Plane > Updates > UniFi OS
3281+
* @return bool true upon success
3282+
* @throws Exception
3283+
*/
3284+
public function update_os_console(): bool
3285+
{
3286+
if (!$this->is_unifi_os) {
3287+
throw new NotAnOsConsoleException();
3288+
}
3289+
3290+
$payload = ['persistFullData' => true];
3291+
3292+
return $this->fetch_results_boolean('/api/firmware/update', $payload, true, false);
3293+
}
3294+
32603295
/**
32613296
* Check firmware update.
32623297
*
@@ -4174,19 +4209,23 @@ protected function fetch_results(
41744209
string $path,
41754210
$payload = null,
41764211
bool $boolean = false,
4177-
bool $login_required = true
4212+
bool $login_required = true,
4213+
bool $prefix_path = true
41784214
)
41794215
{
41804216
/** Guard clause to check if logged in when needed. */
41814217
if ($login_required && !$this->is_logged_in) {
41824218
throw new LoginRequiredException();
41834219
}
41844220

4185-
$this->last_results_raw = $this->exec_curl($path, $payload);
4221+
$this->last_results_raw = $this->exec_curl($path, $payload, $prefix_path);
41864222

41874223
if (is_string($this->last_results_raw)) {
41884224
$response = json_decode($this->last_results_raw);
4189-
$this->get_json_last_error();
4225+
4226+
if (!empty($this->last_results_raw)) {
4227+
$this->get_json_last_error();
4228+
}
41904229

41914230
if (isset($response->meta->rc)) {
41924231
if ($response->meta->rc === 'ok') {
@@ -4226,6 +4265,24 @@ protected function fetch_results(
42264265

42274266
return $response;
42284267
}
4268+
4269+
/** Deal with a responses from an UniFi OS console */
4270+
if (strpos($path, '/api/') === 0) {
4271+
if (isset($response->code)) {
4272+
$this->last_error_message = 'An unknown error was returned by an UniFi OS endpoint.';
4273+
if (isset($response->message)) {
4274+
$this->last_error_message = $response->message;
4275+
}
4276+
4277+
throw new Exception('Error code: ' . $response->code . ', message: ' . $this->last_error_message);
4278+
}
4279+
4280+
if (is_object($response) && !$boolean) {
4281+
return [$response];
4282+
}
4283+
4284+
return true;
4285+
}
42294286
}
42304287

42314288
return false;
@@ -4241,9 +4298,14 @@ protected function fetch_results(
42414298
* @return bool [description]
42424299
* @throws Exception
42434300
*/
4244-
protected function fetch_results_boolean(string $path, $payload = null, bool $login_required = true): bool
4301+
protected function fetch_results_boolean(
4302+
string $path,
4303+
$payload = null,
4304+
bool $login_required = true,
4305+
bool $prefix_path = true
4306+
): bool
42454307
{
4246-
return $this->fetch_results($path, $payload, true, $login_required);
4308+
return $this->fetch_results($path, $payload, true, $login_required, $prefix_path);
42474309
}
42484310

42494311
/**
@@ -4433,15 +4495,15 @@ protected function response_header_callback($ch, string $header_line): int
44334495
* @return bool|string response returned by the controller API
44344496
* @throws CurlGeneralErrorException|CurlTimeoutException|InvalidCurlMethodException|LoginFailedException
44354497
*/
4436-
protected function exec_curl(string $path, $payload = null)
4498+
protected function exec_curl(string $path, $payload = null, $prefix_path = true)
44374499
{
44384500
if (!in_array($this->curl_method, self::CURL_METHODS_ALLOWED)) {
44394501
throw new InvalidCurlMethodException();
44404502
}
44414503

44424504
$url = $this->baseurl . $path;
44434505

4444-
if ($this->is_unifi_os) {
4506+
if ($this->is_unifi_os && $prefix_path) {
44454507
$url = $this->baseurl . '/proxy/network' . $path;
44464508
}
44474509

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace UniFi_API\Exceptions;
4+
5+
use Exception;
6+
7+
class NotAnOsConsoleException extends Exception
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct('This console is not an UniFi OS console.');
12+
}
13+
}

0 commit comments

Comments
 (0)