forked from elastic/openapi-codegen-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractClient.php
62 lines (52 loc) · 1.59 KB
/
AbstractClient.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
<?php
/**
* This file is part of the Elastic OpenAPI PHP code generator.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Elastic\OpenApi\Codegen;
use Elastic\OpenApi\Codegen\Connection\Connection;
/**
* A base client implementation implemented by the generator.
*
* @package Elastic\OpenApi\Codegen
* @author Aurélien FOUCRET <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
*/
abstract class AbstractClient
{
/**
* @var Connection
*/
private $connection;
/**
* @var callable
*/
private $endpointBuilder;
/**
* Client constructor.
*
* @param callable $endpointBuilder Allow to access endpoints.
* @param Connection $connection HTTP connection handler.
*/
public function __construct(callable $endpointBuilder, Connection $connection)
{
$this->endpointBuilder = $endpointBuilder;
$this->connection = $connection;
}
protected function getEndpoint($name)
{
$endpointBuilder = $this->endpointBuilder;
return $endpointBuilder($name);
}
protected function performRequest(Endpoint\EndpointInterface $endpoint)
{
$method = $endpoint->getMethod();
$uri = $endpoint->getURI();
$params = $endpoint->getParams();
$body = $endpoint->getBody();
$response = $this->connection->performRequest($method, $uri, $params, $body);
return isset($response['body']) ? $response['body'] : $response;
}
}