Skip to content

Commit ebb55b3

Browse files
committed
Handle RequestExceptions
1 parent 1cff288 commit ebb55b3

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

src/ConstantContact/Base.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ private function getUrl(array &$parameters) : string
7878
}
7979
$parameter = '{' . \array_key_first($parameters) . '}';
8080

81-
if (null !== \strpos($url, $parameter))
81+
if (str_contains($url, $parameter))
8282
{
8383
$url = \str_replace($parameter, \array_shift($parameters), $url);
8484
}
8585

8686
return $url;
8787
}
8888
}
89+

src/ConstantContact/Client.php

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -174,45 +174,86 @@ public function patch(string $url, array $parameters) : array
174174

175175
public function put(string $url, array $parameters, string $method = 'PUT') : array
176176
{
177-
$json = \json_encode($parameters);
178-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders([
179-
'Connection' => 'keep-alive',
180-
'Content-Length' => \strlen($json),
181-
'Accept-Encoding' => 'gzip, deflate',
182-
'Host' => $this->host,
183-
'Accept' => '*/*']),
184-
'body' => $json, ]);
185-
;
186-
$response = $guzzle->request($method, $url);
177+
try
178+
{
179+
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
180+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders([
181+
'Connection' => 'keep-alive',
182+
'Content-Length' => \strlen($json),
183+
'Accept-Encoding' => 'gzip, deflate',
184+
'Host' => $this->host,
185+
'Accept' => '*/*']),
186+
'body' => $json, ]);
187+
;
188+
$response = $guzzle->request($method, $url);
189+
190+
return $this->process($response);
191+
}
192+
catch (\GuzzleHttp\Exception\RequestException $e)
193+
{
194+
$this->lastError = $e->getMessage();
195+
$this->statusCode = $e->getResponse()->getStatusCode();
196+
}
187197

188-
return $this->process($response);
198+
return [];
189199
}
190200

191201
public function delete(string $url) : bool
192202
{
193-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
194-
$response = $guzzle->request('DELETE', $url);
203+
try
204+
{
205+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
206+
$response = $guzzle->request('DELETE', $url);
195207

196-
$this->process($response);
208+
$this->process($response);
197209

198-
return 204 == $this->statusCode;
210+
return 204 == $this->statusCode;
211+
}
212+
catch (\GuzzleHttp\Exception\RequestException $e)
213+
{
214+
$this->lastError = $e->getMessage();
215+
$this->statusCode = $e->getResponse()->getStatusCode();
216+
}
217+
218+
return false;
199219
}
200220

201221
public function get(string $url, array $parameters) : array
202222
{
203-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
204-
$response = $guzzle->request('GET', $url);
223+
try
224+
{
225+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
226+
$response = $guzzle->request('GET', $url);
205227

206-
return $this->process($response);
228+
return $this->process($response);
229+
}
230+
catch (\GuzzleHttp\Exception\RequestException $e)
231+
{
232+
$this->lastError = $e->getMessage();
233+
$this->statusCode = $e->getResponse()->getStatusCode();
234+
}
235+
236+
return [];
207237
}
208238

209239
public function post(string $url, array $parameters) : array
210240
{
211-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(),
212-
'body' => \json_encode($parameters), ]);
213-
$response = $guzzle->request('POST', $url);
241+
try
242+
{
243+
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
244+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(),
245+
'body' => $json, ]);
246+
$response = $guzzle->request('POST', $url);
214247

215-
return $this->process($response);
248+
return $this->process($response);
249+
}
250+
catch (\GuzzleHttp\Exception\RequestException $e)
251+
{
252+
$this->lastError = $e->getMessage();
253+
$this->statusCode = $e->getResponse()->getStatusCode();
254+
}
255+
256+
return [];
216257
}
217258

218259
private function exec(\CurlHandle $ch) : bool

0 commit comments

Comments
 (0)