1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JDecool\Clubhouse; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
private const ENDPOINT_V1 = 'https://api.clubhouse.io/api/v1'; |
14
|
|
|
private const ENDPOINT_V2 = 'https://api.clubhouse.io/api/v2'; |
15
|
|
|
private const ENDPOINT_BETA = 'https://api.clubhouse.io/api/beta'; |
16
|
|
|
|
17
|
|
|
private $http; |
18
|
|
|
private $baseUri; |
19
|
|
|
private $token; |
20
|
|
|
|
21
|
|
|
public static function createV1(HttpMethodsClient $http, string $token): self |
22
|
|
|
{ |
23
|
|
|
return new self($http, self::ENDPOINT_V1, $token); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function createV2(HttpMethodsClient $http, string $token): self |
27
|
|
|
{ |
28
|
|
|
return new self($http, self::ENDPOINT_V2, $token); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function createBeta(HttpMethodsClient $http, string $token): self |
32
|
|
|
{ |
33
|
|
|
return new self($http, self::ENDPOINT_BETA, $token); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct(HttpMethodsClient $http, string $baseUri, string $token) |
37
|
|
|
{ |
38
|
|
|
if (false === filter_var($baseUri, FILTER_VALIDATE_URL)) { |
39
|
|
|
throw new RuntimeException('Invalid Clubouse base URI.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->http = $http; |
43
|
|
|
$this->baseUri = $baseUri; |
44
|
|
|
$this->token = $token; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function get(string $uri): array |
48
|
|
|
{ |
49
|
|
|
$response = $this->http->get( |
50
|
|
|
$this->endpoint($uri) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if (200 !== $response->getStatusCode()) { |
54
|
|
|
throw $this->createExceptionFromResponse($response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return json_decode((string) $response->getBody(), true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function post(string $uri, array $data): array |
61
|
|
|
{ |
62
|
|
|
$response = $this->http->post( |
63
|
|
|
$this->endpoint($uri), |
64
|
|
|
['Content-Type' => 'application/json'], |
65
|
|
|
json_encode($data) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if (201 !== $response->getStatusCode()) { |
69
|
|
|
throw $this->createExceptionFromResponse($response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return json_decode((string) $response->getBody(), true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function put(string $uri, array $data): array |
76
|
|
|
{ |
77
|
|
|
$response = $this->http->put( |
78
|
|
|
$this->endpoint($uri), |
79
|
|
|
['Content-Type' => 'application/json'], |
80
|
|
|
json_encode($data) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
if (200 !== $response->getStatusCode()) { |
84
|
|
|
throw $this->createExceptionFromResponse($response); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return json_decode((string) $response->getBody(), true); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function delete(string $uri): array |
91
|
|
|
{ |
92
|
|
|
$response = $this->http->delete( |
93
|
|
|
$this->endpoint($uri) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (204 !== $response->getStatusCode()) { |
97
|
|
|
throw $this->createExceptionFromResponse($response); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return json_decode((string) $response->getBody(), true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function endpoint(string $uri): string |
104
|
|
|
{ |
105
|
|
|
return sprintf( |
106
|
|
|
'%s/%s?token=%s', |
107
|
|
|
rtrim($this->baseUri, '/'), |
108
|
|
|
ltrim($uri, '/'), |
109
|
|
|
$this->token |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function createExceptionFromResponse(ResponseInterface $response): ClubhouseException |
114
|
|
|
{ |
115
|
|
|
$content = json_decode((string) $response->getBody(), true); |
116
|
|
|
|
117
|
|
|
return new ClubhouseException($content['message'] ?? 'An error occured.'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|