Skip to content

Commit

Permalink
Updated http provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfocus committed Feb 27, 2024
1 parent f490f12 commit c463dc1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 59 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.2] - 2024-02-27

### Changed

* Updated the HTTP provider to replace HTTPlug factories by PSR-17


## [0.2.1] - 2022-12-17

### Changed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

![Packagist Version](https://img.shields.io/packagist/v/php-weather/tomorrow)
![PHP Weather Common Version](https://img.shields.io/badge/phpweather--core-0.4.*-brightgreen)
![PHP Weather HTTP Provider Version](https://img.shields.io/badge/phpweather--http--provider-0.5.*-brightgreen)
![PHP Weather HTTP Provider Version](https://img.shields.io/badge/phpweather--http--provider-0.6.*-brightgreen)
![GitHub Release Date](https://img.shields.io/github/release-date/php-weather/tomorrow)
![GitHub commits since tagged version](https://img.shields.io/github/commits-since/php-weather/tomorrow/0.2.1)
![GitHub commits since tagged version](https://img.shields.io/github/commits-since/php-weather/tomorrow/0.2.2)
![GitHub last commit](https://img.shields.io/github/last-commit/php-weather/tomorrow)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/php-weather/tomorrow/php.yml?branch=main)
![GitHub](https://img.shields.io/github/license/php-weather/tomorrow)
Expand Down
104 changes: 52 additions & 52 deletions Src/Tomorrow.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,46 @@ protected function getCurrentWeatherQueryString(WeatherQuery $query): string
return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray));
}

/**
* @return string[]
*/
private function getQueryFields(): array
{
return [
'cloudCover',
'dewPoint',
'temperatureApparent',
'humidity',
'precipitationIntensity',
'precipitationProbability',
'pressureSeaLevel',
'temperature',
'weatherCode',
'windSpeed',
'windDirection',
];
}

/**
* @param array<string, mixed> $queryArray
* @return string
*/
private function createQueryString(array $queryArray): string
{
$lines = [];
foreach ($queryArray as $key => $value) {
if (!is_array($value)) {
$lines[] = sprintf('%s=%s', $key, $value);
} else {
foreach ($value as $valueItem) {
$lines[] = sprintf('%s=%s', $key, $valueItem);
}
}
}

return implode('&', $lines);
}

protected function getForecastWeatherQueryString(WeatherQuery $query): string
{
$queryArray = [
Expand All @@ -53,6 +93,11 @@ protected function getForecastWeatherQueryString(WeatherQuery $query): string
return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray));
}

protected function getHistoricalTimeLineWeatherQueryString(WeatherQuery $query): string
{
return $this->getHistoricalWeatherQueryString($query);
}

protected function getHistoricalWeatherQueryString(WeatherQuery $query): string
{
$queryArray = [
Expand All @@ -70,11 +115,6 @@ protected function getHistoricalWeatherQueryString(WeatherQuery $query): string
return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray));
}

protected function getHistoricalTimeLineWeatherQueryString(WeatherQuery $query): string
{
return $this->getHistoricalWeatherQueryString($query);
}

protected function mapRawData(float $latitude, float $longitude, array $rawData, ?string $type = null, ?string $units = null): Weather|WeatherCollection
{
$weatherCollection = new \PhpWeather\Common\WeatherCollection();
Expand Down Expand Up @@ -117,53 +157,6 @@ protected function mapRawData(float $latitude, float $longitude, array $rawData,
return $weatherCollection;
}

public function getSources(): array
{
return [
new Source('tomorrow', 'tomorrow.io', 'https://www.tomorrow.io/'),
];
}

/**
* @return string[]
*/
private function getQueryFields(): array
{
return [
'cloudCover',
'dewPoint',
'temperatureApparent',
'humidity',
'precipitationIntensity',
'precipitationProbability',
'pressureSeaLevel',
'temperature',
'weatherCode',
'windSpeed',
'windDirection',
];
}

/**
* @param array<string, mixed> $queryArray
* @return string
*/
private function createQueryString(array $queryArray): string
{
$lines = [];
foreach ($queryArray as $key => $value) {
if (!is_array($value)) {
$lines[] = sprintf('%s=%s', $key, $value);
} else {
foreach ($value as $valueItem) {
$lines[] = sprintf('%s=%s', $key, $valueItem);
}
}
}

return implode('&', $lines);
}

/**
* @param array<string, mixed> $intervalRawData
* @param float $latitude
Expand Down Expand Up @@ -242,6 +235,13 @@ private function mapRawIntervalData(array $intervalRawData, float $latitude, flo
return $weather;
}

public function getSources(): array
{
return [
new Source('tomorrow', 'tomorrow.io', 'https://www.tomorrow.io/'),
];
}

private function mapWeatherCode(int $weatherCode): ?int
{
$weatherCode = (int)substr((string)$weatherCode, 0, 4);
Expand Down
8 changes: 6 additions & 2 deletions Tests/TomorrowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class TomorrowTest extends TestCase
{
Expand All @@ -29,16 +30,19 @@ public function testCurrentWeather(): void
{
$latitude = 47.8739259;
$longitude = 8.0043961;
$datetime = (new \DateTime())->setTimezone(new \DateTimeZone('UTC'))->setDate(2022, 07,31)->setTime(16,00);
$datetime = (new \DateTime())->setTimezone(new \DateTimeZone('UTC'))->setDate(2022, 07, 31)->setTime(16, 00);
$testQuery = WeatherQuery::create($latitude, $longitude, $datetime);
$testString = 'https://api.tomorrow.io/v4/timelines?location=47.8739259,8.0043961&fields=cloudCover&fields=dewPoint&fields=temperatureApparent&fields=humidity&fields=precipitationIntensity&fields=precipitationProbability&fields=pressureSeaLevel&fields=temperature&fields=weatherCode&fields=windSpeed&fields=windDirection&units=metric&timesteps=current&apikey=key';

$request = $this->createMock(RequestInterface::class);
$this->requestFactory->expects(self::once())->method('createRequest')->with('GET', $testString)->willReturn($request);

$responseBodyString = file_get_contents(__DIR__.'/resources/currentWeather.json');
$body = $this->createMock(StreamInterface::class);
$body->method('__toString')->willReturn($responseBodyString);

$response = $this->createMock(ResponseInterface::class);
$response->expects(self::once())->method('getBody')->willReturn($responseBodyString);
$response->expects(self::once())->method('getBody')->willReturn($body);
$this->client->expects(self::once())->method('sendRequest')->with($request)->willReturn($response);

$currentWeather = $this->provider->getCurrentWeather($testQuery);
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
"require": {
"php": "^8",
"ext-json": "*",
"php-weather/http-provider": "^0.5",
"php-weather/http-provider": "^0.6",
"php-weather/core": "^0.4"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"php-http/guzzle7-adapter": "dev-master",
"php-http/guzzle7-adapter": "^1.0",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": ">=8.0"
},
Expand Down

0 comments on commit c463dc1

Please sign in to comment.