Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from stevenscg/cs-guzzle6
Browse files Browse the repository at this point in the history
[HOLD] Upgrade Guzzle to ~6.0
  • Loading branch information
jippi authored Dec 12, 2016
2 parents a611659 + b7e507e commit 97dba9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"guzzlehttp/guzzle": ">=4,<6",
"guzzlehttp/guzzle": "~6.0",
"psr/log": "~1.0"
},
"autoload": {
Expand Down
60 changes: 34 additions & 26 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Psr7\Request;

use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

use Jippi\Vault\Exception\ClientException;
use Jippi\Vault\Exception\ServerException;

Expand All @@ -14,59 +17,63 @@ class Client
private $client;
private $logger;

public function __construct(array $options = array(), LoggerInterface $logger = null, GuzzleClient $client = null)
public function __construct(array $options = [], LoggerInterface $logger = null, GuzzleClient $client = null)
{
$options = array_replace(array(
'base_url' => 'http://127.0.0.1:8200',
), $options);
$options = array_replace([
'base_uri' => 'http://127.0.0.1:8200',
'http_errors' => false,
'headers' => [
'User-Agent' => 'Vault-PHP-SDK/1.0',
'Content-Type' => 'application/json',
],
], $options);

$this->client = $client ?: new GuzzleClient($options);
$this->client->setDefaultOption('exceptions', false);
$this->logger = $logger ?: new NullLogger();
}

public function get($url = null, array $options = array())
public function get($url = null, array $options = [])
{
return $this->send($this->client->createRequest('GET', $url, $options));
return $this->send(new Request('GET', $url), $options);
}

public function head($url, array $options = array())
public function head($url, array $options = [])
{
return $this->send($this->client->createRequest('HEAD', $url, $options));
return $this->send(new Request('HEAD', $url), $options);
}

public function delete($url, array $options = array())
public function delete($url, array $options = [])
{
return $this->send($this->client->createRequest('DELETE', $url, $options));
return $this->send(new Request('DELETE', $url), $options);
}

public function put($url, array $options = array())
public function put($url, array $options = [])
{
return $this->send($this->client->createRequest('PUT', $url, $options));
return $this->send(new Request('PUT', $url), $options);
}

public function patch($url, array $options = array())
public function patch($url, array $options = [])
{
return $this->send($this->client->createRequest('PATCH', $url, $options));
return $this->send(new Request('PATCH', $url), $options);
}

public function post($url, array $options = array())
public function post($url, array $options = [])
{
return $this->send($this->client->createRequest('POST', $url, $options));
return $this->send(new Request('POST', $url), $options);
}

public function options($url, array $options = array())
public function options($url, array $options = [])
{
return $this->send($this->client->createRequest('OPTIONS', $url, $options));
return $this->send(new Request('OPTIONS', $url), $options);
}

public function send(RequestInterface $request)
public function send(RequestInterface $request, $options = [])
{
$this->logger->info(sprintf('%s "%s"', $request->getMethod(), $request->getUrl()));
$this->logger->debug(sprintf("Request:\n%s", (string) $request));
$this->logger->info(sprintf('%s "%s"', $request->getMethod(), $request->getUri()));
$this->logger->debug(sprintf("Request:\n%s\n%s\n%s", $request->getUri(), $request->getMethod(), json_encode($request->getHeaders())));

try {
$response = $this->client->send($request);
$response = $this->client->send($request, $options);
} catch (TransferException $e) {
$message = sprintf('Something went wrong when calling vault (%s).', $e->getMessage());

Expand All @@ -75,14 +82,15 @@ public function send(RequestInterface $request)
throw new ServerException($message);
}

$this->logger->debug(sprintf("Response:\n%s", $response));
$this->logger->debug(sprintf("Response:\n%s", (string) $response->getBody()));

if (400 <= $response->getStatusCode()) {
$message = sprintf('Something went wrong when calling vault (%s - %s).', $response->getStatusCode(), $response->getReasonPhrase());

$this->logger->error($message);
$this->logger->debug(sprintf("Response:\n%s\n%s\n%s", $response->getStatusCode(), json_encode($response->getHeaders()), $response->getBody()->getContents()));

$message .= "\n$response";
$message .= "\n" . (string) $response->getBody();
if (500 <= $response->getStatusCode()) {
throw new ServerException($message, $response->getStatusCode(), $response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Data
private $client;

/**
* Create a new Sys service with an optional Client
* Create a new Data service with an optional Client
*
* @param Client|null $client
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Sys.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function seal()
*/
public function sealed()
{
return $this->sealStatus()->json()['sealed'];
return json_decode($this->sealStatus()->getBody(), true)['sealed'];
}

/**
Expand All @@ -105,7 +105,7 @@ public function sealed()
*/
public function unsealed()
{
return !$this->sealStatus()->json()['sealed'];
return !json_decode($this->sealStatus()->getBody(), true)['sealed'];
}

/**
Expand Down

0 comments on commit 97dba9b

Please sign in to comment.