Skip to content

Commit

Permalink
STAM-2475
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlin committed Nov 30, 2018
1 parent 717c44e commit cd519e6
Show file tree
Hide file tree
Showing 24 changed files with 682 additions and 323 deletions.
2 changes: 1 addition & 1 deletion src/Accounts/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getAccount(string $accessToken = null)
$header = ['Authorization' => $accessToken];

return $this->options
->getRequest()
->getSync()
->setHeaderParams($header)
->get(API::LIST['account']);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Accounts/Manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getAccountsList(array $params = [])
];

return $this->options
->getRequest()
->getSync()
->setPath($client['client_id'])
->setQuery($pagination)
->setHeaderParams($header)
Expand All @@ -84,7 +84,7 @@ public function getAccountInfo(string $accountId = null)
$header = ['Authorization' => $client['client_secret']];

return $this->options
->getRequest()
->getSync()
->setPath($client['client_id'], $accountId)
->setHeaderParams($header)
->get(API::LIST['listAnAccount']);
Expand All @@ -106,7 +106,7 @@ public function reactiveAccount(string $accountId = null)
$header = ['Authorization' => $client['client_secret']];

return $this->options
->getRequest()
->getSync()
->setPath($client['client_id'], $accountId)
->setHeaderParams($header)
->post(API::LIST['reactiveAnAccount']);
Expand All @@ -128,7 +128,7 @@ public function cancelAccount(string $accountId = null)
$header = ['Authorization' => $client['client_secret']];

return $this->options
->getRequest()
->getSync()
->setPath($client['client_id'], $accountId)
->setHeaderParams($header)
->post(API::LIST['cancelAnAccount']);
Expand Down
4 changes: 2 additions & 2 deletions src/Authentication/Hosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function postOAuthToken(string $code)
$query = array_merge($query, $params);

return $this->options
->getRequest()
->getSync()
->setQuery($query)
->post(API::LIST['oAuthToken']);
}
Expand All @@ -110,7 +110,7 @@ public function postOAuthRevoke(string $accessToken = null)
$header = ['Authorization' => $accessToken];

return $this->options
->getRequest()
->getSync()
->setHeaderParams($header)
->post(API::LIST['oAuthRevoke']);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Authentication/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function postConnectToken(string $code)
$params['code'] = $code;

return $this->options
->getRequest()
->getSync()
->setFormParams($params)
->post(API::LIST['connectToken']);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ public function postConnectAuthorize(array $params)
V::doValidate($rules, $params);

return $this->options
->getRequest()
->getSync()
->setFormParams($params)
->post(API::LIST['connectAuthorize']);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Calendars/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getCalendarsList(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['calendars']);
Expand Down Expand Up @@ -95,7 +95,7 @@ public function getCalendar(string $calendarId, string $accessToken = null)
$header = ['Authorization' => $params['access_token']];

return $this->options
->getRequest()
->getSync()
->setPath($params['id'])
->setHeaderParams($header)
->get(API::LIST['oneCalendar']);
Expand Down
76 changes: 51 additions & 25 deletions src/Contacts/Contact.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php namespace Nylas\Contacts;

use Nylas\Utilities\API;
use Nylas\Utilities\Helper;
use Nylas\Utilities\Options;
use Nylas\Utilities\Validate as V;
use Psr\Http\Message\StreamInterface;

/**
* ----------------------------------------------------------------------------------
Expand Down Expand Up @@ -54,7 +56,7 @@ public function getContactsList(array $params = [])
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['contacts']);
Expand Down Expand Up @@ -87,7 +89,7 @@ public function getContact(string $contactId, string $accessToken = null)
$header = ['Authorization' => $params['access_token']];

return $this->options
->getRequest()
->getSync()
->setPath($params['id'])
->setHeaderParams($header)
->get(API::LIST['oneContact']);
Expand Down Expand Up @@ -115,7 +117,7 @@ public function addContact(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setFormParams($params)
->setHeaderParams($header)
->post(API::LIST['contacts']);
Expand Down Expand Up @@ -146,7 +148,7 @@ public function updateContact(array $params)
unset($params['id'], $params['access_token']);

return $this->options
->getRequest()
->getSync()
->setPath($path)
->setFormParams($params)
->setHeaderParams($header)
Expand Down Expand Up @@ -180,7 +182,7 @@ public function deleteContact(string $contactId, string $accessToken = null)
$header = ['Authorization' => $params['access_token']];

$this->options
->getRequest()
->getSync()
->setPath($params['id'])
->setHeaderParams($header)
->delete(API::LIST['oneContact']);
Expand All @@ -203,45 +205,69 @@ public function getContactGroups(string $accessToken = null)
$header = ['Authorization' => $accessToken];

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

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

/**
* get contact picture
* get contact picture file (support multiple download)
*
* @param array $params
* @param string $accessToken
* @return array
*/
public function getContactPicture(array $params)
public function getContactPicture(array $params, string $accessToken = null)
{
$params['access_token'] =
$params['access_token'] ?? $this->options->getAccessToken();
$downloadArr = Helper::arrayToMulti($params);
$accessToken = $accessToken ?? $this->options->getAccessToken();

V::doValidate($this->pictureRules(), $downloadArr);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$method = [];
$target = API::LIST['contactPic'];
$header = ['Authorization' => $accessToken];

foreach ($downloadArr as $item)
{
$sink = $item['path'];

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

$method[] = function () use ($request, $target, $sink)
{
return $request->getSink($target, $sink);
};
}

return $this->options->getAsync()->pool($method, true);
}

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

