-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.php
98 lines (85 loc) · 2.44 KB
/
Client.php
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
<?php declare(strict_types=1);
namespace ApiClients\Client\Github;
use ApiClients\Client\Github\Resource\UserInterface;
use ApiClients\Foundation\Factory as FoundationClientFactory;
use ApiClients\Foundation\Options;
use ApiClients\Foundation\Resource\ResourceInterface;
use function Clue\React\Block\await;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
use Rx\Scheduler;
final class Client implements ClientInterface
{
/**
* @var LoopInterface
*/
private $loop;
/**
* @var AsyncClient
*/
private $asyncClient;
/**
* @param LoopInterface $loop
* @param AsyncClient $client
*/
private function __construct(LoopInterface $loop, AsyncClient $client)
{
$this->loop = $loop;
$this->asyncClient = $client;
}
/**
* @param AuthenticationInterface $auth
* @param array $options
* @return Client
*/
public static function create(
AuthenticationInterface $auth,
array $options = []
): self {
$loop = Factory::create();
$options = ApiSettings::getOptions($auth, $options, 'Sync');
$rateLimitState = new RateLimitState();
$options[Options::CONTAINER_DEFINITIONS][RateLimitState::class] = $rateLimitState;
$client = FoundationClientFactory::create($loop, $options);
try {
Scheduler::setAsyncFactory(function () use ($loop) {
return new Scheduler\EventLoopScheduler($loop);
});
} catch (\Throwable $t) {
}
$asyncClient = AsyncClient::createFromClient($client, $rateLimitState);
return new self($loop, $asyncClient);
}
public function hydrate(string $resource): ResourceInterface
{
return await(
$this->asyncClient->hydrate($resource),
$this->loop
);
}
public function extract(ResourceInterface $resource): string
{
return await(
$this->asyncClient->extract($resource),
$this->loop
);
}
/**
* @param string $user
* @return UserInterface
*/
public function user(string $user): UserInterface
{
return await(
$this->asyncClient->user($user),
$this->loop
);
}
/**
* @return RateLimitState
*/
public function getRateLimitState(): RateLimitState
{
return $this->asyncClient->getRateLimitState();
}
}