-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from jeremygriffin/master
Update to add job-status endpoint for list and single job-status object (rebased for master)
- Loading branch information
Showing
6 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,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; | ||
|
||
// ------------------------------------------------------------------------------ | ||
} |
This file contains 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,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()), | ||
]; | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
} |
This file contains 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 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,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']]); | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
} |