/** * Transfoms the response body (json) into an array we can more easily * work with. * * @param \GuzzleHttp\Message\Response|false $response * * @return array */ public function getResponseArray($response) { $return = []; if (!$response instanceof \GuzzleHttp\Message\Response) { $this->errors[] = ['source' => 'client', 'attribute' => null, 'kind' => 'networking', 'message' => 'no response']; return $return; } /** * add error for bad status codes */ $code = $response->getStatusCode(); $errorCodes = [404, 401, 500]; if (in_array($code, $errorCodes)) { $this->errors[] = ['source' => 'client', 'attribute' => null, 'kind' => 'status_code', 'message' => 'Bad status code: ' . $code]; } $body = $response->getBody(); $body = trim((string) $body); /** * Return early if we have an empty body, which we can't turn into an array * anyway. This happens in cases where the API returns a 404, and possibly * other status codes. */ if (empty($body)) { return $return; } $return = json_decode($body, true); // set errors, if any if (!empty($return['result']['errors'])) { foreach ($return['result']['errors'] as $error) { $this->errors[] = $error; } } /** * NOTE: There is also, maybe, an errors array nested in $return['meta']['errors'] * Why is this API so inconsistent???!!!!! */ return $return; }
/** * Transfoms the response body (json) into an array we can more easily * work with. * * @param \GuzzleHttp\Message\Response|false $response * * @return array * @todo $this->_errors is populated with errors from Chargify. Should this also populate a separate errors array when we get HTTP 404s or 201s? */ public function getResponseArray($response) { $return = array(); if (!$response instanceof \GuzzleHttp\Psr7\Response) { $this->_errors['Crucial_Service_Chargify']['networking error'] = 'no response'; return $return; } $body = $response->getBody(); $body = trim((string) $body); /** * return early on bad status codes */ $code = $response->getStatusCode(); $errorCodes = array(404, 401, 500); if (in_array($code, $errorCodes)) { $this->_errors['Crucial_Service_Chargify']['Bad status code'] = $code; return $return; } /** * Return early if we have an empty body, which we can't turn into an array * anyway. This happens in cases where the API returns a 404, and possibly * other status codes. */ if (empty($body)) { return $return; } $return = json_decode($body, true); // set errors, if any if (!empty($return['errors'])) { $this->_errors = $return['errors']; } return $return; }