|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace ApiClients\Twitter; |
| 4 | + |
| 5 | +use ApiClients\Foundation\Factory; |
| 6 | +use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
| 7 | +use ApiClients\Foundation\Client; |
| 8 | +use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
| 9 | +use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand; |
| 10 | +use GuzzleHttp\Psr7\Request; |
| 11 | +use JacobKiers\OAuth\Consumer\Consumer; |
| 12 | +use JacobKiers\OAuth\Token\Token; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use React\EventLoop\LoopInterface; |
| 16 | +use React\Promise\PromiseInterface; |
| 17 | +use function React\Promise\resolve; |
| 18 | +use Rx\Extra\Operator\CutOperator; |
| 19 | +use Rx\Observable; |
| 20 | +use Rx\React\Promise; |
| 21 | + |
| 22 | +class AsyncClient |
| 23 | +{ |
| 24 | + const STREAM_DELIMITER = "\r\n"; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Client |
| 28 | + */ |
| 29 | + protected $transport; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + string $consumerKey, |
| 33 | + string $consumerSecret, |
| 34 | + string $accessToken, |
| 35 | + string $accessTokenSecret, |
| 36 | + LoopInterface $loop, |
| 37 | + Client $client = null |
| 38 | + ) { |
| 39 | + if (!($client instanceof Client)) { |
| 40 | + $this->options = ApiSettings::getOptions($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret, 'Async'); |
| 41 | + $client = Factory::create($loop, $this->options); |
| 42 | + } |
| 43 | + $this->client = $client; |
| 44 | + $this->consumer = new Consumer($consumerKey, $consumerSecret); |
| 45 | + $this->token = new Token($accessToken, $accessTokenSecret); |
| 46 | + } |
| 47 | + |
| 48 | + public function user(string $user): PromiseInterface |
| 49 | + { |
| 50 | + return $this->client->handle(new RequestCommand( |
| 51 | + new Request('GET', 'users/show.json?screen_name=' . $user) |
| 52 | + ))->then(function (ResponseInterface $response) { |
| 53 | + return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson()))); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + public function sampleStream(): Observable |
| 58 | + { |
| 59 | + return $this->stream( |
| 60 | + new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json') |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function filteredStream(array $filter = []): Observable |
| 65 | + { |
| 66 | + $postData = http_build_query($filter); |
| 67 | + return $this->stream( |
| 68 | + new Request( |
| 69 | + 'POST', |
| 70 | + 'https://stream.twitter.com/1.1/statuses/filter.json', |
| 71 | + [ |
| 72 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
| 73 | + 'Content-Length' => strlen($postData), |
| 74 | + ], |
| 75 | + $postData |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + protected function stream(RequestInterface $request): Observable |
| 81 | + { |
| 82 | + return Promise::toObservable($this->client->handle(new StreamingRequestCommand( |
| 83 | + $request |
| 84 | + )))->switchLatest()->lift(function () { |
| 85 | + return new CutOperator(self::STREAM_DELIMITER); |
| 86 | + })->filter(function (string $json) { |
| 87 | + return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times |
| 88 | + })->jsonDecode()->flatMap(function (array $document) { |
| 89 | + if (isset($document['delete'])) { |
| 90 | + return Promise::toObservable($this->client->handle(new HydrateCommand('DeletedTweet', $document['delete']))); |
| 91 | + } |
| 92 | + |
| 93 | + return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document))); |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
0 commit comments