Skip to content

Commit 6792276

Browse files
committed
Specific path for repository contents, and list contents from a directory resource
1 parent 4e2a227 commit 6792276

14 files changed

Lines changed: 166 additions & 8 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use ApiClients\Client\Github\AsyncClient;
4+
use ApiClients\Client\Github\Resource\Async\Contents\Directory;
5+
use ApiClients\Client\Github\Resource\Async\Repository;
6+
use ApiClients\Client\Github\Resource\Async\User;
7+
use React\EventLoop\Factory;
8+
use function ApiClients\Foundation\resource_pretty_print;
9+
use Rx\Observable;
10+
11+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
12+
13+
$loop = Factory::create();
14+
15+
$client = AsyncClient::create($loop, require 'resolve_token.php');
16+
17+
$client->user($argv[1] ?? 'php-api-clients')->then(function (User $user) use ($argv) {
18+
resource_pretty_print($user);
19+
return $user->repository($argv[2] ?? 'github');
20+
})->then(function (Repository $repository) {
21+
$repository->contents()->flatMap(function ($node) {
22+
if (!($node instanceof Directory)) {
23+
return Observable::fromArray([$node]);
24+
}
25+
26+
return $node->contents();
27+
})->subscribe(function ($content) {
28+
resource_pretty_print($content, 1, true);
29+
}, function ($error) {
30+
echo (string)$error;
31+
});
32+
})->done(null, 'display_throwable');
33+
34+
$loop->run();
35+
36+
displayState($client->getRateLimitState());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use ApiClients\Client\Github\AsyncClient;
4+
use ApiClients\Client\Github\Resource\Async\Repository;
5+
use ApiClients\Client\Github\Resource\Async\User;
6+
use React\EventLoop\Factory;
7+
use function ApiClients\Foundation\resource_pretty_print;
8+
9+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
10+
11+
$loop = Factory::create();
12+
13+
$client = AsyncClient::create($loop, require 'resolve_token.php');
14+
15+
$client->user($argv[1] ?? 'php-api-clients')->then(function (User $user) use ($argv) {
16+
resource_pretty_print($user);
17+
return $user->repository($argv[2] ?? 'github');
18+
})->then(function (Repository $repository) use ($argv) {
19+
$repository->contents($argv[3] ?? 'github')->subscribe(function ($content) {
20+
resource_pretty_print($content, 1, true);
21+
}, function ($error) {
22+
echo (string)$error;
23+
});
24+
})->done(null, 'display_throwable');
25+
26+
$loop->run();
27+
28+
displayState($client->getRateLimitState());

‎src/CommandBus/Command/Repository/ContentsCommand.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ final class ContentsCommand
1515
private $fullname;
1616

1717
/**
18+
* @var string
19+
*/
20+
private $path;
21+
22+
/**
23+
* ContentsCommand constructor.
1824
* @param string $fullname
25+
* @param string $path
1926
*/
20-
public function __construct(string $fullname)
27+
public function __construct($fullname, $path)
2128
{
2229
$this->fullname = $fullname;
30+
$this->path = $path;
2331
}
2432

2533
/**
@@ -29,4 +37,12 @@ public function getFullname(): string
2937
{
3038
return $this->fullname;
3139
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getPath(): string
45+
{
46+
return $this->path;
47+
}
3248
}

‎src/CommandBus/Handler/Repository/ContentsHandler.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ public function __construct(RequestService $requestService, Hydrator $hydrator)
3737

3838
public function handle(ContentsCommand $command)
3939
{
40+
$path = ltrim($command->getPath(), '/');
41+
$uri = 'repos/' . $command->getFullname() . '/contents/' . $path;
4042
return resolve(
4143
Promise::toObservable(
4244
$this->requestService->request(
43-
new Request('GET', 'repos/' . $command->getFullname() . '/contents/')
45+
new Request('GET', $uri)
4446
)
4547
)->flatMap(function ($contents) {
4648
return Observable::fromArray($contents->getBody()->getJson());
47-
})->map(function ($content) {
49+
})->map(function ($content) use ($command) {
50+
$content['repository_fullname'] = $command->getFullname();
4851
if ($content['type'] === 'file') {
4952
return $this->hydrator->hydrate(
5053
FileInterface::HYDRATE_CLASS,

‎src/Resource/Async/Contents/Directory.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace ApiClients\Client\Github\Resource\Async\Contents;
44

5+
use ApiClients\Client\Github\CommandBus\Command\Repository\ContentsCommand;
56
use ApiClients\Client\Github\Resource\Contents\Directory as BaseDirectory;
7+
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
8+
use Rx\Observable;
69

710
class Directory extends BaseDirectory
811
{
912
public function refresh() : Directory
1013
{
1114
throw new \Exception('TODO: create refresh method!');
1215
}
16+
17+
public function contents(): Observable
18+
{
19+
return unwrapObservableFromPromise(
20+
$this->handleCommand(
21+
new ContentsCommand($this->repository_fullname, $this->path)
22+
)
23+
);
24+
}
1325
}

‎src/Resource/Async/Repository.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function addLabel(string $name, string $colour): PromiseInterface
5454
);
5555
}
5656

57-
public function contents(): Observable
57+
public function contents(string $path = '/'): Observable
5858
{
5959
return unwrapObservableFromPromise(
6060
$this->handleCommand(
61-
new ContentsCommand($this->fullName())
61+
new ContentsCommand($this->fullName(), $path)
6262
)
6363
);
6464
}

‎src/Resource/Contents/Directory.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ abstract class Directory extends AbstractResource implements DirectoryInterface
6464
*/
6565
protected $download_url;
6666

67+
/**
68+
* @var string
69+
*/
70+
protected $repository_fullname;
71+
6772
/**
6873
* @var Links
6974
*/
@@ -151,6 +156,14 @@ public function downloadUrl() : string
151156
return $this->download_url;
152157
}
153158

159+
/**
160+
* @return string
161+
*/
162+
public function repositoryFullname(): string
163+
{
164+
return $this->repository_fullname;
165+
}
166+
154167
/**
155168
* @return Links
156169
*/

‎src/Resource/Contents/DirectoryInterface.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ public function downloadUrl() : string;
6262
* @return Links
6363
*/
6464
public function links() : Links;
65+
66+
/**
67+
* @return string
68+
*/
69+
public function repositoryFullname(): string;
6570
}

‎src/Resource/Contents/EmptyDirectory.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public function downloadUrl() : string
8686
return null;
8787
}
8888

89+
/**
90+
* @return string
91+
*/
92+
public function repositoryFullname(): string
93+
{
94+
return null;
95+
}
96+
8997
/**
9098
* @return Links
9199
*/

‎src/Resource/Contents/EmptyFile.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public function downloadUrl() : string
9494
return null;
9595
}
9696

97+
/**
98+
* @return string
99+
*/
100+
public function repositoryFullname(): string
101+
{
102+
return null;
103+
}
104+
97105
/**
98106
* @return Links
99107
*/

0 commit comments

Comments
 (0)