1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class RequestHandler |
10
|
|
|
{ |
11
|
|
|
private $accessToken; |
12
|
|
|
private $apiUrl; |
13
|
|
|
private $client; |
14
|
|
|
|
15
|
|
|
public function __construct(string $accessToken = null, string $apiUrl = null) |
16
|
|
|
{ |
17
|
|
|
$this->accessToken = $accessToken; |
18
|
|
|
$this->apiUrl = $apiUrl; |
19
|
|
|
$this->client = new Client(['base_uri' => $this->apiUrl . '/']); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getAccessToken(): string |
23
|
|
|
{ |
24
|
|
|
return $this->accessToken; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setAccessToken(string $accessToken) |
28
|
|
|
{ |
29
|
|
|
$this->accessToken = $accessToken; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getApiUrl(): string |
33
|
|
|
{ |
34
|
|
|
return $this->apiUrl; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setApiUrl(string $apiUrl) |
38
|
|
|
{ |
39
|
|
|
$this->apiUrl = $apiUrl; |
40
|
|
|
$this->client = new Client(['base_uri' => $apiUrl . '/']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function handleException(ResponseInterface $response) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
die('beeeurkk'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function get(string $endpoint, array $headers = [], array $params = null) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$request = new GuzzleRequest('GET', 'http://httpbin.org/get'); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function post(string $endpoint, array $params = null, array $headers = []): array |
54
|
|
|
{ |
55
|
|
|
$headers['Content-Type'] = 'application/json'; |
56
|
|
|
$body = null; |
57
|
|
|
if ($params) $body = \GuzzleHttp\json_encode($params); |
58
|
|
|
$request = new GuzzleRequest('POST', $endpoint, $headers, $body); |
59
|
|
|
$response = $this->client->send($request); |
60
|
|
|
|
61
|
|
|
if ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399) { |
62
|
|
|
return \GuzzleHttp\json_decode((string)$response->getBody(), true); |
63
|
|
|
} else { |
64
|
|
|
$this->handleException($response); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.