/** * @param string $resource * @param array $payload * @param string $method * @param array $headers * * @throws HttpException * @return array|bool|mixed */ public function fetch($resource, $payload = array(), $method = self::Get, array $headers = array()) { // Dynamically insert Force API version $_resource = str_ireplace(static::API_VERSION_TAG, $this->_apiVersion ?: static::DEFAULT_API_VERSION, $resource); // Add our default headers if (false === array_search('X-PrettyPrint: 1', $headers)) { $headers = array_merge(array('Content-type: application/json', 'X-PrettyPrint: 1'), $headers); } // Send as JSON $this->_requestFormat = DataFormatTypes::JSON; // Fetch the resource $_response = parent::fetch($_resource, $payload, $method, $headers); // Check the result $_result = Option::get($_response, 'result'); $_code = Option::get($_response, 'code'); // Not a "denied" error... if ($_code < 400) { // Grab the request meta-results $this->_totalSize = Option::get($_result, 'totalSize'); $this->_queryDone = Option::get($_result, 'done', 0); $this->_nextRecordsUrl = Option::get($_result, static::NEXT_RECORDS_URL); } $_error = null; if (HttpResponse::NotFound == $_code) { $_error = 'Resource not found'; } elseif (is_array($_result) && isset($_result[0]->message) && isset($_result[0]->errorCode)) { $_error = $_result[0]->message . ' (' . $_result[0]->errorCode . ')'; } else { return $_result; } $this->_lastError = $_error; $this->_lastErrorCode = $_code; throw new HttpException($_code, $_error); }