Skip to content

Commit

Permalink
Add utilities endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
KerryJones committed Jul 11, 2024
1 parent 03faf1a commit b0fe1eb
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $news = $client->stocks->news(symbol: 'AAPL', from: Carbon::parse('2023-01-01'))
## Testing

```bash
./vendor/bin/phpunit tests
./vendor/bin/phpunit
```

## Changelog
Expand Down
1 change: 1 addition & 0 deletions src/Endpoints/Responses/Markets/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

class Status
{

}
24 changes: 24 additions & 0 deletions src/Endpoints/Responses/Utilities/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

namespace MarketDataApp\Endpoints\Responses\Utilities;

use Carbon\Carbon;

class ApiStatus
{

// Will always be ok when the status information is successfully retrieved.
public string $status;

/* @var ServiceStatus[] $services */
public array $services;

public function __construct(object $response)
{
// Convert the response to this object.
$this->status = $response->s;

for ($i = 0; $i < count($response->service); $i++) {
$this->services[] = new ServiceStatus(
$response->service[$i],
$response->status[$i],
$response->{'uptimePct30d'}[$i],
$response->{'uptimePct90d'}[$i],
Carbon::parse($response->updated[$i]),
);
}
}
}
7 changes: 7 additions & 0 deletions src/Endpoints/Responses/Utilities/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@

class Headers
{
public function __construct(object $response)
{
// Set the headers based on response object.
foreach ($response as $key => $value) {
$this->{$key} = $value;
}
}
}
38 changes: 38 additions & 0 deletions src/Endpoints/Responses/Utilities/ServiceStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace MarketDataApp\Endpoints\Responses\Utilities;

use Carbon\Carbon;

class ServiceStatus
{

// The service being monitored.
public string $service;

// The current status of each service (online or offline).
public string $status;

// The uptime percentage of each service over the last 30 days.
public float $uptime_percentage_30d;

// The uptime percentage of each service over the last 90 days.
public float $uptime_percentage_90d;

// The timestamp of the last update for each service's status.
public Carbon $updated;

public function __construct(
string $service,
string $status,
float $uptime_percentage_30d,
float $uptime_percentage_90d,
Carbon $updated
) {
$this->service = $service;
$this->status = $status;
$this->uptime_percentage_30d = $uptime_percentage_30d;
$this->uptime_percentage_90d = $uptime_percentage_90d;
$this->updated = $updated;
}
}
24 changes: 20 additions & 4 deletions src/Endpoints/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace MarketDataApp\Endpoints;

use GuzzleHttp\Exception\GuzzleException;
use MarketDataApp\Client;
use MarketDataApp\Endpoints\Responses\Utilities\ApiStatus;
use MarketDataApp\Endpoints\Responses\Utilities\Headers;
use MarketDataApp\Exceptions\ApiException;

class Utilities
{
Expand All @@ -16,15 +18,29 @@ public function __construct($client)
$this->client = $client;
}

/**
* Check the current status of Market Data services and historical uptime. The status of the Market Data API is
* updated every 5 minutes. Historical uptime is available for the last 30 and 90 days.
*
* TIP: This endpoint will continue to respond with the current status of the Market Data API, even if the API is
* offline. This endpoint is public and does not require a token.
*
* @throws GuzzleException|ApiException
*/
public function api_status(): ApiStatus
{
// Stub
return new ApiStatus();
return new ApiStatus($this->client->execute("status/"));
}

/**
* This endpoint allows users to retrieve a JSON response of the headers their application is sending, aiding in
* troubleshooting authentication issues, particularly with the Authorization header.
*
* TIP: The values in sensitive headers such as Authorization are partially redacted in the response for security
* purposes.
*/
public function headers(): Headers
{
// Stub
return new Headers();
return new Headers($this->client->execute("headers/"));
}
}
13 changes: 3 additions & 10 deletions tests/IndicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
namespace MarketDataApp\Tests;

use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use MarketDataApp\Client;
use MarketDataApp\Endpoints\Responses\Indices\Candle;
use MarketDataApp\Endpoints\Responses\Indices\Candles;
use MarketDataApp\Endpoints\Responses\Indices\Quote;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;

