Skip to content

Commit d9caee2

Browse files
authored
refactor: api endpoints & connection handling (#124)
1 parent 60f19d5 commit d9caee2

21 files changed

+352
-261
lines changed

src/API/AbstractAPI.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,29 @@
44

55
namespace ArkEcosystem\Client\API;
66

7-
use ArkEcosystem\Client\ArkClient;
87
use ArkEcosystem\Client\Connection;
9-
use ArkEcosystem\Client\Contracts\API;
10-
use ArkEcosystem\Client\Http\Request;
11-
use GuzzleHttp\Client;
128
use Illuminate\Support\Arr;
139
use Illuminate\Support\Str;
1410

1511
abstract class AbstractAPI
1612
{
1713
/**
18-
* The client .
14+
* The connection.
1915
*
20-
* @var ArkClient
16+
* @var Connection
2117
*/
22-
public $client;
18+
public $connection;
2319

2420
private string $api = 'api';
2521

2622
/**
2723
* Create a new API class instance.
2824
*
29-
* @param Connection $client
25+
* @param Connection $connection
3026
*/
31-
public function __construct(ArkClient $client)
27+
public function __construct(Connection $connection)
3228
{
33-
$this->client = $client;
29+
$this->connection = $connection;
3430
}
3531

3632
/**
@@ -43,7 +39,7 @@ public function __construct(ArkClient $client)
4339
*/
4440
protected function requestGet(string $path, array $query = [])
4541
{
46-
$response = $this->client->getHttpClient()->get($this->buildUrl($path), [
42+
$response = $this->connection->getHttpClient()->get($this->buildUrl($path), [
4743
'query' => Arr::dot($query),
4844
]);
4945

@@ -60,7 +56,7 @@ protected function requestGet(string $path, array $query = [])
6056
*/
6157
protected function requestPost(string $path, array $parameters = [])
6258
{
63-
$response = $this->client->getHttpClient()->post(
59+
$response = $this->connection->getHttpClient()->post(
6460
$this->buildUrl($path),
6561
['json' => $parameters]
6662
);
@@ -77,7 +73,7 @@ protected function withApi(string $api): self
7773

7874
private function buildUrl(string $path): string
7975
{
80-
$baseUri = $this->client->getHosts()[$this->api];
76+
$baseUri = $this->connection->getHosts()[$this->api];
8177

8278
// Reset the API to the default value.
8379
$this->api = 'api';

src/ArkClient.php

Lines changed: 66 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -4,162 +4,98 @@
44

55
namespace ArkEcosystem\Client;
66

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;
921
use GuzzleHttp\HandlerStack;
10-
use Illuminate\Support\Arr;
11-
use RuntimeException;
1222

13-
/**
14-
* This is the connection class.
15-
*/
1623
class ArkClient
1724
{
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)
5028
{
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);
7230
}
7331

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
8533
{
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);
9135
}
9236

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
10238
{
103-
if (! in_array($type, ['api', 'transactions', 'evm'], true)) {
104-
throw new \InvalidArgumentException('Invalid host type.');
105-
}
39+
return new Blockchain($this->connection);
40+
}
10641

107-
$this->hosts[$type] = $host;
42+
public function blocks(): Blocks
43+
{
44+
return new Blocks($this->connection);
10845
}
10946

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
11848
{
119-
return $this->hosts;
49+
return new Commits($this->connection);
12050
}
12151

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
13053
{
131-
$name = $name === 'evm' ? 'EVM' : ucfirst($name);
54+
return new Contracts($this->connection);
55+
}
13256

133-
$class = "ArkEcosystem\\Client\\API\\{$name}";
57+
public function evm(): EVM
58+
{
59+
return new EVM($this->connection);
60+
}
13461

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+
}
13871

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);
14090
}
14191

142-
/**
143-
* Get the Guzzle client instance.
144-
*
145-
* @return Client
146-
*/
147-
public function getHttpClient(): Client
92+
public function votes(): Votes
14893
{
149-
return $this->httpClient;
94+
return new Votes($this->connection);
15095
}
15196

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
16098
{
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);
164100
}
165101
}

src/ClientManager.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class ClientManager
3131
* @param string $host
3232
* @param string $name
3333
*
34-
* @return ArkClient
34+
* @return Connection
3535
*/
36-
public function connect(string $host, string $name = 'main'): ArkClient
36+
public function connect(string $host, string $name = 'main'): Connection
3737
{
3838
if (isset($this->clients[$name])) {
3939
throw new InvalidArgumentException("Client [$name] is already configured.");
4040
}
4141

42-
$this->clients[$name] = new ArkClient($host);
42+
$this->clients[$name] = new Connection($host);
4343

4444
return $this->clients[$name];
4545
}
@@ -61,9 +61,9 @@ public function disconnect(?string $name = null): void
6161
*
6262
* @param string|null $name
6363
*
64-
* @return ArkClient
64+
* @return Connection
6565
*/
66-
public function client(?string $name = null): ArkClient
66+
public function client(?string $name = null): Connection
6767
{
6868
$name = $name ?? $this->getDefaultClient();
6969

0 commit comments

Comments
 (0)