Skip to content

Commit 2e5030e

Browse files
authored
Added async and sync examples to the readme
1 parent 46077f4 commit 2e5030e

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

README.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TravisCI API Client for PHP 7
1+
# Async first TravisCI API Client for PHP 7
22

33
[![Build Status](https://travis-ci.org/php-api-clients/travis.svg?branch=master)](https://travis-ci.org/php-api-clients/travis)
44
[![Latest Stable Version](https://poser.pugx.org/api-clients/travis/v/stable.png)](https://packagist.org/packages/api-clients/travis)
@@ -7,12 +7,89 @@
77
[![License](https://poser.pugx.org/api-clients/travis/license.png)](https://packagist.org/packages/api-clients/travis)
88
[![PHP 7 ready](http://php7ready.timesplinter.ch/php-api-clients/travis/badge.svg)](https://travis-ci.org/php-api-clients/travis)
99

10+
# Install
1011

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 `^`.
1213

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.
1693

1794
# License
1895

0 commit comments

Comments
 (0)