Skip to content

Commit 905d3b4

Browse files
authored
Merge pull request #13 from skn-036:v1.0
#15 handing guzzle request on label
2 parents 9c2b377 + 6fbac2f commit 905d3b4

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "skn036/laravel-gmail-api",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Wrapper around gmail api for laravel.",
55
"keywords": [
66
"Muhammad Sajedul Karim",

src/Helper/GmailHelpers.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Skn036\Gmail\Helper;
3+
4+
trait GmailHelpers
5+
{
6+
/**
7+
* resolve Guzzle response if not a request
8+
*
9+
* @param mixed $responseOrRequest
10+
* @param \Google_Client $client
11+
* @param string|null $expectedClass
12+
*
13+
* @return mixed;
14+
*/
15+
protected function executeRequest(
16+
mixed $responseOrRequest,
17+
\Google_Client $client,
18+
?string $expectedClass = null
19+
) {
20+
if (get_class($responseOrRequest) === 'GuzzleHttp\Psr7\Request') {
21+
$responseOrRequest = $client->execute($responseOrRequest, $expectedClass);
22+
}
23+
24+
return $responseOrRequest;
25+
}
26+
}

src/Label/GmailLabelResponse.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
use Skn036\Gmail\Gmail;
55
use Skn036\Gmail\Facades\Gmail as GmailFacade;
66
use Skn036\Gmail\Exceptions\TokenNotValidException;
7+
use Skn036\Gmail\Helper\GmailHelpers;
78

89
class GmailLabelResponse
910
{
11+
use GmailHelpers;
12+
1013
/**
1114
* Gmail Client
1215
* @var Gmail|GmailFacade
@@ -67,7 +70,13 @@ public function list(array $optParams = [])
6770
*/
6871
public function get(string $labelId, array $optParams = [])
6972
{
70-
$label = $this->service->users_labels->get('me', $labelId, $optParams);
73+
$responseOrRequest = $this->service->users_labels->get('me', $labelId, $optParams);
74+
$label = $this->executeRequest(
75+
$responseOrRequest,
76+
$this->client,
77+
'Google_Service_Gmail_Label'
78+
);
79+
7180
return new GmailLabel($label);
7281
}
7382

@@ -91,7 +100,14 @@ public function get(string $labelId, array $optParams = [])
91100
public function create(array $params, array $optParams = [])
92101
{
93102
$label = $this->paramsToLabelPayload($params);
94-
$createdLabel = $this->service->users_labels->create('me', $label, $optParams);
103+
104+
$responseOrRequest = $this->service->users_labels->create('me', $label, $optParams);
105+
$createdLabel = $this->executeRequest(
106+
$responseOrRequest,
107+
$this->client,
108+
'Google_Service_Gmail_Label'
109+
);
110+
95111
return $this->get($createdLabel->getId());
96112
}
97113

@@ -116,7 +132,19 @@ public function create(array $params, array $optParams = [])
116132
public function update(string $labelId, array $params, array $optParams = [])
117133
{
118134
$label = $this->paramsToLabelPayload($params);
119-
$updatedLabel = $this->service->users_labels->update('me', $labelId, $label, $optParams);
135+
136+
$responseOrRequest = $this->service->users_labels->update(
137+
'me',
138+
$labelId,
139+
$label,
140+
$optParams
141+
);
142+
$updatedLabel = $this->executeRequest(
143+
$responseOrRequest,
144+
$this->client,
145+
'Google_Service_Gmail_Label'
146+
);
147+
120148
return $this->get($updatedLabel->getId());
121149
}
122150

@@ -132,28 +160,26 @@ public function update(string $labelId, array $params, array $optParams = [])
132160
*/
133161
public function delete(string $labelId, array $optParams = [])
134162
{
135-
$this->service->users_labels->delete('me', $labelId, $optParams);
163+
$responseOrRequest = $this->service->users_labels->delete('me', $labelId, $optParams);
164+
$this->executeRequest($responseOrRequest, $this->client);
136165
}
137166

138167
/**
139168
* List labels request to gmail
140169
*
141170
* @param array $optParams
142171
*
143-
* @return \Google_Service_Gmail_ListLabelsResponse|\GuzzleHttp\Psr7\Request
172+
* @return \Google_Service_Gmail_ListLabelsResponse
144173
* @throws \Google\Service\Exception
145174
*/
146175
protected function getGmailLabelListResponse($optParams = [])
147176
{
148177
$responseOrRequest = $this->service->users_labels->listUsersLabels('me', $optParams);
149-
150-
if (get_class($responseOrRequest) === 'GuzzleHttp\Psr7\Request') {
151-
$responseOrRequest = $this->service
152-
->getClient()
153-
->execute($responseOrRequest, 'Google_Service_Gmail_ListLabelsResponse');
154-
}
155-
156-
return $responseOrRequest;
178+
return $this->executeRequest(
179+
$responseOrRequest,
180+
$this->client,
181+
'Google_Service_Gmail_ListLabelsResponse'
182+
);
157183
}
158184

159185
/**
@@ -164,7 +190,12 @@ protected function getGmailLabelListResponse($optParams = [])
164190
*/
165191
protected function getGmailLabelResponse($id)
166192
{
167-
return $this->service->users_labels->get('me', $id);
193+
$responseOrRequest = $this->service->users_labels->get('me', $id);
194+
return $this->executeRequest(
195+
$responseOrRequest,
196+
$this->client,
197+
'Google_Service_Gmail_Label'
198+
);
168199
}
169200

170201
/**

0 commit comments

Comments
 (0)