Skip to content

Commit b7940b6

Browse files
committed
feat: add assistant resource and start session endpoint
1 parent 07386f7 commit b7940b6

File tree

9 files changed

+155
-5
lines changed

9 files changed

+155
-5
lines changed

src/Entity/Assistant/Answer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Assistant;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
6+
7+
class Answer
8+
{
9+
use EntityTrait;
10+
11+
private string $answer;
12+
13+
private string $sessionId;
14+
15+
/** @var WeatherData[] */
16+
private array $data = [];
17+
18+
public function __construct(array $data)
19+
{
20+
$this->answer = $data['answer'];
21+
$this->sessionId = $data['session_id'];
22+
23+
if (!empty($data['data'])) {
24+
$this->data = $this->createEntityKeyList(WeatherData::class, $data['data']);
25+
}
26+
}
27+
28+
public function getAnswer(): string
29+
{
30+
return $this->answer;
31+
}
32+
33+
public function getSessionId(): string
34+
{
35+
return $this->sessionId;
36+
}
37+
38+
public function getData(): array
39+
{
40+
return $this->data;
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity\Assistant;
4+
5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
7+
class WeatherData extends BaseWeather
8+
{
9+
private string $locationName;
10+
11+
private float $temperature;
12+
13+
private float $temperatureFeelsLike;
14+
15+
private int $visibility;
16+
17+
private \DateTimeImmutable $sunriseAt;
18+
19+
private \DateTimeImmutable $sunsetAt;
20+
21+
public function __construct(string $location, array $data)
22+
{
23+
parent::__construct($data);
24+
25+
$this->locationName = $location;
26+
$this->temperature = $data['temp'];
27+
$this->temperatureFeelsLike = $data['feels_like'];
28+
$this->visibility = $data['visibility'];
29+
$this->sunriseAt = \DateTimeImmutable::createFromFormat('U', $data['sunrise']);
30+
$this->sunsetAt = \DateTimeImmutable::createFromFormat('U', $data['sunset']);
31+
}
32+
33+
public function getLocationName(): string
34+
{
35+
return $this->locationName;
36+
}
37+
38+
public function getTemperature(): float
39+
{
40+
return $this->temperature;
41+
}
42+
43+
public function getTemperatureFeelsLike(): float
44+
{
45+
return $this->temperatureFeelsLike;
46+
}
47+
48+
public function getVisibility(): int
49+
{
50+
return $this->visibility;
51+
}
52+
53+
public function getSunriseAt(): \DateTimeImmutable
54+
{
55+
return $this->sunriseAt;
56+
}
57+
58+
public function getSunsetAt(): \DateTimeImmutable
59+
{
60+
return $this->sunsetAt;
61+
}
62+
}

src/Entity/OneCall/BaseWeather.php renamed to src/Entity/BaseWeather.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
3+
namespace ProgrammatorDev\OpenWeatherMap\Entity;
44

5-
use ProgrammatorDev\OpenWeatherMap\Entity\Condition;
6-
use ProgrammatorDev\OpenWeatherMap\Entity\Wind;
75
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
86

97
class BaseWeather

src/Entity/OneCall/DayData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
44

5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
57
class DayData extends BaseWeather
68
{
79
private Temperature $temperature;

src/Entity/OneCall/HourData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
44

5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
57
class HourData extends BaseWeather
68
{
79
private float $temperature;

src/Entity/OneCall/WeatherData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Entity\OneCall;
44

5+
use ProgrammatorDev\OpenWeatherMap\Entity\BaseWeather;
6+
57
class WeatherData extends BaseWeather
68
{
79
private float $temperature;

src/OpenWeatherMap.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
1414
use ProgrammatorDev\OpenWeatherMap\Language\Language;
1515
use ProgrammatorDev\OpenWeatherMap\Resource\AirPollutionResource;
16+
use ProgrammatorDev\OpenWeatherMap\Resource\AssistantResource;
1617
use ProgrammatorDev\OpenWeatherMap\Resource\GeocodingResource;
1718
use ProgrammatorDev\OpenWeatherMap\Resource\OneCallResource;
1819
use ProgrammatorDev\OpenWeatherMap\Resource\WeatherResource;
@@ -21,12 +22,12 @@
2122

2223
class OpenWeatherMap extends Api
2324
{
24-
private array $options;
25+
public readonly array $options;
2526

2627
private OptionsResolver $optionsResolver;
2728

2829
public function __construct(
29-
#[\SensitiveParameter] private readonly string $apiKey,
30+
#[\SensitiveParameter] public readonly string $apiKey,
3031
array $options = []
3132
)
3233
{
@@ -38,6 +39,11 @@ public function __construct(
3839
$this->configureApi();
3940
}
4041

42+
public function assistant(): AssistantResource
43+
{
44+
return new AssistantResource($this);
45+
}
46+
4147
public function oneCall(): OneCallResource
4248
{
4349
return new OneCallResource($this);

src/Resource/AssistantResource.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Resource;
4+
5+
use Http\Message\Authentication\Header;
6+
use ProgrammatorDev\Api\Method;
7+
use ProgrammatorDev\OpenWeatherMap\Entity\Assistant\Answer;
8+
use Psr\Http\Client\ClientExceptionInterface;
9+
10+
class AssistantResource extends Resource
11+
{
12+
/**
13+
* Start a new session with the Weather AI Assistant and enter a prompt
14+
*
15+
* @throws ClientExceptionInterface
16+
*/
17+
public function startSession(string $prompt): Answer
18+
{
19+
$this->api->setAuthentication(new Header('X-Api-Key', $this->api->apiKey));
20+
21+
$data = $this->api->request(
22+
method: Method::POST,
23+
path: '/assistant/session',
24+
body: json_encode(['prompt' => $prompt])
25+
);
26+
27+
return new Answer($data);
28+
}
29+
}

src/Util/EntityTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ private function createEntityList(string $entityClass, array $list): array
1010
return new $entityClass($data);
1111
}, $list);
1212
}
13+
14+
private function createEntityKeyList(string $entityClass, array $list): array
15+
{
16+
return array_map(function(array $data, int|string $key) use ($entityClass) {
17+
return new $entityClass($key, $data);
18+
}, $list, array_keys($list));
19+
}
1320
}

0 commit comments

Comments
 (0)