-
Notifications
You must be signed in to change notification settings - Fork 12
Events api integration for CCMC #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mudhoney
wants to merge
12
commits into
Helioviewer-Project:main
Choose a base branch
from
mudhoney:feature/ccmc-events-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6d1b024
add a convenience CORS backdoor for dev environments
mudhoney 0e8961e
implement eventsapi client and the exceptions, use them for CCMC data…
mudhoney 8ede1e6
Merge branch 'main' into feature/ccmc-events-api
mudhoney 6242eab
use default eventsapi url
mudhoney 0c8a2e4
add tests for eventsapi integration
mudhoney 8c0d985
merging with main
mudhoney 7f36226
Merge remote-tracking branch 'origin/main' into feature/ccmc-events-api
mudhoney e83c161
default URL for eventsapi in API config
mudhoney 32727a7
use events api for movie generation and screenshots
mudhoney c491ef6
merge with main
mudhoney 095975a
Merge remote-tracking branch 'origin/main' into feature/ccmc-events-api
mudhoney 7730ef8
merge with main
mudhoney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| namespace Helioviewer\Api\Event; | ||
|
|
||
| use DateTimeInterface; | ||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\ClientInterface; | ||
| use Helioviewer\Api\Sentry\Sentry; | ||
|
|
||
| class EventsApi { | ||
|
|
||
| private ClientInterface $client; | ||
|
|
||
| /** | ||
| * EventsApi constructor. | ||
| * | ||
| * @param ClientInterface|null $client Optional Guzzle client; if not provided, a new Client is created. | ||
| */ | ||
| public function __construct(ClientInterface $client = null) | ||
| { | ||
| $this->client = $client ?? new Client([ | ||
| 'timeout' => 4, | ||
| 'headers' => [ | ||
| 'Accept' => 'application/json', | ||
| 'User-Agent' => 'Helioviewer-API/2.0' | ||
| ] | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Get events for a specific source | ||
| * | ||
| * @param DateTimeInterface $observationTime The observation time | ||
| * @param string $source The data source (e.g. "CCMC") | ||
| * @return array Array of event data | ||
| * @throws EventsApiException on API errors or unexpected responses | ||
| */ | ||
| public function getEventsForSource(DateTimeInterface $observationTime, string $source): array | ||
| { | ||
| // Build the API URL: /api/v1/events/{source}/observation/{datetime} | ||
| $formattedTime = $observationTime->format('Y-m-d H:i:s'); | ||
| $encodedTime = urlencode($formattedTime); | ||
|
|
||
| $baseUrl = defined('HV_EVENTS_API_URL') ? HV_EVENTS_API_URL : 'https://events.helioviewer.org'; | ||
| $url = $baseUrl . "/api/v1/events/{$source}/observation/{$encodedTime}"; | ||
|
|
||
| Sentry::setContext('EventsApi', [ | ||
| 'url' => $url, | ||
| 'source' => $source, | ||
| 'observation_time' => $observationTime->format('Y-m-d\TH:i:s\Z') | ||
| ]); | ||
|
|
||
| $response = $this->client->request('GET', $url); | ||
|
|
||
| return $this->parseResponse($response); | ||
| } | ||
|
|
||
| /** | ||
| * Parse the HTTP response and decode JSON | ||
| * | ||
| * @param \Psr\Http\Message\ResponseInterface $response | ||
| * @return array | ||
| * @throws EventsApiException if JSON decoding fails or response format is unexpected | ||
| */ | ||
| private function parseResponse($response): array | ||
| { | ||
| $body = (string)$response->getBody(); | ||
| $data = json_decode($body, true); | ||
|
|
||
| if (json_last_error() !== JSON_ERROR_NONE) { | ||
| Sentry::setContext('EventsApi', [ | ||
| 'raw_response' => $body, | ||
| 'json_error' => json_last_error_msg(), | ||
| 'response_status' => $response->getStatusCode() | ||
| ]); | ||
|
|
||
| throw new EventsApiException("Failed to decode JSON response: " . json_last_error_msg()); | ||
| } | ||
|
|
||
| if (!is_array($data)) { | ||
| Sentry::setContext('EventsApi', [ | ||
| 'unexpected_response_type' => gettype($data), | ||
| 'raw_response' => $body, | ||
| 'response_status' => $response->getStatusCode() | ||
| ]); | ||
|
|
||
| throw new EventsApiException("Unexpected response format: expected array, got " . gettype($data)); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| namespace Helioviewer\Api\Event; | ||
|
|
||
| class EventsApiException extends \Exception | ||
| { | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @author Kasim Necdet Percinel <kasim.n.percinel@nasa.gov> | ||
| */ | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use GuzzleHttp\ClientInterface; | ||
| use GuzzleHttp\Psr7\Response; | ||
| use Helioviewer\Api\Event\EventsApi; | ||
| use Helioviewer\Api\Event\EventsApiException; | ||
|
|
||
| final class EventsApiTest extends TestCase | ||
| { | ||
| private $mockClient; | ||
| private $eventsApi; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->mockClient = $this->createMock(ClientInterface::class); | ||
| $this->eventsApi = new EventsApi($this->mockClient); | ||
| } | ||
|
|
||
| public function testItShouldGetEventsSuccessfully(): void | ||
| { | ||
| $responseData = [ | ||
| ['id' => 1, 'type' => 'event'.rand()], | ||
| ['id' => 2, 'type' => 'event'.rand()] | ||
| ]; | ||
|
|
||
| $this->mockClient->expects($this->once()) | ||
| ->method('request') | ||
| ->with('GET', $this->stringContains('/api/v1/events/CCMC/observation/')) | ||
| ->willReturn(new Response(200, [], json_encode($responseData))); | ||
|
|
||
| $result = $this->eventsApi->getEventsForSource( | ||
| new DateTimeImmutable('2024-01-15 12:00:00'), | ||
| 'CCMC' | ||
| ); | ||
|
|
||
| $this->assertEquals($responseData, $result); | ||
| } | ||
|
|
||
| public function testItShouldUrlEncodeObservationTime(): void | ||
| { | ||
| $this->mockClient->expects($this->once()) | ||
| ->method('request') | ||
| ->with('GET', $this->stringContains('2024-01-15+12%3A30%3A45')) | ||
| ->willReturn(new Response(200, [], json_encode([]))); | ||
|
|
||
| $this->eventsApi->getEventsForSource( | ||
| new DateTimeImmutable('2024-01-15 12:30:45'), | ||
| 'CCMC' | ||
| ); | ||
| } | ||
|
|
||
| public function testItShouldThrowExceptionOnInvalidJson(): void | ||
| { | ||
| $this->mockClient->expects($this->once()) | ||
| ->method('request') | ||
| ->willReturn(new Response(200, [], 'invalid json {')); | ||
|
|
||
| $this->expectException(EventsApiException::class); | ||
| $this->expectExceptionMessage('Failed to decode JSON response'); | ||
|
|
||
| $this->eventsApi->getEventsForSource( | ||
| new DateTimeImmutable('2024-01-15 12:00:00'), | ||
| 'CCMC' | ||
| ); | ||
| } | ||
|
|
||
| public function testItShouldThrowExceptionWhenResponseIsNotArray(): void | ||
| { | ||
| $this->mockClient->expects($this->once()) | ||
| ->method('request') | ||
| ->willReturn(new Response(200, [], '"just a string"')); | ||
|
|
||
| $this->expectException(EventsApiException::class); | ||
| $this->expectExceptionMessage('Unexpected response format: expected array, got string'); | ||
|
|
||
| $this->eventsApi->getEventsForSource( | ||
| new DateTimeImmutable('2024-01-15 12:00:00'), | ||
| 'CCMC' | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.