1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Linode\Api\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Zurbaev\ApiClient\ApiResource; |
8
|
|
|
use Zurbaev\ApiClient\Contracts\ApiProviderInterface; |
9
|
|
|
use Zurbaev\ApiClient\Contracts\ApiResourceInterface; |
10
|
|
|
use Zurbaev\ApiClient\Exceptions\Resources\UpdateResourceException; |
11
|
|
|
|
12
|
|
|
abstract class LinodeApiResource extends ApiResource |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Resource type. |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public static function resourceType() |
20
|
|
|
{ |
21
|
|
|
return ''; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create new Resource instance from HTTP response. |
26
|
|
|
* |
27
|
|
|
* @param ResponseInterface $response |
28
|
|
|
* @param ApiProviderInterface $api |
29
|
|
|
* @param ApiResourceInterface $owner = null |
30
|
|
|
* |
31
|
|
|
* @return LinodeApiResource |
32
|
|
|
*/ |
33
|
|
|
public static function createFromResponse(ResponseInterface $response, ApiProviderInterface $api, ApiResourceInterface $owner = null) |
34
|
|
|
{ |
35
|
|
|
$data = json_decode((string) $response->getBody(), true); |
36
|
|
|
|
37
|
|
|
return new static($api, $data, $owner); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Update resource data. |
42
|
|
|
* |
43
|
|
|
* @param array $payload |
44
|
|
|
* |
45
|
|
|
* @throws UpdateResourceException |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function update(array $payload): bool |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$response = $this->getHttpClient()->request('PUT', $this->apiUrl(), [ |
53
|
|
|
'json' => $payload, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$this->data = json_decode((string) $response->getBody(), true); |
57
|
|
|
} catch (RequestException $e) { |
58
|
|
|
$this->throwResourceException($e->getResponse(), 'update', UpdateResourceException::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|