|
4 | 4 |
|
5 | 5 | namespace ArkEcosystem\Client;
|
6 | 6 |
|
7 |
| -use BadMethodCallException; |
8 |
| -use GuzzleHttp\Client; |
| 7 | +use ArkEcosystem\Client\API\ApiNodes; |
| 8 | +use ArkEcosystem\Client\API\Blockchain; |
| 9 | +use ArkEcosystem\Client\API\Blocks; |
| 10 | +use ArkEcosystem\Client\API\Commits; |
| 11 | +use ArkEcosystem\Client\API\Contracts; |
| 12 | +use ArkEcosystem\Client\API\EVM; |
| 13 | +use ArkEcosystem\Client\API\Node; |
| 14 | +use ArkEcosystem\Client\API\Peers; |
| 15 | +use ArkEcosystem\Client\API\Receipts; |
| 16 | +use ArkEcosystem\Client\API\Rounds; |
| 17 | +use ArkEcosystem\Client\API\Transactions; |
| 18 | +use ArkEcosystem\Client\API\Validators; |
| 19 | +use ArkEcosystem\Client\API\Votes; |
| 20 | +use ArkEcosystem\Client\API\Wallets; |
9 | 21 | use GuzzleHttp\HandlerStack;
|
10 |
| -use Illuminate\Support\Arr; |
11 |
| -use RuntimeException; |
12 | 22 |
|
13 |
| -/** |
14 |
| - * This is the connection class. |
15 |
| - */ |
16 | 23 | class ArkClient
|
17 | 24 | {
|
18 |
| - /** |
19 |
| - * The Guzzle Client instance. |
20 |
| - * |
21 |
| - * @var Client |
22 |
| - */ |
23 |
| - public $httpClient; |
24 |
| - |
25 |
| - /** |
26 |
| - * The hosts to connect to. |
27 |
| - * |
28 |
| - * @var array{ |
29 |
| - * api: string, |
30 |
| - * transactions: string|null, |
31 |
| - * evm: string|null |
32 |
| - * } |
33 |
| - */ |
34 |
| - private array $hosts; |
35 |
| - |
36 |
| - /** |
37 |
| - * Make a new connection instance. |
38 |
| - * |
39 |
| - * @param string|array{ |
40 |
| - * api: string, |
41 |
| - * transactions: string|null, |
42 |
| - * evm: string|null |
43 |
| - * } $hostOrHosts |
44 |
| - * @param array $clientConfig |
45 |
| - * @param HandlerStack $handler |
46 |
| - * |
47 |
| - * @throws InvalidArgumentException if $hostOrHosts is an array and does not have the required format |
48 |
| - */ |
49 |
| - public function __construct(array|string $hostOrHosts, array $clientConfig = [], ?HandlerStack $handler = null) |
| 25 | + public Connection $connection; |
| 26 | + |
| 27 | + public function __construct(string|array $hostOrHosts, array $clientConfig = [], ?HandlerStack $handler = null) |
50 | 28 | {
|
51 |
| - $this->validateHosts($hostOrHosts); |
52 |
| - |
53 |
| - if (is_array($hostOrHosts)) { |
54 |
| - $this->hosts = $hostOrHosts; |
55 |
| - } else { |
56 |
| - $this->hosts = ['api' => $hostOrHosts]; |
57 |
| - } |
58 |
| - |
59 |
| - $options = [ |
60 |
| - ...$clientConfig, |
61 |
| - 'headers' => [ |
62 |
| - ...Arr::get($clientConfig, 'headers', []), |
63 |
| - 'Content-Type' => 'application/json', |
64 |
| - ], |
65 |
| - ]; |
66 |
| - |
67 |
| - if ($handler instanceof HandlerStack) { |
68 |
| - $options['handler'] = $handler; |
69 |
| - } |
70 |
| - |
71 |
| - $this->httpClient = new Client($options); |
| 29 | + $this->connection = new Connection($hostOrHosts, $clientConfig, $handler); |
72 | 30 | }
|
73 | 31 |
|
74 |
| - /** |
75 |
| - * Handle dynamic method calls into the connection. |
76 |
| - * |
77 |
| - * @param string $name |
78 |
| - * @param mixed $args |
79 |
| - * |
80 |
| - * @throws BadMethodCallException |
81 |
| - * |
82 |
| - * @return ApiInterface |
83 |
| - */ |
84 |
| - public function __call($name, $args) |
| 32 | + public function apiNodes(): ApiNodes |
85 | 33 | {
|
86 |
| - try { |
87 |
| - return $this->api($name); |
88 |
| - } catch (RuntimeException $e) { |
89 |
| - throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name)); |
90 |
| - } |
| 34 | + return new ApiNodes($this->connection); |
91 | 35 | }
|
92 | 36 |
|
93 |
| - /** |
94 |
| - * Set the host for the given type. |
95 |
| - * |
96 |
| - * @param string $host |
97 |
| - * @param string $type |
98 |
| - * |
99 |
| - * @throws InvalidArgumentException if the type is not 'api', 'transactions', or 'evm' |
100 |
| - */ |
101 |
| - public function setHost(string $host, string $type): void |
| 37 | + public function blockchain(): Blockchain |
102 | 38 | {
|
103 |
| - if (! in_array($type, ['api', 'transactions', 'evm'], true)) { |
104 |
| - throw new \InvalidArgumentException('Invalid host type.'); |
105 |
| - } |
| 39 | + return new Blockchain($this->connection); |
| 40 | + } |
106 | 41 |
|
107 |
| - $this->hosts[$type] = $host; |
| 42 | + public function blocks(): Blocks |
| 43 | + { |
| 44 | + return new Blocks($this->connection); |
108 | 45 | }
|
109 | 46 |
|
110 |
| - /** |
111 |
| - * @return array{ |
112 |
| - * api: string, |
113 |
| - * transactions: string|null, |
114 |
| - * evm: string|null |
115 |
| - * } |
116 |
| - */ |
117 |
| - public function getHosts(): array |
| 47 | + public function commits(): Commits |
118 | 48 | {
|
119 |
| - return $this->hosts; |
| 49 | + return new Commits($this->connection); |
120 | 50 | }
|
121 | 51 |
|
122 |
| - /** |
123 |
| - * Make a new resource instance. |
124 |
| - * |
125 |
| - * @param string $name |
126 |
| - * |
127 |
| - * @return API\AbstractAPI |
128 |
| - */ |
129 |
| - public function api(string $name): API\AbstractAPI |
| 52 | + public function contracts(): Contracts |
130 | 53 | {
|
131 |
| - $name = $name === 'evm' ? 'EVM' : ucfirst($name); |
| 54 | + return new Contracts($this->connection); |
| 55 | + } |
132 | 56 |
|
133 |
| - $class = "ArkEcosystem\\Client\\API\\{$name}"; |
| 57 | + public function evm(): EVM |
| 58 | + { |
| 59 | + return new EVM($this->connection); |
| 60 | + } |
134 | 61 |
|
135 |
| - if (! class_exists($class)) { |
136 |
| - throw new RuntimeException("Class [$class] does not exist."); |
137 |
| - } |
| 62 | + public function node(): Node |
| 63 | + { |
| 64 | + return new Node($this->connection); |
| 65 | + } |
| 66 | + |
| 67 | + public function peers(): Peers |
| 68 | + { |
| 69 | + return new Peers($this->connection); |
| 70 | + } |
138 | 71 |
|
139 |
| - return new $class($this); |
| 72 | + public function receipts(): Receipts |
| 73 | + { |
| 74 | + return new Receipts($this->connection); |
| 75 | + } |
| 76 | + |
| 77 | + public function rounds(): Rounds |
| 78 | + { |
| 79 | + return new Rounds($this->connection); |
| 80 | + } |
| 81 | + |
| 82 | + public function transactions(): Transactions |
| 83 | + { |
| 84 | + return new Transactions($this->connection); |
| 85 | + } |
| 86 | + |
| 87 | + public function validators(): Validators |
| 88 | + { |
| 89 | + return new Validators($this->connection); |
140 | 90 | }
|
141 | 91 |
|
142 |
| - /** |
143 |
| - * Get the Guzzle client instance. |
144 |
| - * |
145 |
| - * @return Client |
146 |
| - */ |
147 |
| - public function getHttpClient(): Client |
| 92 | + public function votes(): Votes |
148 | 93 | {
|
149 |
| - return $this->httpClient; |
| 94 | + return new Votes($this->connection); |
150 | 95 | }
|
151 | 96 |
|
152 |
| - /** |
153 |
| - * Validate the hosts array format. |
154 |
| - * |
155 |
| - * @param string|array $hostOrHosts |
156 |
| - * |
157 |
| - * @throws InvalidArgumentException if the hosts array does not have the required format |
158 |
| - */ |
159 |
| - private function validateHosts(array|string $hostOrHosts): void |
| 97 | + public function wallets(): Wallets |
160 | 98 | {
|
161 |
| - if (is_array($hostOrHosts) && ! array_key_exists('api', $hostOrHosts)) { |
162 |
| - throw new \InvalidArgumentException(sprintf('The hosts array must contain the key "api".')); |
163 |
| - } |
| 99 | + return new Wallets($this->connection); |
164 | 100 | }
|
165 | 101 | }
|
0 commit comments