Skip to content

Commit

Permalink
Merge remote-tracking branch 'jgriffin/job_status'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygriffin committed May 10, 2021
2 parents 27b87d5 + f95efcf commit b53f821
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ $nylas->Folders()->Folder()->xxx();
$nylas->Labels()->Label()->xxx();
```

### [Job-Statuses](https://docs.nylas.com/reference#job-statuses)

```php
$nylas->JobStatuses()->JobStatus()->xxx();
```


### [Messages](https://docs.nylas.com/reference#messages)

Expand Down
1 change: 1 addition & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @method Messages\Abs Messages()
* @method Threads\Abs Threads()
* @method Webhooks\Abs Webhooks()
* @method JobStatuses\Abs JobStatuses()
*
* @author lanlin
* @change 2021/03/18
Expand Down
24 changes: 24 additions & 0 deletions src/JobStatuses/Abs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Nylas\JobStatuses;

use Nylas\Utilities\Abs as AbsTrait;

/**
* ----------------------------------------------------------------------------------
* Nylas Abs
* ----------------------------------------------------------------------------------
*
* @method JobStatus JobStatus()
*
* @author lanlin
* @change 2020/04/26
*/
class Abs
{
// ------------------------------------------------------------------------------

use AbsTrait;

// ------------------------------------------------------------------------------
}
139 changes: 139 additions & 0 deletions src/JobStatuses/JobStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Nylas\JobStatuses;

use Nylas\Utilities\API;
use Nylas\Utilities\Helper;
use Nylas\Utilities\Options;
use Nylas\Utilities\Validator as V;

/**
* ----------------------------------------------------------------------------------
* Nylas Job Statuses
* ----------------------------------------------------------------------------------
*
* @see https://docs.nylas.com/reference#job-statuses
*
* @author lanlin
* @update jeremygriffin
* @change 2021/05/05
*/
class JobStatus
{
// ------------------------------------------------------------------------------

/**
* @var \Nylas\Utilities\Options
*/
private Options $options;

// ------------------------------------------------------------------------------

/**
* JobStatus constructor.
*
* @param \Nylas\Utilities\Options $options
*/
public function __construct(Options $options)
{
$this->options = $options;
}

// ------------------------------------------------------------------------------

/**
* get job-status list
*
* @param array $params
*
* @return array
* @throws \Exception
*/
public function getJobStatusesList(array $params = []): array
{
$rules = $this->getBaseRules();

$rules[] = V::keyOptional('view', V::in(['ids', 'count']));
$accessToken = $this->options->getAccessToken();

V::doValidate(V::keySet(...$rules), $params);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$header = ['Authorization' => $accessToken];

return $this->options
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['jobStatuses']);
}

// ------------------------------------------------------------------------------

/**
* get job-status
*
* @param array $params
*
* @return array
*/
public function getJobStatus(array $params): array
{
$rules = $this->getBaseRules();
$params = Helper::arrayToMulti($params);
$accessToken = $this->options->getAccessToken();

$rules = V::simpleArray(V::keySet(
V::key('job_status_id', V::stringType()->notEmpty()),
...$rules
));

V::doValidate($rules, $params);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$queues = [];
$target = API::LIST['oneJobStatus'];
$header = ['Authorization' => $accessToken];

foreach ($params as $item)
{
$id = $item['job_status_id'];
unset($item['job_status_id']);

$request = $this->options
->getAsync()
->setPath($id)
->setFormParams($item)
->setHeaderParams($header);

$queues[] = static function () use ($request, $target)
{
return $request->get($target);
};
}

$jobID = Helper::generateArray($params, 'job_status_id');
$pools = $this->options->getAsync()->pool($queues, false);

return Helper::concatPoolInfos($jobID, $pools);
}

// ------------------------------------------------------------------------------

/**
* job-status base validate rules
*
* @return array
*/
private function getBaseRules(): array
{
return
[
V::keyOptional('limit', V::intType()->min(1)),
V::keyOptional('offset', V::intType()->min(0)),
V::keyOptional('job_status_id', V::stringType()->notEmpty()),
];
}

// ------------------------------------------------------------------------------
}
5 changes: 5 additions & 0 deletions src/Utilities/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class API
'deltaLongpoll' => '/delta/longpoll',
'deltaStreaming' => '/delta/streaming',
'deltaLatestCursor' => '/delta/latest_cursor',

// JobStatuses
'jobStatuses' => '/job-statuses',
'oneJobStatus' => '/job-statuses/%s',

];

// ------------------------------------------------------------------------------
Expand Down
40 changes: 40 additions & 0 deletions tests/JobStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Nylas\Tests;

/**
* ----------------------------------------------------------------------------------
* JobStatus Test
* ----------------------------------------------------------------------------------
*
* @update lanlin
* @change 2021/05/05
*
* @internal
*/
class JobStatusTest extends AbsCase
{
// ------------------------------------------------------------------------------

public function testGetJobStatusList(): void
{
$data = $this->client->JobStatuses()->JobStatus()->getJobStatusesList();

$this->assertTrue(\count($data) > 0);
}

// ------------------------------------------------------------------------------

public function testGetJobStatus(): void
{
$params = ['job_status_id' => 'csihlkp7geos1org29z02xzm8'];

$data = $this->client->JobStatuses()->JobStatus()->getJobStatus($params);

//@NOTE: Format is [ '<job_status_id>' => [data in k => v]]
$this->assertArrayHasKey($params['job_status_id'], $data);
$this->assertArrayHasKey('job_status_id', $data[$params['job_status_id']]);
}

// ------------------------------------------------------------------------------
}

0 comments on commit b53f821

Please sign in to comment.