From 000278569e05aff977c6adf5b99a86792a6b3dae Mon Sep 17 00:00:00 2001 From: jan <j.waldecker@lumaserv.com> Date: Tue, 11 Oct 2016 15:57:53 +0200 Subject: [PATCH] update to guzzlehttp 6.2.1 --- composer.json | 16 +++++++++++----- src/Proxmox.php | 37 +++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 8518901..941974f 100644 --- a/composer.json +++ b/composer.json @@ -1,28 +1,34 @@ { - "name": "zzantares/proxmoxve", + "name": "lumaserv/proxmoxve", "type": "library", "description": "A simple PHP 5.4+ Proxmox API client.", "keywords": ["Proxmox", "API", "KVM", "OpenVZ", "Virtualization"], "homepage": "https://github.com/ZzAntares/ProxmoxVE", "license": "MIT", + "version": "1.1", "authors": [ { "name": "César Muñoz", "email": "zzantares@gmail.com", "role": "Developer" + }, + { + "name": "LUMASERV Systems", + "email": "info@lumaserv.com", + "role": "Developer" } ], "support": { - "issues": "https://github.com/ZzAntares/ProxmoxVE/issues", - "source": "https://github.com/ZzAntares/ProxmoxVE" + "issues": "https://github.com/LUMASERV/ProxmoxVE/issues", + "source": "https://github.com/LUMASERV/ProxmoxVE" }, "require": { - "php": ">=5.3.0", + "php": ">=5.5", "lib-curl": "*", - "guzzlehttp/guzzle": "~4.0" + "guzzlehttp/guzzle": "6.2.1" }, "require-dev": { diff --git a/src/Proxmox.php b/src/Proxmox.php index 0709e8d..5415f02 100644 --- a/src/Proxmox.php +++ b/src/Proxmox.php @@ -9,8 +9,10 @@ namespace ProxmoxVE; +use GuzzleHttp\Client; use ProxmoxVE\Exception\MalformedCredentialsException; use ProxmoxVE\Exception\AuthenticationException; +use Psr\Http\Message\ResponseInterface; /** * ProxmoxVE class. In order to interact with the proxmox server, the desired @@ -54,6 +56,9 @@ class Proxmox */ private $authToken; + /** @var Client $httpClient */ + private $httpClient; + /** * Constructor. @@ -61,10 +66,8 @@ class Proxmox * @param mixed $credentials Credentials object or associative array holding * the login data. * - * @throws \ProxmoxVE\Exception\MalformedCredentialsException If bad args - * supplied. - * @throws \ProxmoxVE\Exception\AuthenticationException If given credentials - * are not valid. + * @param string $responseType + * @param null $httpClient */ public function __construct( $credentials, @@ -89,7 +92,7 @@ public function __construct( * @param string $method HTTP method used in the request, by default * 'GET' method will be used. * - * @return \Guzzle\Http\Message\Response + * @return ResponseInterface * * @throws \InvalidArgumentException If the given HTTP method is not one of * 'GET', 'POST', 'PUT', 'DELETE', @@ -106,7 +109,8 @@ private function requestResource($actionPath, $params = [], $method = 'GET') $headers = [ 'CSRFPreventionToken' => $this->authToken->getCsrf(), ]; - } + } else + $headers = []; switch ($method) { case 'GET': @@ -123,7 +127,7 @@ private function requestResource($actionPath, $params = [], $method = 'GET') 'exceptions' => false, 'cookies' => $cookies, 'headers' => $headers, - 'body' => $params, + 'form_params' => $params, ]); break; case 'PUT': @@ -132,7 +136,7 @@ private function requestResource($actionPath, $params = [], $method = 'GET') 'exceptions' => false, 'cookies' => $cookies, 'headers' => $headers, - 'body' => $params, + 'form_params' => $params, ]); break; case 'DELETE': @@ -141,7 +145,7 @@ private function requestResource($actionPath, $params = [], $method = 'GET') 'exceptions' => false, 'cookies' => $cookies, 'headers' => $headers, - 'body' => $params, + 'form_params' => $params, ]); break; default: @@ -154,7 +158,7 @@ private function requestResource($actionPath, $params = [], $method = 'GET') /** * Parses the response to the desired return type. * - * @param string $response Response sent by the Proxmox server. + * @param ResponseInterface $response Response sent by the Proxmox server. * * @return mixed The parsed response, depending on the response type can be * an array or a string. @@ -163,12 +167,17 @@ private function processHttpResponse($response) { switch ($this->fakeType) { case 'pngb64': - $base64 = base64_encode($response->getBody()); + $base64 = base64_encode($response->getBody()->__toString()); return 'data:image/png;base64,' . $base64; break; case 'object': // 'object' not supported yet, we return array instead. case 'array': - return $response->json(); + $result = json_decode($response->getBody()->__toString(), true); + if (json_last_error() == JSON_ERROR_NONE) { + return $result; + } else { + return $response; + } break; default: return $response->getBody()->__toString(); @@ -184,7 +193,7 @@ private function processHttpResponse($response) */ public function setHttpClient($httpClient = null) { - $this->httpClient = $httpClient ?: new \GuzzleHttp\Client(); + $this->httpClient = $httpClient ?: new Client(); } @@ -210,7 +219,7 @@ public function login() ], ]); - $response = $response->json(); + $response = json_decode($response->getBody()->__toString(), true); if (!$response['data']) { $error = 'Can not login using credentials: ' . $this->credentials;