Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b49be7c

Browse filesBrowse files
[Contracts] add HttpClient\ApiClientInterface et al.
1 parent 1ad6f6f commit b49be7c
Copy full SHA for b49be7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

59 files changed

+5779
-5
lines changed

‎composer.json

Copy file name to clipboardExpand all lines: composer.json
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
"psr/link": "^1.0",
2929
"psr/log": "~1.0",
3030
"psr/simple-cache": "^1.0",
31-
"symfony/contracts": "^1.0.2",
31+
"symfony/contracts": "^1.1",
3232
"symfony/polyfill-ctype": "~1.8",
3333
"symfony/polyfill-intl-icu": "~1.0",
3434
"symfony/polyfill-intl-idn": "^1.10",
3535
"symfony/polyfill-mbstring": "~1.0",
3636
"symfony/polyfill-php72": "~1.5",
37-
"symfony/polyfill-php73": "^1.8"
37+
"symfony/polyfill-php73": "^1.11"
3838
},
3939
"replace": {
4040
"symfony/asset": "self.version",
@@ -55,6 +55,7 @@
5555
"symfony/finder": "self.version",
5656
"symfony/form": "self.version",
5757
"symfony/framework-bundle": "self.version",
58+
"symfony/http-client": "self.version",
5859
"symfony/http-foundation": "self.version",
5960
"symfony/http-kernel": "self.version",
6061
"symfony/inflector": "self.version",
@@ -101,8 +102,10 @@
101102
"doctrine/reflection": "~1.0",
102103
"doctrine/doctrine-bundle": "~1.4",
103104
"monolog/monolog": "~1.11",
105+
"nyholm/psr7": "^1.0",
104106
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
105107
"predis/predis": "~1.1",
108+
"psr/http-client": "^1.0",
106109
"egulias/email-validator": "~1.2,>=1.2.8|~2.0",
107110
"symfony/phpunit-bridge": "~3.4|~4.0",
108111
"symfony/security-acl": "~2.8|~3.0",
+127Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient;
13+
14+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
15+
use Symfony\Component\HttpClient\Response\ApiResponse;
16+
use Symfony\Component\HttpClient\Response\ApiResponseStream;
17+
use Symfony\Contracts\HttpClient\ApiClientInterface;
18+
use Symfony\Contracts\HttpClient\ApiResponseInterface;
19+
use Symfony\Contracts\HttpClient\ApiResponseStreamInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Nicolas Grekas <p@tchwork.com>
24+
*/
25+
final class ApiClient implements ApiClientInterface
26+
{
27+
use HttpClientTrait;
28+
29+
private $client;
30+
private $defaultOptions = [
31+
'headers' => [
32+
'accept' => ['application/json'],
33+
],
34+
] + ApiClientInterface::OPTIONS_DEFAULTS;
35+
36+
public function __construct(HttpClientInterface $client, array $defaultOptions = [])
37+
{
38+
$this->client = $client;
39+
40+
if ($defaultOptions) {
41+
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function get(string $url, iterable $options = []): ApiResponseInterface
49+
{
50+
return $this->requestApi('GET', $url, $options);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function head(string $url, iterable $options = []): ApiResponseInterface
57+
{
58+
return $this->requestApi('HEAD', $url, $options);
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function post(string $url, iterable $options = []): ApiResponseInterface
65+
{
66+
return $this->requestApi('POST', $url, $options);
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function put(string $url, iterable $options = []): ApiResponseInterface
73+
{
74+
return $this->requestApi('PUT', $url, $options);
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function patch(string $url, iterable $options = []): ApiResponseInterface
81+
{
82+
return $this->requestApi('PATCH', $url, $options);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function delete(string $url, iterable $options = []): ApiResponseInterface
89+
{
90+
return $this->requestApi('DELETE', $url, $options);
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function options(string $url, iterable $options = []): ApiResponseInterface
97+
{
98+
return $this->requestApi('OPTIONS', $url, $options);
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function complete($responses, float $timeout = null): ApiResponseStreamInterface
105+
{
106+
if ($responses instanceof ApiResponse) {
107+
$responses = [$responses];
108+
} elseif (!\is_iterable($responses)) {
109+
throw new \TypeError(sprintf('%s() expects parameter 1 to be iterable of ApiResponse objects, %s given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
110+
}
111+
112+
return new ApiResponseStream(ApiResponse::complete($this->client, $responses, $timeout));
113+
}
114+
115+
private function requestApi(string $method, string $url, iterable $options): ApiResponseInterface
116+
{
117+
$options['buffer'] = true;
118+
[, $options] = self::prepareRequest(null, null, $options, $this->defaultOptions);
119+
120+
return new ApiResponse($this->client->request($method, $url, $options));
121+
}
122+
123+
private static function createInvalidArgumentException(string $message, string $file, int $line): \InvalidArgumentException
124+
{
125+
return new InvalidArgumentException($message, $file, $line);
126+
}
127+
}
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
4.3.0
5+
-----
6+
7+
* added the component
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Chunk;
13+
14+
use Symfony\Contracts\HttpClient\ChunkInterface;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*
19+
* @internal
20+
*/
21+
class DataChunk implements ChunkInterface
22+
{
23+
private $offset;
24+
private $content;
25+
26+
public function __construct(int $offset = 0, string $content = '')
27+
{
28+
$this->offset = $offset;
29+
$this->content = $content;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function isTimeout(): bool
36+
{
37+
return false;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function isFirst(): bool
44+
{
45+
return false;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function isLast(): bool
52+
{
53+
return false;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function getContent(): string
60+
{
61+
return $this->content;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function getOffset(): int
68+
{
69+
return $this->offset;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getError(): ?string
76+
{
77+
return null;
78+
}
79+
}
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Chunk;
13+
14+
use Symfony\Component\HttpClient\Exception\TransportException;
15+
use Symfony\Contracts\HttpClient\ChunkInterface;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*
20+
* @internal
21+
*/
22+
class ErrorChunk implements ChunkInterface
23+
{
24+
protected $throw;
25+
26+
private $offset;
27+
private $error;
28+
29+
public function __construct(bool &$throw, int $offset, \Throwable $error = null)
30+
{
31+
$throw = true;
32+
$this->throw = &$throw;
33+
$this->offset = $offset;
34+
$this->error = null !== $error ? [$error->getMessage(), 0, $error] : ['Reading from the response stream reached the inactivity timeout.'];
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function isTimeout(): bool
41+
{
42+
$this->throw = false;
43+
44+
if (1 < \count($this->error)) {
45+
throw new TransportException(...$this->error);
46+
}
47+
48+
return true;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function isFirst(): bool
55+
{
56+
$this->throw = false;
57+
throw new TransportException(...$this->error);
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function isLast(): bool
64+
{
65+
$this->throw = false;
66+
throw new TransportException(...$this->error);
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function getContent(): string
73+
{
74+
$this->throw = false;
75+
throw new TransportException(...$this->error);
76+
}
77+
78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public function getOffset(): int
82+
{
83+
return $this->offset;
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function getError(): ?string
90+
{
91+
return $this->error[0];
92+
}
93+
94+
public function __destruct()
95+
{
96+
if ($this->throw) {
97+
$this->throw = false;
98+
throw new TransportException(...$this->error);
99+
}
100+
}
101+
}
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Chunk;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*
17+
* @internal
18+
*/
19+
class FirstChunk extends DataChunk
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function isFirst(): bool
25+
{
26+
return true;
27+
}
28+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.