From a1d991d1f9282fa6deac0f17d25d8488c50af132 Mon Sep 17 00:00:00 2001 From: Stefan Reusch Date: Thu, 6 Apr 2017 13:29:39 +0200 Subject: [PATCH] changed function calls to PUT / DELETE --- lib/Nitrapi/Common/Http/Client.php | 48 +++++++++++++++++++ .../Services/Gameservers/Packages/Package.php | 4 +- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/Nitrapi/Common/Http/Client.php b/lib/Nitrapi/Common/Http/Client.php index cf104a5..6eb1711 100644 --- a/lib/Nitrapi/Common/Http/Client.php +++ b/lib/Nitrapi/Common/Http/Client.php @@ -67,6 +67,54 @@ public function dataGet($url, $headers = null, $options = array()) { return (isset($json['data'])) ? $json['data'] : $json['message']; } + /** + * @param $url + * @param array $body + * @param array $headers + * @param array $options + * @return mixed + */ + public function dataPut($url, $body = null, $headers = null, $options = array()) { + try { + if (is_array($body)) { + $options['form_params'] = $body; + } + if (is_array($headers)) { + $options['headers'] = $headers; + } + if (is_array($options) && isset($options['query'])) { + $options['query'] = array_merge($options['query'], $this->defaultQuery); + } + + $response = $this->request('PUT', $url, $options); + $this->checkErrors($response); + $json = json_decode($response->getBody(), true); + } catch (RequestException $e) { + if ($e->hasResponse()) { + $response = json_decode($e->getResponse()->getBody(), true); + $msg = isset($response['message']) ? $response['message'] : 'Unknown error'; + if ($e->getResponse()->getStatusCode() == 503) { + throw new NitrapiMaintenanceException($msg); + } + if ($e->getResponse()->getStatusCode() == 428) { + throw new NitrapiConcurrencyException($msg); + } + throw new NitrapiHttpErrorException($msg); + } + throw new NitrapiHttpErrorException($e->getMessage()); + } + + if (isset($json['data']) && is_array($json['data'])) { + return $json['data']; + } + + if (!empty($json['message'])) { + return $json['message']; + } + + return true; + } + /** * @param $url * @param array $body diff --git a/lib/Nitrapi/Services/Gameservers/Packages/Package.php b/lib/Nitrapi/Services/Gameservers/Packages/Package.php index 5ea00e9..2959c9a 100644 --- a/lib/Nitrapi/Services/Gameservers/Packages/Package.php +++ b/lib/Nitrapi/Services/Gameservers/Packages/Package.php @@ -55,13 +55,13 @@ public function install($version) { } public function uninstall() { $url = "/services/".$this->service->getId()."/gameservers/packages/uninstall"; - return $this->service->getApi()->dataPost($url, array( + return $this->service->getApi()->dataDelete($url, array( "package" => $this->name )); } public function reinstall() { $url = "/services/".$this->service->getId()."/gameservers/packages/reinstall"; - return $this->service->getApi()->dataPost($url, array( + return $this->service->getApi()->dataPut($url, array( "package" => $this->name )); }