Skip to content

Commit

Permalink
Cleaner Response Getting
Browse files Browse the repository at this point in the history
That wierdo double parsing thing wasn’t doing it for me.
Makes tests a little clearer as well.
  • Loading branch information
jimlind committed Sep 9, 2015
1 parent 3bd6633 commit 5b81234
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
36 changes: 19 additions & 17 deletions src/JimLind/TiVo/XmlDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
use JimLind\TiVo\Characteristic\XmlTrait;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;

Expand Down Expand Up @@ -84,14 +85,9 @@ public function download($previousShowList = [])
*/
private function downloadXmlPiece($anchorOffset)
{
$request = new Request('GET', $this->uri);
$options = $this->buildGuzzleOptions($anchorOffset);

try {
$response = $this->guzzle->send($request, $options);
} catch (RequestException $exception) {
$response = $this->parseException($exception);
}
$request = new Request('GET', $this->uri);
$options = $this->buildGuzzleOptions($anchorOffset);
$response = $this->getResponse($this->guzzle, $request, $options);

return $this->parseResponse($response);
}
Expand All @@ -118,19 +114,25 @@ private function buildGuzzleOptions($offset)
}

/**
* Parse response from exception
* Always get a response from a request by catching all exceptions
*
* @param RequestException $exception
* @param ClientInterface $client
* @param RequestInterface $request
* @param mixed[] $options
*
* @return Response
* @return ResponseInterface
*/
private function parseException(RequestException $exception)
private function getResponse(ClientInterface $client, RequestInterface $request, $options)
{
if ($exception->hasResponse()) {
return $exception->getResponse();
} else {
return new Response(0, [], $exception->getMessage());
try {
$response = $client->send($request, $options);
} catch (BadResponseException $requestException) {
$response = $requestException->getResponse();
} catch (\Exception $exception) {
$response = new Response(0, [], $exception->getMessage());
}

return $response;
}

/**
Expand Down
28 changes: 13 additions & 15 deletions tests/JimLind/TiVo/XmlDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JimLind\TiVo\Tests;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\BadResponseException;
use JimLind\TiVo\XmlDownloader;

/**
Expand Down Expand Up @@ -92,14 +92,14 @@ public function testAuthOnDownload()
}

/**
* Test a Guzzle exception with response
* Test Guzzle with bad response exception has empty result
*/
public function testRequestExceptionResponseOnDownload()
public function testBadResponseExceptionOnDownload()
{
$request = $this->getMock('\Psr\Http\Message\RequestInterface');
$response = $this->getMock('\Psr\Http\Message\ResponseInterface');

$exception = new ClientException(uniqid(), $request, $response);
$exception = new BadResponseException(uniqid(), $request, $response);

$this->guzzle->method('send')
->will($this->throwException($exception));
Expand All @@ -109,9 +109,9 @@ public function testRequestExceptionResponseOnDownload()
}

/**
* Test a Guzzle exception with response logged
* Test Guzzle with bad response exception logged
*/
public function testRequestExceptionResponseLoggedOnDownload()
public function testBadResponseExceptionLoggedOnDownload()
{
$responseBody = rand();
$responseCode = rand();
Expand All @@ -121,7 +121,7 @@ public function testRequestExceptionResponseLoggedOnDownload()
$response->method('getStatusCode')->willReturn($responseCode);

$request = $this->getMock('\Psr\Http\Message\RequestInterface');
$exception = new ClientException(rand(), $request, $response);
$exception = new BadResponseException(rand(), $request, $response);

$this->guzzle->method('send')
->will($this->throwException($exception));
Expand All @@ -142,12 +142,11 @@ public function testRequestExceptionResponseLoggedOnDownload()
}

/**
* Test a Guzzle exception with message
* Test Guzzle with exception has empty result
*/
public function testRequestExceptionMessageOnDownload()
public function testExceptionOnDownload()
{
$request = $this->getMock('\Psr\Http\Message\RequestInterface');
$exception = new ClientException(uniqid(), $request);
$exception = new \Exception(uniqid());

$this->guzzle->method('send')
->will($this->throwException($exception));
Expand All @@ -157,13 +156,12 @@ public function testRequestExceptionMessageOnDownload()
}

/**
* Test a Guzzle exception with message logged
* Test Guzzle with exception logged
*/
public function testRequestExceptionMessageLoggedOnDownload()
public function testExceptionMessageLoggedOnDownload()
{
$message = uniqid();
$request = $this->getMock('\Psr\Http\Message\RequestInterface');
$exception = new ClientException($message, $request);
$exception = new \Exception($message);

$this->guzzle->method('send')
->will($this->throwException($exception));
Expand Down

0 comments on commit 5b81234

Please sign in to comment.