@@ -4,46 +4,46 @@ |
||
4 | 4 | |
5 | 5 | interface HttpClient |
6 | 6 | { |
7 | - /** |
|
8 | - * @param $uri |
|
9 | - * @param array $params |
|
10 | - * @param array $headers |
|
11 | - * @return array |
|
12 | - */ |
|
13 | - public function get($uri, $params = [], $headers = []); |
|
7 | + /** |
|
8 | + * @param $uri |
|
9 | + * @param array $params |
|
10 | + * @param array $headers |
|
11 | + * @return array |
|
12 | + */ |
|
13 | + public function get($uri, $params = [], $headers = []); |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param string $uri |
|
17 | - * @param array $body |
|
18 | - * @param array $headers |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public function post($uri, $body = [], $headers = []); |
|
15 | + /** |
|
16 | + * @param string $uri |
|
17 | + * @param array $body |
|
18 | + * @param array $headers |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public function post($uri, $body = [], $headers = []); |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param string $uri |
|
25 | - * @param array $body |
|
26 | - * @param array $headers |
|
27 | - * @return mixed |
|
28 | - */ |
|
29 | - public function put($uri, $body = [], $headers = []); |
|
23 | + /** |
|
24 | + * @param string $uri |
|
25 | + * @param array $body |
|
26 | + * @param array $headers |
|
27 | + * @return mixed |
|
28 | + */ |
|
29 | + public function put($uri, $body = [], $headers = []); |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $uri |
|
33 | - * @param array $body |
|
34 | - * @param array $headers |
|
35 | - * @return mixed |
|
36 | - */ |
|
37 | - public function delete($uri, $body = [], $headers = []); |
|
31 | + /** |
|
32 | + * @param string $uri |
|
33 | + * @param array $body |
|
34 | + * @param array $headers |
|
35 | + * @return mixed |
|
36 | + */ |
|
37 | + public function delete($uri, $body = [], $headers = []); |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $url |
|
41 | - * @return $this |
|
42 | - */ |
|
43 | - public function setBaseUrl($url); |
|
39 | + /** |
|
40 | + * @param string $url |
|
41 | + * @return $this |
|
42 | + */ |
|
43 | + public function setBaseUrl($url); |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function getResponseHeaders(); |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function getResponseHeaders(); |
|
49 | 49 | } |
50 | 50 | \ No newline at end of file |
@@ -6,125 +6,125 @@ |
||
6 | 6 | |
7 | 7 | class Endpoint |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $rateLimitInfo; |
|
13 | - |
|
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $allowedMethods = [ |
|
18 | - 'getById', |
|
19 | - 'getAll', |
|
20 | - 'create', |
|
21 | - 'update', |
|
22 | - 'delete', |
|
23 | - ]; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $endpoint; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected $headers = []; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var HttpClient |
|
37 | - */ |
|
38 | - protected $http; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $organizationId; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param HttpClient $http |
|
47 | - */ |
|
48 | - public function __construct(HttpClient $http) |
|
49 | - { |
|
50 | - $this->http = $http; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param string $verb |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function makeRequestUrl($verb = '') |
|
58 | - { |
|
59 | - return "https://favro.com/api/v1/{$this->endpoint}/$verb"; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @param string $method |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - public function isMethodAllowed($method) |
|
67 | - { |
|
68 | - return in_array($method, $this->allowedMethods); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return HttpClient |
|
73 | - */ |
|
74 | - public function getHttp() |
|
75 | - { |
|
76 | - return $this->http; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param array $params |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function getAll(array $params = []) |
|
84 | - { |
|
85 | - return $this |
|
86 | - ->getHttp() |
|
87 | - ->get( |
|
88 | - $this->makeRequestUrl(), |
|
89 | - $params, |
|
90 | - $this->getHeaders() |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $id |
|
96 | - * @return array |
|
97 | - */ |
|
98 | - public function getById($id) |
|
99 | - { |
|
100 | - return $this |
|
101 | - ->getHttp() |
|
102 | - ->get( |
|
103 | - $this->makeRequestUrl($id), |
|
104 | - [], |
|
105 | - $this->getHeaders() |
|
106 | - ); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - protected function getHeaders() |
|
113 | - { |
|
114 | - return array_merge( |
|
115 | - ['organizationId' => $this->organizationId], |
|
116 | - $this->headers |
|
117 | - ); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param string $organizationId |
|
122 | - * @return $this |
|
123 | - */ |
|
124 | - public function setOrganizationId($organizationId) |
|
125 | - { |
|
126 | - $this->organizationId = $organizationId; |
|
127 | - |
|
128 | - return $this; |
|
129 | - } |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $rateLimitInfo; |
|
13 | + |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $allowedMethods = [ |
|
18 | + 'getById', |
|
19 | + 'getAll', |
|
20 | + 'create', |
|
21 | + 'update', |
|
22 | + 'delete', |
|
23 | + ]; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $endpoint; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected $headers = []; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var HttpClient |
|
37 | + */ |
|
38 | + protected $http; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $organizationId; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param HttpClient $http |
|
47 | + */ |
|
48 | + public function __construct(HttpClient $http) |
|
49 | + { |
|
50 | + $this->http = $http; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param string $verb |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function makeRequestUrl($verb = '') |
|
58 | + { |
|
59 | + return "https://favro.com/api/v1/{$this->endpoint}/$verb"; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @param string $method |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + public function isMethodAllowed($method) |
|
67 | + { |
|
68 | + return in_array($method, $this->allowedMethods); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return HttpClient |
|
73 | + */ |
|
74 | + public function getHttp() |
|
75 | + { |
|
76 | + return $this->http; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param array $params |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function getAll(array $params = []) |
|
84 | + { |
|
85 | + return $this |
|
86 | + ->getHttp() |
|
87 | + ->get( |
|
88 | + $this->makeRequestUrl(), |
|
89 | + $params, |
|
90 | + $this->getHeaders() |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $id |
|
96 | + * @return array |
|
97 | + */ |
|
98 | + public function getById($id) |
|
99 | + { |
|
100 | + return $this |
|
101 | + ->getHttp() |
|
102 | + ->get( |
|
103 | + $this->makeRequestUrl($id), |
|
104 | + [], |
|
105 | + $this->getHeaders() |
|
106 | + ); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + protected function getHeaders() |
|
113 | + { |
|
114 | + return array_merge( |
|
115 | + ['organizationId' => $this->organizationId], |
|
116 | + $this->headers |
|
117 | + ); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param string $organizationId |
|
122 | + * @return $this |
|
123 | + */ |
|
124 | + public function setOrganizationId($organizationId) |
|
125 | + { |
|
126 | + $this->organizationId = $organizationId; |
|
127 | + |
|
128 | + return $this; |
|
129 | + } |
|
130 | 130 | } |
131 | 131 | \ No newline at end of file |
@@ -8,77 +8,77 @@ |
||
8 | 8 | |
9 | 9 | class EndpointsContainer |
10 | 10 | { |
11 | - const ENDPOINTS_NAMESPACE = 'seregazhuk\\Favro\\Api\\Endpoints\\'; |
|
11 | + const ENDPOINTS_NAMESPACE = 'seregazhuk\\Favro\\Api\\Endpoints\\'; |
|
12 | 12 | |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * @var HttpInterface |
15 | 15 | */ |
16 | - protected $httpClient; |
|
16 | + protected $httpClient; |
|
17 | 17 | |
18 | - /* |
|
18 | + /* |
|
19 | 19 | * @var array |
20 | 20 | */ |
21 | - protected $endpoints = []; |
|
21 | + protected $endpoints = []; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param HttpClient $httpClient |
|
25 | - */ |
|
26 | - public function __construct(HttpClient $httpClient) |
|
27 | - { |
|
28 | - $this->httpClient = $httpClient; |
|
29 | - } |
|
23 | + /** |
|
24 | + * @param HttpClient $httpClient |
|
25 | + */ |
|
26 | + public function __construct(HttpClient $httpClient) |
|
27 | + { |
|
28 | + $this->httpClient = $httpClient; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $endpoint |
|
33 | - * @return Endpoint |
|
34 | - */ |
|
35 | - public function resolve($endpoint) |
|
36 | - { |
|
37 | - $endpoint = strtolower($endpoint); |
|
31 | + /** |
|
32 | + * @param string $endpoint |
|
33 | + * @return Endpoint |
|
34 | + */ |
|
35 | + public function resolve($endpoint) |
|
36 | + { |
|
37 | + $endpoint = strtolower($endpoint); |
|
38 | 38 | |
39 | - // Check if an instance has already been initiated |
|
40 | - if (!isset($this->endpoints[$endpoint])) { |
|
41 | - $this->addProvider($endpoint); |
|
42 | - } |
|
39 | + // Check if an instance has already been initiated |
|
40 | + if (!isset($this->endpoints[$endpoint])) { |
|
41 | + $this->addProvider($endpoint); |
|
42 | + } |
|
43 | 43 | |
44 | - return $this->endpoints[$endpoint]; |
|
45 | - } |
|
44 | + return $this->endpoints[$endpoint]; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param $endpoint |
|
49 | - * @throws WrongEndpoint |
|
50 | - */ |
|
51 | - protected function addProvider($endpoint) |
|
52 | - { |
|
53 | - $className = self::ENDPOINTS_NAMESPACE . ucfirst($endpoint); |
|
47 | + /** |
|
48 | + * @param $endpoint |
|
49 | + * @throws WrongEndpoint |
|
50 | + */ |
|
51 | + protected function addProvider($endpoint) |
|
52 | + { |
|
53 | + $className = self::ENDPOINTS_NAMESPACE . ucfirst($endpoint); |
|
54 | 54 | |
55 | - if (!class_exists($className)) { |
|
56 | - throw new WrongEndpoint("Endpoint $className not found."); |
|
57 | - } |
|
55 | + if (!class_exists($className)) { |
|
56 | + throw new WrongEndpoint("Endpoint $className not found."); |
|
57 | + } |
|
58 | 58 | |
59 | - $this->endpoints[$endpoint] = $this->buildEndpoint($className); |
|
60 | - } |
|
59 | + $this->endpoints[$endpoint] = $this->buildEndpoint($className); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param string $className |
|
64 | - * @return Endpoint|object |
|
65 | - */ |
|
66 | - protected function buildEndpoint($className) |
|
67 | - { |
|
68 | - return (new ReflectionClass($className))->newInstanceArgs([$this->httpClient]); |
|
69 | - } |
|
62 | + /** |
|
63 | + * @param string $className |
|
64 | + * @return Endpoint|object |
|
65 | + */ |
|
66 | + protected function buildEndpoint($className) |
|
67 | + { |
|
68 | + return (new ReflectionClass($className))->newInstanceArgs([$this->httpClient]); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - public function getRateInfo() |
|
75 | - { |
|
76 | - $responseHeaders = $this->httpClient->getResponseHeaders(); |
|
71 | + /** |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + public function getRateInfo() |
|
75 | + { |
|
76 | + $responseHeaders = $this->httpClient->getResponseHeaders(); |
|
77 | 77 | |
78 | - return [ |
|
79 | - 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
80 | - 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
81 | - 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
82 | - ]; |
|
83 | - } |
|
78 | + return [ |
|
79 | + 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
80 | + 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
81 | + 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
82 | + ]; |
|
83 | + } |
|
84 | 84 | } |
85 | 85 | \ No newline at end of file |
@@ -32,95 +32,95 @@ |
||
32 | 32 | */ |
33 | 33 | class Api |
34 | 34 | { |
35 | - /** |
|
36 | - * @var EndpointsContainer |
|
37 | - */ |
|
38 | - protected $endpointsContainer; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $organizationId; |
|
44 | - |
|
45 | - public function __construct(EndpointsContainer $endpointsContainer) |
|
46 | - { |
|
47 | - $this->endpointsContainer = $endpointsContainer; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Magic method to access different endpoints. |
|
52 | - * |
|
53 | - * @param string $endpoint |
|
54 | - * |
|
55 | - * @return Endpoint |
|
56 | - */ |
|
57 | - public function __get($endpoint) |
|
58 | - { |
|
59 | - $endpoint = $this->endpointsContainer->resolve($endpoint); |
|
60 | - |
|
61 | - if (method_exists($endpoint, 'setOrganizationId')) { |
|
62 | - $endpoint->setOrganizationId($this->organizationId); |
|
63 | - } |
|
64 | - |
|
65 | - return $endpoint; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param $organizationName |
|
70 | - * @return $this |
|
71 | - */ |
|
72 | - public function setOrganization($organizationName) |
|
73 | - { |
|
74 | - if($organization = $this->getOrganizationByName($organizationName)) { |
|
75 | - $this->setOrganizationId($organization['organizationId']); |
|
76 | - } |
|
77 | - |
|
78 | - return $this; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @param int $organizationId |
|
83 | - * @return $this |
|
84 | - */ |
|
85 | - public function setOrganizationId($organizationId) |
|
86 | - { |
|
87 | - $this->organizationId = $organizationId; |
|
88 | - |
|
89 | - return $this; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function getOrganizationId() |
|
96 | - { |
|
97 | - return $this->organizationId; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param string $name |
|
102 | - * @param array $arguments |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function __call($name, $arguments) |
|
106 | - { |
|
107 | - return call_user_func([$this->endpointsContainer, $name], $arguments); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param $organization |
|
112 | - * @return array|bool |
|
113 | - * @throws WrongOrganizationName |
|
114 | - */ |
|
115 | - protected function getOrganizationByName($organization) |
|
116 | - { |
|
117 | - $organizations = $this->organizations->getAll(); |
|
118 | - foreach ($organizations['entities'] as $entity) { |
|
119 | - if ($entity['name'] == $organization) { |
|
120 | - return $entity; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - throw new WrongOrganizationName("Organization $organization not found!"); |
|
125 | - } |
|
35 | + /** |
|
36 | + * @var EndpointsContainer |
|
37 | + */ |
|
38 | + protected $endpointsContainer; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $organizationId; |
|
44 | + |
|
45 | + public function __construct(EndpointsContainer $endpointsContainer) |
|
46 | + { |
|
47 | + $this->endpointsContainer = $endpointsContainer; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Magic method to access different endpoints. |
|
52 | + * |
|
53 | + * @param string $endpoint |
|
54 | + * |
|
55 | + * @return Endpoint |
|
56 | + */ |
|
57 | + public function __get($endpoint) |
|
58 | + { |
|
59 | + $endpoint = $this->endpointsContainer->resolve($endpoint); |
|
60 | + |
|
61 | + if (method_exists($endpoint, 'setOrganizationId')) { |
|
62 | + $endpoint->setOrganizationId($this->organizationId); |
|
63 | + } |
|
64 | + |
|
65 | + return $endpoint; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param $organizationName |
|
70 | + * @return $this |
|
71 | + */ |
|
72 | + public function setOrganization($organizationName) |
|
73 | + { |
|
74 | + if($organization = $this->getOrganizationByName($organizationName)) { |
|
75 | + $this->setOrganizationId($organization['organizationId']); |
|
76 | + } |
|
77 | + |
|
78 | + return $this; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @param int $organizationId |
|
83 | + * @return $this |
|
84 | + */ |
|
85 | + public function setOrganizationId($organizationId) |
|
86 | + { |
|
87 | + $this->organizationId = $organizationId; |
|
88 | + |
|
89 | + return $this; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function getOrganizationId() |
|
96 | + { |
|
97 | + return $this->organizationId; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param string $name |
|
102 | + * @param array $arguments |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function __call($name, $arguments) |
|
106 | + { |
|
107 | + return call_user_func([$this->endpointsContainer, $name], $arguments); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param $organization |
|
112 | + * @return array|bool |
|
113 | + * @throws WrongOrganizationName |
|
114 | + */ |
|
115 | + protected function getOrganizationByName($organization) |
|
116 | + { |
|
117 | + $organizations = $this->organizations->getAll(); |
|
118 | + foreach ($organizations['entities'] as $entity) { |
|
119 | + if ($entity['name'] == $organization) { |
|
120 | + return $entity; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + throw new WrongOrganizationName("Organization $organization not found!"); |
|
125 | + } |
|
126 | 126 | } |
127 | 127 | \ No newline at end of file |
@@ -8,129 +8,129 @@ |
||
8 | 8 | |
9 | 9 | class GuzzleHttpClient implements HttpClient |
10 | 10 | { |
11 | - /** |
|
12 | - * @var ClientInterface |
|
13 | - */ |
|
14 | - protected $client; |
|
15 | - |
|
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $responseHeaders; |
|
20 | - |
|
21 | - /** |
|
22 | - * @param ClientInterface $client |
|
23 | - */ |
|
24 | - public function __construct(ClientInterface $client) |
|
25 | - { |
|
26 | - $this->client = $client; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * @param string $uri |
|
31 | - * @param array $params |
|
32 | - * @param array $headers |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function get($uri, $params = [], $headers = []) |
|
36 | - { |
|
37 | - if (!empty($params)) { |
|
38 | - $uri .= '?' . http_build_query($params); |
|
39 | - } |
|
40 | - |
|
41 | - $response = $this |
|
42 | - ->client |
|
43 | - ->get($uri, ['headers' => $headers]); |
|
44 | - |
|
45 | - return $this->parseResponse($response); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string $uri |
|
50 | - * @param array $body |
|
51 | - * @param array $headers |
|
52 | - * @return string |
|
53 | - */ |
|
54 | - public function post($uri, $body = [], $headers = []) |
|
55 | - { |
|
56 | - $response = $this |
|
57 | - ->client |
|
58 | - ->post( |
|
59 | - $uri, [ |
|
60 | - 'headers' => $headers, |
|
61 | - 'form_params' => $body, |
|
62 | - ] |
|
63 | - ); |
|
64 | - |
|
65 | - return $this->parseResponse($response); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param string $uri |
|
70 | - * @param array $body |
|
71 | - * @param array $headers |
|
72 | - * @return mixed |
|
73 | - */ |
|
74 | - public function put($uri, $body = [], $headers = []) |
|
75 | - { |
|
76 | - $response = $this |
|
77 | - ->client |
|
78 | - ->put( |
|
79 | - $uri, [ |
|
80 | - 'headers' => $headers, |
|
81 | - 'form_params' => $body, |
|
82 | - ] |
|
83 | - ); |
|
84 | - |
|
85 | - return $this->parseResponse($response); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param string $uri |
|
90 | - * @param array $body |
|
91 | - * @param array $headers |
|
92 | - * @return mixed |
|
93 | - */ |
|
94 | - public function delete($uri, $body = [], $headers = []) |
|
95 | - { |
|
96 | - $response = $this |
|
97 | - ->client |
|
98 | - ->delete($uri, [ |
|
99 | - 'headers' => $headers, |
|
100 | - 'form_params' => $body, |
|
101 | - ]); |
|
102 | - |
|
103 | - return $this->parseResponse($response); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $url |
|
108 | - * @return $this |
|
109 | - */ |
|
110 | - public function setBaseUrl($url) |
|
111 | - { |
|
112 | - $this->client->setBaseUrl($url); |
|
113 | - return $this; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @param ResponseInterface $response |
|
118 | - * @return array|null |
|
119 | - */ |
|
120 | - protected function parseResponse(ResponseInterface $response) |
|
121 | - { |
|
122 | - $responseContents = $response->getBody()->getContents(); |
|
123 | - |
|
124 | - $this->responseHeaders = $response->getHeaders(); |
|
125 | - |
|
126 | - return json_decode($responseContents, true); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function getResponseHeaders() |
|
133 | - { |
|
134 | - return $this->responseHeaders; |
|
135 | - } |
|
11 | + /** |
|
12 | + * @var ClientInterface |
|
13 | + */ |
|
14 | + protected $client; |
|
15 | + |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $responseHeaders; |
|
20 | + |
|
21 | + /** |
|
22 | + * @param ClientInterface $client |
|
23 | + */ |
|
24 | + public function __construct(ClientInterface $client) |
|
25 | + { |
|
26 | + $this->client = $client; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * @param string $uri |
|
31 | + * @param array $params |
|
32 | + * @param array $headers |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function get($uri, $params = [], $headers = []) |
|
36 | + { |
|
37 | + if (!empty($params)) { |
|
38 | + $uri .= '?' . http_build_query($params); |
|
39 | + } |
|
40 | + |
|
41 | + $response = $this |
|
42 | + ->client |
|
43 | + ->get($uri, ['headers' => $headers]); |
|
44 | + |
|
45 | + return $this->parseResponse($response); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string $uri |
|
50 | + * @param array $body |
|
51 | + * @param array $headers |
|
52 | + * @return string |
|
53 | + */ |
|
54 | + public function post($uri, $body = [], $headers = []) |
|
55 | + { |
|
56 | + $response = $this |
|
57 | + ->client |
|
58 | + ->post( |
|
59 | + $uri, [ |
|
60 | + 'headers' => $headers, |
|
61 | + 'form_params' => $body, |
|
62 | + ] |
|
63 | + ); |
|
64 | + |
|
65 | + return $this->parseResponse($response); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param string $uri |
|
70 | + * @param array $body |
|
71 | + * @param array $headers |
|
72 | + * @return mixed |
|
73 | + */ |
|
74 | + public function put($uri, $body = [], $headers = []) |
|
75 | + { |
|
76 | + $response = $this |
|
77 | + ->client |
|
78 | + ->put( |
|
79 | + $uri, [ |
|
80 | + 'headers' => $headers, |
|
81 | + 'form_params' => $body, |
|
82 | + ] |
|
83 | + ); |
|
84 | + |
|
85 | + return $this->parseResponse($response); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param string $uri |
|
90 | + * @param array $body |
|
91 | + * @param array $headers |
|
92 | + * @return mixed |
|
93 | + */ |
|
94 | + public function delete($uri, $body = [], $headers = []) |
|
95 | + { |
|
96 | + $response = $this |
|
97 | + ->client |
|
98 | + ->delete($uri, [ |
|
99 | + 'headers' => $headers, |
|
100 | + 'form_params' => $body, |
|
101 | + ]); |
|
102 | + |
|
103 | + return $this->parseResponse($response); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $url |
|
108 | + * @return $this |
|
109 | + */ |
|
110 | + public function setBaseUrl($url) |
|
111 | + { |
|
112 | + $this->client->setBaseUrl($url); |
|
113 | + return $this; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @param ResponseInterface $response |
|
118 | + * @return array|null |
|
119 | + */ |
|
120 | + protected function parseResponse(ResponseInterface $response) |
|
121 | + { |
|
122 | + $responseContents = $response->getBody()->getContents(); |
|
123 | + |
|
124 | + $this->responseHeaders = $response->getHeaders(); |
|
125 | + |
|
126 | + return json_decode($responseContents, true); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function getResponseHeaders() |
|
133 | + { |
|
134 | + return $this->responseHeaders; |
|
135 | + } |
|
136 | 136 | } |
137 | 137 | \ No newline at end of file |