/** * STREAM */ public function testStreamAdapter() { $client = $this->client(); $message = (string) memory_get_usage(true); $routeStream = Route::get('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId())->toStream(); $routeAnswer = Route::post('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId()); // Connect to client $client->adapters->through(StreamBuzzAdapter::class)->request($routeStream)->subscribe(function ($answer) use($client, $message) { $this->assertInternalType('array', $answer); $this->assertArrayHasKey('text', $answer); if ($message === $answer['text']) { $client->disconnect(); } }); // Send message after 1 second $client->loop->addTimer(1, function () use($message, $routeAnswer) { $this->client()->adapters->through(SyncBuzzAdapter::class)->request($routeAnswer->withBody('text', $message)); }); // Throws exception after 10 seconds timeout $client->loop->addTimer(10, function () use($client) { $client->disconnect(); $this->throwException(new \RuntimeException('Client timeout')); }); $client->connect(); }
/** * STREAMING */ public function testStreamAdapter() { $client = $this->client(); $routeStream = Route::get('rooms/{roomId}/chatMessages')->with('roomId', $this->debugRoomId())->toStream(); /** @var \Generator $stream */ $stream = $client->adapters->through(StreamGuzzleAdapter::class)->request($routeStream); $this->assertInstanceOf(\Generator::class, $stream); }
/** * @param string $message * @return Route */ private function buildRoute(string $message) : Route { $icon = $this->level === static::HOOK_LEVEL_ERROR ? 'error' : $this->level; $route = Route::post($this->hookId)->toWebhook()->withBody('message', $message)->withBody('errorLevel', $this->level); if ($this->icon !== null) { $route->withBody('icon', $icon); } return $route; }
/** * @return array * @throws \InvalidArgumentException */ protected function currentUser() : array { if (!array_key_exists($this->client->token, self::$currentUserId)) { $response = $this->using(AdapterInterface::TYPE_SYNC)->request(Route::get('user')->toApi()); $userId = $response[0] ?? null; if ($userId === null) { throw new \InvalidArgumentException('Broken request. Can not fetch current authenticated user'); } self::$currentUserId[$this->client->token] = $userId; } return self::$currentUserId[$this->client->token]; }
/** * @param string $token * @param Route $route * @return Request * @throws \InvalidArgumentException */ protected function prepareRequest(string $token, Route $route) { $headers = $this->prepareHeaders($token); if ($route->getBody() !== null && $route->method() === 'GET') { throw new \InvalidArgumentException('GET requests can not contain a body'); } return new Request($route->method(), $route->build(), $headers, $route->getBody(), '1.1'); }
/** * Use the streaming API to listen messages. * The streaming API allows real-time access to messages fetching. * * @param string $roomId * @return Observer * @throws \InvalidArgumentException */ public function messages(string $roomId) : Observer { return $this->using(AdapterInterface::TYPE_STREAM)->request(Route::get('rooms/{roomId}/chatMessages')->with('roomId', $roomId)->toStream()); }
/** * Update a message * * @param string $roomId Room id * @param string $messageId Message id * @param string $content New message body * @return mixed */ public function update(string $roomId, string $messageId, string $content) { return $this->fetch(Route::put('rooms/{roomId}/chatMessages/{messageId}')->withMany(['roomId' => $roomId, 'messageId' => $messageId])->withBody('text', $content)); }
/** * List of rooms nested under the specified group. * * @param string $groupId * @return mixed */ public function rooms(string $groupId) { return $this->fetch(Route::get('groups/{groupId}/rooms')->with('groupId', $groupId)); }
/** * List of Gitter channels nested under the current user. * * @param string|null $userId * @return mixed * @throws \InvalidArgumentException */ public function channels(string $userId = null) { return $this->fetch(Route::get('user/{userId}/channels')->with('userId', $userId ?? $this->currentUserId())); }