class IndicesTest extends TestCase
{

use MockResponses;

private Client $client;

protected function setUp(): void
Expand Down Expand Up @@ -168,11 +168,4 @@ public function testExceptionHandling_throwsGuzzleException()
$this->expectException(\GuzzleHttp\Exception\GuzzleException::class);
$response = $this->client->indices->quote("INVALID");
}

private function setMockResponses(array $responses): void
{
$mock = new MockHandler($responses);
$handlerStack = HandlerStack::create($mock);
$this->client->setGuzzle(new GuzzleClient(['handler' => $handlerStack]));
}
}
3 changes: 3 additions & 0 deletions tests/MarketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
use GuzzleHttp\Psr7\Response;
use MarketDataApp\Client;
use MarketDataApp\Endpoints\Responses\Markets\Status;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;

class MarketsTest extends TestCase
{

use MockResponses;

private Client $client;

protected function setUp(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/MutualFundsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
use GuzzleHttp\Psr7\Response;
use MarketDataApp\Client;
use MarketDataApp\Endpoints\Responses\MutualFunds\Candles;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;

class MutualFundsTest extends TestCase
{

use MockResponses;

private Client $client;

protected function setUp(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
use MarketDataApp\Endpoints\Responses\Options\OptionChain;
use MarketDataApp\Endpoints\Responses\Options\Quotes;
use MarketDataApp\Endpoints\Responses\Options\Strikes;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;

class OptionsTest extends TestCase
{

use MockResponses;

private Client $client;

protected function setUp(): void
Expand Down
21 changes: 5 additions & 16 deletions tests/StocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace MarketDataApp\Tests;

use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use MarketDataApp\Client;
Expand All @@ -21,11 +18,14 @@
use MarketDataApp\Endpoints\Responses\Stocks\Quote;
use MarketDataApp\Endpoints\Responses\Stocks\Quotes;
use MarketDataApp\Exceptions\ApiException;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;

class StocksTest extends TestCase
{

use MockResponses;

private Client $client;

private array $aapl_mocked_response = [
Expand Down Expand Up @@ -398,9 +398,7 @@ public function testEarnings_success()
'surpriseEPSpct' => -3.0928,
'updated' => 1701690000
];
$this->setMockResponses([
new Response(200, [], json_encode($mocked_response)),
]);
$this->setMockResponses([new Response(200, [], json_encode($mocked_response))]);
$earnings = $this->client->stocks->earnings(symbol: 'AAPL', from: Carbon::parse('2023-01-01'));

$this->assertInstanceOf(Earnings::class, $earnings);
Expand Down Expand Up @@ -439,9 +437,7 @@ public function testNews_success()
'source' => 'https=>//investorplace.com/2023/12/whoa-there-let-apple-stock-take-a-breather-before-jumping-in-headfirst/',
'publicationDate' => 1703041200
];
$this->setMockResponses([
new Response(200, [], json_encode($mocked_response)),
]);
$this->setMockResponses([new Response(200, [], json_encode($mocked_response))]);
$news = $this->client->stocks->news(symbol: 'AAPL', from: Carbon::parse('2023-01-01'));

$this->assertInstanceOf(News::class, $news);
Expand All @@ -468,11 +464,4 @@ public function testExceptionHandling_throwsGuzzleException()
$this->expectException(\GuzzleHttp\Exception\GuzzleException::class);
$response = $this->client->stocks->quote("INVALID");
}

private function setMockResponses(array $responses): void
{
$mock = new MockHandler($responses);
$handlerStack = HandlerStack::create($mock);
$this->client->setGuzzle(new GuzzleClient(['handler' => $handlerStack]));
}
}
19 changes: 19 additions & 0 deletions tests/Traits/MockResponses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace MarketDataApp\Tests\Traits;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;

trait MockResponses
{

private function setMockResponses(array $responses): void
{
$mock = new MockHandler($responses);
$handlerStack = HandlerStack::create($mock);

$this->client->setGuzzle(new GuzzleClient(['handler' => $handlerStack]));
}
}
Loading

0 comments on commit b0fe1eb

Please sign in to comment.