예제 #1
0
 /**
  * @param string $uri
  * @param array  $payload
  * @param array  $curlOptions
  * @param string $method
  *
  * @return array|bool|\stdClass
  * @throws \DreamFactory\Managed\Exceptions\ManagedInstanceException
  */
 protected function callConsole($uri, $payload = [], $curlOptions = [], $method = Request::METHOD_POST)
 {
     try {
         //  Strip leading double-slash
         '//' == substr($uri, 0, 2) && ($uri = substr($uri, 2));
         //  Allow full URIs or manufacture one...
         'http' != substr($uri, 0, 4) && ($uri = $this->config['console-api-url'] . ltrim($uri, '/ '));
         if (false === ($_result = Curl::request($method, $uri, $this->signPayload($payload), $curlOptions))) {
             throw new \RuntimeException('Failed to contact DFE console');
         }
         if (!$_result instanceof \stdClass) {
             if (is_string($_result) && (false === json_decode($_result) || JSON_ERROR_NONE !== json_last_error())) {
                 throw new \RuntimeException('Invalid response received from DFE console');
             }
         }
         return $_result;
     } catch (\Exception $_ex) {
         throw new ManagedInstanceException('DFE Console API Error: ' . $_ex->getMessage(), $_ex->getCode(), $_ex);
     }
 }
예제 #2
0
 /**
  * @param string $method
  * @param string $url
  * @param mixed  $payload
  * @param array  $curlOptions
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws \DreamFactory\Core\Exceptions\NotImplementedException
  * @throws \DreamFactory\Core\Exceptions\RestException
  */
 protected static function externalRequest($method, $url, $payload = [], $curlOptions = [])
 {
     $result = Curl::request($method, $url, $payload, $curlOptions);
     $contentType = Curl::getInfo('content_type');
     $format = DataFormats::fromMimeType($contentType);
     $status = Curl::getLastHttpCode();
     if ($status >= 300) {
         if (!is_string($result)) {
             $result = json_encode($result);
         }
         throw new RestException($status, $result, $status);
     }
     return ResponseFactory::create($result, $format, $status, $contentType);
 }
 /**
  * Makes a shout out to an instance's private back-end. Should be called bootyCall()  ;)
  *
  * @param string $uri     The REST uri (i.e. "/[rest|api][/v[1|2]]/db", "/rest/system/users", etc.) to retrieve
  *                        from the instance
  * @param array  $payload Any payload to send with request
  * @param array  $options Any options to pass to transport layer
  * @param string $method  The HTTP method. Defaults to "POST"
  *
  * @return array|bool|\stdClass
  */
 public function call($uri, $payload = [], $options = [], $method = Request::METHOD_POST)
 {
     $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), [EnterpriseDefaults::CONSOLE_X_HEADER . ': ' . $this->token]);
     try {
         $_response = Curl::request($method, Uri::segment([$this->resourceUri, $uri], false), $payload, $options);
     } catch (\Exception $_ex) {
         return false;
     }
     return $_response;
 }
 /**
  * Makes a shout out to an instance's private back-end. Should be called bootyCall()  ;)
  *
  * @param string $uri     The REST uri (i.e. "/[rest|api][/v[1|2]]/db", "/rest/system/users", etc.) to retrieve
  *                        from the instance
  * @param array  $payload Any payload to send with request
  * @param array  $options Any options to pass to transport layer
  * @param string $method  The HTTP method. Defaults to "POST"
  *
  * @return array|bool|\stdClass
  */
 public function call($uri, $payload = [], $options = [], $method = Request::METHOD_POST)
 {
     $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), $this->headers ?: []);
     if (!empty($payload) && !is_scalar($payload)) {
         $payload = Json::encode($payload);
         $options[CURLOPT_HTTPHEADER] = array_merge(array_get($options, CURLOPT_HTTPHEADER, []), ['Content-Type: application/json']);
     }
     try {
         $_response = Curl::request($method, $this->resourceUri . ltrim($uri, ' /'), $payload, $options);
     } catch (\Exception $_ex) {
         $this->error('[dfe.instance-api-client] ' . $method . ' failure: ' . $_ex->getMessage());
         return false;
     }
     return $_response;
 }
예제 #5
0
 /**
  * @param string $method
  * @param string $url
  * @param mixed  $payload
  * @param array  $curlOptions
  *
  * @return \stdClass|string
  */
 protected static function externalRequest($method, $url, $payload = [], $curlOptions = [])
 {
     try {
         $result = Curl::request($method, $url, $payload, $curlOptions);
         $result = ResponseFactory::create($result);
     } catch (\Exception $ex) {
         $result = ResponseFactory::create($ex);
         Log::error('Exception: ' . $ex->getMessage(), ['response' => $result]);
     }
     return ResponseFactory::sendScriptResponse($result);
 }
예제 #6
0
 /**
  * @throws \DreamFactory\Core\Exceptions\RestException
  * @return bool
  */
 protected function processRequest()
 {
     $data = $this->request->getContent();
     $resource = !empty($this->resourcePath) ? ltrim($this->resourcePath, '/') : null;
     if ($resource) {
         $this->url = rtrim($this->baseUrl, '/') . '/' . $resource;
     } else {
         $this->url = $this->baseUrl;
     }
     if (!empty($this->query)) {
         $splicer = false === strpos($this->baseUrl, '?') ? '?' : '&';
         $this->url .= $splicer . $this->query;
     }
     $cacheKey = '';
     if ($this->cacheEnabled) {
         switch ($this->action) {
             case Verbs::GET:
                 // build cache_key
                 $cacheKey = $this->action . ':' . $this->name;
                 if ($resource) {
                     $cacheKey .= ':' . $resource;
                 }
                 if (!empty($this->cacheQuery)) {
                     $cacheKey .= ':' . $this->cacheQuery;
                 }
                 $cacheKey = hash('sha256', $cacheKey);
                 if (null !== ($result = $this->getFromCache($cacheKey))) {
                     return $result;
                 }
                 break;
         }
     }
     Log::debug('Outbound HTTP request: ' . $this->action . ': ' . $this->url);
     Curl::setDecodeToArray(true);
     $result = Curl::request($this->action, $this->url, $data, $this->curlOptions);
     if (false === $result) {
         $error = Curl::getError();
         throw new RestException(ArrayUtils::get($error, 'code', 500), ArrayUtils::get($error, 'message'));
     }
     $status = Curl::getLastHttpCode();
     if ($status >= 300) {
         if (!is_string($result)) {
             $result = json_encode($result);
         }
         throw new RestException($status, $result, $status);
     }
     $contentType = Curl::getInfo('content_type');
     $format = DataFormats::fromMimeType($contentType);
     $response = ResponseFactory::create($result, $format, $status, $contentType);
     if ($this->cacheEnabled) {
         switch ($this->action) {
             case Verbs::GET:
                 $this->addToCache($cacheKey, $result);
                 break;
         }
     }
     return $response;
 }