From cd519e68b2e41c04cfa4e7adaa8d76774b840aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E6=9E=97?= Date: Fri, 30 Nov 2018 18:21:49 +0800 Subject: [PATCH] STAM-2475 --- src/Accounts/Account.php | 2 +- src/Accounts/Manage.php | 8 +- src/Authentication/Hosted.php | 4 +- src/Authentication/Native.php | 4 +- src/Calendars/Calendar.php | 4 +- src/Contacts/Contact.php | 76 ++++-- src/Deltas/Delta.php | 8 +- src/Drafts/Draft.php | 19 +- src/Drafts/Sending.php | 4 +- src/Events/Event.php | 35 +-- src/Files/File.php | 153 ++++++++---- src/Folders/Folder.php | 14 +- src/Labels/Label.php | 14 +- src/Messages/Message.php | 9 +- src/Messages/Search.php | 2 +- src/Messages/Sending.php | 4 +- .../Request.php => Request/AbsBase.php} | 187 +-------------- src/Request/Async.php | 218 ++++++++++++++++++ src/Request/Sync.php | 194 ++++++++++++++++ src/Threads/Search.php | 2 +- src/Threads/Thread.php | 6 +- src/Utilities/Options.php | 27 ++- src/Utilities/Validate.php | 7 +- src/Webhooks/Webhook.php | 4 +- 24 files changed, 682 insertions(+), 323 deletions(-) rename src/{Utilities/Request.php => Request/AbsBase.php} (61%) create mode 100644 src/Request/Async.php create mode 100644 src/Request/Sync.php diff --git a/src/Accounts/Account.php b/src/Accounts/Account.php index ab94718..46ef7f7 100644 --- a/src/Accounts/Account.php +++ b/src/Accounts/Account.php @@ -52,7 +52,7 @@ public function getAccount(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->get(API::LIST['account']); } diff --git a/src/Accounts/Manage.php b/src/Accounts/Manage.php index 4bc9e9c..951aa5b 100644 --- a/src/Accounts/Manage.php +++ b/src/Accounts/Manage.php @@ -61,7 +61,7 @@ public function getAccountsList(array $params = []) ]; return $this->options - ->getRequest() + ->getSync() ->setPath($client['client_id']) ->setQuery($pagination) ->setHeaderParams($header) @@ -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']); @@ -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']); @@ -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']); diff --git a/src/Authentication/Hosted.php b/src/Authentication/Hosted.php index a8f98cc..1c06b1e 100644 --- a/src/Authentication/Hosted.php +++ b/src/Authentication/Hosted.php @@ -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']); } @@ -110,7 +110,7 @@ public function postOAuthRevoke(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->post(API::LIST['oAuthRevoke']); } diff --git a/src/Authentication/Native.php b/src/Authentication/Native.php index 748922b..0bbda97 100644 --- a/src/Authentication/Native.php +++ b/src/Authentication/Native.php @@ -61,7 +61,7 @@ public function postConnectToken(string $code) $params['code'] = $code; return $this->options - ->getRequest() + ->getSync() ->setFormParams($params) ->post(API::LIST['connectToken']); } @@ -92,7 +92,7 @@ public function postConnectAuthorize(array $params) V::doValidate($rules, $params); return $this->options - ->getRequest() + ->getSync() ->setFormParams($params) ->post(API::LIST['connectAuthorize']); } diff --git a/src/Calendars/Calendar.php b/src/Calendars/Calendar.php index 4dfa5dd..6f583d4 100644 --- a/src/Calendars/Calendar.php +++ b/src/Calendars/Calendar.php @@ -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']); @@ -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']); diff --git a/src/Contacts/Contact.php b/src/Contacts/Contact.php index aadff6d..21496a9 100644 --- a/src/Contacts/Contact.php +++ b/src/Contacts/Contact.php @@ -1,8 +1,10 @@ options - ->getRequest() + ->getSync() ->setQuery($params) ->setHeaderParams($header) ->get(API::LIST['contacts']); @@ -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']); @@ -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']); @@ -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) @@ -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']); @@ -203,7 +205,7 @@ public function getContactGroups(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->get(API::LIST['contactsGroups']); } @@ -211,37 +213,61 @@ public function getContactGroups(string $accessToken = null) // ------------------------------------------------------------------------------ /** - * 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) + )); } // ------------------------------------------------------------------------------ diff --git a/src/Deltas/Delta.php b/src/Deltas/Delta.php index 0087f44..030b8bc 100644 --- a/src/Deltas/Delta.php +++ b/src/Deltas/Delta.php @@ -51,7 +51,7 @@ public function getLatestCursor(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->post(API::LIST['deltaLatestCursor']); } @@ -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']); @@ -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']); @@ -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']); diff --git a/src/Drafts/Draft.php b/src/Drafts/Draft.php index 55343cb..eb9bbab 100644 --- a/src/Drafts/Draft.php +++ b/src/Drafts/Draft.php @@ -9,6 +9,7 @@ * Nylas Drafts * ---------------------------------------------------------------------------------- * + * @info include inline image * @author lanlin * @change 2018/11/23 */ @@ -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']); @@ -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']); @@ -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']); @@ -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) @@ -164,7 +165,7 @@ public function updateDraft(array $params) * delete draft * * @param array $params - * @return mixed + * @return void */ public function deleteDraft(array $params) { @@ -172,7 +173,7 @@ public function deleteDraft(array $params) $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()) ); @@ -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) @@ -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); diff --git a/src/Drafts/Sending.php b/src/Drafts/Sending.php index acd6d34..b9f7336 100644 --- a/src/Drafts/Sending.php +++ b/src/Drafts/Sending.php @@ -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()) ); @@ -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']); diff --git a/src/Events/Event.php b/src/Events/Event.php index 387e78c..76e5248 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -42,7 +42,7 @@ public function __construct(Options $options) * @param array $params * @return array */ - public function getEventsList(array $params) + public function getEventsList(array $params = []) { $rules = $this->getBaseRules(); @@ -56,7 +56,7 @@ public function getEventsList(array $params) unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setQuery($params) ->setHeaderParams($header) ->get(API::LIST['events']); @@ -86,7 +86,7 @@ public function getEvent(array $params) unset($params['id'], $params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) @@ -108,13 +108,14 @@ public function addEvent(array $params) V::doValidate($this->addEventRules(), $params); + $notify = 'notify_participants'; $header = ['Authorization' => $params['access_token']]; - $query = ['notify_participants' => $params['notify_participants']]; + $query = isset($params[$notify]) ? [$notify => $params[$notify]] : []; unset($params['access_token'], $params['notify_participants']); return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setFormParams($params) ->setHeaderParams($header) @@ -137,13 +138,14 @@ public function updateEvent(array $params) V::doValidate($this->updateEventRules(), $params); $path = $params['id']; + $notify = 'notify_participants'; $header = ['Authorization' => $params['access_token']]; - $query = ['notify_participants' => $params['notify_participants']]; + $query = isset($params[$notify]) ? [$notify => $params[$notify]] : []; unset($params['id'], $params['access_token'], $params['notify_participants']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setQuery($query) ->setFormParams($params) @@ -157,7 +159,7 @@ public function updateEvent(array $params) * delete event * * @param array $params - * @return mixed + * @return void */ public function deleteEvent(array $params) { @@ -172,13 +174,12 @@ public function deleteEvent(array $params) V::doValidate($rule, $params); + $notify = 'notify_participants'; $header = ['Authorization' => $params['access_token']]; + $query = isset($params[$notify]) ? [$notify => $params[$notify]] : []; - $temps = empty($params['notify_participants']); - $query = $temps ? [] : ['notify_participants' => $params['notify_participants']]; - - return $this->options - ->getRequest() + $this->options + ->getSync() ->setPath($params['id']) ->setQuery($query) ->setHeaderParams($header) @@ -209,14 +210,14 @@ public function rsvping(array $params) V::doValidate($rules, $params); - $temps = empty($params['notify_participants']); - $query = $temps ? [] : ['notify_participants' => $params['notify_participants']]; - + $notify = 'notify_participants'; $header = ['Authorization' => $params['access_token']]; + $query = isset($params[$notify]) ? [$notify => $params[$notify]] : []; + unset($params['access_token'], $params['notify_participants']); return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setFormParams($params) ->setHeaderParams($header) diff --git a/src/Files/File.php b/src/Files/File.php index c27d393..4f56df4 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1,8 +1,10 @@ options->getAccessToken(); @@ -63,7 +65,8 @@ public function getFilesList(array $params) unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() + ->setQuery($params) ->setHeaderParams($header) ->get(API::LIST['files']); } @@ -71,13 +74,13 @@ public function getFilesList(array $params) // ------------------------------------------------------------------------------ /** - * get file + * get file infos (not download file) * * @param string $fileId * @param string $accessToken * @return array */ - public function getFile(string $fileId, string $accessToken = null) + public function getFileInfo(string $fileId, string $accessToken = null) { $params = [ @@ -95,7 +98,7 @@ public function getFile(string $fileId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneFile']); @@ -108,7 +111,7 @@ public function getFile(string $fileId, string $accessToken = null) * * @param string $fileId * @param string $accessToken - * @return mixed + * @return void */ public function deleteFile(string $fileId, string $accessToken = null) { @@ -127,8 +130,8 @@ public function deleteFile(string $fileId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; - return $this->options - ->getRequest() + $this->options + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->delete(API::LIST['oneFile']); @@ -137,7 +140,7 @@ public function deleteFile(string $fileId, string $accessToken = null) // ------------------------------------------------------------------------------ /** - * add file + * upload file (support multiple upload) * * @param array $file * @param string $accessToken @@ -145,62 +148,120 @@ public function deleteFile(string $fileId, string $accessToken = null) */ public function uploadFile(array $file, string $accessToken = null) { - $params = - [ - 'file' => $file, - 'access_token' => $accessToken ?? $this->options->getAccessToken(), - ]; + $fileUploads = Helper::arrayToMulti($file); + $accessToken = $accessToken ?? $this->options->getAccessToken(); - $rule = V::keySet( - V::key('file', V::arrayType()->notEmpty()), - V::key('access_token', V::stringType()->notEmpty()) - ); + V::doValidate($this->multipartRules(), $fileUploads); + V::doValidate(V::stringType()->notEmpty(), $accessToken); - V::doValidate($rule, $params); + $upload = []; + $target = API::LIST['files']; + $header = ['Authorization' => $accessToken]; - $header = ['Authorization' => $params['access_token']]; + foreach ($fileUploads as $item) + { + $item['name'] = 'file'; - unset($params['access_token']); + $request = $this->options + ->getAsync() + ->setFormFiles($item) + ->setHeaderParams($header); - return $this->options - ->getRequest() - ->setFormFiles($params) - ->setHeaderParams($header) - ->post(API::LIST['files']); + $upload[] = function () use ($request, $target) + { + return $request->post($target); + }; + } + + $temp = $this->options->getAsync()->pool($upload); + + foreach ($temp as $key => $val) + { + $temp[$key] = Helper::isAssoc($val) ? $val : current($val); + } + + return $temp; } // ------------------------------------------------------------------------------ /** - * download file + * download file (support multiple download) * - * @param string $fileId + * @param array $params * @param string $accessToken - * @return mixed + * @return array */ - public function downloadFile(string $fileId, string $accessToken = null) + public function downloadFile(array $params, string $accessToken = null) { - $params = - [ - 'id' => $fileId, - 'access_token' => $accessToken ?? $this->options->getAccessToken(), - ]; + $downloadArr = Helper::arrayToMulti($params); + $accessToken = $accessToken ?? $this->options->getAccessToken(); - $rule = V::keySet( - V::key('id', V::stringType()->notEmpty()), - V::key('access_token', V::stringType()->notEmpty()) - ); + V::doValidate($this->downloadRules(), $downloadArr); + V::doValidate(V::stringType()->notEmpty(), $accessToken); - V::doValidate($rule, $params); + $method = []; + $target = API::LIST['downloadFile']; + $header = ['Authorization' => $accessToken]; - $header = ['Authorization' => $params['access_token']]; + foreach ($downloadArr as $item) + { + $sink = $item['path']; - unset($params['access_token']); + $request = $this->options + ->getAsync() + ->setPath($item['id']) + ->setHeaderParams($header); - return $this->options - ->getRequest() - ->setHeaderParams($header) - ->get(API::LIST['downloadFile']); + $method[] = function () use ($request, $target, $sink) + { + return $request->getSink($target, $sink); + }; + } + + return $this->options->getAsync()->pool($method, true); + } + + // ------------------------------------------------------------------------------ + + /** + * rules for download params + * + * @return \Respect\Validation\Validator + */ + private function downloadRules() + { + $path = V::oneOf( + V::resourceType(), + V::stringType()->notEmpty(), + V::instance(StreamInterface::class) + ); + + return V::arrayType()->each(V::keySet( + V::key('id', V::stringType()->notEmpty()), + V::key('path', $path) + )); + } + + // ------------------------------------------------------------------------------ + + /** + * multipart upload rules + * + * @return \Respect\Validation\Validator + */ + private function multipartRules() + { + return V::arrayType()->each(V::keyset( + V::key('headers', V::arrayType(), false), + V::key('filename', V::stringType()->length(1, null), false), + + V::key('contents', V::oneOf( + V::resourceType(), + V::stringType()->notEmpty(), + V::instance(StreamInterface::class) + )) + )); } // ------------------------------------------------------------------------------ diff --git a/src/Folders/Folder.php b/src/Folders/Folder.php index 9e3f2b2..51a339f 100644 --- a/src/Folders/Folder.php +++ b/src/Folders/Folder.php @@ -53,7 +53,7 @@ public function getFoldersList(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->get(API::LIST['folders']); } @@ -85,7 +85,7 @@ public function getFolder(string $folderId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneFolder']); @@ -118,7 +118,7 @@ public function addFolder(string $displayName = null, string $accessToken = null unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setFormParams($params) ->setHeaderParams($header) ->post(API::LIST['folders']); @@ -151,7 +151,7 @@ public function updateFolder(array $params) unset($params['id'], $params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) @@ -165,7 +165,7 @@ public function updateFolder(array $params) * * @param string $folderId * @param string $accessToken - * @return mixed + * @return void */ public function deleteFolder(string $folderId, string $accessToken = null) { @@ -184,8 +184,8 @@ public function deleteFolder(string $folderId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; - return $this->options - ->getRequest() + $this->options + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->delete(API::LIST['oneFolder']); diff --git a/src/Labels/Label.php b/src/Labels/Label.php index a6b5db8..fde341c 100644 --- a/src/Labels/Label.php +++ b/src/Labels/Label.php @@ -53,7 +53,7 @@ public function getLabelsList(string $accessToken = null) $header = ['Authorization' => $accessToken]; return $this->options - ->getRequest() + ->getSync() ->setHeaderParams($header) ->get(API::LIST['labels']); } @@ -85,7 +85,7 @@ public function getLabel(string $labelId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneLabel']); @@ -120,7 +120,7 @@ public function addLabel(string $displayName, string $accessToken = null) unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setFormParams($params) ->setHeaderParams($header) ->post(API::LIST['labels']); @@ -153,7 +153,7 @@ public function updateLabel(array $params) unset($params['id'], $params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) @@ -166,7 +166,7 @@ public function updateLabel(array $params) * delete label * * @param array $params - * @return mixed + * @return void */ public function deleteLabel(array $params) { @@ -186,8 +186,8 @@ public function deleteLabel(array $params) unset($params['id'], $params['access_token']); - return $this->options - ->getRequest() + $this->options + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) diff --git a/src/Messages/Message.php b/src/Messages/Message.php index ea4026b..a280dc7 100644 --- a/src/Messages/Message.php +++ b/src/Messages/Message.php @@ -10,6 +10,7 @@ * Nylas Message * ---------------------------------------------------------------------------------- * + * @info include inline image * @author lanlin * @change 2018/11/22 */ @@ -62,7 +63,7 @@ public function getMessagesList(array $params) $query = array_merge($params, $query); return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setHeaderParams($header) ->get(API::LIST['messages']); @@ -95,7 +96,7 @@ public function getMessage(string $messageId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneMessage']); @@ -132,7 +133,7 @@ public function getRawMessage(string $messageId, string $accessToken = null) ]; $rawStream = $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneMessage']); @@ -173,7 +174,7 @@ public function updateMessage(array $params) unset($params['access_token'], $params['id']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) diff --git a/src/Messages/Search.php b/src/Messages/Search.php index 6c6d45d..4e60893 100644 --- a/src/Messages/Search.php +++ b/src/Messages/Search.php @@ -62,7 +62,7 @@ public function messages(string $q, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setHeaderParams($header) ->get(API::LIST['searchMessages']); diff --git a/src/Messages/Sending.php b/src/Messages/Sending.php index 15c7dbe..36b0a75 100644 --- a/src/Messages/Sending.php +++ b/src/Messages/Sending.php @@ -58,7 +58,7 @@ public function sendDirectly(array $params) unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setFormParams($params) ->setHeaderParams($header) ->post(API::LIST['sending']); @@ -103,7 +103,7 @@ public function sendRawMIME(array $params) unset($params['access_token']); return $this->options - ->getRequest() + ->getSync() ->setBody($body) ->setFormParams($params) ->setHeaderParams($header) diff --git a/src/Utilities/Request.php b/src/Request/AbsBase.php similarity index 61% rename from src/Utilities/Request.php rename to src/Request/AbsBase.php index d61a706..42971dd 100644 --- a/src/Utilities/Request.php +++ b/src/Request/AbsBase.php @@ -1,19 +1,21 @@ -concatApiPath($api); - $options = $this->concatOptions(); - - try - { - $response = $this->guzzle->get($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - return $this->parseResponse($response); - } - - // ------------------------------------------------------------------------------ - - /** - * put request - * - * @param string $api - * @return mixed - * @throws \Exception - */ - public function put(string $api) - { - $apiPath = $this->concatApiPath($api); - $options = $this->concatOptions(); - - try - { - $response = $this->guzzle->put($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - return $this->parseResponse($response); - } - - // ------------------------------------------------------------------------------ - - /** - * post request - * - * @param string $api - * @return mixed - * @throws \Exception - */ - public function post(string $api) - { - $apiPath = $this->concatApiPath($api); - $options = $this->concatOptions(); - - try - { - $response = $this->guzzle->post($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - return $this->parseResponse($response); - } - - // ------------------------------------------------------------------------------ - - /** - * delete request - * - * @param string $api - * @return mixed - * @throws \Exception - */ - public function delete(string $api) - { - $apiPath = $this->concatApiPath($api); - $options = $this->concatOptions(); - - try - { - $response = $this->guzzle->delete($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - return $this->parseResponse($response); - } - - // ------------------------------------------------------------------------------ - - /** - * get request & return body stream without preloaded - * - * @param string $api - * @return mixed - * @throws \Exception - */ - public function getStream(string $api) - { - $apiPath = $this->concatApiPath($api); - $options = $this->concatOptions(); - $options = array_merge($options, ['stream' => true]); - - try - { - $response = $this->guzzle->get($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - return $this->parseResponse($response); - } - - // ------------------------------------------------------------------------------ - - /** - * get request & save body to some where - * - * @param string $api - * @param string|resource|\Psr\Http\Message\StreamInterface $sink - * @return array - * @throws \Exception - */ - public function getSink(string $api, $sink) - { - $rules = V::oneOf( - V::resourceType(), - V::stringType()->notEmpty(), - V::instance('\Psr\Http\Message\StreamInterface') - ); - - V::doValidate($rules, $sink); - - $options = $this->concatOptions(); - $options = array_merge($options, ['sink' => $sink]); - $apiPath = $this->concatApiPath($api); - - try - { - $response = $this->guzzle->get($apiPath, $options); - } - catch (\Exception $e) - { - throw new NylasException($this->getExceptionMsg($e)); - } - - $type = $response->getHeader('Content-Type'); - $length = $response->getHeader('Content-Length'); - - return [current($type) => current($length)]; - } - - // ------------------------------------------------------------------------------ - /** * concat api path for request * @@ -394,11 +226,14 @@ private function concatOptions(bool $httpErrors = false) * Parse the JSON response body and return an array * * @param ResponseInterface $response + * @param bool $headers TIPS: true for get headers, false get body data * @return mixed * @throws \Exception if the response body is not in JSON format */ - private function parseResponse(ResponseInterface $response) + private function parseResponse(ResponseInterface $response, bool $headers = false) { + if ($headers) { return $response->getHeaders(); } + $expc = 'application/json'; $type = $response->getHeader('Content-Type'); diff --git a/src/Request/Async.php b/src/Request/Async.php new file mode 100644 index 0000000..3e2dc97 --- /dev/null +++ b/src/Request/Async.php @@ -0,0 +1,218 @@ +concatApiPath($api); + $options = $this->concatOptions(); + + return $this->guzzle->getAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * put request async + * + * @param string $api + * @return PromiseInterface + * @throws \Exception + */ + public function put(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + return $this->guzzle->putAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * post request async + * + * @param string $api + * @return PromiseInterface + * @throws \Exception + */ + public function post(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + return $this->guzzle->postAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * delete request async + * + * @param string $api + * @return PromiseInterface + * @throws \Exception + */ + public function delete(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + return $this->guzzle->deleteAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * get request & return body stream without preloaded + * + * @param string $api + * @return PromiseInterface + * @throws \Exception + */ + public function getStream(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + $options = array_merge($options, ['stream' => true]); + + return $this->guzzle->getAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * get request & save body to some where + * + * @param string $api + * @param string|resource|\Psr\Http\Message\StreamInterface $sink + * @return PromiseInterface + * @throws \Exception + */ + public function getSink(string $api, $sink) + { + $rules = V::oneOf( + V::resourceType(), + V::stringType()->notEmpty(), + V::instance(StreamInterface::class) + ); + + V::doValidate($rules, $sink); + + $options = $this->concatOptions(); + $options = array_merge($options, ['sink' => $sink]); + $apiPath = $this->concatApiPath($api); + + return $this->guzzle->getAsync($apiPath, $options); + } + + // ------------------------------------------------------------------------------ + + /** + * pool for requests + * + * @param array $funcs + * @param bool $headers + * @return mixed + */ + public function pool(array $funcs, bool $headers = false) + { + foreach ($funcs as $func) + { + if (!is_callable($func)) + { + throw new NylasException('callable function required.'); + } + } + + $data = Pool::batch($this->guzzle, $funcs); + + foreach ($data as $key => $item) + { + $data[$key] = + $item instanceof ResponseInterface ? + $this->whenSuccess($item, $headers) : $this->whenFailed($item); + } + + return $data; + } + + // ------------------------------------------------------------------------------ + + /** + * parse data when failed + * + * @param \Exception $exception + * @return array + */ + private function whenFailed(\Exception $exception) + { + return + [ + 'error' => true, + 'message' => $this->getExceptionMsg($exception), + 'exception' => $exception + ]; + } + + // ------------------------------------------------------------------------------ + + /** + * parse data when success + * + * @param ResponseInterface $response + * @param bool $headers + * @return array + */ + private function whenSuccess(ResponseInterface $response, bool $headers = false) + { + try + { + return $this->parseResponse($response, $headers); + } + catch (\Exception $e) + { + return + [ + 'error' => true, + 'message' => $e->getMessage(), + 'exception' => $e + ]; + } + } + + // ------------------------------------------------------------------------------ + +} diff --git a/src/Request/Sync.php b/src/Request/Sync.php new file mode 100644 index 0000000..dd95af6 --- /dev/null +++ b/src/Request/Sync.php @@ -0,0 +1,194 @@ +concatApiPath($api); + $options = $this->concatOptions(); + + try + { + $response = $this->guzzle->get($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, false); + } + + // ------------------------------------------------------------------------------ + + /** + * put request sync + * + * @param string $api + * @return mixed + * @throws \Exception + */ + public function put(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + try + { + $response = $this->guzzle->put($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, false); + } + + // ------------------------------------------------------------------------------ + + /** + * post request sync + * + * @param string $api + * @return mixed + * @throws \Exception + */ + public function post(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + try + { + $response = $this->guzzle->post($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, false); + } + + // ------------------------------------------------------------------------------ + + /** + * delete request sync + * + * @param string $api + * @return mixed + * @throws \Exception + */ + public function delete(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + + try + { + $response = $this->guzzle->delete($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, false); + } + + // ------------------------------------------------------------------------------ + + /** + * get request & return body stream without preloaded + * + * @param string $api + * @return mixed + * @throws \Exception + */ + public function getStream(string $api) + { + $apiPath = $this->concatApiPath($api); + $options = $this->concatOptions(); + $options = array_merge($options, ['stream' => true]); + + try + { + $response = $this->guzzle->get($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, false); + } + + // ------------------------------------------------------------------------------ + + /** + * get request & save body to some where + * + * @param string $api + * @param string|resource|\Psr\Http\Message\StreamInterface $sink + * @return array + * @throws \Exception + */ + public function getSink(string $api, $sink) + { + $rules = V::oneOf( + V::resourceType(), + V::stringType()->notEmpty(), + V::instance(StreamInterface::class) + ); + + V::doValidate($rules, $sink); + + $options = $this->concatOptions(); + $options = array_merge($options, ['sink' => $sink]); + $apiPath = $this->concatApiPath($api); + + try + { + $response = $this->guzzle->get($apiPath, $options); + } + catch (\Exception $e) + { + throw new NylasException($this->getExceptionMsg($e)); + } + + return $this->parseResponse($response, true); + } + + // ------------------------------------------------------------------------------ + +} diff --git a/src/Threads/Search.php b/src/Threads/Search.php index a7b3c9b..15f5b9a 100644 --- a/src/Threads/Search.php +++ b/src/Threads/Search.php @@ -62,7 +62,7 @@ public function threads(string $q, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setHeaderParams($header) ->get(API::LIST['searchThreads']); diff --git a/src/Threads/Thread.php b/src/Threads/Thread.php index 8a9080d..b52d552 100644 --- a/src/Threads/Thread.php +++ b/src/Threads/Thread.php @@ -61,7 +61,7 @@ public function getThreadsList(array $params) $query = array_merge($params, $query); return $this->options - ->getRequest() + ->getSync() ->setQuery($query) ->setHeaderParams($header) ->get(API::LIST['threads']); @@ -94,7 +94,7 @@ public function getThread(string $threadId, string $accessToken = null) $header = ['Authorization' => $params['access_token']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['id']) ->setHeaderParams($header) ->get(API::LIST['oneThread']); @@ -131,7 +131,7 @@ public function addThread(array $params) unset($params['access_token'], $params['id']); return $this->options - ->getRequest() + ->getSync() ->setPath($path) ->setFormParams($params) ->setHeaderParams($header) diff --git a/src/Utilities/Options.php b/src/Utilities/Options.php index 5fc4547..7b0eaf0 100644 --- a/src/Utilities/Options.php +++ b/src/Utilities/Options.php @@ -1,5 +1,7 @@ debug; $server = $this->getServer(); @@ -208,7 +210,26 @@ public function getRequest() $debug = fopen($this->logFile, 'a'); } - return new Request($server, $debug); + return new Sync($server, $debug); + } + + // ------------------------------------------------------------------------------ + + /** + * get async request instance + */ + public function getAsync() + { + $debug = $this->debug; + $server = $this->getServer(); + + // when set log file + if ($this->debug && !empty($this->logFile)) + { + $debug = fopen($this->logFile, 'a'); + } + + return new Async($server, $debug); } // ------------------------------------------------------------------------------ diff --git a/src/Utilities/Validate.php b/src/Utilities/Validate.php index 58b2091..45bd53a 100644 --- a/src/Utilities/Validate.php +++ b/src/Utilities/Validate.php @@ -1,8 +1,9 @@ assert($input); } - catch (\Exception $e) + catch (NestedValidationException $e) { - throw new NylasException($e->getMessage()); + throw new NylasException($e->getFullMessage()); } } diff --git a/src/Webhooks/Webhook.php b/src/Webhooks/Webhook.php index 6226ba5..694569e 100644 --- a/src/Webhooks/Webhook.php +++ b/src/Webhooks/Webhook.php @@ -55,7 +55,7 @@ public function getWebhookList() $header = ['Authorization' => $params['client_secret']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['client_id']) ->setHeaderParams($header) ->get(API::LIST['webhooks']); @@ -86,7 +86,7 @@ public function getWebhook(string $webhookId) $header = ['Authorization' => $params['client_secret']]; return $this->options - ->getRequest() + ->getSync() ->setPath($params['client_id'], $params['id']) ->setHeaderParams($header) ->get(API::LIST['oneWebhook']);