Skip to content

Commit 70a66d1

Browse files
committed
Use PSR-17 and PSR-18 for HTTP calls
1 parent d239115 commit 70a66d1

23 files changed

+824
-415
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ matrix:
1111
- php: 7.1
1212
- php: 7.0
1313
- php: 5.6
14-
- php: 5.5
15-
- php: 5.4
16-
- php: 5.3
17-
dist: precise
1814
fast_finish: true
1915
allow_failures:
2016
- php: nightly
2117

18+
cache:
19+
directories:
20+
- "$HOME/.composer/cache"
21+
2222
before_install:
2323
- phpenv config-rm xdebug.ini || echo "xdebug not available"
2424
- INI_FILE=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
@@ -28,7 +28,7 @@ before_install:
2828
install:
2929
# Use composer update instead of composer install to install a working set of
3030
# dependencies on all PHP versions.
31-
- composer update
31+
- composer update --prefer-dist --prefer-stable
3232

3333
script:
3434
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

Cmfcmf/OpenWeatherMap.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
use Cmfcmf\OpenWeatherMap\UVIndex;
2222
use Cmfcmf\OpenWeatherMap\CurrentWeatherGroup;
2323
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
24-
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
25-
use Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface;
26-
use Cmfcmf\OpenWeatherMap\Fetcher\FileGetContentsFetcher;
2724
use Cmfcmf\OpenWeatherMap\WeatherForecast;
2825
use Cmfcmf\OpenWeatherMap\WeatherHistory;
2926
use Psr\Cache\CacheItemPoolInterface;
27+
use Psr\Http\Client\ClientInterface;
28+
use Psr\Http\Message\RequestFactoryInterface;
3029

3130
/**
3231
* Main class for the OpenWeatherMap-PHP-API. Only use this class.
@@ -89,9 +88,14 @@ class OpenWeatherMap
8988
private $wasCached = false;
9089

9190
/**
92-
* @var FetcherInterface The url fetcher.
91+
* @var ClientInterface
9392
*/
94-
private $fetcher;
93+
private $httpClient;
94+
95+
/**
96+
* @var RequestFactoryInterface
97+
*/
98+
private $httpRequestFactory;
9599

96100
/**
97101
* @var string
@@ -101,37 +105,31 @@ class OpenWeatherMap
101105
/**
102106
* Constructs the OpenWeatherMap object.
103107
*
104-
* @param string $apiKey The OpenWeatherMap API key. Required.
105-
* @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
106-
* CurlFetcher() if cURL is available. Otherwise defaults to
107-
* FileGetContentsFetcher() using 'file_get_contents()'.
108-
* @param null|CacheItemPoolInterface $cache If set to null, caching is disabled. Otherwise this must be
109-
* a PSR 16-compatible cache instance.
110-
* @param int $ttl How long weather data shall be cached. Defaults to 10 minutes.
111-
* Only used if $cache is not null.
108+
* @param string $apiKey The OpenWeatherMap API key. Required.
109+
* @param ClientInterface $httpClient A PSR-18 compatible HTTP client implementation.
110+
* @param RequestFactoryInterface $httpRequestFactory A PSR-17 compatbile HTTP request factory implementation.
111+
* @param null|CacheItemPoolInterface $cache If set to null, caching is disabled. Otherwise this must be
112+
* a PSR 16-compatible cache instance.
113+
* @param int $ttl How long weather data shall be cached. Defaults to 10 minutes.
114+
* Only used if $cache is not null.
112115
*
113116
* @api
114117
*/
115-
public function __construct($apiKey, $fetcher = null, $cache = null, $ttl = 600)
118+
public function __construct($apiKey, $httpClient, $httpRequestFactory, $cache = null, $ttl = 600)
116119
{
117120
if (!is_string($apiKey) || empty($apiKey)) {
118121
throw new \InvalidArgumentException("You must provide an API key.");
119122
}
120123

121-
if ($cache !== null && !($cache instanceof CacheItemPoolInterface)) {
122-
throw new \InvalidArgumentException('The cache class must implement the \Psr\Cache\CacheItemPoolInterface!');
123-
}
124124
if (!is_numeric($ttl)) {
125-
throw new \InvalidArgumentException('$seconds must be numeric.');
126-
}
127-
if (!isset($fetcher)) {
128-
$fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
125+
throw new \InvalidArgumentException('$ttl must be numeric.');
129126
}
130127

131128
$this->apiKey = $apiKey;
129+
$this->httpClient = $httpClient;
130+
$this->httpRequestFactory = $httpRequestFactory;
132131
$this->cache = $cache;
133132
$this->ttl = $ttl;
134-
$this->fetcher = $fetcher;
135133
}
136134

