Skip to content

Commit

Permalink
Added a new Exception for Cache failures; added new methods to the ma…
Browse files Browse the repository at this point in the history
…in Api class and to the Alert class
  • Loading branch information
benjaminhansen committed Jul 3, 2024
1 parent f742dbc commit 8963f29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use NWS\Features\ObservationStation;
use NWS\Exceptions\InvalidRequestException;
use NWS\Exceptions\ApiNotOkException;
use NWS\Exceptions\CacheException;
use DateTimeZone;

class Api
Expand All @@ -26,17 +27,28 @@ class Api
public function __construct(string $domain, string $email)
{
// derive our user agent for the API requests
$this->user_agent = "($domain, $email)";
$this->setUserAgent($domain, $email);

// build up our HTTP client for making requests to the API
$this->client = new HttpClient([
'http_errors' => false,
'headers' => [
'User-Agent' => $this->user_agent
'User-Agent' => $this->getUserAgent()
]
]);
}

public function getUserAgent(): string
{
return $this->user_agent;
}

public function setUserAgent(string $domain, string $email): self
{
$this->user_agent = "({$domain}, {$email})";
return $this;
}

public function getCacheLifetime(): int
{
return $this->cache_lifetime;
Expand Down Expand Up @@ -78,13 +90,13 @@ public function setBaseUrl(string $base_url): self
return $this;
}

public function clearCache(): bool|self
public function clearCache(): self
{
if($this->cache->clear()) {
return $this;
}

return false;
throw new CacheException("Failed to clear the cache!");
}

public function setTimezone(string $timezone): self
Expand All @@ -101,6 +113,7 @@ public function getTimezone(): DateTimeZone
public function get($url): object|bool
{
// the user did not opt to use the cache
// so make a direct request to the URL
if(!$this->cache) {
$http_request = $this->client->get($url);
$http_response_code = $http_request->getStatusCode();
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/CacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace NWS\Exceptions;

use Exception;

class CacheException extends Exception
{
//
}
19 changes: 19 additions & 0 deletions src/Features/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use NWS\Enums\AlertMessageType;
use NWS\Traits\IsCallable;
use NWS\Support\Carbon;
use Illuminate\Support\Collection;

class Alert
{
Expand Down Expand Up @@ -120,6 +121,24 @@ public function instruction(): string
return $this->properties_instruction();
}

public function affectedZones(): Collection
{
$return = [];

$zones = $this->properties_affectedZones();
foreach($zones as $zone_url) {
$return[] = new ForecastZone($this->api->get($zone_url), $this->api);
}

return collect($return);
}

public function affectedZone(int $i = 0): ForecastZone
{
$zone = $this->properties_affectedZones()[$i];
return new ForecastZone($this->api->get($zone), $this->api);
}

public function response(): AlertResponse
{
return AlertResponse::fromName($this->properties_response());
Expand Down

0 comments on commit 8963f29

Please sign in to comment.