|
2 | 2 |
|
3 | 3 | namespace AmsterdamPHP\Console\Api; |
4 | 4 |
|
| 5 | +use AmsterdamPHP\Console\Api\Middleware\DefaultStackFactory; |
| 6 | +use AmsterdamPHP\Console\Api\Middleware\JsonAwareResponse; |
5 | 7 | use GuzzleHttp\Client; |
6 | | -use GuzzleHttp\Collection; |
| 8 | +use GuzzleHttp\ClientInterface; |
| 9 | +use GuzzleHttp\Exception\GuzzleException; |
| 10 | +use JsonException; |
7 | 11 | use function json_encode; |
8 | | -use function var_dump; |
| 12 | +use function preg_match; |
| 13 | +use const JSON_THROW_ON_ERROR; |
9 | 14 |
|
10 | | -final class JoindInClient extends Client |
| 15 | +class JoindInClient |
11 | 16 | { |
12 | | - const DEFAULT_BASE_URL = 'https://api.joind.in/v2.1'; |
| 17 | + private ClientInterface $client; |
13 | 18 |
|
14 | 19 | /** |
15 | 20 | * Constructor |
16 | 21 | */ |
17 | | - public function __construct(array $config) { |
18 | | - $defaults = array('base_url' => self::DEFAULT_BASE_URL); |
19 | | - $required = array('base_url'); |
| 22 | + public function __construct(string $token, string $baseUrl) { |
| 23 | + $this->client = new Client([ |
| 24 | + 'base_uri' => $baseUrl, |
| 25 | + 'headers' => [ |
| 26 | + 'Authorization' => 'OAuth ' . $token, |
| 27 | + 'Accept-Charset' => 'utf-8', |
| 28 | + 'Accept' => 'application/json', |
| 29 | + 'Content-Type' => 'application/json', |
| 30 | + ], |
| 31 | + 'handler' => DefaultStackFactory::createJsonHandlingStack(), |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @throws GuzzleException |
| 37 | + * @throws JsonException |
| 38 | + */ |
| 39 | + public function addEventHost($eventId, $eventHost): JsonAwareResponse |
| 40 | + { |
| 41 | + /** @var JsonAwareResponse $result */ |
| 42 | + $result = $this->client->post('v2.1/events/'.$eventId.'/hosts', [ |
| 43 | + 'body' => json_encode(['host_name' => $eventHost], JSON_THROW_ON_ERROR) |
| 44 | + ]); |
20 | 45 |
|
21 | | - $configuration = Collection::fromConfig($config, $defaults, $required); |
| 46 | + return $result; |
| 47 | + } |
22 | 48 |
|
23 | | - parent::__construct($configuration->toArray()); |
| 49 | + /** |
| 50 | + * @throws GuzzleException |
| 51 | + * @throws JsonException |
| 52 | + */ |
| 53 | + public function submitEvent($event): string |
| 54 | + { |
| 55 | + /** @var JsonAwareResponse $result */ |
| 56 | + $result = $this->client->post('v2.1/events', [ |
| 57 | + 'body' => json_encode($event, JSON_THROW_ON_ERROR) |
| 58 | + ]); |
24 | 59 |
|
25 | | - if ($configuration->get('access_token')) { |
26 | | - $this->setDefaultOption('headers/Authorization', 'OAuth ' . $configuration->get('access_token')); |
27 | | - } |
28 | | - $this->setDefaultOption('headers/Accept-Charset', 'utf-8'); |
29 | | - $this->setDefaultOption('headers/Accept', 'application/json'); |
30 | | - $this->setDefaultOption('headers/Content-Type', 'application/json'); |
| 60 | + return $this->extractIdFromLocation($result->getLocationHeader()); |
31 | 61 | } |
32 | 62 |
|
33 | | - public function addEventHost($eventId, $eventHost) |
| 63 | + private function extractIdFromLocation($locationUrl): string |
34 | 64 | { |
35 | | - $result = $this->post('v2.1/events/'.$eventId.'/hosts', ['body' => json_encode(['host_name' => $eventHost])]); |
36 | | - return $result; |
| 65 | + $matches = []; |
| 66 | + preg_match( |
| 67 | + "/events\/(\d*)/", |
| 68 | + $locationUrl, |
| 69 | + $matches |
| 70 | + ); |
| 71 | + |
| 72 | + return $matches[1] ?? ''; |
37 | 73 | } |
38 | 74 | } |
0 commit comments