-
Notifications
You must be signed in to change notification settings - Fork 14
/
Client.php
82 lines (69 loc) · 2.83 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Jane\Component\OpenApiRuntime\Client;
use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @deprecated Deprecated since v6.2.0, will removed in v7.0.0 and is already replaced by generated Runtime
*/
abstract class Client
{
public const FETCH_RESPONSE = 'response';
public const FETCH_OBJECT = 'object';
/**
* @var ClientInterface
*/
protected $httpClient;
/**
* @var RequestFactoryInterface
*/
protected $requestFactory;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* @var StreamFactoryInterface
*/
protected $streamFactory;
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, SerializerInterface $serializer, StreamFactoryInterface $streamFactory)
{
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->serializer = $serializer;
$this->streamFactory = $streamFactory;
}
public function executeEndpoint(Endpoint $endpoint, string $fetch = self::FETCH_OBJECT)
{
[$bodyHeaders, $body] = $endpoint->getBody($this->serializer, $this->streamFactory);
$queryString = $endpoint->getQueryString();
$uriGlue = false === strpos($endpoint->getUri(), '?') ? '?' : '&';
$uri = $queryString !== '' ? $endpoint->getUri() . $uriGlue . $queryString : $endpoint->getUri();
$request = $this->requestFactory->createRequest($endpoint->getMethod(), $uri);
if ($body) {
if ($body instanceof StreamInterface) {
$request = $request->withBody($body);
} elseif (\is_resource($body)) {
$request = $request->withBody($this->streamFactory->createStreamFromResource($body));
} elseif (is_file($body)) {
$request = $request->withBody($this->streamFactory->createStreamFromFile($body));
} else {
$request = $request->withBody($this->streamFactory->createStream($body));
}
}
foreach ($endpoint->getHeaders($bodyHeaders) as $name => $value) {
$request = $request->withHeader($name, $value);
}
if (\count($endpoint->getAuthenticationScopes()) > 0) {
$scopes = [];
foreach ($endpoint->getAuthenticationScopes() as $scope) {
$scopes[] = $scope;
}
$request = $request->withHeader(AuthenticationRegistry::SCOPES_HEADER, $scopes);
}
return $endpoint->parseResponse($this->httpClient->sendRequest($request), $this->serializer, $fetch);
}
}