1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guapa\TimeChimp; |
4
|
|
|
|
5
|
|
|
use Guapa\TimeChimp\Exceptions\ClientException; |
6
|
|
|
use Guapa\TimeChimp\Exceptions\NotFoundException; |
7
|
|
|
use Guapa\TimeChimp\Exceptions\UnauthorizedException; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
abstract class AbstractRequest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \GuzzleHttp\Client |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \GuzzleHttp\Client|string |
21
|
|
|
*/ |
22
|
|
|
protected $url = 'https://api.timechimp.com/'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $accessToken; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $url |
31
|
|
|
* |
32
|
|
|
* @return \Guapa\TimeChimp\AbstractRequest |
33
|
|
|
*/ |
34
|
|
|
public function setUrl(string $url): AbstractRequest |
35
|
|
|
{ |
36
|
|
|
$this->url = $url; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $token |
43
|
|
|
* |
44
|
|
|
* @return \Guapa\TimeChimp\AbstractRequest |
45
|
|
|
*/ |
46
|
|
|
public function setAccessToken(string $token): AbstractRequest |
47
|
|
|
{ |
48
|
|
|
$this->accessToken = $token; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \GuzzleHttp\Client |
55
|
|
|
*/ |
56
|
|
|
public function createClient(): Client |
57
|
|
|
{ |
58
|
|
|
if ($this->client === null) { |
59
|
|
|
// Default Options |
60
|
|
|
$options = [ |
61
|
|
|
'base_uri' => $this->url, |
62
|
|
|
'headers' => [ |
63
|
|
|
'Accept' => 'application/json', |
64
|
|
|
'Content-Type' => 'application/json', |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
if ($this->accessToken !== null) { |
69
|
|
|
$options['headers']['Authorization'] = "Bearer {$this->accessToken}"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->client = new Client($options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->client; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Allows the user to set a specific client should the moment arise. |
80
|
|
|
* |
81
|
|
|
* @param \GuzzleHttp\Client $client |
82
|
|
|
*/ |
83
|
|
|
public function setClient(Client $client) |
84
|
|
|
{ |
85
|
|
|
$this->client = $client; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the request url |
90
|
|
|
* |
91
|
|
|
* @param string $resource |
92
|
|
|
* |
93
|
|
|
* @return \Psr\Http\Message\UriInterface |
94
|
|
|
*/ |
95
|
|
|
public function getRequestUrl($resource) |
96
|
|
|
{ |
97
|
|
|
return \GuzzleHttp\Psr7\uri_for($this->getApi().'/'.$resource); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the Api to call agains |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getApi() |
106
|
|
|
{ |
107
|
|
|
return '/v1'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Execute the request and return the response as a stream |
112
|
|
|
* |
113
|
|
|
* @param string $method |
114
|
|
|
* @param string $resource |
115
|
|
|
* @param array $parameters |
116
|
|
|
* |
117
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
118
|
|
|
* @throws \Guapa\TimeChimp\Exceptions\ClientException |
119
|
|
|
* @throws \Guapa\TimeChimp\Exceptions\NotFoundException |
120
|
|
|
* @throws \Guapa\TimeChimp\Exceptions\UnauthorizedException |
121
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
122
|
|
|
*/ |
123
|
|
|
protected function execute(string $method, string $resource, array $parameters = []): ResponseInterface |
124
|
|
|
{ |
125
|
|
|
$method = strtoupper($method); |
126
|
|
|
$client = $this->createClient(); |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
return $client->request($method, $this->getRequestUrl($resource), $parameters); |
130
|
|
|
} catch (RequestException $exception) { |
131
|
|
|
$response = \json_decode((string)$exception->getResponse()->getBody(), true); |
132
|
|
|
$message = $response['message'] ??$exception->getMessage(); |
133
|
|
|
|
134
|
|
|
switch ($exception->getCode()) { |
135
|
|
|
case 401: |
136
|
|
|
throw new UnauthorizedException($message, 401, $exception); |
137
|
|
|
case 404: |
138
|
|
|
throw new NotFoundException($message, 404, $exception); |
139
|
|
|
default: |
140
|
|
|
throw new ClientException($message, $exception->getCode(), $exception); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|