-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiRestInternalClient.php
More file actions
117 lines (98 loc) · 2.96 KB
/
ApiRestInternalClient.php
File metadata and controls
117 lines (98 loc) · 2.96 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Smartbox\ApiRestClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
/**
* Class ApiRestInternalClient.
*/
class ApiRestInternalClient
{
public static $class = 'Smartbox\ApiRestClient\ApiRestInternalClient';
const FORMAT_JSON = 'json';
const HTTP_METHOD_GET = 'GET';
const HTTP_METHOD_POST = 'POST';
const HTTP_METHOD_PUT = 'PUT';
const HTTP_METHOD_PATCH = 'PATCH';
const HTTP_METHOD_DELETE = 'DELETE';
/** @var Client */
protected $client;
/** @var string */
protected $baseUrl;
/** @var string */
protected $username;
/** @var string */
protected $password;
/** @var EventSubscriberInterface */
protected $subscribers;
/**
* Return the available.
*
* @return array
*/
public static function getAvailableHttpMethod()
{
return array(
self::HTTP_METHOD_DELETE,
self::HTTP_METHOD_GET,
self::HTTP_METHOD_POST,
self::HTTP_METHOD_PUT,
self::HTTP_METHOD_PATCH,
);
}
/**
* ApiRestInternalClient constructor.
*
* @param $username
* @param $password
* @param $baseUrl
*/
public function __construct($username, $password, $baseUrl)
{
$this->client = new Client();
$this->password = $password;
$this->username = $username;
$this->baseUrl = $baseUrl;
}
/**
* Send a request to a given URI.
*
* @param $method
* @param $path
* @param mixed|null $object
* @param array $filters
* @param array $headers
* @param string $deserializationType
*
* @return ApiRestResponse
*
* @throws \Exception
*/
public function request($method, $path, $object = null, array $filters = array(), array $headers = array(), $deserializationType = null)
{
if (!\in_array($method, self::getAvailableHttpMethod())) {
throw new \Exception("Unknown HTTP method $method");
}
$uri = $this->baseUrl.$path;
$request = ApiRestRequestBuilder::buildRequest($method, $uri, $this->username, $this->password, $object, $headers, $filters);
if (!empty($this->subscribers)) {
foreach ($this->subscribers as $subscriber) {
$request->addSubscriber($subscriber);
}
}
try {
/* @var Response*/
$response = $this->client->send($request);
} catch (RequestException $e) {
$errorResponse = $e->getResponse();
if (!empty($errorResponse)) {
$response = ApiRestResponseBuilder::buildResponse($errorResponse);
throw new ApiRestException($response, $e);
} else {
throw $e;
}
}
return ApiRestResponseBuilder::buildResponse($response, $deserializationType);
}
}