-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAbstractAPI.php
More file actions
83 lines (68 loc) · 1.79 KB
/
AbstractAPI.php
File metadata and controls
83 lines (68 loc) · 1.79 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
<?php
declare(strict_types=1);
namespace ArkEcosystem\Client\API;
use ArkEcosystem\Client\Connection;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
abstract class AbstractAPI
{
/**
* The connection.
*
* @var Connection
*/
public $connection;
private string $api = 'api';
/**
* Create a new API class instance.
*
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Send a GET request with query parameters.
*
* @param string $path
* @param array $query
*
* @return array|null|bool
*/
protected function requestGet(string $path, array $query = [])
{
$response = $this->connection->getHttpClient()->get($this->buildUrl($path), [
'query' => Arr::dot($query),
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* Send a POST request with JSON-encoded parameters.
*
* @param string $path
* @param array $parameters
*
* @return array|null|bool
*/
protected function requestPost(string $path, array $parameters = [])
{
$response = $this->connection->getHttpClient()->post(
$this->buildUrl($path),
['json' => $parameters]
);
return json_decode($response->getBody()->getContents(), true);
}
protected function withApi(string $api): self
{
$this->api = $api;
return $this;
}
private function buildUrl(string $path): string
{
$baseUri = $this->connection->getHosts()[$this->api];
// Reset the API to the default value.
$this->api = 'api';
return Str::finish($baseUri, '/').$path;
}
}