Skip to content

Commit 4016b31

Browse files
author
Grace Yim
committed
Try to fix ApiRawRequester.request
1 parent bb940b4 commit 4016b31

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

lib/ToopherApi.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,18 @@ private function request($method, $endpoint, $parameters = array(), $rawRequest
181181
$err = json_decode($resultBody, true);
182182
if ($err === NULL) {
183183
$jsonError = $this->json_error_to_string(json_last_error());
184-
if (!empty($jsonError))
184+
if ($jsonError)
185185
{
186186
error_log(sprintf('Error parsing response body JSON: %s', $jsonError));
187187
error_log(sprintf('Response body: %s', $result->getBody()));
188188
throw new ToopherRequestException(sprintf('JSON Parsing Error: %s', $jsonError));
189189
}
190190
}
191-
192191
if(array_key_exists('error_message', $err))
193192
{
194193
throw new ToopherRequestException($err['error_message'], $err['error_code']);
195-
} else {
196-
throw new ToopherRequestException(sprintf('%s - %s', $result->getReasonPhrase(), $resultBody), $result->getStatus());
197194
}
195+
throw new ToopherRequestException(sprintf('%s - %s', $result->getReasonPhrase(), $resultBody), $result->getStatus());
198196
}
199197

200198
if ($rawRequest) {
@@ -203,14 +201,14 @@ private function request($method, $endpoint, $parameters = array(), $rawRequest
203201
$decoded = json_decode($resultBody, true);
204202
if ($decoded === NULL) {
205203
$jsonError = $this->json_error_to_string(json_last_error());
206-
if (!empty($jsonError))
204+
if ($jsonError)
207205
{
208206
error_log(sprintf('Error parsing response body JSON: %s', $jsonError));
209207
error_log(sprintf('Response body: %s', $result->getBody()));
210208
throw new ToopherRequestException(sprintf('JSON Parsing Error: %s', $jsonError));
211209
}
212210
}
213-
return $decoded;
211+
return $decoded;
214212
}
215213
}
216214

test/ToopherApiTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ public function test403WithEmptyMessageRaisesToopherRequestException()
510510
* @expectedException ToopherRequestException
511511
* @expectedExceptionMessage JSON Parsing Error: Syntax error, malformed JSON
512512
*/
513-
public function test200WithEmptyMessageRaisesToopherRequestException()
513+
public function test403WithBadJsonRaisesToopherRequestException()
514514
{
515-
$resp = new HTTP_Request2_Response('HTTP/1.1 200 OK', false, 'https://api.toopher.com/v1/authentication_requests/1');
515+
$resp = new HTTP_Request2_Response('HTTP/1.1 403 Forbidden', false, 'https://api.toopher.com/v1/authentication_requests/1');
516516
$resp->appendBody('stuff');
517517
$this->mock->addResponse($resp);
518518
$toopher = $this->getToopherApi($this->mock);
@@ -523,9 +523,9 @@ public function test200WithEmptyMessageRaisesToopherRequestException()
523523
* @expectedException ToopherRequestException
524524
* @expectedExceptionMessage JSON Parsing Error: Syntax error, malformed JSON
525525
*/
526-
public function test403WithBadJsonRaisesToopherRequestException()
526+
public function test200WithEmptyMessageRaisesToopherRequestException()
527527
{
528-
$resp = new HTTP_Request2_Response('HTTP/1.1 403 Forbidden', false, 'https://api.toopher.com/v1/authentication_requests/1');
528+
$resp = new HTTP_Request2_Response('HTTP/1.1 200 OK', false, 'https://api.toopher.com/v1/authentication_requests/1');
529529
$resp->appendBody('stuff');
530530
$this->mock->addResponse($resp);
531531
$toopher = $this->getToopherApi($this->mock);

0 commit comments

Comments
 (0)