From 58bb9f1ea26d8076603255f90d167ee015f6950a Mon Sep 17 00:00:00 2001 From: mrquatsch Date: Sat, 17 Feb 2018 08:59:47 -0600 Subject: [PATCH] Updated to be enhanced --- app/SupportedApps/Plexpy.php | 69 ++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/app/SupportedApps/Plexpy.php b/app/SupportedApps/Plexpy.php index 212c3dd1..4b044fac 100644 --- a/app/SupportedApps/Plexpy.php +++ b/app/SupportedApps/Plexpy.php @@ -1,6 +1,12 @@ buildRequest('arnold'); + switch($res->getStatusCode()) { + case 200: + $data = json_decode($res->getBody()); + if(isset($data->error) && !empty($data->error)) { + echo 'Failed: '.$data->error; + } else { + echo 'Successfully connected to the API'; + } + break; + case 401: + echo 'Failed: Invalid credentials'; + break; + case 404: + echo 'Failed: Please make sure your URL is correct and that there is a trailing slash'; + break; + default: + echo 'Something went wrong... Code: '.$res->getStatusCode(); + break; + } + } + public function executeConfig() + { + $output = ''; + $res = $this->buildRequest('get_activity'); + $data = json_decode($res->getBody()); + $stream_count = $data->response->data->stream_count; + + $output = ' + + '; + + return $output; + } + public function buildRequest($endpoint) + { + $config = $this->config; + $url = $config->url; + $apikey = $config->apikey; + + $url = rtrim($url, '/'); + + $api_url = $url.'/api/v2?apikey='.$apikey.'&cmd='.$endpoint; + //die( $api_url.' --- '); + + $client = new Client(['http_errors' => false]); + $res = $client->request('GET', $api_url); + return $res; + + } + +}