Skip to content

Commit 1733b01

Browse files
committed
Builder as class, call forwarding in Request, more HTTP methods as magic
1 parent f2b3dc9 commit 1733b01

File tree

4 files changed

+108
-20
lines changed

4 files changed

+108
-20
lines changed

src/Traits/Buildable.php renamed to src/Builder.php

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
<?php
22

3-
namespace Ivan770\HttpClient\Traits;
3+
namespace Ivan770\HttpClient;
44

5-
trait Buildable
5+
class Builder
66
{
7+
/**
8+
* Request options, that will be passed to Symfony HttpClient
9+
*
10+
* @var array
11+
*/
712
protected $request = [];
813

9-
protected function applyRequestOptions($options)
14+
/**
15+
* Apply request options to current builder
16+
*
17+
* @param $options
18+
*/
19+
public function applyRequestOptions($options)
1020
{
1121
$this->request = array_merge($this->request, $options);
1222
}
1323

14-
protected function resetBuilderState()
24+
/**
25+
* Reset request options
26+
*/
27+
public function resetRequest()
1528
{
1629
$this->request = [];
1730
}
1831

19-
protected function returnAndResetBuilderState()
32+
/**
33+
* Get request options
34+
*
35+
* @return array
36+
*/
37+
public function getRequest()
2038
{
21-
$request = $this->request;
22-
$this->resetBuilderState();
23-
return $request;
39+
return $this->request;
2440
}
2541

2642
/**

src/HttpClient.php

+42-3
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,62 @@
33
namespace Ivan770\HttpClient;
44

55
use Illuminate\Support\Traits\Macroable;
6-
use Ivan770\HttpClient\Traits\Buildable;
76
use Ivan770\HttpClient\Traits\InteractsWithEloquent;
87
use Ivan770\HttpClient\Traits\Requestable;
98
use Symfony\Component\HttpClient\HttpClient as Client;
109

1110
/**
11+
* @method HttpClient auth(string $type, array|string $credentials) Authentication credentials
12+
* @method HttpClient authBasic(array|string $credentials) Add HTTP basic auth to request
13+
* @method HttpClient authBearer(string $credentials) Add Bearer token to request
14+
* @method HttpClient headers(array $headers) Add headers to request
15+
* @method HttpClient body(array|string|resource|\Traversable|\Closure $body) Add body to request
16+
* @method HttpClient json(array|\JsonSerializable $json) Add JSON to request
17+
* @method HttpClient query(array $query) Add query string values to request
18+
* @method HttpClient withoutRedirects() Ignore all redirects for this request
19+
* @method HttpClient proxy(string $proxy, string $noproxy) Change proxy for this request
1220
* @method Response get(string $url, array $arguments = []) Send a GET request
1321
* @method Response head(string $url, array $arguments = []) Send a HEAD request
1422
* @method Response post(string $url, array $arguments = []) Send a POST request
1523
* @method Response put(string $url, array $arguments = []) Send a PUT request
1624
* @method Response delete(string $url, array $arguments = []) Send a DELETE request
25+
* @method Response connect(string $url, array $arguments = []) Send a CONNECT request
26+
* @method Response options(string $url, array $arguments = []) Send a OPTIONS request
27+
* @method Response trace(string $url, array $arguments = []) Send a TRACE request
28+
* @method Response patch(string $url, array $arguments = []) Send a PATCH request
29+
*
30+
* @see Builder
1731
*/
1832
class HttpClient
1933
{
20-
use InteractsWithEloquent, Buildable, Requestable, Macroable {
34+
use InteractsWithEloquent, Requestable, Macroable {
2135
Macroable::__call as macroCall;
2236
}
2337

38+
/**
39+
* @var \Symfony\Contracts\HttpClient\HttpClientInterface
40+
*/
2441
protected $client;
2542

26-
public function __construct(Client $client, $clientArgs = [])
43+
/**
44+
* @var Builder
45+
*/
46+
protected $builder;
47+
48+
public function __construct(Client $client, Builder $builder, $clientArgs = [])
2749
{
2850
$this->client = $client->create($clientArgs);
51+
$this->builder = $builder;
52+
}
53+
54+
/**
55+
* Get builder instance
56+
*
57+
* @return Builder
58+
*/
59+
public function getBuilder()
60+
{
61+
return $this->builder;
2962
}
3063

3164
public function __call($name, $arguments)
@@ -38,6 +71,12 @@ public function __call($name, $arguments)
3871
return $this->callRequestMethod($name, $arguments);
3972
}
4073

74+
if(method_exists($this->builder, $name)) {
75+
$this->builder->$name(...$arguments);
76+
77+
return $this;
78+
}
79+
4180
return $this->client->$name(...$arguments);
4281
}
4382
}

src/Request.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
use Ivan770\HttpClient\Contracts\Request as RequestContract;
77

8+
/**
9+
* @method RequestContract auth(string $type, array|string $credentials) Authentication credentials
10+
* @method RequestContract authBasic(array|string $credentials) Add HTTP basic auth to request
11+
* @method RequestContract authBearer(string $credentials) Add Bearer token to request
12+
* @method RequestContract headers(array $headers) Add headers to request
13+
* @method RequestContract body(array|string|resource|\Traversable|\Closure $body) Add body to request
14+
* @method RequestContract json(array|\JsonSerializable $json) Add JSON to request
15+
* @method RequestContract query(array $query) Add query string values to request
16+
* @method RequestContract withoutRedirects() Ignore all redirects for this request
17+
* @method RequestContract proxy(string $proxy, string $noproxy) Change proxy for this request
18+
*
19+
* @see Builder
20+
*/
821
abstract class Request implements RequestContract
922
{
1023
/**
@@ -34,6 +47,8 @@ abstract class Request implements RequestContract
3447
public function __construct(HttpClient $client)
3548
{
3649
$this->client = $client;
50+
51+
$this->defaultAttach($this->client);
3752
}
3853

3954
/**
@@ -107,8 +122,6 @@ public function mock($test)
107122
*/
108123
public function execute()
109124
{
110-
$this->defaultAttach($this->client);
111-
112125
$method = $this->getMethod();
113126

114127
return $this->client->$method($this->getResource());
@@ -123,4 +136,11 @@ public function get()
123136
{
124137
return $this->execute()->getContent();
125138
}
139+
140+
public function __call($name, $arguments)
141+
{
142+
$this->client->$name(...$arguments);
143+
144+
return $this;
145+
}
126146
}

src/Traits/Requestable.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
trait Requestable
88
{
99
protected $methods = [
10-
"get",
11-
"head",
12-
"post",
13-
"put",
14-
"delete",
10+
'get',
11+
'head',
12+
'post',
13+
'put',
14+
'delete',
15+
'connect',
16+
'options',
17+
'trace',
18+
'patch',
1519
];
1620

1721
protected function isRequestMethod($name)
@@ -21,9 +25,18 @@ protected function isRequestMethod($name)
2125

2226
protected function callRequestMethod($name, $arguments)
2327
{
24-
$this->applyRequestOptions($arguments[1] ?? []);
25-
$request = $this->returnAndResetBuilderState();
28+
$this->builder->applyRequestOptions($arguments[1] ?? []);
29+
$request = $this->getRequest();
30+
2631
$response = $this->client->request(strtoupper($name), $arguments[0], $request);
2732
return new Response($response);
2833
}
34+
35+
protected function getRequest()
36+
{
37+
$request = $this->builder->getRequest();
38+
$this->builder->resetRequest();
39+
40+
return $request;
41+
}
2942
}

0 commit comments

Comments
 (0)