-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncClient.php
More file actions
172 lines (148 loc) · 4.59 KB
/
AsyncClient.php
File metadata and controls
172 lines (148 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types=1);
namespace ApiClients\Client\Github;
use ApiClients\Client\Github\CommandBus\Command;
use ApiClients\Foundation\ClientInterface;
use ApiClients\Foundation\Factory;
use ApiClients\Foundation\Options;
use ApiClients\Foundation\Resource\ResourceInterface;
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use React\Stream\ReadableStreamInterface;
use Rx\Observable;
use Rx\Scheduler;
final class AsyncClient implements AsyncClientInterface
{
/**
* @var ClientInterface
*/
private $client;
/**
* @var RateLimitState
*/
private $rateLimitState;
/**
* @param ClientInterface $client
* @param RateLimitState $rateLimitState
*/
private function __construct(ClientInterface $client, RateLimitState $rateLimitState)
{
$this->client = $client;
$this->rateLimitState = $rateLimitState;
}
/**
* @param LoopInterface $loop
* @param AuthenticationInterface $auth
* @param array $options
* @return AsyncClient
*/
public static function create(
LoopInterface $loop,
AuthenticationInterface $auth,
array $options = []
): self {
$options = ApiSettings::getOptions($auth, $options, 'Async');
$rateLimitState = new RateLimitState();
$options[Options::CONTAINER_DEFINITIONS][RateLimitState::class] = $rateLimitState;
$client = Factory::create($loop, $options);
try {
Scheduler::setAsyncFactory(function () use ($loop) {
return new Scheduler\EventLoopScheduler($loop);
});
} catch (\Throwable $t) {
}
return new self($client, $rateLimitState);
}
/**
* @internal
* @param ClientInterface $client
* @param RateLimitState $rateLimitState
* @return AsyncClient
*/
public static function createFromClient(ClientInterface $client, RateLimitState $rateLimitState): self
{
return new self($client, $rateLimitState);
}
public function hydrate(string $resource): CancellablePromiseInterface
{
return $this->client->hydrate($resource);
}
public function extract(ResourceInterface $resource): CancellablePromiseInterface
{
return $this->client->extract($resource);
}
public function meta(): PromiseInterface
{
return $this->client->handle(new Command\MetaCommand());
}
/**
* @param string $user
* @return PromiseInterface
*/
public function user(string $user): PromiseInterface
{
return $this->client->handle(new Command\UserCommand($user));
}
/**
* @param string $organization
* @return PromiseInterface
*/
public function organization(string $organization): PromiseInterface
{
return $this->client->handle(new Command\OrganizationCommand($organization));
}
/**
* @return PromiseInterface
*/
public function whoami(): PromiseInterface
{
return $this->client->handle(new Command\UserCommand());
}
/**
* @return Observable
*/
public function emojis(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\EmojisCommand()));
}
public function myOrganizations(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\MyOrganizationsCommand()));
}
public function licenses(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\LicensesCommand()));
}
public function watching(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\WatchingCommand()));
}
/**
* @return RateLimitState
*/
public function getRateLimitState(): RateLimitState
{
return clone $this->rateLimitState;
}
public function rateLimit(): PromiseInterface
{
return $this->client->handle(new Command\RateLimitCommand());
}
public function app(): PromiseInterface
{
return $this->client->handle(new Command\AppCommand());
}
public function renderMarkdown(
ReadableStreamInterface $stream,
string $mode = 'markdown',
string $context = ''
): PromiseInterface {
$stream->pause();
return $this->client->handle(new Command\RenderMarkdownCommand(
$stream,
$mode,
$context
));
}
}