Skip to content

Commit 12f623f

Browse files
authored
Merge pull request #46 from programmatordev/OPA-49-add-global-base-url
Add global base url
2 parents c57b09c + 0b13a31 commit 12f623f

File tree

7 files changed

+27
-47
lines changed

7 files changed

+27
-47
lines changed

src/Config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class Config
1515
{
16+
private string $baseUrl = 'https://api.openweathermap.org';
17+
1618
private array $options;
1719

1820
public function __construct(array $options = [])
@@ -50,6 +52,11 @@ private function resolveOptions(array $options): array
5052
return $resolver->resolve($options);
5153
}
5254

55+
public function getBaseUrl(): string
56+
{
57+
return $this->baseUrl;
58+
}
59+
5360
public function getApplicationKey(): string
5461
{
5562
return $this->options['applicationKey'];

src/Endpoint/AbstractEndpoint.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Psr\Cache\CacheItemPoolInterface;
2121
use Psr\Http\Message\ResponseInterface;
2222
use Psr\Http\Message\StreamInterface;
23-
use Psr\Http\Message\UriInterface;
2423
use Psr\Log\LoggerInterface;
2524

2625
class AbstractEndpoint
@@ -62,7 +61,7 @@ public function __construct(protected OpenWeatherMap $api)
6261
*/
6362
protected function sendRequest(
6463
string $method,
65-
UriInterface|string $baseUrl,
64+
string $path,
6665
array $query = [],
6766
array $headers = [],
6867
StreamInterface|string $body = null
@@ -72,13 +71,13 @@ protected function sendRequest(
7271

7372
$response = $this->httpClientBuilder->getHttpClient()->send(
7473
$method,
75-
$this->buildUrl($baseUrl, $query),
74+
$this->buildUrl($path, $query),
7675
$headers,
7776
$body
7877
);
7978

80-
if (($statusCode = $response->getStatusCode()) >= 400) {
81-
$this->handleResponseErrors($response, $statusCode);
79+
if ($response->getStatusCode() >= 400) {
80+
$this->handleResponseErrors($response);
8281
}
8382

8483
return ResponseMediator::toArray($response);
@@ -113,13 +112,13 @@ private function configurePlugins(): void
113112
* @throws UnauthorizedException
114113
* @throws BadRequestException
115114
*/
116-
private function handleResponseErrors(ResponseInterface $response, int $statusCode): void
115+
private function handleResponseErrors(ResponseInterface $response): void
117116
{
118117
$error = new Error(
119118
ResponseMediator::toArray($response)
120119
);
121120

122-
match ($statusCode) {
121+
match ($response->getStatusCode()) {
123122
400 => throw new BadRequestException($error),
124123
401 => throw new UnauthorizedException($error),
125124
404 => throw new NotFoundException($error),
@@ -128,17 +127,13 @@ private function handleResponseErrors(ResponseInterface $response, int $statusCo
128127
};
129128
}
130129

131-
private function buildUrl(UriInterface|string $baseUrl, array $query): string
130+
private function buildUrl(string $path, array $query): string
132131
{
133-
if ($baseUrl instanceof UriInterface) {
134-
$baseUrl = (string) $baseUrl;
135-
}
136-
137132
// Add application key to all requests
138133
$query = $query + [
139134
'appid' => $this->config->getApplicationKey()
140135
];
141136

142-
return \sprintf('%s?%s', $baseUrl, http_build_query($query));
137+
return \sprintf('%s%s?%s', $this->config->getBaseUrl(), $path, http_build_query($query));
143138
}
144139
}

src/Endpoint/AirPollutionEndpoint.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616
class AirPollutionEndpoint extends AbstractEndpoint
1717
{
18-
private string $urlAirPollution = 'https://api.openweathermap.org/data/2.5/air_pollution';
19-
20-
private string $urlAirPollutionForecast = 'https://api.openweathermap.org/data/2.5/air_pollution/forecast';
21-
22-
private string $urlAirPollutionHistory = 'https://api.openweathermap.org/data/2.5/air_pollution/history';
23-
2418
/**
2519
* @throws Exception
2620
* @throws BadRequestException
@@ -37,7 +31,7 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat
3731

3832
$data = $this->sendRequest(
3933
method: 'GET',
40-
baseUrl: $this->urlAirPollution,
34+
path: '/data/2.5/air_pollution',
4135
query: [
4236
'lat' => $latitude,
4337
'lon' => $longitude
@@ -63,7 +57,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionLoca
6357

6458
$data = $this->sendRequest(
6559
method: 'GET',
66-
baseUrl: $this->urlAirPollutionForecast,
60+
path: '/data/2.5/air_pollution/forecast',
6761
query: [
6862
'lat' => $latitude,
6963
'lon' => $longitude
@@ -98,7 +92,7 @@ public function getHistory(
9892

9993
$data = $this->sendRequest(
10094
method: 'GET',
101-
baseUrl: $this->urlAirPollutionHistory,
95+
path: '/data/2.5/air_pollution/history',
10296
query: [
10397
'lat' => $latitude,
10498
'lon' => $longitude,

src/Endpoint/GeocodingEndpoint.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ class GeocodingEndpoint extends AbstractEndpoint
2020

2121
private const NUM_RESULTS = 5;
2222

23-
private string $urlGeocodingDirect = 'https://api.openweathermap.org/geo/1.0/direct';
24-
25-
private string $urlGeocodingZipCode = 'https://api.openweathermap.org/geo/1.0/zip';
26-
27-
private string $urlGeocodingReverse = 'https://api.openweathermap.org/geo/1.0/reverse';
28-
2923
protected int $cacheTtl = 60 * 60 * 24 * 30; // 30 days
3024

3125
/**
@@ -45,7 +39,7 @@ public function getByLocationName(string $locationName, int $numResults = self::
4539

4640
$data = $this->sendRequest(
4741
method: 'GET',
48-
baseUrl: $this->urlGeocodingDirect,
42+
path: '/geo/1.0/direct',
4943
query: [
5044
'q' => $locationName,
5145
'limit' => $numResults
@@ -71,7 +65,7 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocat
7165

7266
$data = $this->sendRequest(
7367
method: 'GET',
74-
baseUrl: $this->urlGeocodingZipCode,
68+
path: '/geo/1.0/zip',
7569
query: [
7670
'zip' => \sprintf('%s,%s', $zipCode, $countryCode)
7771
]
@@ -98,7 +92,7 @@ public function getByCoordinate(float $latitude, float $longitude, int $numResul
9892

9993
$data = $this->sendRequest(
10094
method: 'GET',
101-
baseUrl: $this->urlGeocodingReverse,
95+
path: '/geo/1.0/reverse',
10296
query: [
10397
'lat' => $latitude,
10498
'lon' => $longitude,

src/Endpoint/OneCallEndpoint.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ class OneCallEndpoint extends AbstractEndpoint
2121
use WithUnitSystemTrait;
2222
use WithLanguageTrait;
2323

24-
private string $urlOneCall = 'https://api.openweathermap.org/data/3.0/onecall';
25-
26-
private string $urlOneCallHistoryMoment = 'https://api.openweathermap.org/data/3.0/onecall/timemachine';
27-
28-
private string $urlOneCallHistoryAggregate = 'https://api.openweathermap.org/data/3.0/onecall/day_summary';
29-
3024
/**
3125
* @throws Exception
3226
* @throws BadRequestException
@@ -43,7 +37,7 @@ public function getWeather(float $latitude, float $longitude): OneCall
4337

4438
$data = $this->sendRequest(
4539
method: 'GET',
46-
baseUrl: $this->urlOneCall,
40+
path: '/data/3.0/onecall',
4741
query: [
4842
'lat' => $latitude,
4943
'lon' => $longitude,
@@ -72,7 +66,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
7266

7367
$data = $this->sendRequest(
7468
method: 'GET',
75-
baseUrl: $this->urlOneCallHistoryMoment,
69+
path: '/data/3.0/onecall/timemachine',
7670
query: [
7771
'lat' => $latitude,
7872
'lon' => $longitude,
@@ -102,7 +96,7 @@ public function getHistoryAggregate(float $latitude, float $longitude, \DateTime
10296

10397
$data = $this->sendRequest(
10498
method: 'GET',
105-
baseUrl: $this->urlOneCallHistoryAggregate,
99+
path: '/data/3.0/onecall/day_summary',
106100
query: [
107101
'lat' => $latitude,
108102
'lon' => $longitude,

src/Endpoint/WeatherEndpoint.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class WeatherEndpoint extends AbstractEndpoint
2222

2323
private const NUM_RESULTS = 40;
2424

25-
private string $urlWeather = 'https://api.openweathermap.org/data/2.5/weather';
26-
27-
private string $urlWeatherForecast = 'https://api.openweathermap.org/data/2.5/forecast';
28-
2925
/**
3026
* @throws Exception
3127
* @throws BadRequestException
@@ -42,7 +38,7 @@ public function getCurrent(float $latitude, float $longitude): WeatherLocation
4238

4339
$data = $this->sendRequest(
4440
method: 'GET',
45-
baseUrl: $this->urlWeather,
41+
path: '/data/2.5/weather',
4642
query: [
4743
'lat' => $latitude,
4844
'lon' => $longitude,
@@ -71,7 +67,7 @@ public function getForecast(float $latitude, float $longitude, int $numResults =
7167

7268
$data = $this->sendRequest(
7369
method: 'GET',
74-
baseUrl: $this->urlWeatherForecast,
70+
path: '/data/2.5/forecast',
7571
query: [
7672
'lat' => $latitude,
7773
'lon' => $longitude,

tests/AbstractEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ private function mockSendRequest(OpenWeatherMap $api): void
4747
$endpoint = new AbstractEndpoint($api);
4848
$reflectionClass = new \ReflectionClass($endpoint);
4949
$sendRequest = $reflectionClass->getMethod('sendRequest');
50-
$sendRequest->invokeArgs($endpoint, ['GET', 'https://mock.test']);
50+
$sendRequest->invokeArgs($endpoint, ['GET', '/test']);
5151
}
5252
}

0 commit comments

Comments
 (0)