Skip to content

Fetch all repositories for the authenticated user #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 26, 2016
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
"api-clients/rx": "dev-master"
},
"require-dev": {
"api-clients/middleware-pool": "dev-master",
"api-clients/resource-generator": "dev-master",
"api-clients/test-utilities": "^2.0"
},
"suggest": {
"api-clients/middleware-pool": "Keep your request count under control"
},
"autoload": {
"psr-4": {
"ApiClients\\Client\\Travis\\": "src/"
Expand Down
122 changes: 111 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions examples/repositories-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

use ApiClients\Client\Travis\AsyncClient;
use ApiClients\Client\Travis\Resource\RepositoryInterface;
use ApiClients\Foundation\Options;
use ApiClients\Foundation\Pool\Middleware\PoolMiddleware;
use ApiClients\Foundation\Transport\Options as TransportOptions;
use React\EventLoop\Factory;
use ResourcePool\Pool;
use Rx\Observer\CallbackObserver;
use function ApiClients\Foundation\resource_pretty_print;

require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';

$loop = Factory::create();
$client = new AsyncClient(
$loop,
require 'resolve_key.php',
[ // We're passing these extra options to ensure we don't request all repositories at once (!!!)
Options::TRANSPORT_OPTIONS => [
TransportOptions::DEFAULT_REQUEST_OPTIONS => [
PoolMiddleware::class => [
\ApiClients\Foundation\Pool\Options::POOL => new Pool(1),
],
],
TransportOptions::MIDDLEWARE => [
PoolMiddleware::class,
],
],
]
);

$client->repositories()->subscribe(new CallbackObserver(
function (RepositoryInterface $repository) {
resource_pretty_print($repository);
},
function ($e) {
echo (string)$e, PHP_EOL;
},
function () {
echo 'Done!', PHP_EOL;
}
));

$loop->run();
19 changes: 12 additions & 7 deletions src/ApiSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ApiClients\Foundation\Transport\Options as TransportOptions;
use ApiClients\Foundation\Transport\UserAgentStrategies;
use ApiClients\Client\Travis\Middleware\TokenAuthorizationHeaderMiddleware;
use function ApiClients\Foundation\options_merge;

class ApiSettings
{
Expand Down Expand Up @@ -46,19 +47,23 @@ class ApiSettings
*/
public static function getOptions(
string $token,
string $suffix
string $suffix,
array $suppliedOptions = []
): array {
$options = self::TRANSPORT_OPTIONS;
$options = options_merge(self::TRANSPORT_OPTIONS, $suppliedOptions);
$options[FoundationOptions::HYDRATOR_OPTIONS][HydratorOptions::NAMESPACE_SUFFIX] = $suffix;

if (!empty($token)) {
$transportOptions = $options[FoundationOptions::TRANSPORT_OPTIONS];
$transportOptions[TransportOptions::MIDDLEWARE][] = TokenAuthorizationHeaderMiddleware::class;
$transportOptions[TransportOptions::DEFAULT_REQUEST_OPTIONS] = [
TokenAuthorizationHeaderMiddleware::class => [
Options::TOKEN => $token,
],
];
$transportOptions[TransportOptions::DEFAULT_REQUEST_OPTIONS] = array_merge_recursive(
$transportOptions[TransportOptions::DEFAULT_REQUEST_OPTIONS] ?? [],
[
TokenAuthorizationHeaderMiddleware::class => [
Options::TOKEN => $token,
],
]
);
$options[FoundationOptions::TRANSPORT_OPTIONS] = $transportOptions;
}

Expand Down
29 changes: 24 additions & 5 deletions src/AsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
namespace ApiClients\Client\Travis;

use ApiClients\Client\Travis\CommandBus\Command;
use ApiClients\Client\Travis\Resource\HookInterface;
use ApiClients\Foundation\Client;
use ApiClients\Foundation\Factory;
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
use ApiClients\Foundation\Transport\CommandBus\Command\SimpleRequestCommand;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\React\Promise;
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
use function React\Promise\resolve;

Expand All @@ -26,12 +26,17 @@ class AsyncClient
/**
* @param LoopInterface $loop
* @param string $token
* @param array $options
* @param Client|null $client
*/
public function __construct(LoopInterface $loop, string $token = '', Client $client = null)
{
public function __construct(
LoopInterface $loop,
string $token = '',
array $options = [],
Client $client = null
) {
if (!($client instanceof Client)) {
$this->options = ApiSettings::getOptions($token, 'Async');
$this->options = ApiSettings::getOptions($token, 'Async', $options);
$client = Factory::create($loop, $this->options);
}
$this->client = $client;
Expand All @@ -46,6 +51,20 @@ public function repository(string $repository): CancellablePromiseInterface
return $this->client->handle(new Command\RepositoryCommand($repository));
}

/**
* @return ObservableInterface
*/
public function repositories(): ObservableInterface
{
return $this->hooks()->filter(function ($hook) {
return $hook->active();
})->flatMap(function (HookInterface $hook) {
return Promise::toObservable($this->client->handle(
new Command\RepositoryIdCommand($hook->id())
));
});
}

/**
* @return PromiseInterface
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public function hooks(): array
);
}

/**
* @return array
*/
public function repositories(): array
{
return await(
Promise::fromObservable(
$this->client->repositories()->toArray()
),
$this->loop
);
}

/**
* @return array
*/
Expand Down
Loading