/**
* rules for download picture
*
* @return \Respect\Validation\Validator
*/
private function pictureRules()
{
$path = V::oneOf(
V::resourceType(),
V::stringType()->notEmpty(),
V::instance('\Psr\Http\Message\StreamInterface')
V::instance(StreamInterface::class)
);

$rule = V::keySet(
return V::arrayType()->each(V::keySet(
V::key('id', V::stringType()->notEmpty()),
V::key('path', $path),
V::key('access_token', V::stringType()->notEmpty())
);

V::doValidate($rule, $params);

$header = ['Authorization' => $params['access_token']];

return $this->options
->getRequest()
->setPath($params['id'])
->setHeaderParams($header)
->getSink(API::LIST['contactPic'], $params['path']);
V::key('path', $path)
));
}

// ------------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/Deltas/Delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getLatestCursor(string $accessToken = null)
$header = ['Authorization' => $accessToken];

return $this->options
->getRequest()
->getSync()
->setHeaderParams($header)
->post(API::LIST['deltaLatestCursor']);
}
Expand All @@ -78,7 +78,7 @@ public function getSetOfDeltas(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['delta']);
Expand Down Expand Up @@ -109,7 +109,7 @@ public function longPollingDelta(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['deltaLongpoll']);
Expand Down Expand Up @@ -137,7 +137,7 @@ public function streamingDelta(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setQuery($params)
->setHeaderParams($header)
->get(API::LIST['deltaStreaming']);
Expand Down
19 changes: 10 additions & 9 deletions src/Drafts/Draft.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Nylas Drafts
* ----------------------------------------------------------------------------------
*
* @info include inline image <img src="cid:file_id">
* @author lanlin
* @change 2018/11/23
*/
Expand Down Expand Up @@ -61,7 +62,7 @@ public function getDraftsList(string $anyEmail = null, string $accessToken = nul
$query = empty($emails) ? [] : ['any_email' => $emails];

return $this->options
->getRequest()
->getSync()
->setQuery($query)
->setHeaderParams($header)
->get(API::LIST['drafts']);
Expand Down Expand Up @@ -94,7 +95,7 @@ public function getDraft(string $draftId, string $accessToken = null)
$header = ['Authorization' => $params['access_token']];

return $this->options
->getRequest()
->getSync()
->setPath($params['id'])
->setHeaderParams($header)
->get(API::LIST['oneDraft']);
Expand Down Expand Up @@ -122,7 +123,7 @@ public function addDraft(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setFormParams($params)
->setHeaderParams($header)
->post(API::LIST['drafts']);
Expand Down Expand Up @@ -151,7 +152,7 @@ public function updateDraft(array $params)
unset($params['id'], $params['access_token']);

return $this->options
->getRequest()
->getSync()
->setPath($path)
->setFormParams($params)
->setHeaderParams($header)
Expand All @@ -164,15 +165,15 @@ public function updateDraft(array $params)
* delete draft
*
* @param array $params
* @return mixed
* @return void
*/
public function deleteDraft(array $params)
{
$params['access_token'] = $params['access_token'] ?? $this->options->getAccessToken();

$rule = V::keySet(
V::key('id', V::stringType()->notEmpty()),
V::key('version', V::stringType()->notEmpty()),
V::key('version', V::intType()->min(0)),
V::key('access_token', V::stringType()->notEmpty())
);

Expand All @@ -183,8 +184,8 @@ public function deleteDraft(array $params)

unset($params['id'], $params['access_token']);

return $this->options
->getRequest()
$this->options
->getSync()
->setPath($path)
->setFormParams($params)
->setHeaderParams($header)
Expand Down Expand Up @@ -238,7 +239,7 @@ private function getUpdateRules()
$update =
[
V::key('id', V::stringType()->notEmpty()),
V::key('version', V::stringType()->length(1, null))
V::key('version', V::intType()->min(0))
];

return array_merge($rules, $update);
Expand Down
4 changes: 2 additions & 2 deletions src/Drafts/Sending.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function sendDraft(array $params)
$params['access_token'] ?? $this->options->getAccessToken();

$rules = V::keySet(
V::key('draft', V::stringType()->notEmpty()),
V::key('version', V::intType()->min(0)),
V::key('draft_id', V::stringType()->notEmpty()),
V::key('access_token', V::stringType()->notEmpty())
);

Expand All @@ -60,7 +60,7 @@ public function sendDraft(array $params)
unset($params['access_token']);

return $this->options
->getRequest()
->getSync()
->setFormParams($params)
->setHeaderParams($header)
->post(API::LIST['sending']);
Expand Down
Loading

0 comments on commit cd519e6

Please sign in to comment.