|
1 | | -# TravisCI API Client for PHP 7 |
| 1 | +# Async first TravisCI API Client for PHP 7 |
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/php-api-clients/travis) |
4 | 4 | [](https://packagist.org/packages/api-clients/travis) |
|
7 | 7 | [](https://packagist.org/packages/api-clients/travis) |
8 | 8 | [](https://travis-ci.org/php-api-clients/travis) |
9 | 9 |
|
| 10 | +# Install |
10 | 11 |
|
11 | | -# Goals |
| 12 | +To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `^`. |
12 | 13 |
|
13 | | -* 100% TravisCI [API](https://docs.travis-ci.com/api) support |
14 | | -* Live log streaming support from Pusher |
15 | | -* Both synchronous and asynchronous |
| 14 | +``` |
| 15 | +composer require api-clients/travis |
| 16 | +``` |
| 17 | + |
| 18 | +# Usage |
| 19 | + |
| 20 | +The client needs two things, the ReactPHP event loop, and optionally an authentication token. Once you created the client you can call the `user` method to show the currently authenticated user. |
| 21 | + |
| 22 | +```php |
| 23 | +use React\EventLoop\Factory; |
| 24 | +use ApiClients\Client\Travis\AsyncClient; |
| 25 | +use ApiClients\Client\Travis\Resource\UserInterface; |
| 26 | +use function ApiClients\Foundation\resource_pretty_print; |
| 27 | + |
| 28 | +$loop = Factory::create(); |
| 29 | +$client = AsyncClient::create( |
| 30 | + $loop, |
| 31 | + 'your travis key from https://blog.travis-ci.com/2013-01-28-token-token-token/' |
| 32 | +); |
| 33 | + |
| 34 | +$client->user()->then(function (UserInterface $user) { |
| 35 | + resource_pretty_print($user); |
| 36 | +}); |
| 37 | + |
| 38 | +$loop->run(); |
| 39 | +``` |
| 40 | + |
| 41 | +## Results stream |
| 42 | + |
| 43 | +The above example used a promise, when there is more then one result an observabe is returned instead. [`RxPHP`](https://github.com/reactivex/rxphp) is used for the observables. This means you can apply a huge list of methods to the stream of results |
| 44 | + |
| 45 | +```php |
| 46 | + |
| 47 | +use React\EventLoop\Factory; |
| 48 | +use ApiClients\Client\Travis\AsyncClient; |
| 49 | +use ApiClients\Client\Travis\Resource\BroadcastInterface; |
| 50 | +use function ApiClients\Foundation\resource_pretty_print; |
| 51 | + |
| 52 | +$loop = Factory::create(); |
| 53 | +$client = AsyncClient::create($loop, 'your key'); |
| 54 | + |
| 55 | +$client->broadcasts()->subscribe(function (BroadcastInterface $broadcast) { |
| 56 | + resource_pretty_print($broadcast); |
| 57 | +}); |
| 58 | + |
| 59 | +$loop->run(); |
| 60 | +``` |
| 61 | + |
| 62 | +# Synchronous usage |
| 63 | + |
| 64 | +The synchronous client works nearly the same as the asynchronous, infact it wraps the asynchronous client to do all the work. This examples does the same as the asynchronous usage example. |
| 65 | + |
| 66 | +```php |
| 67 | +use ApiClients\Client\Travis\Client; |
| 68 | +use function ApiClients\Foundation\resource_pretty_print; |
| 69 | + |
| 70 | +$client = Client::create('your travis key'); |
| 71 | + |
| 72 | +resource_pretty_print($client->user()); |
| 73 | +``` |
| 74 | + |
| 75 | +## Synchronous results stream |
| 76 | + |
| 77 | +Synchronous results streams are returned as an array. |
| 78 | + |
| 79 | +```php |
| 80 | +use ApiClients\Client\Travis\Client; |
| 81 | +use function ApiClients\Foundation\resource_pretty_print; |
| 82 | + |
| 83 | +$client = Client::create('your travis key'); |
| 84 | + |
| 85 | +foreach ($client->broadcasts() as $broadcast) { |
| 86 | + resource_pretty_print($broadcast); |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +# Examples |
| 91 | + |
| 92 | +The [`examples`](https://github.com/php-api-clients/travis/tree/master/examples) directory is filled with all kinds of examples for this package. |
16 | 93 |
|
17 | 94 | # License |
18 | 95 |
|
|
0 commit comments