Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 658aa9b

Browse files
committed
Updated API methods
1 parent 7099e14 commit 658aa9b

File tree

7 files changed

+80
-121
lines changed

7 files changed

+80
-121
lines changed

src/Api/Api.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Codenixsv\MessariApi\MessariClient;
99
use Codenixsv\MessariApi\Api\Query\QueryBuilder;
1010
use Codenixsv\MessariApi\Api\Query\QueryBuilderInterface;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
1112

1213
/**
1314
* Class Api
@@ -43,4 +44,35 @@ public function getClient(): MessariClient
4344
{
4445
return $this->client;
4546
}
47+
48+
/**
49+
* @param string $path
50+
* @param string $assetKey
51+
* @param string $metricId
52+
* @param array $params
53+
* @return array
54+
* @throws \Exception
55+
*/
56+
protected function timeseries(string $path, string $assetKey, string $metricId, array $params): array
57+
{
58+
$query = $this->queryBuilder->buildQuery($params);
59+
$response = $this->client->getBaseClient()->get('/' . $path . '/' . strtolower($assetKey) . '/metrics/'
60+
. $metricId . '/time-series' . $query);
61+
62+
return $this->transformer->transform($response);
63+
}
64+
65+
/**
66+
* @param string $path
67+
* @param array $params
68+
* @return array
69+
* @throws \Exception
70+
*/
71+
protected function all(string $path, array $params): array
72+
{
73+
$query = $this->queryBuilder->buildQuery($params);
74+
$response = $this->client->getBaseClient()->get('/' . $path . $query);
75+
76+
return $this->transformer->transform($response);
77+
}
4678
}

src/Api/Assets.php

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,13 @@ class Assets extends Api
1515
/**
1616
* Get the paginated list of all assets and their metrics and profiles.
1717
*
18-
* @param bool $withMetrics existence of this query param filters assets to those with quantitative data
19-
* @param bool $withProfiles existence of this query param filters assets to those with qualitative data
20-
* @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
21-
* @param string|null $sort default sort is "marketcap desc", but the only valid value for this query
22-
* param is "id" which translates to "id asc", which is useful for a stable sort while paginating
23-
* @param int|null $page Page number, starts at 1. Increment to paginate through
24-
* results (until result is empty array)
25-
* @param int|null $limit default is 20, max is 500
18+
* @param array $params
2619
* @return array
2720
* @throws Exception
2821
*/
29-
public function getAll(
30-
bool $withMetrics = false,
31-
bool $withProfiles = false,
32-
?string $fields = null,
33-
?string $sort = null,
34-
?int $page = null,
35-
?int $limit = null
36-
): array {
37-
$params = compact('fields', 'sort', 'page', 'limit');
38-
if (true === $withMetrics) {
39-
$params['with-metrics'] = '';
40-
}
41-
if (true === $withProfiles) {
42-
$params['with-profiles'] = '';
43-
}
44-
45-
$query = $this->queryBuilder->buildQuery($params);
46-
$response = $this->client->getBaseClient()->get('/assets' . $query);
47-
48-
return $this->transformer->transform($response);
22+
public function getAll($params = []): array
23+
{
24+
return $this->all('assets', $params);
4925
}
5026

5127
/**
@@ -118,35 +94,14 @@ public function getMarketData(string $assetKey, ?string $fields = null): array
11894
* Retrieve historical timeseries data for an asset.
11995
*
12096
* @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
121-
* @param string $metricID The metricID is a unique identifier which describes the type of data returned by
97+
* @param string $metricId The metricID is a unique identifier which describes the type of data returned by
12298
* time-series endpoints.
123-
* @param string|null $start The "start" query parameter can be used to set the start date after which points
124-
* are returned.
125-
* @param string|null $end The "end" query parameter can be used to set the end date after which no more points
126-
* will be returned.
127-
* @param string|null $interval Defines what interval the resulting points will be returned in.
128-
* @param string|null $columns A comma separated list of strings that controls which columns will be returned and
129-
* in what order.
130-
* @param string|null $order Order controls whether points in the response are returned in ascending or
131-
* descending order.
132-
* @param string|null $format Specify format = csv to download data as CSV.
99+
* @param array $params
133100
* @return array
134101
* @throws Exception
135102
*/
136-
public function getTimeseries(
137-
string $assetKey,
138-
string $metricID,
139-
?string $start = null,
140-
?string $end = null,
141-
?string $interval = null,
142-
?string $columns = null,
143-
?string $order = null,
144-
?string $format = null
145-
): array {
146-
$query = $this->queryBuilder->buildQuery(compact('start', 'end', 'interval', 'columns', 'order', 'format'));
147-
$response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . '/metrics/'
148-
. $metricID . '/time-series' . $query);
149-
150-
return $this->transformer->transform($response);
103+
public function getTimeseries(string $assetKey, string $metricId, array $params = []): array
104+
{
105+
return $this->timeseries('assets', $assetKey, $metricId, $params);
151106
}
152107
}

src/Api/Markets.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,29 @@
1313
class Markets extends Api
1414
{
1515
/**
16-
* @param int|null $page Page number, starts at 1. Increment to paginate through results (until
17-
* result is empty array)
18-
* @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
16+
* Get the list of all exchanges and pairs that WebSocket-based market real-time market data API supports.
17+
*
18+
* @param array $params
1919
* @return array
2020
* @throws Exception
2121
*/
22-
public function getAll(?int $page = null, ?string $fields = null): array
22+
public function getAll($params = []): array
2323
{
24-
$query = $this->queryBuilder->buildQuery(compact('fields', 'page'));
25-
$response = $this->client->getBaseClient()->get('/markets' . $query);
26-
27-
return $this->transformer->transform($response);
24+
return $this->all('markets', $params);
2825
}
2926

3027
/**
3128
* Retrieve historical timeseries data for a market.
3229
*
3330
* @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
34-
* @param string $metricID The metricID is a unique identifier which describes the type of data returned by
31+
* @param string $metricId The metricID is a unique identifier which describes the type of data returned by
3532
* time-series endpoints.
36-
* @param string|null $start The "start" query parameter can be used to set the start date after which points
37-
* are returned.
38-
* @param string|null $end The "end" query parameter can be used to set the end date after which no more points
39-
* will be returned.
40-
* @param string|null $interval Defines what interval the resulting points will be returned in.
41-
* @param string|null $columns A comma separated list of strings that controls which columns will be returned and
42-
* in what order.
43-
* @param string|null $order Order controls whether points in the response are returned in ascending or
44-
* descending order.
45-
* @param string|null $format Specify format = csv to download data as CSV.
33+
* @param array $params
4634
* @return array
4735
* @throws Exception
4836
*/
49-
public function getTimeseries(
50-
string $assetKey,
51-
string $metricID,
52-
?string $start = null,
53-
?string $end = null,
54-
?string $interval = null,
55-
?string $columns = null,
56-
?string $order = null,
57-
?string $format = null
58-
): array {
59-
$query = $this->queryBuilder->buildQuery(compact('start', 'end', 'interval', 'columns', 'order', 'format'));
60-
$response = $this->client->getBaseClient()->get('/markets/' . strtolower($assetKey) . '/metrics/'
61-
. $metricID . '/time-series' . $query);
62-
63-
return $this->transformer->transform($response);
37+
public function getTimeseries(string $assetKey, string $metricId, array $params = []): array
38+
{
39+
return $this->timeseries('markets', $assetKey, $metricId, $params);
6440
}
6541
}

src/Api/News.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,28 @@
1313
class News extends Api
1414
{
1515
/**
16-
* @param int|null $page Page number, starts at 1. Increment to paginate through results (until
17-
* result is empty array)
18-
* @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
16+
* Get the latest (paginated) news and analysis for all assets.
17+
*
18+
* @param array $params
1919
* @return array
2020
* @throws Exception
2121
*/
22-
public function getAll(?int $page = null, ?string $fields = null): array
22+
public function getAll($params = []): array
2323
{
24-
$query = $this->queryBuilder->buildQuery(compact('fields', 'page'));
25-
$response = $this->client->getBaseClient()->get('/news' . $query);
26-
27-
return $this->transformer->transform($response);
24+
return $this->all('news', $params);
2825
}
2926

3027

3128
/**
32-
* @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
33-
* @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
34-
* @param bool|null $asMarkdown formatting (other than HTML links) is hidden. Use this query param to return
35-
* content with markdown syntax
29+
* Get the latest (paginated) news and analysis for an asset.
30+
*
31+
* @param string $assetKey
32+
* @param array $params
3633
* @return array
3734
* @throws Exception
3835
*/
39-
public function getForAsset(string $assetKey, ?string $fields = null, ?bool $asMarkdown = null): array
36+
public function getForAsset(string $assetKey, array $params = []): array
4037
{
41-
$params = [];
42-
43-
if (!is_null($fields)) {
44-
$params['fields'] = $fields;
45-
}
46-
if (!is_null($asMarkdown)) {
47-
$params['as_markdown'] = $asMarkdown;
48-
}
49-
5038
$query = $this->queryBuilder->buildQuery($params);
5139
$response = $this->client->getBaseClient()->get('/news/' . strtolower($assetKey) . $query);
5240

tests/Api/AssetsTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testGetAll()
2727
public function testGetAllWithParams()
2828
{
2929
$assets = $this->createAssets($this->mockSuccessfulResponse());
30-
$assets->getAll(true, true);
30+
$assets->getAll(['with-metrics' => '', 'with-profiles' => '']);
3131

3232
/** @var RequestInterface $request */
3333
$request = $assets->getClient()->getBaseClient()->getHttpClient()->getLastRequest();
@@ -96,7 +96,11 @@ public function testGetTimeseries()
9696
public function testGetTimeseriesWithParams()
9797
{
9898
$assets = $this->createAssets($this->mockSuccessfulResponse());
99-
$assets->getTimeseries('btc', 'price', '2020-01-01', '2020-01-07', '1d');
99+
$assets->getTimeseries('btc', 'price', [
100+
'start' => '2020-01-01',
101+
'end' => '2020-01-07',
102+
'interval' => '1d'
103+
]);
100104

101105
/** @var RequestInterface $request */
102106
$request = $assets->getClient()->getBaseClient()->getHttpClient()->getLastRequest();

tests/Api/MarketsTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testGetAll()
2727
public function testGetAllWithParams()
2828
{
2929
$markets = $this->createMarkets($this->mockSuccessfulResponse());
30-
$markets->getAll(1);
30+
$markets->getAll(['page' => 1]);
3131

3232
/** @var RequestInterface $request */
3333
$request = $markets->getClient()->getBaseClient()->getHttpClient()->getLastRequest();
@@ -50,7 +50,11 @@ public function testGetTimeseries()
5050
public function testGetTimeseriesWithParams()
5151
{
5252
$markets = $this->createMarkets($this->mockSuccessfulResponse());
53-
$markets->getTimeseries('binance-btc-usdt', 'price', '2020-01-01', '2020-01-07', '1d');
53+
$markets->getTimeseries('binance-btc-usdt', 'price', [
54+
'start' => '2020-01-01',
55+
'end' => '2020-01-07',
56+
'interval' => '1d'
57+
]);
5458

5559
/** @var RequestInterface $request */
5660
$request = $markets->getClient()->getBaseClient()->getHttpClient()->getLastRequest();

tests/Api/NewsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class NewsTest extends TestCase
1616
{
1717
public function testGetAll()
1818
{
19-
$news = $this->createMarkets($this->mockSuccessfulResponse());
19+
$news = $this->createNews($this->mockSuccessfulResponse());
2020
$news->getAll();
2121

2222
/** @var RequestInterface $request */
@@ -26,8 +26,8 @@ public function testGetAll()
2626

2727
public function testGetAllWithParams()
2828
{
29-
$news = $this->createMarkets($this->mockSuccessfulResponse());
30-
$news->getAll(1);
29+
$news = $this->createNews($this->mockSuccessfulResponse());
30+
$news->getAll(['page' => 1]);
3131

3232
/** @var RequestInterface $request */
3333
$request = $news->getClient()->getBaseClient()->getHttpClient()->getLastRequest();
@@ -36,7 +36,7 @@ public function testGetAllWithParams()
3636

3737
public function testGetForAsset()
3838
{
39-
$news = $this->createMarkets($this->mockSuccessfulResponse());
39+
$news = $this->createNews($this->mockSuccessfulResponse());
4040
$news->getForAsset('btc');
4141

4242
/** @var RequestInterface $request */
@@ -46,8 +46,8 @@ public function testGetForAsset()
4646

4747
public function testGetForAssetWithParams()
4848
{
49-
$news = $this->createMarkets($this->mockSuccessfulResponse());
50-
$news->getForAsset('btc', 'title,content,author/name', true);
49+
$news = $this->createNews($this->mockSuccessfulResponse());
50+
$news->getForAsset('btc', ['fields' => 'title,content,author/name', 'as_markdown' => true]);
5151

5252
/** @var RequestInterface $request */
5353
$request = $news->getClient()->getBaseClient()->getHttpClient()->getLastRequest();
@@ -61,7 +61,7 @@ public function testGetForAssetWithParams()
6161
* @param ResponseInterface $response
6262
* @return News
6363
*/
64-
private function createMarkets(ResponseInterface $response): News
64+
private function createNews(ResponseInterface $response): News
6565
{
6666
$httpClientMock = new HttpMockClient();
6767
$httpClientMock->addResponse($response);

0 commit comments

Comments
 (0)