File tree Expand file tree Collapse file tree 6 files changed +123
-0
lines changed
Expand file tree Collapse file tree 6 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace OhDear \PhpSdk \Concerns ;
4+
5+ use OhDear \PhpSdk \Dto \Uptime ;
6+ use OhDear \PhpSdk \Requests \Uptime \GetUptimeRequest ;
7+
8+ trait SupportsUptimeEndpoints
9+ {
10+ /** @return list<Uptime> */
11+ public function uptime (int $ monitorId , string $ startedAt , string $ endedAt ): array
12+ {
13+ $ request = new GetUptimeRequest ($ monitorId , $ startedAt , $ endedAt );
14+
15+ return $ this ->send ($ request )->dtoOrFail ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace OhDear \PhpSdk \Dto ;
6+
7+ use Saloon \Http \Response ;
8+
9+ class Uptime
10+ {
11+ public function __construct (
12+ public string $ datetime ,
13+ public int $ uptimePercentage ,
14+ ) {}
15+
16+ public static function fromResponse (array $ data ): self
17+ {
18+ return new self (
19+ datetime: $ data ['datetime ' ],
20+ uptimePercentage: $ data ['uptime_percentage ' ],
21+ );
22+ }
23+
24+ public static function collect (Response $ response ): array
25+ {
26+ return array_map (
27+ fn (array $ item ) => self ::fromResponse ($ item ),
28+ $ response ->json ()
29+ );
30+ }
31+ }
Original file line number Diff line number Diff line change 1717use OhDear \PhpSdk \Concerns \SupportsMonitorEndpoints ;
1818use OhDear \PhpSdk \Concerns \SupportsSitemapEndpoints ;
1919use OhDear \PhpSdk \Concerns \SupportsStatusPageEndpoints ;
20+ use OhDear \PhpSdk \Concerns \SupportsUptimeEndpoints ;
2021use OhDear \PhpSdk \Concerns \SupportsUptimeMetricsEndpoints ;
2122use OhDear \PhpSdk \Exceptions \OhDearException ;
2223use OhDear \PhpSdk \Exceptions \ValidationException ;
@@ -50,6 +51,7 @@ class OhDear extends Connector implements HasPagination
5051 use SupportsMonitorEndpoints;
5152 use SupportsSitemapEndpoints;
5253 use SupportsStatusPageEndpoints;
54+ use SupportsUptimeEndpoints;
5355 use SupportsUptimeMetricsEndpoints;
5456
5557 protected string $ apiToken ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace OhDear \PhpSdk \Requests \Uptime ;
4+
5+ use OhDear \PhpSdk \Dto \Uptime ;
6+ use OhDear \PhpSdk \Helpers \Helpers ;
7+ use Saloon \Enums \Method ;
8+ use Saloon \Http \Request ;
9+ use Saloon \Http \Response ;
10+
11+ class GetUptimeRequest extends Request
12+ {
13+ protected Method $ method = Method::GET ;
14+
15+ public function __construct (
16+ protected int $ monitorId ,
17+ protected string $ startedAt ,
18+ protected string $ endedAt ,
19+ ) {}
20+
21+ public function resolveEndpoint (): string
22+ {
23+ return "/monitors/ {$ this ->monitorId }/uptime " ;
24+ }
25+
26+ protected function defaultQuery (): array
27+ {
28+ return [
29+ 'filter ' => [
30+ 'started_at ' => Helpers::convertDateFormat ($ this ->startedAt ),
31+ 'ended_at ' => Helpers::convertDateFormat ($ this ->endedAt ),
32+ ],
33+ ];
34+ }
35+
36+ public function createDtoFromResponse (Response $ response ): array
37+ {
38+ return Uptime::collect ($ response );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ {
2+ "statusCode" : 200 ,
3+ "headers" : {
4+ "Date" : " Tue, 26 Aug 2025 14:40:00 GMT" ,
5+ "Content-Type" : " application\/ json"
6+ },
7+ "data" : " [{\" datetime\" :\" 2025-08-01 00:00:00.\" , \" uptime_percentage\" : 100}]" ,
8+ "context" : []
9+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use OhDear \PhpSdk \Requests \Uptime \GetUptimeRequest ;
4+ use Saloon \Http \Faking \MockClient ;
5+ use Saloon \Http \Faking \MockResponse ;
6+
7+ beforeEach (function () {
8+ $ this ->ohDear = ohDearMock ();
9+ });
10+
11+ it ('can get uptime periods ' , function () {
12+ MockClient::global ([
13+ GetUptimeRequest::class => MockResponse::fixture ('uptime ' ),
14+ ]);
15+
16+ $ uptimePeriods = $ this ->ohDear ->uptime (82065 , '2025-08-25 00:00:00 ' , '2025-08-26 23:59:59 ' );
17+
18+ expect ($ uptimePeriods )->toBeArray ();
19+
20+ foreach ($ uptimePeriods as $ uptime ) {
21+ expect ($ uptime ->datetime )->toBeString ();
22+ expect ($ uptime ->uptimePercentage )->toBeInt ();
23+ }
24+ });
You can’t perform that action at this time.
0 commit comments