Skip to content

Commit

Permalink
Support for activating/deactivating a scenario scheduler
Browse files Browse the repository at this point in the history
- added method `ScenarioSchedulersController::activateScenarioScheduler()`
- added method `ScenarioSchedulersController::deactivateScenarioScheduler()`
- added property `bool $active` and mapping for value object `ScenarioSchedulerListingItem`, request body `ScenarioSchedulerRequestBody` and response body `ScenarioSchedulerResponseBody`
- updated README
- added tests
  • Loading branch information
tg666 committed Jul 10, 2023
1 parent c5afa25 commit bcb06a8
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ 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).

## Unreleased
## 1.2.0 - 2023-07-11
### Added
- Added method `ScenarioSchedulersController::activateScenarioScheduler()`.
- Added method `ScenarioSchedulersController::deactivateScenarioScheduler()`.
- Added property `bool $active` and mapping for value object `ScenarioSchedulerListingItem`, request body `ScenarioSchedulerRequestBody` and response body `ScenarioSchedulerResponseBody`.

### Changed
- Updated README.

## 1.1.1 - 2023-06-21
### Added
- Added method `CrawlerClientInterface::getSerializer()`.
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ As a scenario config we can pass a normal array or use prepared value objects. B
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: [
'scenes' => [ /* ... */ ],
Expand All @@ -314,6 +315,7 @@ $etag = $response->getEtag(); # you need Etag for update
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: (new ScenarioConfig(new Entrypoint('<url>', 'default')))
->withOptions(/* ... */)
Expand Down Expand Up @@ -350,6 +352,7 @@ As a scenario config we can pass a normal array or use prepared value objects. B
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: [
'scenes' => [ /* ... */ ],
Expand All @@ -371,6 +374,7 @@ $etag = $response->getEtag(); # you need Etag for next update
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: (new ScenarioConfig(new Entrypoint('<url>', 'default')))
->withOptions(/* ... */)
Expand Down Expand Up @@ -402,6 +406,7 @@ As a scenario config we can pass a normal array or use prepared value objects. B
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: [
'scenes' => [ /* ... */ ],
Expand All @@ -422,6 +427,7 @@ $response = $controller->validateScenarioScheduler($requestBody);
$requestBody = new ScenarioSchedulerRequestBody(
name: 'My scenario',
flags: ['my_flag' => 'my_flag_value'],
active: true,
expression: '0 2 * * *',
config: (new ScenarioConfig(new Entrypoint('<url>', 'default')))
->withOptions(/* ... */)
Expand All @@ -434,6 +440,29 @@ $requestBody = new ScenarioSchedulerRequestBody(
$response = $controller->validateScenarioScheduler($requestBody);
```

### Activate/deactivate scenario scheduler

```php
/**
* @param string $scenarioSchedulerId
*
* @returns \SixtyEightPublishers\CrawlerClient\Controller\ScenarioScheduler\ScenarioSchedulerResponse
*
* @throws \SixtyEightPublishers\CrawlerClient\Exception\BadRequestException
* @throws \SixtyEightPublishers\CrawlerClient\Exception\NotFoundException
*/
```

```php
use SixtyEightPublishers\CrawlerClient\Controller\ScenarioScheduler\RequestBody\ScenarioSchedulerRequestBody;

# to activate the scenario scheduler:
$response = $controller->activateScenarioScheduler('<id>');

# to deactivate the scenario scheduler:
$response = $controller->deactivateScenarioScheduler('<id>');
```

### Delete scenario scheduler

```php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ final class ScenarioSchedulerRequestBody
/** @var array<string, string> */
public array $flags;

public bool $active;

public string $expression;

/** @var ScenarioConfig|array<string, mixed> */
Expand All @@ -25,11 +27,13 @@ final class ScenarioSchedulerRequestBody
public function __construct(
string $name,
array $flags,
bool $active,
string $expression,
$config
) {
$this->name = $name;
$this->flags = $flags;
$this->active = $active;
$this->expression = $expression;
$this->config = $config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ SixtyEightPublishers\CrawlerClient\Controller\ScenarioScheduler\RequestBody\Scen
type: string
flags:
type: array<string, string>
active:
type: boolean
expression:
type: string
config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class ScenarioSchedulerResponseBody

public DateTimeImmutable $updatedAt;

public bool $active;

public string $expression;

/** @var array<string, string> */
Expand All @@ -38,6 +40,7 @@ public function __construct(
string $name,
DateTimeImmutable $createdAt,
DateTimeImmutable $updatedAt,
bool $active,
string $expression,
array $flags,
ScenarioConfig $config
Expand All @@ -48,6 +51,7 @@ public function __construct(
$this->name = $name;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
$this->active = $active;
$this->expression = $expression;
$this->flags = $flags;
$this->config = $config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ SixtyEightPublishers\CrawlerClient\Controller\ScenarioScheduler\ResponseBody\Sce
type: DateTimeImmutable
updatedAt:
type: DateTimeImmutable
active:
type: boolean
expression:
type: string
flags:
Expand Down
22 changes: 22 additions & 0 deletions src/Controller/ScenarioScheduler/ScenarioSchedulersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,26 @@ public function deleteScenarioScheduler(string $scenarioSchedulerId): NoContentR

return new NoContentResponse($response);
}

/** @noinspection PhpUnhandledExceptionInspection */
public function activateScenarioScheduler(string $scenarioSchedulerId): ScenarioSchedulerResponse
{
return new ScenarioSchedulerResponse(
...$this->handle(
fn () => $this->client->request('PUT', 'scenario-schedulers/' . $scenarioSchedulerId . '/activate'),
ScenarioSchedulerResponseBody::class,
),
);
}

/** @noinspection PhpUnhandledExceptionInspection */
public function deactivateScenarioScheduler(string $scenarioSchedulerId): ScenarioSchedulerResponse
{
return new ScenarioSchedulerResponse(
...$this->handle(
fn () => $this->client->request('PUT', 'scenario-schedulers/' . $scenarioSchedulerId . '/deactivate'),
ScenarioSchedulerResponseBody::class,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class ScenarioSchedulerListingItem

public DateTimeImmutable $updatedAt;

public bool $active;

public string $expression;

/** @var array<string, string> */
Expand All @@ -35,6 +37,7 @@ public function __construct(
string $name,
DateTimeImmutable $createdAt,
DateTimeImmutable $updatedAt,
bool $active,
string $expression,
array $flags
) {
Expand All @@ -44,6 +47,7 @@ public function __construct(
$this->name = $name;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
$this->active = $active;
$this->expression = $expression;
$this->flags = $flags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ SixtyEightPublishers\CrawlerClient\Controller\ScenarioScheduler\ValueObject\Scen
type: DateTimeImmutable
updatedAt:
type: DateTimeImmutable
active:
type: boolean
expression:
type: string
flags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ public function testDeleteScenarioScheduler(): void
Assert::same($history[0]['response'], $returnedMappedResponse->getResponse());
}

public function testActivateScenarioScheduler(): void
{
$responseHelper = new FileFixtureHelper(__DIR__ . '/responses');
[$controller, $history] = $this->createControllerAndHistory([
new Response(200, ['Content-Type' => 'application/json', 'Etag' => 'new_etag'], $responseHelper->getRaw('get-scenario-scheduler')),
]);

$returnedMappedResponse = $controller->activateScenarioScheduler('b5f052d8-f285-49ac-9030-bd50ec73393c');

Assert::same(200, $returnedMappedResponse->getStatusCode());
Assert::same('PUT', $history[0]['request']->getMethod());
Assert::same('https://www.crawler.com/api/scenario-schedulers/b5f052d8-f285-49ac-9030-bd50ec73393c/activate', (string) $history[0]['request']->getUri());
Assert::same($history[0]['response'], $returnedMappedResponse->getResponse());
Assert::equal($responseHelper->getPhp('get-scenario-scheduler'), $returnedMappedResponse->getBody());
}

public function testDeactivateScenarioScheduler(): void
{
$responseHelper = new FileFixtureHelper(__DIR__ . '/responses');
[$controller, $history] = $this->createControllerAndHistory([
new Response(200, ['Content-Type' => 'application/json', 'Etag' => 'new_etag'], $responseHelper->getRaw('get-scenario-scheduler')),
]);

$returnedMappedResponse = $controller->deactivateScenarioScheduler('b5f052d8-f285-49ac-9030-bd50ec73393c');

Assert::same(200, $returnedMappedResponse->getStatusCode());
Assert::same('PUT', $history[0]['request']->getMethod());
Assert::same('https://www.crawler.com/api/scenario-schedulers/b5f052d8-f285-49ac-9030-bd50ec73393c/deactivate', (string) $history[0]['request']->getUri());
Assert::same($history[0]['response'], $returnedMappedResponse->getResponse());
Assert::equal($responseHelper->getPhp('get-scenario-scheduler'), $returnedMappedResponse->getBody());
}

public function dataProviderListScenarioSchedulers(): array
{
$helper = new FileFixtureHelper(__DIR__ . '/responses');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[
'optional' => 'true',
],
true,
'0 1 * * *',
[
'options' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"flags": {
"optional": "true"
},
"active": true,
"expression": "0 1 * * *",
"config": {
"options": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
[
'optional' => 'true',
],
true,
'0 1 * * *',
(new ScenarioConfig(new Entrypoint('https://www.example.com', 'startup')))
->withCallbackUri('https://www.example-api.com/results')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"name": "Test 1",
"createdAt": "2023-06-07T02:20:02.020Z",
"updatedAt": "2023-06-07T02:20:02.020Z",
"active": true,
"expression": "0 1 * * *",
"flags": {
"optional": "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'Test 1',
new DateTimeImmutable('2023-06-07T02:20:02.020Z'),
new DateTimeImmutable('2023-06-07T02:20:02.020Z'),
true,
'0 1 * * *',
[
'optional' => 'true',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"name": "Test 1",
"createdAt": "2023-06-07T02:20:02.020Z",
"updatedAt": "2023-06-07T02:20:02.020Z",
"active": true,
"expression": "0 1 * * *",
"flags": {
"optional": "true"
Expand All @@ -26,6 +27,7 @@
"name": "Test 2",
"createdAt": "2023-06-06T04:57:12.922Z",
"updatedAt": "2023-06-06T04:57:12.922Z",
"active": true,
"expression": "0 2 * * *",
"flags": {
"optional": "true"
Expand All @@ -38,6 +40,7 @@
"name": "Test 3",
"createdAt": "2023-06-06T04:52:12.922Z",
"updatedAt": "2023-06-06T04:52:12.922Z",
"active": true,
"expression": "0 3 * * *",
"flags": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'Test 1',
new DateTimeImmutable('2023-06-07T02:20:02.020Z'),
new DateTimeImmutable('2023-06-07T02:20:02.020Z'),
true,
'0 1 * * *',
[
'optional' => 'true',
Expand All @@ -30,6 +31,7 @@
'Test 2',
new DateTimeImmutable('2023-06-06T04:57:12.922Z'),
new DateTimeImmutable('2023-06-06T04:57:12.922Z'),
true,
'0 2 * * *',
[
'optional' => 'true',
Expand All @@ -42,6 +44,7 @@
'Test 3',
new DateTimeImmutable('2023-06-06T04:52:12.922Z'),
new DateTimeImmutable('2023-06-06T04:52:12.922Z'),
true,
'0 3 * * *',
[],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"name": "Test 4",
"createdAt": "2023-06-05T02:11:54.021Z",
"updatedAt": "2023-06-05T02:11:54.021Z",
"active": true,
"expression": "0 4 * * *",
"flags": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'Test 4',
new DateTimeImmutable('2023-06-05T02:11:54.021Z'),
new DateTimeImmutable('2023-06-05T02:11:54.021Z'),
true,
'0 4 * * *',
[],
),
Expand Down

0 comments on commit bcb06a8

Please sign in to comment.