Skip to content

Commit d1fe1d2

Browse files
committed
Update http/guzzle libs
closes #13, #20
1 parent 0284aab commit d1fe1d2

File tree

6 files changed

+66
-67
lines changed

6 files changed

+66
-67
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
"require": {
2828
"php": "~5.5|~7.0",
2929
"psr/http-message": "^1.0",
30-
"php-http/httplug": "^1.0",
31-
"php-http/discovery": "^1.0",
30+
"php-http/httplug": "^2.4.1",
31+
"php-http/discovery": "^1.20",
3232
"php-http/client-implementation": "^1.0",
33-
"php-http/client-common": "^1.1",
33+
"php-http/client-common": "^2.7.2",
3434
"php-http/cache-plugin": "^1.6"
3535
},
3636
"require-dev": {
3737
"phpunit/phpunit": "4.*",
3838
"mockery/mockery": "^0.9.5",
39-
"php-http/guzzle6-adapter": "^1.0",
39+
"php-http/guzzle7-adapter": "^1.0",
4040
"guzzlehttp/psr7": "^1.2",
41-
"php-http/mock-client": "^0.3",
41+
"php-http/mock-client": "^1.6.1",
4242
"squizlabs/php_codesniffer": "^2.6"
4343
},
4444
"autoload": {

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Opensaucesystems\Lxd\Exception\ServerException;
88
use Opensaucesystems\Lxd\HttpClient\Plugin\PathPrepend;
99
use Opensaucesystems\Lxd\HttpClient\Plugin\PathTrimEnd;
10-
use Opensaucesystems\Lxd\HttpClient\Plugin\LxdExceptionThower;
10+
use Opensaucesystems\Lxd\HttpClient\Plugin\LxdExceptionThrower;
1111
use Http\Client\Common\HttpMethodsClient;
1212
use Http\Client\Common\Plugin;
1313
use Http\Client\Common\PluginClient;
@@ -84,7 +84,7 @@ public function __construct(
8484
$this->url = $url ?: 'https://127.0.0.1:8443';
8585
$this->projectName = $projectName;
8686

87-
$this->addPlugin(new LxdExceptionThower());
87+
$this->addPlugin(new LxdExceptionThrower());
8888

8989
$this->setUrl($this->url);
9090
}

src/HttpClient/Plugin/LxdExceptionThower.php

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Opensaucesystems\Lxd\HttpClient\Plugin;
4+
5+
use Http\Promise\Promise;
6+
use Http\Client\Common\Plugin;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Http\Client\Exception\HttpException;
10+
use Opensaucesystems\Lxd\Exception\BadRequestException;
11+
use Opensaucesystems\Lxd\Exception\OperationException;
12+
use Opensaucesystems\Lxd\Exception\AuthenticationFailedException;
13+
use Opensaucesystems\Lxd\Exception\NotFoundException;
14+
use Opensaucesystems\Lxd\Exception\ConflictException;
15+
16+
/**
17+
* Handle LXD errors.
18+
*/
19+
class LxdExceptionThrower implements Plugin
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
25+
{
26+
return $next($request)->then(
27+
function (ResponseInterface $response) {
28+
// Successful response, just return it.
29+
return $response;
30+
},
31+
function (\Throwable $e) use ($request) {
32+
if ($e instanceof HttpException) {
33+
$response = $e->getResponse();
34+
$status = $response->getStatusCode();
35+
36+
switch ($status) {
37+
case 400:
38+
throw new BadRequestException($request, $response, $e);
39+
case 401:
40+
throw new OperationException($request, $response, $e);
41+
case 403:
42+
throw new AuthenticationFailedException($request, $response, $e);
43+
case 404:
44+
throw new NotFoundException($request, $response, $e);
45+
case 409:
46+
throw new ConflictException($request, $response, $e);
47+
}
48+
}
49+
50+
// Rethrow unhandled exceptions
51+
throw $e;
52+
}
53+
);
54+
}
55+
}

src/HttpClient/Plugin/PathPrepend.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Opensaucesystems\Lxd\HttpClient\Plugin;
44

5+
use Http\Promise\Promise;
56
use Http\Client\Common\Plugin;
67
use Psr\Http\Message\RequestInterface;
78

@@ -25,7 +26,7 @@ public function __construct($path)
2526
/**
2627
* {@inheritdoc}
2728
*/
28-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
29+
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
2930
{
3031
$currentPath = $request->getUri()->getPath();
3132
$uri = $request->getUri()->withPath($this->path.$currentPath);

src/HttpClient/Plugin/PathTrimEnd.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Opensaucesystems\Lxd\HttpClient\Plugin;
33

4+
use Http\Promise\Promise;
45
use Http\Client\Common\Plugin;
56
use Psr\Http\Message\RequestInterface;
67

@@ -23,7 +24,7 @@ public function __construct($trim = '/')
2324
/**
2425
* {@inheritdoc}
2526
*/
26-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
27+
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
2728
{
2829
$trimPath = rtrim($request->getUri()->getPath(), $this->trim);
2930
$uri = $request->getUri()->withPath($trimPath);

0 commit comments

Comments
 (0)