class RestResponse in Salesforce Suite 8.4
Same name and namespace in other branches
- 8.3 src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse
- 5.0.x src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse
Class RestResponse.
@package Drupal\salesforce\Rest
Hierarchy
- class \Drupal\salesforce\Rest\RestResponse extends \GuzzleHttp\Psr7\Response uses StringTranslationTrait
Expanded class hierarchy of RestResponse
2 files declare their use of RestResponse
- RestClientTest.php in tests/
src/ Unit/ RestClientTest.php - TestRestClient.php in src/
Tests/ TestRestClient.php
File
- src/
Rest/ RestResponse.php, line 15
Namespace
Drupal\salesforce\RestView source
class RestResponse extends Response {
use StringTranslationTrait;
/**
* The original Response used to build this object.
*
* @var \GuzzleHttp\Psr7\Response
* @see __get()
*/
protected $response;
/**
* The json-decoded response body.
*
* @var mixed
* @see __get()
*/
protected $data;
/**
* RestResponse constructor.
*
* @param \Psr\Http\Message\ResponseInterface $response
* A response.
*/
public function __construct(ResponseInterface $response) {
$this->response = $response;
parent::__construct($response
->getStatusCode(), $response
->getHeaders(), $response
->getBody(), $response
->getProtocolVersion(), $response
->getReasonPhrase());
$this
->handleJsonResponse();
}
/**
* Magic getter method to return the given property.
*
* @param string $key
* The property name.
*
* @return mixed
* The property value.
*
* @throws \Exception
* If $key property does not exist.
*/
public function __get($key) {
if (!property_exists($this, $key)) {
throw new \Exception("Undefined property {$key}");
}
return $this->{$key};
}
/**
* Helper function to eliminate repetitive json parsing.
*
* @return $this
*
* @throws \Drupal\salesforce\Rest\RestException
*/
private function handleJsonResponse() {
$this->data = '';
$response_body = $this
->getBody()
->getContents();
if (empty($response_body)) {
return NULL;
}
// Allow any exceptions here to bubble up:
try {
$data = Json::decode($response_body);
} catch (\Exception $e) {
throw new RestException($this, $e
->getMessage(), $e
->getCode(), $e);
}
if (empty($data) || !is_array($data)) {
throw new RestException($this, $this
->t('Invalid response'));
}
if (!empty($data['error'])) {
throw new RestException($this, $data['error']);
}
if (!empty($data[0]) && count($data) == 1) {
$data = $data[0];
}
if (!empty($data['error'])) {
throw new RestException($this, $data['error']);
}
if (!empty($data['errorCode'])) {
throw new RestException($this, $data['errorCode']);
}
$this->data = $data;
return $this;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RestResponse:: |
protected | property | The json-decoded response body. | |
RestResponse:: |
protected | property | The original Response used to build this object. | |
RestResponse:: |
private | function | Helper function to eliminate repetitive json parsing. | |
RestResponse:: |
public | function | RestResponse constructor. | 2 |
RestResponse:: |
public | function | Magic getter method to return the given property. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |