Skip to content

Commit 0b4b4bd

Browse files
authored
Merge pull request #12 from MarketDataApp/add-html-csv-outputs
Add universal parameters
2 parents cc063eb + d45bf4d commit 0b4b4bd

37 files changed

Lines changed: 812 additions & 104 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.6.0-beta
4+
5+
Added universal parameters to all endpoints with the ability to change format to CSV and HTML (beta).
6+
37
## v0.5.0-beta
48

59
Added indices->quotes to parallelize and speed up multiple index quotes.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ $status = $client->utilities->api_status();
7272
$headers = $client->utilities->headers();
7373
```
7474

75+
### Universal Parameters
76+
77+
All endpoints (other than utilities) supports universal parameters.
78+
79+
For instance, you can change the format to CSV
80+
81+
```
82+
$option_chain = $client->options->option_chain(
83+
symbol: 'AAPL',
84+
expiration: '2025-01-17',
85+
side: Side::CALL,
86+
parameters: new Parameters(format: Format::CSV),
87+
);
88+
```
89+
7590
## Testing
7691

7792
```bash

src/ClientBase.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setGuzzle(GuzzleClient $guzzleClient): void
3131
/**
3232
* @throws \Throwable
3333
*/
34-
public function executeInParallel(array $calls): array
34+
public function execute_in_parallel(array $calls): array
3535
{
3636
$promises = [];
3737
foreach ($calls as $call) {
@@ -59,8 +59,9 @@ protected function async($method, array $arguments = []): PromiseInterface
5959
public function execute($method, array $arguments = []): object
6060
{
6161
try {
62+
$format = array_key_exists('format', $arguments) ? $arguments['format'] : 'json';
6263
$response = $this->guzzle->get($method, [
63-
'headers' => $this->headers(),
64+
'headers' => $this->headers($format),
6465
'query' => $arguments,
6566
]);
6667
} catch (\GuzzleHttp\Exception\ClientException $e) {
@@ -70,22 +71,37 @@ public function execute($method, array $arguments = []): object
7071
};
7172
}
7273

73-
$json_response = (string)$response->getBody();
74+
switch ($format) {
75+
case 'csv':
76+
case 'html':
77+
$object_response = (object)array(
78+
$arguments['format'] => (string)$response->getBody()
79+
);
80+
break;
7481

75-
$response = json_decode($json_response);
82+
case 'json':
83+
default:
84+
$json_response = (string)$response->getBody();
7685

77-
if (isset($response->s) && $response->s === 'error') {
78-
throw new ApiException(message: $response->errmsg, response: $response);
86+
$object_response = json_decode($json_response);
87+
88+
if (isset($object_response->s) && $object_response->s === 'error') {
89+
throw new ApiException(message: $object_response->errmsg, response: $response);
90+
}
7991
}
8092

81-
return $response;
93+
return $object_response;
8294
}
8395

84-
protected function headers(): array
96+
protected function headers(string $format = 'json'): array
8597
{
8698
return [
8799
'Host' => self::API_HOST,
88-
'Accept' => 'application/json',
100+
'Accept' => match ($format) {
101+
'json' => 'application/json',
102+
'csv' => 'text/csv',
103+
'html' => 'text/html',
104+
},
89105
'Authorization' => "Bearer $this->token",
90106
];
91107
}

src/Endpoints/Indices.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use MarketDataApp\Client;
7+
use MarketDataApp\Endpoints\Requests\Parameters;
78
use MarketDataApp\Endpoints\Responses\Indices\Candles;
89
use MarketDataApp\Endpoints\Responses\Indices\Quote;
910
use MarketDataApp\Endpoints\Responses\Indices\Quotes;
1011
use MarketDataApp\Exceptions\ApiException;
12+
use MarketDataApp\Traits\UniversalParameters;
1113

1214
class Indices
1315
{
1416

17+
use UniversalParameters;
18+
1519
private Client $client;
1620
public const BASE_URL = "v1/indices/";
1721

@@ -28,32 +32,40 @@ public function __construct($client)
2832
*
2933
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
3034
*
35+
* @param Parameters|null $parameters Universal parameters for all methods (such as format).
36+
*
3137
* @throws GuzzleException|ApiException
3238
*/
33-
public function quote(string $symbol, bool $fifty_two_week = false): Quote
34-
{
35-
return new Quote($this->client->execute(self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]));
39+
public function quote(
40+
string $symbol,
41+
bool $fifty_two_week = false,
42+
?Parameters $parameters = null
43+
): Quote {
44+
return new Quote($this->execute("quotes/$symbol", ['52week' => $fifty_two_week], $parameters));
3645
}
3746

3847

39-
4048
/**
4149
* Get a real-time price quote for a multiple indices by doing parallel requests.
4250
*
4351
* @param array $symbols The ticker symbols to return in the response.
4452
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
53+
* @param Parameters|null $parameters Universal parameters for all methods (such as format).
4554
*
4655
* @throws \Throwable
4756
*/
48-
public function quotes(array $symbols, bool $fifty_two_week = false): Quotes
49-
{
57+
public function quotes(
58+
array $symbols,
59+
bool $fifty_two_week = false,
60+
?Parameters $parameters = null
61+
): Quotes {
5062
// Execute standard quotes in parallel
5163
$calls = [];
5264
foreach ($symbols as $symbol) {
53-
$calls[] = [self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]];
65+
$calls[] = ["quotes/$symbol", ['52week' => $fifty_two_week]];
5466
}
5567

56-
return new Quotes($this->client->executeInParallel($calls));
68+
return new Quotes($this->execute_in_parallel($calls, $parameters));
5769
}
5870

5971
/**
@@ -78,17 +90,19 @@ public function quotes(array $symbols, bool $fifty_two_week = false): Quotes
7890
* @param int|null $countback Will fetch a number of candles before (to the left of) to. If you use from, countback
7991
* is not required.
8092
*
93+
* @param Parameters|null $parameters Universal parameters for all methods (such as format).
94+
*
8195
* @throws ApiException|GuzzleException
8296
*/
8397
public function candles(
8498
string $symbol,
8599
string $from,
86100
string $to = null,
87101
string $resolution = 'D',
88-
int $countback = null
102+
int $countback = null,
103+
?Parameters $parameters = null
89104
): Candles {
90-
return new Candles($this->client->execute(self::BASE_URL . "candles/{$resolution}/{$symbol}/",
91-
compact('from', 'to', 'countback')
92-
));
105+
return new Candles($this->execute("candles/{$resolution}/{$symbol}/", compact('from', 'to', 'countback'),
106+
$parameters));
93107
}
94108
}

src/Endpoints/Markets.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use MarketDataApp\Client;
7+
use MarketDataApp\Endpoints\Requests\Parameters;
78
use MarketDataApp\Endpoints\Responses\Markets\Statuses;
89
use MarketDataApp\Exceptions\ApiException;
10+
use MarketDataApp\Traits\UniversalParameters;
911

1012
class Markets
1113
{
1214

15+
use UniversalParameters;
16+
1317
private Client $client;
1418
public const BASE_URL = "v1/markets/";
1519

@@ -37,16 +41,19 @@ public function __construct($client)
3741
* @param int|null $countback Countback will fetch a number of dates before to If you use from, countback is not
3842
* required.
3943
*
44+
* @param Parameters|null $parameters Universal parameters for all methods (such as format).
45+
*
4046
* @throws GuzzleException|ApiException
4147
*/
4248
public function status(
4349
string $country = "US",
4450
string $date = null,
4551
string $from = null,
4652
string $to = null,
47-
int $countback = null
53+
int $countback = null,
54+
?Parameters $parameters = null
4855
): Statuses {
49-
return new Statuses($this->client->execute(self::BASE_URL . "status/",
50-
compact('country', 'date', 'from', 'to', 'countback')));
56+
return new Statuses($this->execute("status/",
57+
compact('country', 'date', 'from', 'to', 'countback'), $parameters));
5158
}
5259
}

src/Endpoints/MutualFunds.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use MarketDataApp\Client;
7+
use MarketDataApp\Endpoints\Requests\Parameters;
78
use MarketDataApp\Endpoints\Responses\MutualFunds\Candles;
89
use MarketDataApp\Exceptions\ApiException;
10+
use MarketDataApp\Traits\UniversalParameters;
911

1012
class MutualFunds
1113
{
1214

15+
use UniversalParameters;
16+
1317
private Client $client;
1418
public const BASE_URL = "v1/funds/";
1519

@@ -40,6 +44,8 @@ public function __construct($client)
4044
* @param int|null $countback Will fetch a number of candles before (to the left of) to. If you use from, countback
4145
* is not required.
4246
*
47+
* @param Parameters|null $parameters Universal parameters for all methods (such as format).
48+
*
4349
* @return Candles
4450
* @throws GuzzleException|ApiException
4551
*/
@@ -49,9 +55,10 @@ public function candles(
4955
string $to = null,
5056
string $resolution = 'D',
5157
int $countback = null,
58+
?Parameters $parameters = null
5259
): Candles {
53-
return new Candles($this->client->execute(self::BASE_URL . "candles/{$resolution}/{$symbol}/",
54-
compact('from', 'to', 'countback')
60+
return new Candles($this->execute("candles/{$resolution}/{$symbol}/",
61+
compact('from', 'to', 'countback'), $parameters
5562
));
5663
}
5764
}

0 commit comments

Comments
 (0)