137135
/**
@@ -581,13 +579,17 @@ private function cacheOrFetchResult($url)
581579
$this->wasCached = true;
582580
return $item->get();
583581
}
582+
}
583+
584+
$result = $this->httpClient
585+
->sendRequest($this->httpRequestFactory->createRequest("GET", $url))
586+
->getBody()
587+
->getContents();
584588

585-
$result = $this->fetcher->fetch($url);
589+
if ($this->cache !== null) {
586590
$item->set($result);
587591
$item->expiresAfter($this->ttl);
588592
$this->cache->save($item);
589-
} else {
590-
$result = $this->fetcher->fetch($url);
591593
}
592594
$this->wasCached = false;
593595

Cmfcmf/OpenWeatherMap/Fetcher/CurlFetcher.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

Cmfcmf/OpenWeatherMap/Fetcher/FetcherInterface.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

Cmfcmf/OpenWeatherMap/Fetcher/FileGetContentsFetcher.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

Examples/CurrentWeather.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
use Cmfcmf\OpenWeatherMap;
1818
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
19+
use Http\Factory\Guzzle\RequestFactory;
20+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
1921

2022
require_once __DIR__ . '/bootstrap.php';
2123

@@ -32,8 +34,12 @@
3234
// Units (can be 'metric' or 'imperial' [default]):
3335
$units = 'metric';
3436

35-
// Get OpenWeatherMap object
36-
$owm = new OpenWeatherMap($myApiKey);
37+
// You can use every PSR-17 compatible HTTP request factory
38+
// and every PSR-18 compatible HTTP client.
39+
$httpRequestFactory = new RequestFactory();
40+
$httpClient = GuzzleAdapter::createWithConfig([]);
41+
42+
$owm = new OpenWeatherMap($myApiKey, $httpClient, $httpRequestFactory);
3743

3844
// Example 1: Get current temperature in Berlin.
3945
$weather = $owm->getWeather('Berlin', $units, $lang);

Examples/UVIndex.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* @see http://openweathermap.org/appid
1616
*/
1717
use Cmfcmf\OpenWeatherMap;
18+
use Http\Factory\Guzzle\RequestFactory;
19+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
1820

1921
require_once __DIR__ . '/bootstrap.php';
2022

@@ -31,8 +33,12 @@
3133
// Units (can be 'metric' or 'imperial' [default]):
3234
$units = 'metric';
3335

34-
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
35-
$owm = new OpenWeatherMap($myApiKey);
36+
// You can use every PSR-17 compatible HTTP request factory
37+
// and every PSR-18 compatible HTTP client.
38+
$httpRequestFactory = new RequestFactory();
39+
$httpClient = GuzzleAdapter::createWithConfig([]);
40+
41+
$owm = new OpenWeatherMap($myApiKey, $httpClient, $httpRequestFactory);
3642

3743
// Example 1: Get current uv index in Berlin.
3844
$uvIndex = $owm->getCurrentUVIndex(52.520008, 13.404954);

Examples/WeatherForecast.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717

1818
use Cmfcmf\OpenWeatherMap;
19+
use Http\Factory\Guzzle\RequestFactory;
20+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
1921

2022
require_once __DIR__ . '/bootstrap.php';
2123

@@ -25,8 +27,12 @@
2527
// Units (can be 'metric' or 'imperial' [default]):
2628
$units = 'metric';
2729

28-
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
29-
$owm = new OpenWeatherMap($myApiKey);
30+
// You can use every PSR-17 compatible HTTP request factory
31+
// and every PSR-18 compatible HTTP client.
32+
$httpRequestFactory = new RequestFactory();
33+
$httpClient = GuzzleAdapter::createWithConfig([]);
34+
35+
$owm = new OpenWeatherMap($myApiKey, $httpClient, $httpRequestFactory);
3036

3137
// Example 1: Get forecast for the next 5 days for Berlin.
3238
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 5);

Examples/WeatherHistory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* @see http://openweathermap.org/appid
1616
*/
1717
use Cmfcmf\OpenWeatherMap;
18+
use Http\Factory\Guzzle\RequestFactory;
19+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
1820

1921
require_once __DIR__ . '/bootstrap.php';
2022

@@ -24,8 +26,12 @@
2426
// Units (can be 'metric' or 'imperial' [default]):
2527
$units = 'metric';
2628

27-
// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
28-
$owm = new OpenWeatherMap($myApiKey);
29+
// You can use every PSR-17 compatible HTTP request factory
30+
// and every PSR-18 compatible HTTP client.
31+
$httpRequestFactory = new RequestFactory();
32+
$httpClient = GuzzleAdapter::createWithConfig([]);
33+
34+
$owm = new OpenWeatherMap($myApiKey, $httpClient, $httpRequestFactory);
2935

3036
// Example 1: Get hourly weather history between 2014-01-01 and today.
3137
$history = $owm->getWeatherHistory('Berlin', new \DateTime('2014-01-01'), new \DateTime('now'), 'hour', $units, $lang);

0 commit comments

Comments
 (0)