public function testConstructor() { $curl = new CURLTransport(); // This is prone to failure because instance() caches // the class. $trans = '\\HPCloud\\Transport\\CURLTransport'; \HPCloud\Bootstrap::setConfiguration(array('transport' => $trans)); // Need to test getting instance from Bootstrap. $this->assertInstanceOf($trans, Transport::instance()); }
/** * Rescope the authentication token to a different tenant. * * Note that this will rebuild the service catalog and user information for * the current object, since this information is sensitive to tenant info. * * An authentication token can be in one of two states: * * - unscoped: It has no associated tenant ID. * - scoped: It has a tenant ID, and can thus access that tenant's services. * * This method allows you to do any of the following: * * - Begin with an unscoped token, and assign it a tenant ID. * - Change a token from one tenant ID to another (re-scoping). * - Remove the tenant ID from a scoped token (unscoping). * * @param string $tenantName * The tenant name that this present token should be bound to. If this is the * empty string (`''`), the present token will be "unscoped" and its tenant * name will be removed. * * @retval string * @return string * The authentication token. * @throws HPCloud::Transport::AuthorizationException * If authentication failed. * @throws HPCloud::Exception * For abnormal network conditions. The message will give an indication as * to the underlying problem. */ public function rescopeUsingTenantName($tenantName) { $url = $this->url() . '/tokens'; $token = $this->token(); $data = array('auth' => array('tenantName' => $tenantName, 'token' => array('id' => $token))); $body = json_encode($data); $headers = array('Accept' => self::ACCEPT_TYPE, 'Content-Type' => 'application/json', 'Content-Length' => strlen($body)); $client = \HPCloud\Transport::instance(); $response = $client->doRequest($url, 'POST', $headers, $body); $this->handleResponse($response); return $this->token(); }
/** * Helper function for fetching an object. * * @param boolean $fetchContent * If this is set to TRUE, a GET request will be issued, which will * cause the remote host to return the object in the response body. * The response body is not handled, though. If this is set to * FALSE, a HEAD request is sent, and no body is returned. * @retval HPCloud::Transport::Response * @return \HPCloud\Transport\Response * containing the object metadata and (depending on the * $fetchContent flag) optionally the data. */ protected function fetchObject($fetchContent = FALSE) { $method = $fetchContent ? 'GET' : 'HEAD'; $client = \HPCloud\Transport::instance(); $headers = array('X-Auth-Token' => $this->token); if (empty($this->cdnUrl)) { $response = $client->doRequest($this->url, $method, $headers); } else { $response = $client->doRequest($this->cdnUrl, $method, $headers); } if ($response->status() != 200) { throw new \HPCloud\Exception('An unknown exception occurred during transmission.'); } $this->extractFromHeaders($response); return $response; }
/** * Run the given method on the given container. * * Checks to see if the expected result is returned. * * @param string $name * The name of the container. * @param string $method * The appropriate HTTP verb. * @param int $expects * The expected HTTP code. */ protected function modifyContainer($name, $method, $headers = array(), $qstring = '') { $url = $this->url . '/' . rawurlencode($name) . $qstring; $headers['X-Auth-Token'] = $this->token; $client = \HPCloud\Transport::instance(); $response = $client->doRequest($url, $method, $headers); return $response; }
/** * Remove the named object from storage. * * @param string $name * The name of the object to remove. * @retval boolean * @return boolean * TRUE if the file was deleted, FALSE if no such file is found. */ public function delete($name) { $url = self::objectUrl($this->url, $name); $headers = array('X-Auth-Token' => $this->token); $client = \HPCloud\Transport::instance(); try { $response = $client->doRequest($url, 'DELETE', $headers); } catch (\HPCloud\Transport\FileNotFoundException $fnfe) { return FALSE; } if ($response->status() != 204) { throw new \HPCloud\Exception("An unknown exception occured while deleting {$name}."); } return TRUE; }
/** * Internal request issuing command. */ protected function req($url, $method = 'GET', $jsonDecode = TRUE, $body = '') { $client = \HPCloud\Transport::instance(); $headers = array('X-Auth-Token' => $this->token()); $raw = $client->doRequest($url, $method, $headers, $body); if (!$jsonDecode) { return $raw; } return json_decode($raw->content(), TRUE); }