Skip to content

Commit 144c69e

Browse files
committed
Better exceptions, Pipeable trait, Laravel 8 and Symfony 5
1 parent 33daf58 commit 144c69e

22 files changed

+178
-202
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
2-
.idea/
2+
.idea/
3+
/composer.lock

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Obtaining instance via Facade alias
77
use HttpClient;
88
// You can use Facade class to access HttpClient
9-
use Ivan770\HttpClient\Facade;
9+
use Ivan770\HttpClient\Facades\HttpClient;
1010
// Or, you can obtain HttpClient instance directly
1111
use Ivan770\HttpClient\HttpClient;
1212
public function method(HttpClient $client)

composer.json

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
"type": "library",
55
"license": "MIT",
66
"require": {
7-
"symfony/http-client": "^4.3",
8-
"symfony/browser-kit": "^4.3",
9-
"illuminate/support": "^5.8|^6.0|^7.0"
7+
"symfony/http-client": "^5.0",
8+
"symfony/browser-kit": "^5.0",
9+
"illuminate/support": "^8.0",
10+
"php": "^7.3"
11+
},
12+
"require-dev": {
13+
"phpunit/phpunit": "^9.0",
14+
"illuminate/container": "^8.0",
15+
"illuminate/cache": "^8.0",
16+
"illuminate/pipeline": "^8.0"
1017
},
1118
"suggest": {
1219
"illuminate/pipeline": "Allows to send response data through pipelines",
@@ -22,7 +29,7 @@
2229
"extra": {
2330
"laravel": {
2431
"aliases": {
25-
"HttpClient": "Ivan770\\HttpClient\\Facade"
32+
"HttpClient": "Ivan770\\HttpClient\\Facades\\HttpClient"
2633
},
2734
"providers": [
2835
"Ivan770\\HttpClient\\ServiceProvider"

src/Builder.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Ivan770\HttpClient;
44

5+
use Closure;
56
use Illuminate\Contracts\Support\Arrayable;
7+
use JsonSerializable;
8+
use Traversable;
69

710
class Builder
811
{
@@ -93,7 +96,7 @@ public function headers($headers)
9396
/**
9497
* Add body to request
9598
*
96-
* @param array|string|resource|\Traversable|\Closure $body Request body
99+
* @param array|string|resource|Traversable|Closure $body Request body
97100
* @return $this
98101
*/
99102
public function body($body)
@@ -105,7 +108,7 @@ public function body($body)
105108
/**
106109
* Add JSON to request
107110
*
108-
* @param array|\JsonSerializable $json JSON-compatible value
111+
* @param array|JsonSerializable $json JSON-compatible value
109112
* @return $this
110113
*/
111114
public function json($json)
@@ -140,11 +143,11 @@ public function withoutRedirects()
140143
/**
141144
* Change proxy for this request
142145
*
143-
* @param string $proxy Proxy value for CURLOPT_PROXY
144-
* @param string $noproxy Comma-separated list of hosts, that do not require proxy
146+
* @param string $proxy Proxy value for CURLOPT_PROXY
147+
* @param string $noproxy Comma-separated list of hosts, that do not require proxy
145148
* @return $this
146149
*/
147-
public function proxy($proxy = null, $noproxy = null)
150+
public function proxy(string $proxy, string $noproxy): Builder
148151
{
149152
$this->applyRequestOptions(['proxy' => $proxy, 'no_proxy' => $noproxy]);
150153
return $this;

src/Commands/HttpRequestMakeCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ protected function getStub()
5555
/**
5656
* Build the class with the given name.
5757
*
58-
* @param string $name
58+
* @param string $name
5959
* @return string
60-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
61-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
6260
*/
6361
protected function buildClass($name)
6462
{

src/Contracts/Request.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
namespace Ivan770\HttpClient\Contracts;
55

6+
use Closure;
7+
use Illuminate\Support\Collection;
68
use Ivan770\HttpClient\Response\Response;
79

810
interface Request
911
{
1012
/**
1113
* Attach builder properties. HttpClient instance is passed into Closure
1214
*
13-
* @param \Closure $callback
15+
* @param Closure $callback
1416
* @return Request
1517
*/
1618
public function attach($callback);
@@ -25,7 +27,7 @@ public function execute();
2527
/**
2628
* Run request, and retrieve response contents
2729
*
28-
* @return \Illuminate\Support\Collection|string
30+
* @return Collection|string
2931
*/
3032
public function get();
3133
}

src/Contracts/Response.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
namespace Ivan770\HttpClient\Contracts;
44

5-
use Illuminate\Pipeline\Pipeline;
5+
use Closure;
6+
use Illuminate\Contracts\Pipeline\Pipeline;
7+
use Illuminate\Support\Collection;
68

79
interface Response
810
{
911
/**
1012
* Create collection from response
1113
*
12-
* @return \Illuminate\Support\Collection
14+
* @return Collection
1315
*/
1416
public function toCollection();
1517

1618
/**
1719
* Get body of response, or collection, if response is JSON-compatible
1820
*
1921
* @param bool $throw
20-
* @return \Illuminate\Support\Collection|string
22+
* @return Collection|string
2123
*/
2224
public function getContent($throw = true);
2325

2426
/**
2527
* Pass response content to function
2628
*
27-
* @param \Closure $function Function to call
29+
* @param Closure $function Function to call
2830
*/
2931
public function then($function);
3032

src/Exceptions/Cache/BrowserKitCache.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Ivan770\HttpClient\Exceptions\Cache;
44

5-
use Exception;
5+
use Ivan770\HttpClient\Exceptions\ClientException;
66

7-
class BrowserKitCache extends Exception
7+
class BrowserKitCache extends ClientException
88
{
9-
protected $message = "BrowserKit caching is not supported";
10-
11-
public function __toString() {
12-
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
9+
protected function getBaseMessage(): string
10+
{
11+
return 'BrowserKit caching is not supported';
1312
}
1413
}

src/Exceptions/Cache/NullRepository.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Ivan770\HttpClient\Exceptions\Cache;
44

5-
use Exception;
5+
use Ivan770\HttpClient\Exceptions\ClientException;
66

7-
class NullRepository extends Exception
7+
class NullRepository extends ClientException
88
{
9-
protected $message = "Cache repository was not provided";
10-
11-
public function __toString() {
12-
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
9+
protected function getBaseMessage(): string
10+
{
11+
return 'Cache repository was not provided';
1312
}
1413
}

src/Exceptions/ClassIsNotModel.php

-17
This file was deleted.

src/Exceptions/ClientException.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
use Exception;
6+
7+
abstract class ClientException extends Exception
8+
{
9+
/**
10+
* Get base exception message, without any context
11+
*
12+
* @return string
13+
*/
14+
abstract protected function getBaseMessage(): string;
15+
16+
public function __toString() {
17+
return sprintf('%s: %s', __CLASS__, $this->getBaseMessage());
18+
}
19+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
class ContainerNotAvailable extends ClientException
6+
{
7+
protected function getBaseMessage(): string
8+
{
9+
return 'Container not available';
10+
}
11+
}

src/Exceptions/DataIsNotCollection.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
namespace Ivan770\HttpClient\Exceptions;
44

5-
use Exception;
6-
7-
class DataIsNotCollection extends Exception
5+
class DataIsNotCollection extends ClientException
86
{
9-
public function __construct($message, $code = 0, Exception $previous = null) {
10-
parent::__construct($message, $code, $previous);
11-
}
12-
13-
public function __toString() {
14-
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
7+
protected function getBaseMessage(): string
8+
{
9+
return 'Passed data has to be collection instance';
1510
}
16-
}
11+
}

src/Exceptions/EloquentNotAvailable.php

-17
This file was deleted.

src/Exceptions/PipelineNotAvailable.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace Ivan770\HttpClient\Exceptions;
44

5-
use Exception;
6-
7-
class PipelineNotAvailable extends Exception
5+
class PipelineNotAvailable extends ClientException
86
{
9-
public function __construct($message, $code = 0, Exception $previous = null) {
10-
parent::__construct($message, $code, $previous);
11-
}
12-
13-
public function __toString() {
14-
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
7+
protected function getBaseMessage(): string
8+
{
9+
return 'Pipeline class not found';
1510
}
16-
1711
}

src/Facade.php

-13
This file was deleted.

src/Facades/HttpClient.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Facades;
4+
5+
use Illuminate\Support\Facades\Facade as Base;
6+
use Ivan770\HttpClient\HttpClient as BaseClient;
7+
8+
class HttpClient extends Base
9+
{
10+
protected static function getFacadeAccessor()
11+
{
12+
return BaseClient::class;
13+
}
14+
}

src/HttpClient.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
namespace Ivan770\HttpClient;
44

5+
use Closure;
56
use Illuminate\Contracts\Support\Arrayable;
67
use Illuminate\Support\Traits\Macroable;
78
use Ivan770\HttpClient\Response\Response;
89
use Ivan770\HttpClient\Traits\InteractsWithEloquent;
910
use Ivan770\HttpClient\Traits\Requestable;
11+
use JsonSerializable;
1012
use Symfony\Component\HttpClient\HttpClient as Client;
1113
use Symfony\Contracts\HttpClient\HttpClientInterface;
14+
use Traversable;
1215

1316
/**
1417
* @method HttpClient auth(string $type, array|string $credentials) Authentication credentials
1518
* @method HttpClient authBasic(array|string $credentials) Add HTTP basic auth to request
1619
* @method HttpClient authBearer(string $credentials) Add Bearer token to request
1720
* @method HttpClient headers(array $headers) Add headers to request
18-
* @method HttpClient body(array|string|resource|\Traversable|\Closure $body) Add body to request
19-
* @method HttpClient json(array|\JsonSerializable $json) Add JSON to request
21+
* @method HttpClient body(array|string|resource|Traversable|Closure $body) Add body to request
22+
* @method HttpClient json(array|JsonSerializable $json) Add JSON to request
2023
* @method HttpClient query(array $query) Add query string values to request
2124
* @method HttpClient withoutRedirects() Ignore all redirects for this request
2225
* @method HttpClient proxy(string $proxy, string $noproxy) Change proxy for this request

0 commit comments

Comments
 (0)