/** * Make a request to the Dashbord's internal API. * * @param $realm * Permissions realm for data request: currently "user" or "site" but in the * future this could also be "organization" or another high-level business * object (e.g. "product" for managing your app). Can also be "public" to * simply pull read-only data that is not privileged. * * @param $uuid * The UUID of the item in the realm you want to access. * * @param $method * HTTP method (verb) to use. * * @param $data * A native PHP data structure (int, string, arary or simple object) to be * sent along with the request. Will be encoded as JSON for you. */ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $options = NULL) { if (!in_array($realm, array('login', 'user', 'public')) and !Terminus::is_test()) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); // combine session realm uuid and path to get a unique key // @todo need cache "groups" $cachekey = md5(Session::getValue('user_uuid') . $uuid . $realm . $path); $data = $cache->get_data($cachekey); // check the request cache if ("GET" == $method and !Terminus::get_config('nocache') and !getenv('CLI_TEST_MODE') and !empty($data)) { if (Terminus::get_config('debug')) { Logger::debug('CacheKey: ' . $cachekey); } return (array) $data; } // for some methods we'll assume the cache should be invalidated if (in_array($method, array("POST", "PUT", "DELETE"))) { $cache->flush(null, 'session'); } if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Logger::debug('Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(TRUE); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); $cache->put_data($cachekey, $data); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); \Terminus::error("%s", $response->getBody(TRUE)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); //die(Terminus_Command::stripSensitiveData()); $sanitized_request = Terminus_Command::stripSensitiveData((string) $request, Terminus_Command::$_blacklist); \Terminus::error("Request %s had failed: %s", array($sanitized_request, $e->getMessage())); } catch (Exception $e) { \Terminus::error("Unrecognised request failure: %s", $e->getMessage()); } }
/** * Simplified request method for Pantheon API * * @param [string] $path API path (URL) * @param [array] $options Options for the request * [string] method GET is default * [mixed] data Native PHP data structure (e.g. int, string array, or * simple object) to be sent along with the request. Will be encoded as * JSON for you. * @return [array] $data */ public static function simple_request($path, $options = array()) { $req_options = array(); $method = 'get'; if (isset($options['method'])) { $method = $options['method']; } if (isset($options['data'])) { $req_options['body'] = json_encode($options['data']); $req_options['headers'] = array('Content-type' => 'application/json'); } $url = 'https://' . TERMINUS_HOST . '/api/' . $path; if (Session::getValue('session')) { $req_options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $req_options['verify'] = false; } try { $resp = Request::send($url, $method, $req_options); } catch (Guzzle\Http\Exception\BadResponseException $e) { \Terminus::error('Request Failure: %s', $e->getMessage()); return; } $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; }
/** * "Wake" a site * * @return [array] $return_data */ public function wake() { $hostnames = $this->getHostnames(); $target = key($hostnames); $response = Request::send("http://{$target}/pantheon_healthcheck", 'GET'); $return_data = array('success' => $response->isSuccessful(), 'time' => $response->getInfo('total_time'), 'styx' => $response->getHeader('X-Pantheon-Styx-Hostname'), 'response' => $response, 'target' => $target); return $return_data; }
/** * Retrieves current version number from repository and saves it to the cache * * @return [string] $response->name The version number */ private function checkCurrentVersion() { $url = 'https://api.github.com/repos/pantheon-systems/cli/releases?per_page=1'; $response = Request::send($url, 'GET'); $json = $response->getBody(true); $data = json_decode($json); $release = array_shift($data); $this->cache->put_data('latest_release', array('version' => $release->name, 'check_date' => time())); return $release->name; }