feat: 🔒️ Added error handling on server attribute fetching
This commit is contained in:
parent
48d5e59fda
commit
d09004df17
|
@ -48,7 +48,11 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public static function getEggs(Nest $nest)
|
public static function getEggs(Nest $nest)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . self::PER_PAGE);
|
$response = self::client()->get("/application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . self::PER_PAGE);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
return $response->json()['data'];
|
return $response->json()['data'];
|
||||||
}
|
}
|
||||||
|
@ -59,7 +63,11 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public static function getNodes()
|
public static function getNodes()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->get('/application/nodes?per_page=' . self::PER_PAGE);
|
$response = self::client()->get('/application/nodes?per_page=' . self::PER_PAGE);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
return $response->json()['data'];
|
return $response->json()['data'];
|
||||||
}
|
}
|
||||||
|
@ -70,7 +78,11 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public static function getNests()
|
public static function getNests()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->get('/application/nests?per_page=' . self::PER_PAGE);
|
$response = self::client()->get('/application/nests?per_page=' . self::PER_PAGE);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
return $response->json()['data'];
|
return $response->json()['data'];
|
||||||
}
|
}
|
||||||
|
@ -81,8 +93,13 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public static function getLocations()
|
public static function getLocations()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->get('/application/locations?per_page=' . self::PER_PAGE);
|
$response = self::client()->get('/application/locations?per_page=' . self::PER_PAGE);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
|
|
||||||
return $response->json()['data'];
|
return $response->json()['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,8 +142,13 @@ class Pterodactyl
|
||||||
public static function getAllocations(Node $node)
|
public static function getAllocations(Node $node)
|
||||||
{
|
{
|
||||||
$per_page = Configuration::getValueByKey('ALLOCATION_LIMIT', 200);
|
$per_page = Configuration::getValueByKey('ALLOCATION_LIMIT', 200);
|
||||||
|
try {
|
||||||
$response = self::client()->get("/application/nodes/{$node->id}/allocations?per_page={$per_page}");
|
$response = self::client()->get("/application/nodes/{$node->id}/allocations?per_page={$per_page}");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
|
|
||||||
return $response->json();
|
return $response->json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,15 +198,25 @@ class Pterodactyl
|
||||||
|
|
||||||
public static function suspendServer(Server $server)
|
public static function suspendServer(Server $server)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
|
$response = self::client()->post("/application/servers/$server->pterodactyl_id/suspend");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function unSuspendServer(Server $server)
|
public static function unSuspendServer(Server $server)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
|
$response = self::client()->post("/application/servers/$server->pterodactyl_id/unsuspend");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
if ($response->failed()) throw self::getException();
|
if ($response->failed()) throw self::getException();
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,9 +227,29 @@ class Pterodactyl
|
||||||
*/
|
*/
|
||||||
public function getUser(int $pterodactylId)
|
public function getUser(int $pterodactylId)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$response = self::client()->get("/application/users/{$pterodactylId}");
|
$response = self::client()->get("/application/users/{$pterodactylId}");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
|
if ($response->failed()) throw self::getException();
|
||||||
|
|
||||||
if ($response->failed()) return $response->json();
|
return $response->json()['attributes'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get serverAttributes by pterodactyl id
|
||||||
|
* @param int $pterodactylId
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function getServerAttributes(string $pterodactylId)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = self::client()->get("/application/servers/{$pterodactylId}?include=egg,nest,location");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw self::getException();
|
||||||
|
}
|
||||||
|
if ($response->failed()) throw self::getException();
|
||||||
return $response->json()['attributes'];
|
return $response->json()['attributes'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class ServerController extends Controller
|
||||||
foreach ($servers as $server) {
|
foreach ($servers as $server) {
|
||||||
|
|
||||||
//Get server infos from ptero
|
//Get server infos from ptero
|
||||||
$serverAttributes = Pterodactyl::client()->get('/application/servers/' . $server->pterodactyl_id . '?include=egg,nest,location')->json()['attributes'];
|
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
|
||||||
|
|
||||||
$serverRelationships = $serverAttributes['relationships'];
|
$serverRelationships = $serverAttributes['relationships'];
|
||||||
$serverLocationAttributes = $serverRelationships['location']['attributes'];
|
$serverLocationAttributes = $serverRelationships['location']['attributes'];
|
||||||
|
|
Loading…
Reference in a new issue